Ask Your Question

Revision history [back]

Convert UIImave to IplImage in OpenCV with iOS

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 :)

click to hide/show revision 2
retagged

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

berak gravatar image

Convert UIImave to IplImage in OpenCV with iOS

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 :)