2008-12-27 142 views
11

我需要将8位IplImage转换为32位IplImage。使用来自全网的文档我试过以下的东西:如何将8位OpenCV IplImage *转换为32位IplImage *?

// general code 
img2 = cvCreateImage(cvSize(img->width, img->height), 32, 3); 
int height = img->height; 
int width  = img->width; 
int channels = img->nChannels; 
int step1  = img->widthStep; 
int step2  = img2->widthStep; 
int depth1 = img->depth; 
int depth2 = img2->depth; 
uchar *data1 = (uchar *)img->imageData; 
uchar *data2 = (uchar *)img2->imageData; 

for(h=0;h<height;h++) for(w=0;w<width;w++) for(c=0;c<channels;c++) { 
    // attempt code... 
} 

// attempt one 
// result: white image, two red spots which appear in the original image too. 
// this is the closest result, what's going wrong?! 
// see: http://files.dazjorz.com/cache/conversion.png 
((float*)data2+h*step2+w*channels+c)[0] = data1[h*step1+w*channels+c]; 

// attempt two 
// when I change float to unsigned long in both previous examples, I get a black screen. 

// attempt three 
// result: seemingly random data to the top of the screen. 
data2[h*step2+w*channels*3+c] = data1[h*step1+w*channels+c]; 
data2[h*step2+w*channels*3+c+1] = 0x00; 
data2[h*step2+w*channels*3+c+2] = 0x00; 

// and then some other things. Nothing did what I wanted. I couldn't get an output 
// image which looked the same as the input image. 

正如你所看到的,我并不真正知道自己在做什么。我很想知道,但如果我能够正确完成这项工作,我会更加喜欢它。 感谢您的帮助!

回答

6

也许这个link可以帮到您吗?

编辑为响应OP的第二编辑和注释

您是否尝试过

float value = 0.5

,而不是

float value = 0x0000001;

我以为范围对于float颜色值从0.0到1.0,wh 1.0是白色的。

+0

1有该网页上的一些有趣的信息,代码段,其解释我所已经尝试过了,结果是一样的,但是请看下面的链接,查看一直出错的例子: http://files.dazjorz.com/cache/conversion.png – sgielen 2008-12-27 15:45:19

+0

哇,是的,那是一个好的,你是对的!所以这给出了以下线索:对于红点,R是1.0,GB是0.0,而对于白色,三者都是1.0。因此,在转换时,RGB值为d从0到255转换为0.0到1.0。将值更改为b/255可使图像变为黑色+红色。 – sgielen 2008-12-27 17:50:57

2

浮点颜色从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; 

很多很多的感谢斯特凡施密特帮我解决这个问题!

28

您正在寻找的功能是cvConvertScale()。它自动为您做任何类型的转换。您只需指定要缩放1/255倍(将范围[0 ... 255]映射到[0 ... 1])。

例子:

IplImage *im8 = cvLoadImage(argv[1]); 
IplImage *im32 = cvCreateImage(cvSize(im8->width, im8->height), 32, 3); 

cvConvertScale(im8, im32, 1/255.); 

注意在1/255.点 - 强制双师。没有它,你得到的0

1

如果你不把点(。)的比例,一些编译器会明白是作为一个int师,给你(在这种情况下,零)INT结果。

1

您可以使用boost :: shared_ptr和模板元编程创建一个IplImage包装器。我已经做到了,我可以自动进行垃圾收集,以及从一个深度到另一个深度的自动图像转换,或者从单通道图像到多通道图像。

我已经叫API blImageAPI,它可以在这里找到: http://www.barbato.us/2010/10/14/image-data-structure-based-shared_ptr-iplimage/

这是非常快,而且使代码非常可读的,(有利于维持算法)

它也可以用来而不是在opencv算法中的IplImage而不改变任何东西。

祝你好运,玩得开心写算法!

0

IplImage * img8,* img32;
img8 = cvLoadImage(“a。JPG”,1);

cvNamedWindow("Convert",1); 
    img32 = cvCreateImage(cvGetSize(img8),IPL_DEPTH_32F,3); 
    cvConvertScale(img8,img32,1.0/255.0,0.0); 

//进行确认检查的像素值(在0 - )

for(int row = 0; row < img32->height; row++){ 
       float* pt = (float*) (img32->imageData + row * img32->widthStep); 
       for (int col = 0; col < width; col++) 
          printf("\n %3.3f , %3.3f , %3.3f ",pt[3*col],pt[3*col+1],pt[3*col+2]);     
      } 

    cvShowImage("Convert",img32); 
    cvWaitKey(0); 
    cvReleaseImage(&img8); 
    cvReleaseImage(&img32); 
    cvDestroyWindow("Convert");