Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Mat from UIImage (iOS) differences between emulator and device

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?