1

I just want to display an array of floats in a window, the code I am using is:

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

The image that pops up is all white. What am I doing wrong ?

Thanks

EDIT:

Thanks to Martin Beckett for answering this fast. I also have the same problem with 16bit images, is it linked? (my image ranges from 0 to 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();
mskfisher
  • 3,291
  • 4
  • 35
  • 48
Simon
  • 277
  • 1
  • 5
  • 13

2 Answers2

3

OpenCV can only display floating point images if the values are between 0-1

Whats the range of your float data?

Martin Beckett
  • 94,801
  • 28
  • 188
  • 263
1

Is it safe to assume that your chars arent already formatted as floats?

floating point colors go from 0.0 to 1.0, and uchars go from 0 to 255. The following code fixes it:

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

this might be of help too: How to convert an 8-bit OpenCV IplImage* to a 32-bit IplImage*?

Community
  • 1
  • 1
f0ster
  • 549
  • 3
  • 12