使用OpenCV显示浮点数组时出现问题

时间:2011-02-25 16:29:26

标签: image opencv floating-point

我只想在窗口中显示一个浮点数组,我使用的代码是:

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 Beckett快速回答这个问题。我也有与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();

2 个答案:

答案 0 :(得分:3)

如果值在0-1

之间,OpenCV只能显示浮点图像

您的浮动数据范围是什么?

答案 1 :(得分: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*?