开发者

OpenCV IplImage data to float

Is there a way to convert IplImage pointer to float pointer? Basically converting the ima开发者_JAVA技巧gedata to float. Appreciate any help on this.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜