1、计算图像中像素非零个数, 函数 int countNonZero( InputArray src ),单通道图像。
1 int val=countNonZero(img)//返回值为图像img所有不为0的像素
2、返回图像src像素的平均值,Scalar mean(InputArray src, InputArray mask = noArray());
Scalar val= mean(src);
3、返回像素不为0的平均值,float ImageNoZeroMean(Mat img)
1 float ImageNoZeroMean(Mat img) 2 { 3 int c = img.cols; 4 int r = img.rows; 5 int val = r * c; 6 int a = countNonZero(img); 7 Scalar b = mean(img); 8 double Imgmean = b[0] * val / a; 9 return Imgmean; 10 11 }
1 float ImageNoZeroMean(Mat img) 2 { 3 int c = img.cols; 4 int r = img.rows; 5 int val = r * c; 6 Scalar b = sum(img);//所有像素总和 7 double Imgmean = b[0] /val; 8 return Imgmean; 9 10 }