簡體   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