Ask Your Question

What's the best way to segment different coloured blobs?

asked 2012-07-11 17:44:43 -0600

Tru gravatar image

I have a requirement to segment an image like this into blobs of each colour. The goal is to find out how many blobs of each colour are present. That can be handled using findContours if I can create a binary image each for each coloured blobs. In the image below it would be - 3 blobs for red image, 1 green, 1 blue, 2 white/grey.

I've tried converting to the HSV color space and I can use the Hue channel to threshold whatever colour I want using cvInRange. But the problem is since the black background and white blobs also have an Hue value, I keep getting them in the results for other colours. Ideally, I need to eliminate the whites first and ignore the black background. Then get each of the other colours.

I tried splitting channels to extract specific channels too, before using cvInRange on it. - Core.extractChannel(mIn, v, 2);

Is there a better approach?


edit retag flag offensive close merge delete


Is your application real objects captured by a camera or synthetic circles like the example image?

kevin gravatar imagekevin ( 2012-07-12 20:16:41 -0600 )edit

It's actually real coloured pellets on a black background.

Tru gravatar imageTru ( 2012-07-14 15:51:36 -0600 )edit

2 answers

Sort by ยป oldest newest most voted

answered 2012-07-12 02:04:34 -0600

Michael Burdinov gravatar image

If the background is completely black you can simply use cvtColor(src,dst,CV_RGB2GRAY) to transfer this image into gray image. Than use findContours on dst, and check number of polygons.

If the background is not black, but at least uniform and you know its color, you can use compare(src,color,dst,CMP_EQ) to determine which pixels are part of the background and which are not. Again use findContours on dst.

If the background is not uniform, the problem became more complicate and will require more complicate solutions. Also note that detection of hue is not something that will help you for blob detection (usually).

edit flag offensive delete link more


I have a requirement to figure out how many blobs of each colour are present, hence the use of HSV. Is there any way I can extract colour details for an area inside a detected contour, so that I can get an output such as 2 red blobs and 1 blue blob?

Tru gravatar imageTru ( 2012-07-12 02:31:22 -0600 )edit

In that case it is enough to check color of pixel at center of each contour. Center can be calculated via bounding box or moments.

Michael Burdinov gravatar imageMichael Burdinov ( 2012-07-12 05:41:30 -0600 )edit

answered 2012-07-13 01:48:51 -0600

Kirill Kornyakov gravatar image

I would suggest to try histograms. You can call calcHist and then calcBackProject. As a result you will have a Mat with pixel values which show the histogram bin index = color ID. Having such Mat it should be easy to understand how many blobs of every color ID do you have, i.e. by contour analysis.

edit flag offensive delete link more

Question Tools


Asked: 2012-07-11 17:44:43 -0600

Seen: 6,954 times

Last updated: Jul 13 '12