| 192 | |
| 193 | |
| 194 | def mask_to_bbox(mask): |
| 195 | # Mask shape is (1, height, width) |
| 196 | mask = mask[0] |
| 197 | |
| 198 | # Find rows and columns where the mask is not zero |
| 199 | rows = np.any(mask, axis=1) |
| 200 | cols = np.any(mask, axis=0) |
| 201 | |
| 202 | if not rows.any() or not cols.any(): |
| 203 | # If there are no nonzero values in the mask, return None |
| 204 | return None |
| 205 | |
| 206 | # Find the bounding box's edges |
| 207 | ymin, ymax = np.where(rows)[0][[0, -1]] |
| 208 | xmin, xmax = np.where(cols)[0][[0, -1]] |
| 209 | |
| 210 | # Return the bounding box coordinates |
| 211 | return int(xmin), int(ymin), int(xmax), int(ymax) |
| 212 | |
| 213 | |
| 214 | |