Skip to content

Commit

Permalink
Remove small contours
Browse files Browse the repository at this point in the history
  • Loading branch information
vietanhdev committed Apr 16, 2023
1 parent f2eab50 commit da45667
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions anylabeling/services/auto_labeling/segment_anything.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,18 +225,34 @@ def post_process(self, masks, resized_ratio):
"""
Post process masks
"""
shapes = []
# Find contours
contours, _ = cv2.findContours(
masks.astype(np.uint8), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE
)

# Refine contours
approx_contours = []
for contour in contours:
# Approximate contour
epsilon = 0.001 * cv2.arcLength(contour, True)
approx = cv2.approxPolyDP(contour, epsilon, True)
points = approx.reshape(-1, 2)

approx_contours.append(approx)

# Remove small contours (area < 20% of average area)
if len(approx_contours) > 1:
areas = [cv2.contourArea(contour) for contour in approx_contours]
avg_area = np.mean(areas)
approx_contours = [
contour
for contour, area in zip(approx_contours, areas, strict=False)
if area > avg_area * 0.2
]

# Contours to shapes
shapes = []
for approx in approx_contours:
# Scale points
points = approx.reshape(-1, 2)
points[:, 0] = points[:, 0] / resized_ratio[0]
points[:, 1] = points[:, 1] / resized_ratio[1]
points = points.tolist()
Expand Down

0 comments on commit da45667

Please sign in to comment.