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


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,还是要有点英文基础的,先看完这段再决定要不要学习吧

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......
    ......
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 195,783评论 5 462
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 82,360评论 2 373
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 142,942评论 0 325
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,507评论 1 267
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,324评论 5 358
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,299评论 1 273
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,685评论 3 386
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,358评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,652评论 1 293
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,704评论 2 312
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,465评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,318评论 3 313
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,711评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,991评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,265评论 1 251
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,661评论 2 342
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,864评论 2 335

推荐阅读更多精彩内容