Tutorial on setup OpenCV on iOS

This tutorial is to setup OpenCV on iOS. To include opencv2.framework to iOS8 Objective C Project.

By following this tutorial: http://docs.opencv.org/doc/tutorials/ios/hello/hello.html#opencvioshelloworld

1) to download prebuilt opencv2.framework, the fine working version is 2.4.9.

2) to create new XCode Project, target iOS8.0 if you are under XCode 6.0.1 as this version not yet support iOS8.1

3) link opencv2.framework to XCode Project. Go to project build phase, and link following frameworks:
opencv2.framework
AssetsLibrary.framework
AVFoundation.framework
CoreGraphics.framework
CoreImage.framework
CoreMedia.framework
CoreVideo.framework
Foundation.framework
UIKit.framework
Accelerate.framework

Screen Shot 2015-01-20 at 12.42.28 am

4) Create <NameOfProject>-Prefix.pch, with following snippets of code add between ifdef / endif block.

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

5) in ViewController.h, add the following to header
#import <opencv2/highgui/cap_ios.h>
#include <opencv2/opencv.hpp>

Note, the most important line is #include <opencv2/opencv.hpp>, this is important to make the stuffs works. I spent few hours and found this solve my problems, which unable to recognize cvtColor function, OK, will introduce in next steps.

6) To do some testing, by adding a video camera, reading 30 frames per second, process the images by inverting the color…

7) Rename ViewController.m to ViewController.mm as we are going to put C PlusPlus Code. Extension mm allows coexist of Objective C and C PlusPlus.

8) In ViewController.h, add following line
@property (nonatomic, retain) CvVideoCamera* videoCamera;

9) In ViewController.mm, we initialize videoCamera and starts it.

self.videoCamera = [[CvVideoCamera alloc] initWithParentView:_ImageView];
self.videoCamera.delegate = self;
self.videoCamera.defaultAVCaptureDevicePosition = AVCaptureDevicePositionFront;
self.videoCamera.defaultAVCaptureSessionPreset = AVCaptureSessionPreset352x288;
self.videoCamera.defaultAVCaptureVideoOrientation = AVCaptureVideoOrientationPortrait;
self.videoCamera.defaultFPS = 30;
[self.videoCamera start];

10) In ViewController.h, add CvVideoCameraDelegate

Like this

@interface ViewController : UIViewController

11) In ViewController.mm, add following snippets of code before @end.

#pragma mark – Protocol CvVideoCameraDelegate

#ifdef __cplusplus
– (void)processImage:(Mat&)image;
{
// Do some OpenCV stuff with the image
Mat image_copy;
cvtColor(image, image_copy, CV_BGRA2BGR);
bitwise_not(image_copy, image_copy);
cvtColor(image_copy, image, CV_BGR2BGRA);
}
#endif

12) Run the project. Cheers.

Photo 20-1-15 12 51 57 am

Author: fyhao

Jebsen & Jessen Comms Singapore INTI University College Bsc (Hon) of Computer Science, Coventry University

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.