Ruchir
Ruchir

Reputation: 845

Finding number of pixels in each blob using OpenCV in C++

I have a binary image with multiple blobs. I want to find the number of pixels in each blob. My version of openCV is below 3.0, so I don't have connectedComponents function. I was trying findContourinstead. If the image is im, I execute findContour as:

cv::findContours(im.clone(), contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);

Now, size of contour[i] or contourArea(contours[i]); don't really give the number of pixels. Is there any other function to find the number of pixels in each blob?

A post recommends cvBlobslib library but is there an inbuilt function in OpenCV (version lower than 3.0)?

Upvotes: 1

Views: 948

Answers (1)

Michael Burdinov
Michael Burdinov

Reputation: 4438

If upgrading your OpenCV is not an option, you still can take the implementation of connectedComponents from OpenCV3.0 and plant it into your own code. OpenCV is an open source project. You can get implementation of connectedComponents() from gitHub.

There are other ways to get the number of pixels, but they are really inefficient. For example you can use drawContours() function to draw contours found by findContours() one by one on some empty image, and use countNonZero() to count number of non-black pixels.

Upvotes: 3

Related Questions