You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is your feature request related to a problem? Please describe.
The current implementation creates compression artefacts due to interpolation. We can do something more effective using shapely
Describe the solution you'd like
There should not be compression artefacts due to the reduction of the number of polygon coordinates.
Describe alternatives you've considered
Using shapely and rewriting the function as:
fromshapely.geometryimportPolygonimportnumpyasnpfromtypingimportListdefmask_to_polygon(
mask: np.ndarray,
simplify_tolerance: float=1.0,
**kwargs
) ->List:
""" Converts segmentation mask to a list of polygons. To reduce the number of vertices in the obtained polygon, try to increase the value of `polygon_simplify_tolerance`. Args: mask: numpy array containing multiple segmentation masks. Each mask must be associated with a different number, where 0 is for background, and other numbers related to different objects. For example, objects of a class A may be associated with a value of 1, class B to values 10, class C to 255, and so on. simplify_tolerance: a tolerance value used to remove redundant vertexes for the polygons extracted from the mask. **kwargs: extra parameters are ignored. Returns: List of polygons segmentation masks. """polygons= []
classes=list(np.unique(mask))
if0inclasses:
classes.remove(0)
# iterate over the different segmentations (classes) inside the mask:forclsinclasses:
# pick the class-related segmentation and convert it to binary:binary=np.zeros_like(mask)
binary[mask==cls] =1# compute mask contours and then convert them to polygons:binary=binary.astype(np.uint8)
contours, hierarchy=cv2.findContours(binary, mode=cv2.RETR_EXTERNAL, method=cv2.CHAIN_APPROX_SIMPLE)
# simplify contourscontours= [cnt.reshape((-1, 2)) forcntincontours]
forcntincontours:
sqz_cnt=cntifcnt.shape[1] ==1:
sqz_cnt=np.squeeze(sqz_cnt, axis=1)
iflen(sqz_cnt) >=3: # a polygon must contain at least 3 pointspolygon=Polygon(sqz_cnt)
polygon=polygon.simplify(tolerance=simplify_tolerance, preserve_topology=True)
polygon=polygon.exterior.coords# get coordinatesx_coords= [c[0] forcinpolygon]
y_coords= [c[1] forcinpolygon]
sgm= []
forx, yinzip(x_coords, y_coords):
sgm.extend([float(x), float(y)])
polygons.append(sgm)
returnpolygons
Additional context
Add any other context or screenshots about the feature request here.
The text was updated successfully, but these errors were encountered:
Is your feature request related to a problem? Please describe.
The current implementation creates compression artefacts due to interpolation. We can do something more effective using shapely
Describe the solution you'd like
There should not be compression artefacts due to the reduction of the number of polygon coordinates.
Describe alternatives you've considered
Using shapely and rewriting the function as:
Additional context
Add any other context or screenshots about the feature request here.
The text was updated successfully, but these errors were encountered: