从.tif转换为浮点图像



我对c++和一般编码比较陌生,并且在尝试将图像转换为浮点图像时遇到了一个问题。我试图通过计算图像像素强度的平均值和标准偏差来消除舍入误差,因为它开始对数据产生相当大的影响。我的代码如下:

Mat img = imread("Cells2.tif");
cv::namedWindow("stuff", CV_WINDOW_NORMAL);
cv::imshow("stuff",img);
CvMat cvmat = img;
Mat dst = cvCreateImage(cvGetSize(&cvmat),IPL_DEPTH_32F,1);
cvConvertScale(&cvmat,&dst);
cvScale(&dst,&dst,1.0/255);
cvNamedWindow("Test",CV_WINDOW_NORMAL);
cvShowImage("Test",&dst);

我遇到了这个错误

OpenCV错误:错误的参数(数组应该是CvMat或IplImage)在一个未知的函数,文件......modulescoresrc Array .cpp,行1238

我到处都看了,每个人都说要把我转换成CvMat,我在上面尝试过。当我这样做的时候,正如上面的代码所示,我得到OpenCV错误:未知函数中的错误参数(未知数组类型),文件......modulescoresrcmatrix.cpp行697

提前感谢您的帮助

使用c++ OpenCV接口代替C接口,使用convertTo函数在数据类型之间进行转换

Mat img = imread("Cells2.tif");  
cv::imshow("source",img);
Mat dst;  // destination image
// check if we have RGB or grayscale image
if (img.channels() == 3) {
    // convert 3-channel (RGB) 8-bit uchar image to 32 bit float
    src.convertTo(dst, CV_32FC3);   
}
else if (img.channels() == 1) {
    // convert 1-chanel (grayscale) 8-bit uchar image to 32 bit float
    img1.convertTo(dst, CV_32FC1);
}
// display output, note that to display dst image correctly 
// we have to divide each element of dst by 255 to keep 
// the pixel values in the range [0,1].
cv::imshow("output",dst/255); 
waitKey();

问题第二部分计算dst中所有元素的平均值

cv::Salar avg_pixel;
double avg;
// note that Scalar is a vector. 
// If your image is RGB, Scalar will contain 3 values, 
// representing color values for each channel.
avg_pixel = cv::mean(dst);
if (dst.channels() == 3) {
    //if 3 channels
    avg = (avg_pixel[0] + avg_pixel[1] + avg_pixel[2]) / 3;  
}
if(dst.channels() == 1) {
    avg = avg_pixel[0];
} 
cout << "average element of m: " << avg << endl; 

这是我在c++ OpenCV中计算平均值的代码。

int NumPixels = img.total();

double avg;
double c;
    for(int y = 0; y <= img.cols; y++)
        for(int x = 0; x <= dst.rows; x++)
        c+=img.at<uchar>(x,y);
        avg = c/NumPixels;
    cout << "Avg Valuen" << 255*avg;

对于MATLAB,我只是加载图像并取Q = mean(img(:));哪个返回1776.23对于1612.36的返回,我使用cv:Scalar z = mean(dst);

相关内容

  • 没有找到相关文章