demo地址:https://github.com/GoldenRocking/OpenCV_iOS
OpenCV是一个开源的计算机视觉库。OpenCV采用C/C++语言编写。关于OpenCV的简单介绍,会在下一节讲到。这一节主要介绍如何在iOS平台调用OpenCV函数。
首先,我们需要到OpenCV官网下载其framework:http://opencv.org/downloads.html;
下载好opencv2.framework后,我们便可以将其引入xcode工程。
在用OpenCV处理图像以前,我们首先要做的的是先将UIImage格式的图片转换为cvMat的数据类型:
+(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;
}
openCV可以通过其相关的函数来处理cvMat 数据。处理结束之后,我们需要将cvMat 数据转化为UIImage格式:
+(UIImage *)UIImageFromCVMat:(cv::Mat)cvMat
{
NSData *data = [NSData dataWithBytes:cvMat.data length:cvMat.elemSize()*cvMat.total()];
CGColorSpaceRef colorSpace;
if (cvMat.elemSize() == 1) {
colorSpace = CGColorSpaceCreateDeviceGray();
} else {
colorSpace = CGColorSpaceCreateDeviceRGB();
}
CGDataProviderRef provider = CGDataProviderCreateWithCFData((__bridge CFDataRef)data);
// Creating CGImage from cv::Mat
CGImageRef imageRef = CGImageCreate(cvMat.cols, //width
cvMat.rows, //height
8, //bits per component
8 * cvMat.elemSize(), //bits per pixel
cvMat.step[0], //bytesPerRow
colorSpace, //colorspace
kCGImageAlphaNone|kCGBitmapByteOrderDefault,// bitmap info
provider, //CGDataProviderRef
NULL, //decode
false, //should interpolate
kCGRenderingIntentDefault //intent
);
// Getting UIImage from CGImage
UIImage *finalImage = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
CGDataProviderRelease(provider);
CGColorSpaceRelease(colorSpace);
return finalImage;
}
我们可以创建一个PrefixHeader.pch文件,在工程的Build Settings->Prefix Header中将其引入:
#ifndef PrefixHeader_pch
#define PrefixHeader_pch
// Include any system framework and library headers here that should be included in all compilation units.
// You will also need to set the Prefix Header build setting of one or more of your targets to reference this file.
#ifdef __cplusplus
#import <opencv2/opencv.hpp>
#endif
#endif /* PrefixHeader_pch */
这样就不需要每次在ViewController中导入头文件了。
由于OpenCV是用C++所写,所以我们在引用的地方需要将.m文件修改为.mm。为了方便使用库函数,我们可以在调用的类中添加如下代码:
using namespace cv;
下面是一个简单的图像灰度处理方法的调用:
UIImage *image = [UIImage imageNamed:@"timg"];
Mat mat = [cvprocess cvMatFromUIImage:image];
Mat grayMat;
cvtColor(mat, grayMat, CV_BGR2GRAY);
UIImage *cvimage = [cvprocess UIImageFromCVMat:grayMat];
UIImageView *imview = [[UIImageView alloc]initWithImage:cvimage];
imview.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
[self.view addSubview:imview];
处理之前的图像为:
timg.png处理之后的图像为:
gray.pngdemo地址:https://github.com/GoldenRocking/OpenCV_iOS
网友评论
UIImageToMat(inputImage, outImage); // UIImage转Mat的方法。
UIImage *lastImage = MatToUIImage(matImage); // Mat转UIImage的方法。