Ask Your Question
0

Convert UIImave to IplImage in OpenCV with iOS

asked 2013-09-29 13:45:24 -0600

updated 2013-09-30 03:01:30 -0600

berak gravatar image

Hello all,

I use theses function to do the job :

    + (IplImage *)CreateIplImageFromUIImage:(UIImage *)image {
// Getting CGImage from UIImage
CGImageRef imageRef = image.CGImage;

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
// Creating temporal IplImage for drawing
IplImage *iplimage = cvCreateImage(
                                   cvSize(image.size.width,image.size.height), IPL_DEPTH_8U, 4
                                   );
// Creating CGContext for temporal IplImage
CGContextRef contextRef = CGBitmapContextCreate(
                                                iplimage->imageData, iplimage->width, iplimage->height,
                                                iplimage->depth, iplimage->widthStep,
                                                colorSpace, kCGImageAlphaPremultipliedLast|kCGBitmapByteOrderDefault
                                                );
// Drawing CGImage to CGContext
CGContextDrawImage(
                   contextRef,
                   CGRectMake(0, 0, image.size.width, image.size.height),
                   imageRef
                   );
CGContextRelease(contextRef);
CGColorSpaceRelease(colorSpace);


NSLog(@"image->width = %d" , iplimage->width);
NSLog(@"image->height = %d" , iplimage->height);

// Creating result IplImage
IplImage *ret = cvCreateImage(cvGetSize(iplimage), IPL_DEPTH_8U, 3);
cvCvtColor(iplimage, ret, CV_RGBA2BGR);
cvReleaseImage(&iplimage);



return ret;
    }


    +(UIImage *)UIImageFromIplImage:(cv::Mat)aMat 
    {

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

unsigned char* data = new unsigned char[4*aMat.cols * aMat.rows];
for (int y = 0; y < aMat.rows; ++y)
{
    cv::Vec3b *ptr = aMat.ptr<cv::Vec3b>(y);
    unsigned char *pdata = data + 4*y*aMat.cols;

    for (int x = 0; x < aMat.cols; ++x, ++ptr)
    {
        *pdata++ = (*ptr)[2];
        *pdata++ = (*ptr)[1];
        *pdata++ = (*ptr)[0];
        *pdata++ = 0;
    }
}

// Bitmap context
CGContextRef context = CGBitmapContextCreate(data, aMat.cols, aMat.rows, 8, 4*aMat.cols, colorSpace, kCGImageAlphaNoneSkipLast);



CGImageRef cgimage = CGBitmapContextCreateImage(context);

UIImage *ret = [UIImage imageWithCGImage:cgimage scale:1.0 
                             orientation:UIImageOrientationUp];

CGImageRelease(cgimage);

CGContextRelease(context);  

// CGDataProviderRelease(provider);

CGColorSpaceRelease(colorSpace);


return ret;
    }

But i read that with OpenCV 2.4 we don't need it anymore as there is built-in function to do that.

Where are these new functions ? how to call them ? can someone give me an example ?

Thanks :)

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2013-09-30 04:47:31 -0600

The following conversion functions(UIImage <--> cv::Mat) are prepared after OpenCV 2.4.6.

  • MatToUIImage
  • UIImageToMat

You can find the definition of these functions in the following header.

opencv2/highgui/ios.h

And, you can find the implementation of these functions in the following source code.

modules/highgui/src/ios_conversions.mm

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2013-09-29 13:45:24 -0600

Seen: 2,019 times

Last updated: Sep 30 '13