2

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 = 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;
}
Till
  • 27,559
  • 13
  • 88
  • 122
p.goe
  • 61
  • 3

1 Answers1

0

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.

Community
  • 1
  • 1
p.goe
  • 61
  • 3
  • It would be helpful if you posted sample images to show what you mean with "colour intensity". First guess would be gamma correction. – etarion May 24 '11 at 13:08
  • Sorry I can´t post sample images. I haven´t enough reputation. – p.goe May 24 '11 at 13:31