Opencv 2.4.3如何将IPL_DEPTH_8U转换为IPL_DEPTH_32F?

时间:2013-01-03 15:19:04

标签: opencv

char* filename1="1.bmp";  
IplImage* greyLeftImg= cvLoadImage(filename1,0);
char* filename2="2.bmp";
IplImage* greyRightImg= cvLoadImage(filename2,0); 

IplImage* greyLeftImg32=cvCreateImage(cvSize(width,height),32,greyLeftImg->nChannels);//IPL_DEPTH_32F 
IplImage* greyRightImg32=cvCreateImage(cvSize(width,height),32,greyRightImg->nChannels);

总是失败,在未知函数中说“断言失败(src.size == dst.size&& dst.type()== CV_8UC(src.channels())”

我搜索了很多方法,但它们似乎都没有用?

2 个答案:

答案 0 :(得分:2)

将opencv中的任何灰度8位或16位uint图像转换为32位浮动类型的简单步骤就像这样......

IplImage* img = cvLoadImage( "E:\\Work_DataBase\\earth.jpg",0);
IplImage* out = cvCreateImage( cvGetSize(img), IPL_DEPTH_32F, img->nChannels);

double min,max;

cvMinMaxLoc(img,&min,&max);

// Remember values of the floating point image are in the range of 0 to 1, which u 
// can't visualize by cvShowImage().......
cvCvtScale(img,out,1/ max,0);

希望这很简单...

答案 1 :(得分:1)

这是一个将任何IplImage转换为32位浮点数的简单函数。

IplImage* convert_to_float32(IplImage* img)
{
    IplImage* img32f = cvCreateImage(cvGetSize(img),IPL_DEPTH_32F,img->nChannels);

    for(int i=0; i<img->height; i++)
    {
        for(int j=0; j<img->width; j++)
        {
            cvSet2D(img32f,i,j,cvGet2D(img,i,j));
        }
    }
    return img32f;
}

一个重要的考虑因素是,对于OpenCV中的浮点图像,只能看到像素值为0.0和1.0的那些图像。 要显示浮点图像,必须将值从0.0缩放到1.0。

以下是如何执行此操作的示例:

IplImage* img8u = cvLoadImage(filename1,0);

IplImage* img32f = convert_to_float32(img8u);

cvShowImage("float image",img32f); //Image will not be shown correctly
cvWaitKey(0);

cvScale(img32f, img32f, 1.0/255.0);

cvShowImage("float image normalized",img32f); //Image will be shown correctly now 
cvWaitKey(0);