我需要將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.
正如你所看到的,我並不真正知道自己在做什麼。我很想知道,但如果我能夠正確完成這項工作,我會更加喜歡它。 感謝您的幫助!
1有該網頁上的一些有趣的信息,代碼段,其解釋我所已經嘗試過了,結果是一樣的,但是請看下面的鏈接,查看一直出錯的例子: http://files.dazjorz.com/cache/conversion.png – sgielen 2008-12-27 15:45:19
哇,是的,那是一個好的,你是對的!所以這給出了以下線索:對於紅點,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