Ask Your Question

Finding the pixel precise area of white blobs

asked 2015-06-16 03:50:51 -0500

Jon Snow gravatar image

Dear OpenCV community,

I'm fairly new to OpenCV and I need to find the pixel precise area of white blobs in images similar to this one:

I know by now that the results returned by findCountours(...) and a subsequent call to contourArea(...) are only an approximation. (I'm passing CV_CHAIN_APPROX_NONE to the findCountours method, if this is of any relevance...)

An example of my problem: The result returned by contourArea(...) for the blob with the red bounding box is 28, but the number of white pixels is 38. How can I obtain this number in OpenCV? I know about cv::countNonZero, but in some of my images white blobs overlap with the bounding rectangle of neighbouring blobs, making it impossible to extract a rectangular ROI for said function. So, what would be the best course of action to measure the blob area as precisely as possible?

Thanks in advance for any help you are able to provide!

edit retag flag offensive close merge delete



I believe the return value of cv::floodfill is the area, but it seems to be undocumented. Maybe there are better options I don't know of.

boaz001 gravatar imageboaz001 ( 2015-06-16 04:00:42 -0500 )edit

Thanks a lot boaz001, I like your answer because it's so simple. ;-) Indeed, this seems to work perfectly, why on earth is this undocumented?

The only downside to this approach is speed. Some of my images are huge (4096 x 10000 is not uncommon) and it can take up to 1000 ms to floodfill all blobs, which is unacceptable in my case.

Does anyone know of a more performant solution?

Jon Snow gravatar imageJon Snow ( 2015-06-16 04:53:06 -0500 )edit

Use connectedComponents. It will attached a number to blobs and after scan this images to have all points

LBerger gravatar imageLBerger ( 2015-06-16 09:20:42 -0500 )edit

Hmmm make use of the hierarchy parameter, which allows you to find blobs inside the main blob. I would not use the approximations for the contours, to avoid this difference of white pixels.

StevenPuttemans gravatar imageStevenPuttemans ( 2015-06-17 06:14:17 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2015-06-22 08:59:34 -0500

Jon Snow gravatar image

Thanks guys! I think I will use floodfill for really small blobs where precision is important to me and findContours for the larger ones. This twofold solution may not be elegant, but it is precise enough for me, it's reasonably fast and above all very easy to implement!

edit flag offensive delete link more


Btw, I noticed that cv::floodfill runs about 10 times faster when I pass 0 instead of 4 (the default) for its "flags" parameter. It still returns the correct blob size!

The documentation states: Operation flags. The first 8 bits contain a connectivity value. The default value of 4 means that only the four nearest neighbor pixels (those that share an edge) are considered. A connectivity value of 8 means that the eight nearest neighbor pixels (those that share a corner) will be considered. The next 8 bits (8-16) contain a value between 1 and 255 with which to fill the mask (the default value is 1). For example, 4 | ( 255 << 8 ) will consider 4 nearest neighbours and fill the mask with a value of 255.

Jon Snow gravatar imageJon Snow ( 2015-06-23 08:39:45 -0500 )edit

Question Tools

1 follower


Asked: 2015-06-16 03:50:51 -0500

Seen: 1,285 times

Last updated: Jun 22 '15