OpenCV  3.1.0
Open Source Computer Vision
OpenCV iOS - Video Processing

This tutorial explains how to process video frames using the iPhone's camera and OpenCV.

Prerequisites:

Including OpenCV library in your iOS project

The OpenCV library comes as a so-called framework, which you can directly drag-and-drop into your XCode project. Download the latest binary from http://sourceforge.net/projects/opencvlibrary/files/opencv-ios/. Alternatively follow this guide Installation in iOS to compile the framework manually. Once you have the framework, just drag-and-drop into XCode:

xcode_hello_ios_framework_drag_and_drop.png

Also you have to locate the prefix header that is used for all header files in the project. The file is typically located at "ProjectName/Supporting Files/ProjectName-Prefix.pch". There, you have add an include statement to import the opencv library. However, make sure you include opencv before you include UIKit and Foundation, because else you will get some weird compile errors that some macros like min and max are defined multiple times. For example the prefix header could look like the following:

1 //
2 // Prefix header for all source files of the 'VideoFilters' target in the 'VideoFilters' project
3 //
4 
5 #import <Availability.h>
6 
7 #ifndef __IPHONE_4_0
8 #warning "This project uses features only available in iOS SDK 4.0 and later."
9 #endif
10 
11 #ifdef __cplusplus
12 #import <opencv2/opencv.hpp>
13 #endif
14 
15 #ifdef __OBJC__
16  #import <UIKit/UIKit.h>
17  #import <Foundation/Foundation.h>
18 #endif

Example video frame processing project

User Interface

First, we create a simple iOS project, for example Single View Application. Then, we create and add an UIImageView and UIButton to start the camera and display the video frames. The storyboard could look like that:

xcode_hello_ios_viewcontroller_layout.png

Make sure to add and connect the IBOutlets and IBActions to the corresponding ViewController:

1 @interface ViewController : UIViewController
2 {
3  IBOutlet UIImageView* imageView;
4  IBOutlet UIButton* button;
5 }
6 
7 - (IBAction)actionStart:(id)sender;
8 
9 @end

Adding the Camera

We add a camera controller to the view controller and initialize it when the view has loaded:

1 #import <opencv2/videoio/cap_ios.h>
2 using namespace cv;
3 
4 
5 @interface ViewController : UIViewController
6 {
7  ...
8  CvVideoCamera* videoCamera;
9 }
10 ...
11 @property (nonatomic, retain) CvVideoCamera* videoCamera;
12 
13 @end
1 - (void)viewDidLoad
2 {
3  [super viewDidLoad];
4  // Do any additional setup after loading the view, typically from a nib.
5 
6  self.videoCamera = [[CvVideoCamera alloc] initWithParentView:imageView];
7  self.videoCamera.defaultAVCaptureDevicePosition = AVCaptureDevicePositionFront;
8  self.videoCamera.defaultAVCaptureSessionPreset = AVCaptureSessionPreset352x288;
9  self.videoCamera.defaultAVCaptureVideoOrientation = AVCaptureVideoOrientationPortrait;
10  self.videoCamera.defaultFPS = 30;
11  self.videoCamera.grayscale = NO;
12 }

In this case, we initialize the camera and provide the imageView as a target for rendering each frame. CvVideoCamera is basically a wrapper around AVFoundation, so we provie as properties some of the AVFoundation camera options. For example we want to use the front camera, set the video size to 352x288 and a video orientation (the video camera normally outputs in landscape mode, which results in transposed data when you design a portrait application).

The property defaultFPS sets the FPS of the camera. If the processing is less fast than the desired FPS, frames are automatically dropped.

The property grayscale=YES results in a different colorspace, namely "YUV (YpCbCr 4:2:0)", while grayscale=NO will output 32 bit BGRA.

Additionally, we have to manually add framework dependencies of the opencv framework. Finally, you should have at least the following frameworks in your project:

Processing frames

We follow the delegation pattern, which is very common in iOS, to provide access to each camera frame. Basically, the View Controller has to implement the CvVideoCameraDelegate protocol and has to be set as delegate to the video camera:

1 @interface ViewController : UIViewController<CvVideoCameraDelegate>
1 - (void)viewDidLoad
2 {
3  ...
4  self.videoCamera = [[CvVideoCamera alloc] initWithParentView:imageView];
5  self.videoCamera.delegate = self;
6  ...
7 }
1 #pragma mark - Protocol CvVideoCameraDelegate
2 
3 #ifdef __cplusplus
4 - (void)processImage:(Mat&)image;
5 {
6  // Do some OpenCV stuff with the image
7 }
8 #endif

Note that we are using C++ here (cv::Mat). Important: You have to rename the view controller's extension .m into .mm, so that the compiler compiles it under the assumption of Objective-C++ (Objective-C and C++ mixed). Then, __cplusplus is defined when the compiler is processing the file for C++ code. Therefore, we put our code within a block where __cplusplus is defined.

Basic video processing

From here you can start processing video frames. For example the following snippet color-inverts the image:

1 - (void)processImage:(Mat&)image;
2 {
3  // Do some OpenCV stuff with the image
4  Mat image_copy;
5  cvtColor(image, image_copy, COLOR_BGR2GRAY);
6 
7  // invert image
8  bitwise_not(image_copy, image_copy);
9 
10  //Convert BGR to BGRA (three channel to four channel)
11  Mat bgr;
12  cvtColor(image_copy, bgr, COLOR_GRAY2BGR);
13 
14  cvtColor(bgr, image, COLOR_BGR2BGRA);
15 }

Start!

Finally, we have to tell the camera to actually start/stop working. The following code will start the camera when you press the button, assuming you connected the UI properly:

1 #pragma mark - UI Actions
2 
3 - (IBAction)actionStart:(id)sender;
4 {
5  [self.videoCamera start];
6 }

Hints

Try to avoid costly matrix copy operations as much as you can, especially if you are aiming for real-time. As the image data is passed as reference, work in-place, if possible.

When you are working on grayscale data, turn set grayscale = YES as the YUV colorspace gives you directly access the luminance plane.

The Accelerate framework provides some CPU-accelerated DSP filters, which come handy in your case.