2011-02-25 77 views
1

只想在一個窗口中顯示float數組,我使用的代碼是:問題顯示float數組與OpenCV的

CvSize size; 
size.height = HEIGHT ; //(640) 
size.width = WIDTH; //(1680) 
IplImage* ipl_image_p = cvCreateImageHeader(size, IPL_DEPTH_32F, 1); 
ipl_image_p->imageData = (char*)my_float_image_data; //imageData only accepts char* 
ipl_image_p->imageDataOrigin = ipl_image_p->imageData; 


cvNamedWindow("Image", 1); 
cvShowImage("Image", ipl_image_p); 
cvWaitKey(0); 
cvDestroyAllWindows(); 

彈出是全白的圖像。我究竟做錯了什麼 ?

感謝

編輯:

感謝Martin貝克特快速回答這個。我也有與16位圖像相同的問題,是否鏈接? (我的圖像從0到1000的範圍內)

unsigned short* temp = new unsigned short[HEIGHT*WIDTH]; 
for(int i = 0 ; i < HEIGHT*WIDTH; i++){ 
    temp[i] = (unsigned short)my_float_image_data[i]; 
} 

CvSize size; 
size.height = HEIGHT ; 
size.width = DISPLAY_DATA_WIDTH; 
IplImage* ipl_image_p = cvCreateImageHeader(size, IPL_DEPTH_16U, 1); 
ipl_image_p->imageData = (char*)temp; 
ipl_image_p->imageDataOrigin = ipl_image_p->imageData; 

cvNamedWindow("Image", 1); 
cvShowImage("Image", ipl_image_p); 
cvWaitKey(0); 
cvDestroyAllWindows(); 
+0

AFAIK你不能顯示16位數據 - 您需要先轉換爲8位,請參閱cvtColor() – 2011-02-25 19:17:22

回答

3

如果值是0-1

之間的OpenCV只能顯示浮點圖像不知您浮點數據的範圍?

+0

感謝您的快速回答 – Simon 2011-02-25 16:53:34

1

假設你的字符沒有被格式化爲浮動圖案是否安全?

浮點色彩去從0.0到1.0,並uchars去從0到255以下代碼修復它:

// h is height, w is width, c is current channel (0 to 2) 
int b = ((uchar *)(img->imageData + h*img->widthStep))[w*img->nChannels + c]; 
((float *)(img2->imageData + h*img2->widthStep))[w*img2->nChannels + c] = ((float)b)/255.0; 

這可能是幫助過:How to convert an 8-bit OpenCV IplImage* to a 32-bit IplImage*?