OpenCV For iOS預備知識(編譯,安裝) 附:人臉檢測Demo


DetectingFaces Demo 傳送門
相關書籍推薦
instant-opencv-for-ios
iOS Application Development with OpenCV 3


文章分三部分:

  • OpenCV 環(huán)境配置: 講述 iOS 中三種配置方式;
  • 使用: 結合一個簡單的人臉檢測的Demo,作為演示;
  • OpenCV 內(nèi)容概覽: 一覽OpenCV包涵的功能模塊,以便根據(jù)需求快速定位需要學習的部分,避免地毯式學習;

從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
也許編譯完成之后你發(fā)現(xiàn)來看一下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 時,也許你會發(fā)現(xiàn),最新版為3.x.x,細心地童鞋也許還看到了"OpenCV-iOS" 這個庫,那么為什么不用3.x.x 的最新版呢? "OpenCV-iOS" 不就是給 iOS用的么? 下面一一作答:

  • 2.4.x 與 3.x.x 的區(qū)別:
    區(qū)別總結一句話: 3.x.x 牛逼了很多!!!
    Opencv3.2各個模塊功能詳細簡介(包括與Opencv2.4的區(qū)別)

    為什么不用涅 ? 樓主也是個菜逼呀,我能說不會用么 ??反正我沒弄成功,哈哈,入門的東西沒必要卡在環(huán)境配置問題上;所以果斷選擇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中的基本數(shù)據(jù)結構,圖像的矩陣表示。這一部分建議必讀,這樣你可以知道如何去讀寫圖像的像素,以及相關的操作;此部分建議直接讀 于仕琪 老師的OpenCV 入門教程,如若Cpp功力不錯,請嘗試瀏覽源碼;

  • imgproc 模塊. 圖像處理
    imgproc 主要涉及圖像處理,包含大量的圖像處理函數(shù),比如 blur\erode\ threshold \sobel\Canny等等非常常用的函數(shù),也是圖像處理的基礎,建議必讀;

  • highgui 高階的GUI 和 圖像/視頻 iO 庫
    提供了UI控件 和 圖像/視頻讀寫API,該部分建議參考官方文檔,根據(jù)需要瀏覽學習吧;其中UI部分偏向于PC,個人覺得移動端簡要瀏覽即可;

  • calib3d 模塊. 相機標定和三維重建
    先簡單的理解為圖像測量吧,所謂的相機標定是指:在圖像測量過程以及機器視覺應用中,為確定空間物體表面某點的三維幾何位置與其在圖像中對應點之間的相互關系,必須建立相機成像的幾何模型,這些幾何模型參數(shù)就是相機參數(shù)。在大多數(shù)條件下這些參數(shù)必須通過實驗與計算才能得到,這個求解參數(shù)的過程就稱之為相機標定;
    具體的我也沒學習到,在此不做贅述;

  • feature2d 模塊. 2D特征框架
    這個看名字就比較好理解了,2D圖像的特征點,主要涉及特征點的描述,識別,匹配等等...

  • video 模塊. 視頻分析
    主要涉及運動分析和物體追蹤;

  • objdetect 模塊. 物體檢測
    目標物體檢測,比如上述Demo中的人臉檢測;當然iOS CoreImage框架本身包含了 CIDetetor類,也可實現(xiàn)人臉的檢測;同CI一樣,OpenCV也提供了一些訓練好的模型給我們;同時objdetect模塊可以訓練自己的目標模型;

  • ml 模塊. 機器學習
    恩,這個ml,我就不調(diào)侃了,他的原意是: Machine Learning;也是最新灰?;页;馃岬姆较?基本的套路: 數(shù)據(jù) -- 模型 -- 數(shù)據(jù) -- 模型 ....

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......
    ......
最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容