我需要一个8位的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.

正如你看到的我真的不知道我在做什么。我很想找到答案,但我喜欢它更多,如果我能得到这个正确。 感谢您的帮助,我得到!

有帮助吗?

解决方案

也许这链路可以帮助您?

修改响应于OP的第二编辑和注释

你试过

float value = 0.5

代替

float value = 0x0000001;

我想了浮色值的范围变为从0.0到1.0,其中1.0是白色的。

其他提示

你正在寻找的功能是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分。

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

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

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

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

我叫API blImageAPI,它可以在这里找到: http://www.barbato.us/十分之二千零十/ 14 /图像数据结构为基础的 - 的shared_ptr-的IplImage /

这是非常快,并使代码非常可读的,(好用于维持算法)

它也可以被用来代替在的IplImage OpenCV的算法,而不改变任何东西。

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

的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 - 1)

    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");
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top