The fastest way to get the number of white pixels in a binary image using OpenCV

What is the fastest way to get the number of white pixels in a binary image using OpenCV? Is there anything faster than using two for loops and pixel-by-pixel access to the image?

+7
source share
1 answer

The most concise way to accomplish this is:

cv::Mat image, mask; //image is CV_8UC1 cv::inRange(image, 255, 255, mask); int count = cv::countNonZero(mask); 

If you are working with a binary image, a cv::inRange() call is not needed, and just cv::countNonZero() will be enough.

Despite the fact that any method must go through all the pixels, it can use the built-in OpenCV parallel_for_() , which allows parallel execution.

If your image is continuous, you can iterate over all the data with a single loop.

+11
source

All Articles