OpenCV gets black pixels within the border of the path

I'm currently trying to get all the black pixels that are present within the border of the path. I'm not interested in the contour border, but rather in the black pixels that lie inside the border and make up the actual image. It would be great if I could get the actual coordinates of the image.

I tried using the copyTo method using a mask, but I believe that I am not setting the correct parameters. While I also tried to use Core.fillPoly, which simply fills the entire contour area with the color specified in the command, and this is not useful for getting pixel information. Can someone direct me here? I am working on Android 2.2 with OpenCV 2.3.1.

+4
source share
1 answer

There is a set of pixels that fall within the boundary of the path. There is another set of pixels that are black. You want to find the intersection of these two sets, that is, a lot of pixels that are both inside the border and black.

To do this, I would:

  • Draw the outline as a filled shape, white on black (in its own way) so that it is a mask. You can use cv::drawContours or cv::fillPoly .

  • Filter the black pixels from the image as another mask. You can use cv::threshold with THRESH_BINARY_INV and a threshold value of zero.

  • Find the intersecting set using bitwise_and or just the & operator, for example matResult = mat1 & mat2;

The result is a matrix of the same size as your image, with non-zero values ​​only for pixels that satisfy your criteria for being inside the outline and black in the image. You can get individual coordinates by looping this matrix and checking non-zero values.

+4
source

All Articles