将NSImage转换为IplImage

时间:2011-05-23 13:22:21

标签: iphone objective-c opencv nsimage

我有两个问题将我的图像(只有jpeg文件)从NSImage转换为IplImage。我的第一个问题是转换后的图像与原始图像的颜色强度不同。转换后的图像在某种程度上比原始图像更无张力 我的第二个问题是只有8/10个图像被完全转换,通过2/10个图像,只有一部分图像被转换。我的意思是只会转换图像的左上角。我希望你理解我,我的英语不是最好的。

    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;
}

1 个答案:

答案 0 :(得分: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]);

但我仍然有失去色彩强度的问题。

相关问题