2011-05-23 118 views
2

我有兩個問題將我的圖像(只有jpeg文件)從NSImage轉換爲IplImage。我的第一個問題是轉換後的圖像與原始圖像顏色強度不一樣。轉換後的圖像在某種程度上比原始圖像更無音。
我的第二個問題是隻有8/10圖像完全轉換,2/10圖像只有部分圖像會被轉換。我的意思是隻有圖像的左上角會被轉換。我希望你能理解我,我的英語不是最好的。轉換NSImage到IplImage

IplImage* convert(NSImage* bild){ 

    //converting the NSImage into an IplImage 
    NSBitmapImageRep *bitmap2 = [NSBitmapImageRep imageRepWithData:[bild TIFFRepresentation]]; 

    int depth  = [bitmap2 bitsPerSample]; 
    int channels = [bitmap2 samplesPerPixel]; 
    int height  = [bitmap2 size].height; 
    int width  = [bitmap2 size].width; 


    IplImage *iplpic = cvCreateImage(cvSize( width,height), depth, channels); 
    cvSetImageData(iplpic, [bitmap2 bitmapData], [bitmap2 bytesPerRow]); 

    for (int i = 0; i < iplpic->imageSize; i += 3) { 
     uchar tempR, tempG, tempB; 
     tempR = iplpic->imageData[i]; 
     tempG = iplpic->imageData[i+1]; 
     tempB = iplpic->imageData[i+2]; 

     iplpic->imageData[i+2] = tempR; 
     iplpic->imageData[i+1] =tempG; 
     iplpic->imageData[i] = tempB; 

    } 

    cvSaveImage("/Users/goette/out.jpg", iplpic, 0); 
    return iplpic; 
} 

回答

0

我解決了第二個問題。我改變了這樣的代碼:

NSBitmapImageRep *bitmap2 = [NSBitmapImageRep imageRepWithData:[bild TIFFRepresentation]]; 

NSImage* bild1 = [[NSImage alloc] initWithSize:NSMakeSize([bitmap2 pixelsWide],[bitmap2 pixelsHigh])]; 

int depth  = [bitmap2 bitsPerSample]; 
int channels = [bitmap2 samplesPerPixel]; 
int height  = [bild1 size].height; 
int width  = [bild1 size].width; 


IplImage *iplpic = cvCreateImage(cvSize( width,height), depth, channels); 
cvSetImageData(iplpic, [bitmap2 bitmapData], [bitmap2 bytesPerRow]); 

但我仍然有失去顏色強度的問題。

+0

如果您張貼樣本圖像以顯示「顏色強度」的含義,這將對您有所幫助。首先猜測是伽馬校正。 – etarion 2011-05-24 13:08:39

+0

對不起,我不能發佈樣本圖片。我沒有足夠的聲譽。 – 2011-05-24 13:31:32

+1

您可以在http://imgur.com上載並在此處張貼鏈接。 – etarion 2011-05-24 13:50:23