python - 如何从轮廓矢量转换为MAT?

标签 python python-3.x opencv contour mat

这是我必须找到图像轮廓的代码:

contour, heir= cv2.findContours(hmg,cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

我想找到一个轮廓区域
cv2.contourArea(contour)

但这需要MAT数据类型,而不是轮廓 vector 类型

如何转换轮廓以便执行此操作?

最佳答案

contour对象实际上是图像中找到的所有轮廓的列表,每个轮廓的格式正确。像contours这样的名称可能更合适,但这不是必需的。

您的帖子采用了奇异的轮廓;由于过滤噪声,这种情况很少发生,但是您可以通过按降序区域排序来选择轮廓:

contours, heir= cv2.findContours(hmg,cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
contours = sorted(contours, key=cv2.contourArea, reverse=True)
print(cv2.contourArea(contours[0]))

关于python - 如何从轮廓矢量转换为MAT?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56248172/

相关文章:

python - 如何使用 CommentedMap 对 YAML 进行递归排序?

python - 如何使用 python OpenCV 缩放图像中字符的粗细?

python - 如何在 Django 中将一个 html 页面的值传递给另一个页面?

python - 在 python 中使用 pcapy 进行网络流量监控

python - `tabulate` 按键排序列

javascript - 在 PythonAnywhere 上的 html 文件中运行脚本

python-3.x - 云函数属性错误: 'bytes' object has no attribute 'get' when reading json file from cloud storage

html - Python3 html转pdf

python - 无法在 mac os x 上使用 python 和 opencv2 编写视频

python - 如何将 cv2.frames 发送到浏览器