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?
Could you attach results for simulator and real device? What version of iOS do you use? What device?
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
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