我看到有类似的问题,但并不能完全回答我的问题,所以这是我的问题。
在C++中使用OpenCV运行我提供的代码,它返回一个平均像素值为6.32。然而,当我打开图像并在MATLAB中使用mean函数时,它返回大约6.92左右的平均像素强度。正如您所见,我将OpenCV值转换为double以尝试缓解此问题,并发现OpenCV将图像加载为一组整数,而MATLAB将图像加载为十进制值,这些值大致相同,但显然不完全相同于整数。所以我的问题是,作为编码新手,哪个是正确的?我假设MATLAB返回更准确的值,如果是这样,我想知道是否有一种方法以相同的方式加载图像以避免差异。
谢谢,以下是代码
Mat img = imread("Cells2.tif");
cv::cvtColor(img, img, CV_BGR2GRAY);
cv::imshow("stuff",img);
Mat dst;
if(img.channels() == 3)
{
img.convertTo(dst, CV_64FC1);
}
else if (img.channels() == 1)
{
img.convertTo(dst, CV_64FC1);
}
cv::imshow("output",dst/255);
int NumPixels = img.total();
double avg;
double c = 0;
double std;
for(int y = 0; y < dst.cols; y++)
{
for(int x = 0; x < dst.rows; x++)
{
c+=dst.at<double>(x,y)*255;
}
}
avg = c/NumPixels;
cout << "asfa = " << c << endl;
double deviation;
double var;
double z = 0;
double q;
//for(int a = 0; a<= img.cols; a++)
for(int y = 0; y< dst.cols; y++)
{
//for(int b = 0; b<= dst.rows; b++)
for(int x = 0; x< dst.rows; x++)
{
q=dst.at<double>(x,y);
deviation = q - avg;
z = z + pow(deviation,2);
//cout << "q = " << q << endl;
}
}
var = z/(NumPixels);
std = sqrt(var);
cv::Scalar avgPixel = cv::mean(dst);
cout << "Avg Value = " << avg << endl;
cout << "StdDev = " << std << endl;
cout << "AvgPixel =" << avgPixel;
cvWaitKey(0);
return 0;
}