MCPcopy Create free account
hub / github.com/CASIA-LMC-Lab/FastSAM / get_bbox_from_mask

Function get_bbox_from_mask

utils/tools.py:75–92  ·  view source on GitHub ↗
(mask)

Source from the content-addressed store, hash-verified

73
74
75def 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
95def fast_process(

Callers 1

crop_imageFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected