简体   繁体   English

OpenCV,Python:查找轮廓内的像素

[英]OpenCV, Python : Find pixels inside a contour

I am working on a project where I am able, after some process, to find a binary image where moving objects are white, the rest being all black : 我正在一个项目上,经过一些过程,我能够找到一个二进制图像,其中移动的对象是白色的,其余的都是黑色的:

Binary image 二进制图像

Then, there's an algorithm that agglomerates the blobs which are supposed to belong together, based on the distance between them (the one in the center for example). 然后,有一种算法可以根据它们之间的距离(例如,中间的一个)将应该属于这些斑点的斑点聚在一起。 To do this, they use the findContour function so that each blob, which is flagged with a number, is represented by its contour pixels (there would be 5 in my image, the one in the center being composed of two close blobs). 为此,他们使用findContour函数,以一个带有数字标记的斑点由其轮廓像素表示(我的图像中有5个斑点,中间的斑点由两个闭合的斑点组成)。 The output of the algorithm is the flag of the blobs belonging together, so for example with the above image, from top to bottom : (1, [2, 3], 4, 5). 该算法的输出是这些斑点的标志,例如上面的图像,从上到下是属于这些斑点的标记:(1,[2、3],4、5)。

Now I want to compute a concave hull for each of these agglomerated blobs. 现在,我想为每个这些聚集的斑点计算一个凹壳。 I have the algorithm to do it, but I can't apply it on the outer pixels, I need the pixels of the whole object ! 我有算法可以执行此操作,但是无法将其应用于外部像素,我需要整个对象的像素!

How can I do that ? 我怎样才能做到这一点 ?

The problem is that if I retrieve the pixels from the original image, I lose the connection between "pixels of the image" and "blobs". 问题是,如果我从原始图像中检索像素,则会丢失“图像的像素”和“斑点”之间的连接。 The blobs only have information about the contour. 斑点仅具有有关轮廓的信息。

I'd be grateful if you had an idea on how to solve this. 如果您有解决此问题的想法,我将不胜感激。 :) :)

How about using connectedComponents (or connectedComponentsWithStats ) instead of findContours ? 如何使用connectedComponents (或connectedComponentsWithStats )代替findContours
It will find your blobs while giving you a list of all pixels in these blobs (not only the contour), in its "output" return array. 它会在“输出”返回数组中找到这些斑点的同时为您提供这些斑点中所有像素的列表(不仅是轮廓)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM