Skip to content

Commit

Permalink
Fix find_contours on the image border
Browse files Browse the repository at this point in the history
  • Loading branch information
wkentaro committed May 16, 2023
1 parent 76584bb commit abd4424
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion labelme/ai/models/segment_anything.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,12 +168,13 @@ def _compute_polygon_from_points(
"mask.jpg", imgviz.label2rgb(mask, imgviz.rgb2gray(image))
)

contours = skimage.measure.find_contours(mask)
contours = skimage.measure.find_contours(np.pad(mask, pad_width=1))
contour = max(contours, key=_get_contour_length)
polygon = skimage.measure.approximate_polygon(
coords=contour,
tolerance=np.ptp(contour, axis=0).max() / 100,
)
polygon = np.clip(polygon, (0, 0), (mask.shape[0] - 1, mask.shape[1] - 1))
polygon = polygon[:-1] # drop last point that is duplicate of first point
if 0:
image_pil = PIL.Image.fromarray(image)
Expand Down

0 comments on commit abd4424

Please sign in to comment.