OpenCV For iOS预备知识(编译,安装) 附:人脸检测Demo

2018-02-27 11:09:39来源:https://www.jianshu.com/p/9ff6fad55b37作者:hehtao人点击

分享



DetectingFaces Demo 传送门
相关书籍推荐
instant-opencv-for-ios
iOS Application Development with OpenCV 3


文章分三部分:


OpenCV 环境配置: 讲述 iOS 中三种配置方式;
使用: 结合一个简单的人脸检测的Demo,作为演示;
OpenCV 内容概览: 一览OpenCV包涵的功能模块,以便根据需求快速定位需要学习的部分,避免地毯式学习;


从2.4.2 开始支持iOS
In 2012 OpenCV development team actively worked on adding extended support for iOS. Full integration is available since version 2.4.2 (2012).



一. 安装

OpenCV 常用三种安装方式:


下载源代码编译
使用CocoaPods安装
使用官方的framework
M1: 下载源代码编译:


Required Packages
CMake 2.8.8 or higher
Xcode 4.2 or higher
Getting the Cutting-edge OpenCV from Git Repository
Launch GIT client and clone OpenCV repository from here
In MacOS it can be done using the following command in Terminal:


cd ~/<my_working _directory>
git clone https://github.com/opencv/opencv.git


Building OpenCV from Source, using CMake and Command Line
1.Make symbolic link for Xcode to let OpenCV build scripts find the compiler, header files etc.


cd /
sudo ln -s /Applications/Xcode.app/Contents/Developer Developer


Build OpenCV framework:


cd ~/<my_working_directory>
python opencv/platforms/ios/build_framework.py ios


If everything's fine, a few minutes later you will get /<my_working_directory>/ios/opencv2.framework. You can add this framework to your Xcode projects.



上述引用官方文档:Installation in iOS
也许编译完成之后你发现来看一下opencv2.framework有200多M的体积,包含X86_64,ARM64,armv7s等平台,我们开看下build_framework.py line279 ~ 287 ,如下:


    b = iOSBuilder(args.opencv, args.contrib, args.dynamic, args.bitcodedisabled, args.without,
[
(["armv7s", "arm64"], "iPhoneOS"),
] if os.environ.get('BUILD_PRECOMMIT', None) else
[
(["armv7s", "arm64"], "iPhoneOS"),
(["x86_64"], "iPhoneSimulator"),
])
b.build(args.out)

如果你不想要 armv7s或x86_64平台,此处出掉即可;


M2:使用CocoaPods安装
 pod 'OpenCV', '~> 2.4.13'
pod install

在 pod search OpenCV 时,也许你会发现,最新版为3.x.x,细心地童鞋也许还看到了"OpenCV-iOS" 这个库,那么为什么不用3.x.x 的最新版呢? "OpenCV-iOS" 不就是给 iOS用的么? 下面一一作答:



2.4.x 与 3.x.x 的区别:
区别总结一句话: 3.x.x 牛逼了很多!!!
Opencv3.2各个模块功能详细简介(包括与Opencv2.4的区别)


为什么不用涅 ? 楼主也是个菜逼呀,我能说不会用么 ??反正我没弄成功,哈哈,入门的东西没必要卡在环境配置问题上;所以果断选择2.4.13;



"OpenCV-iOS" 不就是给 iOS用的么?


答案是肯定的,但是看这里


Snip20171124_1.png

M3:使用官方的framework

这个就很简单了呀:




Snip20171124_2.png

对,就是这个家伙OpenCV Releases,下载对应的iOS版本,拖进工程,完事!!!

哦,对了,你可能要引入以下框架!!!!


libc++.tbd
AVFoundation.framework
CoreImage.framework
CoreGraphics.framework
QuartzCore.framework
Accelerate.framework
CoreVideo.framework
CoreMedia.framework
AssetsLibrary.framework

二.使用:

走个小小的官方Demo: DetectingFaces(人脸检测)
xxx.pch 引入头文件:


#ifdef __cplusplus
#import <opencv2/opencv.hpp>
#import <opencv2/highgui/ios.h>
#endif

ViewController.m 如下:


- (void)viewDidLoad
{
[super viewDidLoad];

//calculate path to the resource file
NSString* filename = [[NSBundle mainBundle]
pathForResource:@"haarcascade_frontalface_alt" ofType:@"xml"];
//load Haar cascade from the XML file
faceCascade.load([filename UTF8String]);
//compute path to the resource file
NSString* filePath = [[NSBundle mainBundle]
pathForResource:@"lena" ofType:@"png"];
//read the image
UIImage* image = [UIImage imageWithContentsOfFile:filePath];
//convert UIImage* to cv::Mat
cv::Mat cvImage;
UIImageToMat(image, cvImage);

//vector for storing found faces
std::vector<cv::Rect> faces;

cv::Mat cvGrayImage;
//convert the image to the one-channel
cvtColor(cvImage, cvGrayImage, CV_BGR2GRAY);
//find faces on the image
faceCascade.detectMultiScale(cvGrayImage, faces, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE, cv::Size(30,30));
//draw all found faces
for(int i = 0; i < faces.size(); i++)
{
const cv::Rect& currentFace = faces[i];
//calculate two corner points to draw a rectangle
cv::Point upLeftPoint(currentFace.x, currentFace.y);
cv::Point bottomRightPoint = upLeftPoint + cv::Point(currentFace.width, currentFace.height);
//draw rectangle around the face
cv::rectangle(cvImage, upLeftPoint, bottomRightPoint, cv::Scalar(255,0,255), 4, 8, 0);
}
//show resulted image on imageView component
imageView.image = MatToUIImage(cvImage);
}

接下来, command + R ,效果如下:





Snip20171124_3.png

congratulation!!!


三.OpenCV 一览:

OpenCV 2.4.13 文件目录如下:


Snip20171124_4.png

core 模块. 核心功能:
主要涉及OpenCV中的基本数据结构,图像的矩阵表示。这一部分建议必读,这样你可以知道如何去读写图像的像素,以及相关的操作;此部分建议直接读 于仕琪 老师的OpenCV 入门教程,如若Cpp功力不错,请尝试浏览源码;


imgproc 模块. 图像处理
imgproc 主要涉及图像处理,包含大量的图像处理函数,比如 blur/erode/ threshold /sobel/Canny等等非常常用的函数,也是图像处理的基础,建议必读;


highgui 高阶的GUI 和 图像/视频 iO 库
提供了UI控件 和 图像/视频读写API,该部分建议参考官方文档,根据需要浏览学习吧;其中UI部分偏向于PC,个人觉得移动端简要浏览即可;


calib3d 模块. 相机标定和三维重建
先简单的理解为图像测量吧,所谓的相机标定是指:在图像测量过程以及机器视觉应用中,为确定空间物体表面某点的三维几何位置与其在图像中对应点之间的相互关系,必须建立相机成像的几何模型,这些几何模型参数就是相机参数。在大多数条件下这些参数必须通过实验与计算才能得到,这个求解参数的过程就称之为相机标定;
具体的我也没学习到,在此不做赘述;


feature2d 模块. 2D特征框架
这个看名字就比较好理解了,2D图像的特征点,主要涉及特征点的描述,识别,匹配等等...


video 模块. 视频分析
主要涉及运动分析和物体追踪;


objdetect 模块. 物体检测
目标物体检测,比如上述Demo中的人脸检测;当然iOS CoreImage框架本身包含了 CIDetetor类,也可实现人脸的检测;同CI一样,OpenCV也提供了一些训练好的模型给我们;同时objdetect模块可以训练自己的目标模型;


ml 模块. 机器学习
恩,这个ml,我就不调侃了,他的原意是: Machine Learning;也是最新灰常灰常火热的方向;基本的套路: 数据 -- 模型 -- 数据 -- 模型 ....



The Machine Learning Library (MLL) is a set of classes and functions for statistical classification, regression, and clustering of data.
Most of the classification and regression algorithms are implemented as C++ classes. As the algorithms have different sets of features (like an ability to handle missing measurements or categorical input variables), there is a little common ground between the classes. This common ground is defined by the class CvStatModel that all the other ML classes are derived from.



想学ML,还是要有点英文基础的,先看完这段再决定要不要学习吧


gpu 模块. 使用GPU加速的计算机视觉


The OpenCV GPU module is a set of classes and functions to utilize GPU computational capabilities. It is implemented using NVIDIA* CUDA* Runtime API and supports only NVIDIA GPUs. The OpenCV GPU module includes utility functions, low-level vision primitives, and high-level algorithms. The utility functions and low-level primitives provide a powerful infrastructure for developing fast vision algorithms taking advantage of GPU whereas the high-level functionality includes some state-of-the-art algorithms (such as stereo correspondence, face and people detectors, and others) ready to be used by the application developers.




...... Others......
......







最新文章

123

最新摄影

闪念基因

微信扫一扫

第七城市微信公众平台