RSS
 

OpenCV: Image range from [0...255] to [0...1]

21 Oct

Some time we need the image to be in some special range to be useful for some algorithm. In this case I needed my image to be in [0...1] range. I found this helpful piece of code, I tried it and I wanted to share it.

//read in a image
//this will result in a 8bit image [0...255]
IplImage* image_8U = cvLoadImage("test.jpg");

//to change the bit-depth you will have to allocate new memory
IplImage* image_32F = cvCreateImage(cvGetSize(image_8U),IPL_DEPTH_32F,image_8U->nChannels);

//now convert the 8bit Image into 32bit floating points
cvCvtScale(image_8U,image_32F);

//now all your data is stored in float values but in the original scale [0...255]
//to change this you'll have to normalize your matrix

// this will result in an image that is scaled to [0...1] by dividing through the highest value in the array
cvNormalize(image_32F,image32_F,0,1,CV_MINMAX);

[ad#AdBrite inline]

 
No Comments

Posted in OpenCV

 

Leave a Reply