I should be able to input a pixel position and get all the same coloured(in my case it should be black) pixels which are connected to it. How to do this in opencv with c++. Simply the output pixels should be connected to each other with color black. FindContours() method does not work as it cannot be feeded with a pixel.
I think you're looking for floodFill
. From the documentation:
The functions
floodFill
fill a connected component starting from the seed point with the specified color. The connectivity is determined by the color/brightness closeness of the neighbor pixels. The pixel at (x,y) is considered to belong to the repainted domain if: [...]Use these functions to either mark a connected component with the specified color in-place, or build a mask and then extract the contour, or copy the region to another image, and so on.
You're probably interested in this flag parameter:
FLOODFILL_MASK_ONLY
If set, the function does not change the image ( newVal is ignored), and only fills the mask with the value specified in bits 8-16 of flags as described above. This option only make sense in function variants that have the mask parameter.
-
$\begingroup$ floodFill() does not return the connected pixels but just replace their color. That is not what expected. $\endgroup$ – Samitha Chathuranga Apr 5 '15 at 2:56
-
2$\begingroup$ @SamithaChathuranga: Read the second paragraph again: floodFill can "either mark a connected component with the specified color in-place, or build a mask". You're probably looking for
FLOODFILL_MASK_ONLY
. $\endgroup$ – Niki Estner Apr 5 '15 at 7:10 -
$\begingroup$ Can u please describe about this Mask? I do not understand what is this Mask or what is the usage of it $\endgroup$ – Samitha Chathuranga Apr 5 '15 at 8:19
-
$\begingroup$ @SamithaChathuranga: Which part of the linked documentation did you not understand? It says "On output, pixels in the mask corresponding to filled pixels in the image are set to 1 or to the a value specified in flags...". That seems clear to me. $\endgroup$ – Niki Estner Apr 5 '15 at 8:49
-
1$\begingroup$ It is used to find the connected set of pixels with the same color, containing the seed pixel. Please read the linked documentation. There's a sample, too. $\endgroup$ – Niki Estner Apr 5 '15 at 9:37