OpenCV For iOSiOS点点滴滴iOS Development

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

2017-11-24  本文已影响593人  hehtao

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


文章分三部分:

从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 常用三种安装方式:

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用的么? 下面一一作答:

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

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.

上一篇 下一篇

猜你喜欢

热点阅读