OpenCV IplImage data to float

旧城冷巷雨未停 提交于 2019-12-01 21:29:58

Use cvConvert(src,dst) where src is the source image and dst is the preallocated floating point image.

E.g.

dst = cvCreateImage(cvSize(src->width,src->height),IPL_DEPTH_32F,1);
cvConvert(src,dst);
// Original image gets loaded as IPL_DEPTH_8U
IplImage* colored = cvLoadImage("coins.jpg", CV_LOAD_IMAGE_UNCHANGED);
if (!colored)
{   
    printf("cvLoadImage failed!\n");
    return; 
}   

// Allocate a new IPL_DEPTH_32F image with the same dimensions as the original
IplImage* img_32f = cvCreateImage(cvGetSize(colored), 
                                  IPL_DEPTH_32F, 
                                  colored->nChannels);
if (!img_32f)
{   
    printf("cvCreateImage failed!\n");
    return;
}   

cvConvertScale(colored, img_32f);

// quantization for 32bit. Without it, this img would not be displayed properly
cvScale(img_32f, img_32f, 1.0/255);

cvNamedWindow("test", CV_WINDOW_AUTOSIZE);
cvShowImage ("test", img_32f);

You can't convert the image to float by simply casting the pointer. You need to loop over every pixel and calculate the new value.

Note that most float image types assume a range of 0-1 so you need to divide each pixel by whatever you want the maximum to be.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!