I am trying to count all the white pixels in an OpenCV binary image. My current code is as follows:
whitePixels = 0;
for (int i = 0; i < height; ++i)
for (int j = 0; j < width; ++j)
if (binary.at<int>(i, j) != 0)
++whitePixels;
However, after profiling with gprof I’ve found that this is a very slow piece of code, and a large bottleneck in the program.
Is there a method which can compute the same value faster?
cv::CountNonZero. Usually the OpenCV implementation of a task is heavily optimized.