我想在非黑色像素的cv::mean
上执行cv::mat
。使用像:
cv::threshold(image, thresholded, 0, 255, cv::THRESH_BINARY);
return cv::mean(image,thresholded);
但是,还有更快的方法吗?也许在OpenCV中有某种内置的颜色屏蔽技术?
P.S。黑色我的意思是(0)
或(0,0,0)
;不应该进行四舍五入。
答案 0 :(得分:3)
更紧凑的形式是:
threshold(image, thresholded, 1, 255, cv::THRESH_BINARY);
还要注意:
threshold(image, thresholded, 0, 255, cv::THRESH_BINARY);
您正在屏蔽所有>的像素1,即0和1都将设置为0.您需要:
With threshold: 6.98269
With > 0 : 4.75043
掩盖所有非零像素。
<强>性能强>
这种方法也快一点:
#include "opencv2/opencv.hpp"
#include <iostream>
using namespace std;
using namespace cv;
int main(int, char**)
{
Mat1b img(1000, 1000);
randu(img, 0, 3);
{
double tic = double(getTickCount());
Mat1b thresholded;
threshold(img, thresholded, 0, 255, cv::THRESH_BINARY);
Scalar m = cv::mean(img, thresholded);
double toc = (double(getTickCount()) - tic) * 1000.0 / getTickFrequency();
cout << "With threshold: " << toc << endl;
}
{
double tic = double(getTickCount());
Scalar m = mean(img, img > 0);
double toc = (double(getTickCount()) - tic) * 1000.0 / getTickFrequency();
cout << "With > 0 : " << toc << endl;
}
getchar();
return 0;
}
测试代码:
compat-libstdc++-33-3.2.3-69-el6.x86_64.rpm