gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 20:39:29 25 4
gpt4 key购买 nike

我需要将 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.

如你所见,我真的不知道自己在做什么。我很想知道,但如果我能正确完成这件事,我会更喜欢它。感谢我得到的任何帮助!

最佳答案

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

关于opencv - 如何将 8 位 OpenCV IplImage* 转换为 32 位 IplImage*?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/394488/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com