开发者

Converting NSImage to IplImage

I have two problems converting my image (only jpeg-files) from NSImage to IplImage. My first problem is that the converted image has not the same colour intensity like the original one. The converted image is somehow more toneless than the original one.

My second problem is only 8/10 images are converted totally, by 2/10 images only a part of the image will be converted. What I mean is that only the upper left corner of the image will be converted. I hope you understand me, my english is not the best.

    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 = i开发者_如何学Cplpic->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;
}


I solved the second problem. I changed the code like this:

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]);

But I still have the problem losing the colour intensity.

Converting NSImage to IplImage

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜