OpenCV:使用findContours在二进制图像中仅获取黑色轮廓 [英] OpenCV: Get only black contours in a binary image with findContours

查看:959
本文介绍了OpenCV:使用findContours在二进制图像中仅获取黑色轮廓的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是OpenCV的新手,请多多包涵.

I am new to OpenCV, so please bear with me.

当前,我在二进制图像中同时获得了白色和黑色物体的轮廓.我只想要黑色轮廓(因此值是0).我可以调整某种设置以获得此结果吗?还是可以过滤它们?

Currently, I get contours of both white and black things in my binary image. I only want black contours though (so where the value is 0). Is there some kind of setting I can adjust to get this result? Or can I filter them?

也:cv.findContours()确实返回轮廓和层次结构.层次结构是用来做什么的?

Also: cv.findContours() does return both the contours and a hierarchy. What is hierarchy used for?

最后:轮廓似乎由具有多个坐标的数组组成.是什么意思?

And lastsly: Contours seemingly consist of an array with multiple coordinates. What do they mean?

推荐答案

cv2.findContours查找图像中的所有轮廓.有些在内部,有些在外部,有些嵌套在其他轮廓之内.

cv2.findContours finds all the contours in your image. Some are internal, some are external, some are nested inside other contours.

因此,该方法返回多个坐标. 层次结构是一个矢量,其中包含有关所提取轮廓的这些不同级别(外部,嵌套,内部等)的信息.

For this reason the method returns multiple coordinates. Hierarchy is a vector that contains information about these different levels of contours extracted (external, nested, internal etc..).

不过,您可以设置 retrievalMode 根据层次过滤轮廓.

You can however set a retrievalMode to filter contours based on hierarchy.

在任何情况下它们都不会包含有关颜色的信息,因此您需要以某种方式对其进行过滤.

Under no circumstances they contain information about color so you need to filter them in some way.

我可能会补充说,您可以做的明智的事情是在获取轮廓之前对图像进行过滤,因此您只能根据所选择的颜色或颜色范围在创建的蒙版中找到轮廓(请参见cv2.inRange)

I might add that a sensible thing you can do is filter the image before getting contours, so you find contours only in the mask you create, based on the color or range of colours of your choice (see cv2.inRange)

这篇关于OpenCV:使用findContours在二进制图像中仅获取黑色轮廓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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