Ask Your Question
1

Mat from UIImage (iOS) differences between emulator and device

asked 2013-11-14 03:18:59 -0600

Ricciolo gravatar image

I'm using this function to convert UIImage to Mat

- (cv::Mat)cvMatFromUIImage:(UIImage *)image
{
CGColorSpaceRef colorSpace = CGImageGetColorSpace(image.CGImage);
CGFloat cols = image.size.width;
CGFloat rows = image.size.height;

cv::Mat cvMat(rows, cols, CV_8UC4); // 8 bits per component, 4 channels (color channels + alpha)

CGContextRef contextRef = CGBitmapContextCreate(cvMat.data,                 // Pointer to  data
                                         cols,                       // Width of bitmap
                                         rows,                       // Height of bitmap
                                         8,                          // Bits per component
                                         cvMat.step[0],              // Bytes per row
                                         colorSpace,                 // Colorspace
                                         kCGImageAlphaNoneSkipLast |
                                         kCGBitmapByteOrderDefault); // Bitmap info flags

CGContextDrawImage(contextRef, CGRectMake(0, 0, cols, rows), image.CGImage);
CGContextRelease(contextRef);

return cvMat

On emulator it works great and I get image processing as expected. When using the same code on the device with the same image (same jpeg file) I get unexpected result. Looking inside Mat I see different pixel data. Do you know why?

edit retag flag offensive close merge delete

Comments

Could you attach results for simulator and real device? What version of iOS do you use? What device?

AlexanderShishkov gravatar imageAlexanderShishkov ( 2013-11-14 04:06:51 -0600 )edit

Device is iPhone 4 with iOS 7.0.3 If I dump cvMat.at<uchar>(0,0) cvMat.at<uchar>(1,0) I get on simulator: c b on iphone: c c

Ricciolo gravatar imageRicciolo ( 2013-11-14 04:32:17 -0600 )edit

That's odd, but using imread the cv::Mat is correct. But the image will come from UIImagePickerController so I need to convert from UIImage

Ricciolo gravatar imageRicciolo ( 2013-11-14 08:37:09 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2013-11-14 04:11:32 -0600

AlexanderShishkov gravatar image

Coud you try code from OpenCV:

void UIImageToMat(const UIImage* image,
                         cv::Mat& m, bool alphaExist) {
    CGColorSpaceRef colorSpace = CGImageGetColorSpace(image.CGImage);
    CGFloat cols = image.size.width, rows = image.size.height;
    CGContextRef contextRef;
    CGBitmapInfo bitmapInfo = kCGImageAlphaPremultipliedLast;
    if (CGColorSpaceGetModel(colorSpace) == 0)
    {
        m.create(rows, cols, CV_8UC1); // 8 bits per component, 1 channel
        bitmapInfo = kCGImageAlphaNone;
        if (!alphaExist)
            bitmapInfo = kCGImageAlphaNone;
        contextRef = CGBitmapContextCreate(m.data, m.cols, m.rows, 8,
                                           m.step[0], colorSpace,
                                           bitmapInfo);
    }
    else
    {
        m.create(rows, cols, CV_8UC4); // 8 bits per component, 4 channels
        if (!alphaExist)
            bitmapInfo = kCGImageAlphaNoneSkipLast |
                                kCGBitmapByteOrderDefault;
        contextRef = CGBitmapContextCreate(m.data, m.cols, m.rows, 8,
                                           m.step[0], colorSpace,
                                           bitmapInfo);
    }
    CGContextDrawImage(contextRef, CGRectMake(0, 0, cols, rows),
                       image.CGImage);
    CGContextRelease(contextRef);
    CGColorSpaceRelease(colorSpace);
}
edit flag offensive delete link more

Comments

Same issue :-(

Ricciolo gravatar imageRicciolo ( 2013-11-14 04:32:59 -0600 )edit

Question Tools

Stats

Asked: 2013-11-14 03:18:59 -0600

Seen: 1,263 times

Last updated: Nov 14 '13