OpenCV: Get only black contours in a binary image with findContours

本小妞迷上赌 提交于 2019-12-13 20:25:10

问题


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

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?

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?


回答1:


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..).

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.

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)



来源:https://stackoverflow.com/questions/58036541/opencv-get-only-black-contours-in-a-binary-image-with-findcontours

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!