| 73 | |
| 74 | |
| 75 | def get_bbox_from_mask(mask): |
| 76 | mask = mask.astype(np.uint8) |
| 77 | contours, hierarchy = cv2.findContours( |
| 78 | mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE |
| 79 | ) |
| 80 | x1, y1, w, h = cv2.boundingRect(contours[0]) |
| 81 | x2, y2 = x1 + w, y1 + h |
| 82 | if len(contours) > 1: |
| 83 | for b in contours: |
| 84 | x_t, y_t, w_t, h_t = cv2.boundingRect(b) |
| 85 | # 将多个bbox合并成一个 |
| 86 | x1 = min(x1, x_t) |
| 87 | y1 = min(y1, y_t) |
| 88 | x2 = max(x2, x_t + w_t) |
| 89 | y2 = max(y2, y_t + h_t) |
| 90 | h = y2 - y1 |
| 91 | w = x2 - x1 |
| 92 | return [x1, y1, x2, y2] |
| 93 | |
| 94 | |
| 95 | def fast_process( |