Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将8位OpenCV IplImage*转换为32位IplImage*?_Opencv_Iplimage_Bpp_Bits Per Pixel - Fatal编程技术网

如何将8位OpenCV IplImage*转换为32位IplImage*?

如何将8位OpenCV IplImage*转换为32位IplImage*?,opencv,iplimage,bpp,bits-per-pixel,Opencv,Iplimage,Bpp,Bits Per Pixel,我需要将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 = i

我需要将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.
//通用代码
img2=cvCreateImage(cvSize(img->宽度,img->高度),32,3);
int height=img->height;
int width=img->width;
int channels=img->n通道;
int step1=img->WITHSTEP;
int step2=img2->WITHSTEP;
int depth1=img->深度;
int depth2=img2->深度;
uchar*data1=(uchar*)img->imageData;
uchar*data2=(uchar*)img2->imageData;
对于(h=0;h也许这可以帮助你

编辑以响应OP和评论的第二次编辑

你试过了吗

float值=0.5

而不是

float值=0x0000001;


我认为浮点颜色值的范围从0.0到1.0,其中1.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;

非常感谢Stefan Schmidt帮助我解决这个问题!

您正在寻找的函数是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。

如果不使用点(),一些编译器将理解为整数除法,并给出整数结果(在本例中为零).

您可以使用boost::shared_ptr和template metaprogramming创建一个IplImage包装器。我已经完成了这项工作,并实现了自动垃圾收集,以及从一个深度到另一个深度或从一个通道到多通道图像的自动图像转换

我调用了API blImageAPI,可以在这里找到:

它速度非常快,并且使代码可读性非常强(有利于维护算法)

它也可以用来代替opencv算法中的IplImage,而无需更改任何内容

祝你好运,祝你编写算法愉快!!!

IplImage*img8,*img32;
img8=cvLoadImage(“a.jpg”,1)

//为了确认,请检查像素值(0-1之间)

for(int row=0;rowheight;row++){
浮点*pt=(浮点*)(img32->imageData+row*img32->widthStep);
for(int col=0;col
该页面上有一些有趣的信息,一些代码解释了我已经尝试过的内容。但是结果是一样的。请查看下面的链接,以了解经常出错的示例:哇,是的,这是一个好消息,你是对的!因此给出了以下线索:对于红色斑点,R是1.0,GB是0.0,a对于白色,所有三个都是1.0。因此,在转换时,RGB值应该从0到255转换为0.0到1.0。将值更改为b/255会使图像变为黑色+红色。明白了!int b=((uchar)(img->imageData+himg->widthStep))[wimg->nChannels+0];//b((float*)(img2->imageData+himg2->widthStep))[w*img2->nChannels+0]=((float)b)/255.0;我试过了,但它既不接受输出也不接受输入比例。或者可能我只是错误地使用了它。传递双精度作为比例因子很重要(这就是255后面有“.”的原因)1/255将被评估为整数除法,导致缩放因子为0。您的代码究竟与CVCurnStCube()没有什么关系?我希望我能多加一点赞成,我也赞成你的评论和你的答案。我已经在这个问题上挣扎了至少8个小时!对于使用C++ API的人:IM8.;而不是使用
cvGetSize(im8->宽度,im8->高度)
使用
cvGetSize(im8)
    cvNamedWindow("Convert",1);
    img32 = cvCreateImage(cvGetSize(img8),IPL_DEPTH_32F,3);
    cvConvertScale(img8,img32,1.0/255.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");