你的位置:首页 > 软件开发 > 操作系统 > opencv源码阅读之——iOS的两条接口UIImageToMat()和MatToUIImage()

opencv源码阅读之——iOS的两条接口UIImageToMat()和MatToUIImage()

发布时间:2016-04-14 17:00:07
在ios下开发基于opencv的程序时经常会用到两条接口,分别是UIImageToMat()和MatToUIImage(),这两条接口是UIImage和cv::Mat之间的互转。关于这两条api的信息opencv文档里面没有给出太多的信息,所以,需要通过阅读源码来分析。1.UII ...

opencv源码阅读之——iOS的两条接口UIImageToMat()和MatToUIImage()

在ios下开发基于opencv的程序时经常会用到两条接口,分别是UIImageToMat()和MatToUIImage(),这两条接口是UIImage和cv::Mat之间的互转。关于这两条api的信息opencv文档里面没有给出太多的信息,所以,需要通过阅读源码来分析。

1.UIImageToMat的细节

关于这条api,我们总想知道返回的mat的一些细节,比如是几通道的,是否带alpha通道,色彩空间是RGBA还BGR的,我们都不清楚,带着这几个问题,我们一起来阅读源码:

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);}

 

海外公司注册、海外银行开户、跨境平台代入驻、VAT、EPR等知识和在线办理:https://www.xlkjsw.com

原标题:opencv源码阅读之——iOS的两条接口UIImageToMat()和MatToUIImage()

关键词:IOS

IOS
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。