Finding the pixel precise area of white blobs
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:
http://abload.de/img/partkgs9p.png
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!
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.
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?
Use connectedComponents. It will attached a number to blobs and after scan this images to have all points
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.