2.4.3如何转换IPL_DEPTH_8U到IPL_DEPTH_32F


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

Always failed,表示" Assertion failed (src. src. error) "。Size == dst。大小,,dst.type() == CV_8UC(src.channels())) in unknown function"

我已经搜索了许多方法,但似乎没有一个工作?

将opencv中的灰度8位或16位单元图像转换为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);

下面是一个将任意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);

相关内容

  • 没有找到相关文章

最新更新