利用opencv3和python3对图像轮廓按面积排序的最优解

2024-10-04 03:23:08 发布

您现在位置:Python中文网/ 问答频道 /正文

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

_, 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变量中按区域对轮廓进行排序。在


Tags: lambda图像排序zip解决方案cv2python3轮廓
1条回答
网友
1楼 · 发布于 2024-10-04 03:23:08

众所周知,等高线是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]

相关问题 更多 >