最快的方式得到使用OpenCV的二进制图像的白色像素数 [英] Fastest way to get number of white pixels in a binary image using OpenCV

查看:126
本文介绍了最快的方式得到使用OpenCV的二进制图像的白色像素数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是获得使用OpenCV的二进制图片中白色的像素数最快的方法?有什么比使用两个for循环和像素访问图像像素更快吗?

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?

推荐答案

要做到这一点,最简洁的方法是

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);

如果您是一个二进制图像上操作,然后调用 CV: :INRANGE()是不必要的,并简单地 CV :: countNonZero()就足够了。

If you are operating on a binary image, then the call to cv::inRange() is unnecessary, and simply cv::countNonZero() will suffice.

虽然任何方法都必须通过所有的像素迭代,这可能是能够利用OpenCV中的内置 parallel_for时_(),允许并行执行。

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.

这篇关于最快的方式得到使用OpenCV的二进制图像的白色像素数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆