繁体   English   中英

使用OpenCV 3和python 3按其区域对图像轮廓进行排序的最佳解决方案

[英]optimal solution to sort contours of image by their areas using which OpenCV 3 and python 3

使用OpenCV 3和Python 3对图像轮廓进行排序的最佳(最佳)解决方案是什么? 我尝试这个。 这是最好的解决方案吗?

_, contours, _ = cv2.findContours(image , cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
areas = list(map(lambda c : cv2.contourArea(c), contours))
contoursWithAreas = zip(contours, areas)
sortedContoursWithArea = sorted(contoursWithAreas, key=lambda s : s[1])
sortedContours ,sortedAreas = zip(*sortedContoursWithArea)

最后,我们在sortedContours变量中按区域对Contours进行了排序。

众所周知,轮廓是np.ndarray的列表。 因此,我们无法避免使用numpy。 那为什么不使用np.argsort()

cnts = cv2.findContours(threshed, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)[-2]
areas = np.array([cv2.contourArea(cnt) for cnt in cnts])
idxs  = areas.argsort()
cnts2 = [cnts[i] for i in idxs]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM