# How to detect all the connected neighboring pixels which have same color value in opencv

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.

• floodFill() does not return the connected pixels but just replace their color. That is not what expected. – Samitha Chathuranga Apr 5 '15 at 2:56
• @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. – Niki Estner Apr 5 '15 at 7:10