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.
Aurelius
source share