Fastest way to get number of white pixels in a binary image using OpenCV

依然范特西╮ 提交于 2019-12-19 02:14:30

问题


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


回答1:


The most succinct 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 operating on a binary image, then the call to cv::inRange() is unnecessary, and simply cv::countNonZero() will suffice.

Although any method must iterate through all the pixels, this may be able to harness OpenCV's built-in parallel_for_(), which allows parallel execution.

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



来源:https://stackoverflow.com/questions/16929190/fastest-way-to-get-number-of-white-pixels-in-a-binary-image-using-opencv

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!