2013-01-03 96 views
2
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()))的未知函數」Opencv 2.4.3如何將IPL_DEPTH_8U轉換爲IPL_DEPTH_32F?

我已搜查對於很多方法來說,但他們都沒有工作?

回答

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

THX,我現在解決它。 – Leoxu

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

希望它是簡單的方法...

+0

Thx爲您的答案,它的工作! – Leoxu