Converting to Floating Point Image from .tif

流过昼夜 提交于 2019-12-12 01:06:39

问题


I am relatively new to C++ and coding in general and have run into a problem when attempting to convert an image to a floating point image. I am attempting to do this to eliminate round off errors with calculating the mean and standard deviation of pixel intensity for images as it starts to effect data quite substantially. My code is below.

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

And I am running into this error

OpenCV Error: Bad argument (Array should be CvMat or IplImage) in an unknown function, file ......\modules\core\src\array.cpp, line 1238

I've looked everywhere and everyone was saying to convert img to CvMat which I attempted above. When I did that as above code shows I get OpenCV Error: Bad argument (Unknown array type) in unknown function, file ......\modules\core\src\matrix.cpp line 697

Thanks for your help in advance.


回答1:


Just use C++ OpenCV interface instead of C interface and use convertTo function to convert between data types.

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

Second part of the question To calculate the mean of all elements in 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; 



回答2:


Here is my code for calculating the average in 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 Value\n" << 255*avg;

For MATLAB I just load the image and take Q = mean(img(:)); which returns 1776.23 And for the return of 1612.36 I used cv:Scalar z = mean(dst);



来源:https://stackoverflow.com/questions/16741791/converting-to-floating-point-image-from-tif

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!