将UIImage转换为cv :: Mat [英] Converting UIImage to cv::Mat

查看:810
本文介绍了将UIImage转换为cv :: Mat的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UIImage,它是从iPhone相机拍摄的照片,现在我希望将UIImage转换为cv :: Mat(OpenCV)。我使用以下代码行来完成此任务:

I have a UIImage which is a pic captured from an iPhone Camera now I want the UIImage to be converted to cv::Mat (OpenCV). I am using the following lines of code to accomplish this :

-(cv::Mat)CVMat
{

CGColorSpaceRef colorSpace = CGImageGetColorSpace(self.CGImage);
CGFloat cols = self.size.width;
CGFloat rows = self.size.height;

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

CGContextRef contextRef = CGBitmapContextCreate(cvMat.data,                 // Pointer to backing 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), self.CGImage);
CGContextRelease(contextRef);

return cvMat;
}

此代码适用于横向模式下的UIImage,但是当我使用相同时使用从纵向模式拍摄的图像的代码,图像向右旋转90度。

This Code works fine for the UIImage in Landscape mode but when I use the same code with Images taken from Portrait mode the images gets rotated by 90 degrees towards the right.

我是iOS和Objective C中的新手,因此我无法计算出了什么问题。

I am a newbie in iOS and Objective C and hence I am not able to figure out what is wrong.

有人可以告诉我代码有什么问题,或者我错过了什么。

Can somebody tell me what is wrong with the code or am I missing out something.

推荐答案

这将是因为的UIImage 实际上不是画像。使用iPhone相机拍摄的所有照片都是原始位图状态的横向,例如3264宽x 2488高。通过图像中设置的方向EXIF标志显示肖像照片,例如,照片库应用程序根据该标志和相机的查看方向旋转图像。

This will be because the UIImage is not actually portrait. All photos taken with the iPhone camera are landscape in their raw bitmap state, eg 3264 wide x 2488 high. A "portrait" photo is displayed as such by the orientation EXIF flag set in the image, which is honoured, for example, by the photo library app which swivels images according to this flag and the viewing orientation of the camera.

该标志还会影响 UIImage 如何报告其宽度和高度属性,将它们从位图值转换为标记为肖像的图像。

The flag also affects how UIImage reports its width and height properties, transposing them from their bitmap values for images flagged as portrait.

cv :: Mat 不打扰其中任何一个。这意味着(i)在转换为 cv :: Mat 时,肖像图像将具有 size.width size.height 转置的值,以及(ii)从 cv :: Mat 转换回来时,您将丢失方向标记。

cv::Mat doesn't bother with any of that. This means that (i) when translating to cv::Mat a portrait image will have its size.width and size.height values transposed, and (ii) when translating back from cv::Mat you will have lost the orientation flag.

UIImage 转到 cv :: Mat <时,处理此问题的最简单方法/ code>是交换宽度和高度值,如果图像被标记为肖像:

The simplest way to handle this when going from UIImage to cv::Mat is to swap width and height values if the image is flagged as portrait:

if  (self.imageOrientation == UIImageOrientationLeft
      || self.imageOrientation == UIImageOrientationRight) {
         cols = self.size.height;
         rows = self.size.width;
    }

cv :: Mat转换回来 UIImage ,您将需要恢复方向标志。假设您的 cv :: Mat - > UIImage 代码包含:

When translating back from cv::Mat to UIImage, you will want to reinstate the orientation flag. Assuming your cv::Mat -> UIImage code contains this:

self = [self initWithCGImage:imageRef];

您可以使用此方法,并按照原始方式重置方向。

you can use this method instead, and reset the orientation as per the original.

self = [self initWithCGImage:imageRef scale:1 orientation:orientation];

这篇关于将UIImage转换为cv :: Mat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆