def find_contours(self, min_area=0.0, max_area=np.inf):
"""Returns a list of connected components with an area between
min_area and max_area.
Parameters
----------
min_area : float
The minimum area for a contour
max_area : float
The maximum area for a contour
Returns
-------
:obj:`list` of :obj:`Contour`
A list of resuting contours
"""
# get all contours (connected components) from the binary image
_, contours, hierarchy = cv2.findContours(
self.data.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
num_contours = len(contours)
kept_contours = []
# find which contours need to be pruned
for i in range(num_contours):
area = cv2.contourArea(contours[i])
logging.debug('Contour %d area: %.3f' % (len(kept_contours), area))
if area > min_area and area < max_area:
boundary_px = contours[i].squeeze()
boundary_px_ij_swapped = np.zeros(boundary_px.shape)
boundary_px_ij_swapped[:, 0] = boundary_px[:, 1]
boundary_px_ij_swapped[:, 1] = boundary_px[:, 0]
kept_contours.append(
Contour(
boundary_px_ij_swapped,
area=area,
frame=self._frame))
return kept_contours
评论列表
文章目录