OpenCV:按区域排序轮廓(Python) [英] OpenCV: Ordering a contours by area (Python)

查看:412
本文介绍了OpenCV:按区域排序轮廓(Python)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

OpenCV库提供了一个函数,该函数返回二进制(阈值)图像的轮廓。

The OpenCV library provides a function that returns a set of contours for a binary (thresholded) image. The contourArea() can be applied to find associated areas.

默认情况下,findContours()输出的轮廓列表是否按面积排序?如果不是,有人知道cv2函数按区域排序轮廓列表吗?

Is the list of contours out outputted by findContours() ordered in terms of area by default? If not, does anyone know of a cv2 function that orders a list of contours by area?

请使用Python而不是C语言提供响应。

Please provide responses in Python, not C.

推荐答案

使用排序的并按区域键排序:

cnts = cv2.findContours(boolImage.astype(np.uint8), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if imutils.is_cv2() else cnts[1]  
cntsSorted = sorted(cnts, key=lambda x: cv2.contourArea(x))

这篇关于OpenCV:按区域排序轮廓(Python)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆