从.tif转换为浮点图像 [英] Converting to Floating Point Image from .tif

查看:146
本文介绍了从.tif转换为浮点图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一般都不是C ++和编码专家,在尝试将图像转换为浮点图像时遇到了问题.我正在尝试通过计算图像像素强度的均值和标准偏差来消除舍入误差,因为它开始显着影响数据.我的代码在下面.

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

我遇到了这个错误

OpenCV错误:未知函数中的错误参数(数组应为CvMat或IplImage),文件...... \ modules \ core \ src \ array.cpp,第1238行

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

我到处都看过,每个人都在说要将img转换为我在上面尝试过的CvMat. 当我这样做时,如上面的代码所示,我得到了 OpenCV错误:未知函数,文件...... \ modules \ core \ src \ matrix.cpp第697行错误的参数(未知数组类型)

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

谢谢您的帮助.

推荐答案

只需使用C ++ OpenCV接口而不是C接口并使用

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

问题的第二部分:计算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; 

这篇关于从.tif转换为浮点图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆