Launch SVN client and checkout the current OpenCV snapshot from here: http://code.opencv.org/svn/opencv/trunk/opencv
Launch GIT client and clone OpenCV repository from here: http://github.com/itseez/opencv
In MacOS it can be done using the following command in Terminal:
In MacOS it can be done using the following command in Terminal:
.. code-block:: bash
.. code-block:: bash
cd ~/<my_working _directory>
cd ~/<my_working _directory>
svn co http://code.opencv.org/svn/opencv/trunk/opencv
git clone https://github.com/Itseez/opencv.git
Building OpenCV from source using CMake, using the command line
Building OpenCV from source using CMake, using the command line
...
@@ -35,8 +35,13 @@ Building OpenCV from source using CMake, using the command line
...
@@ -35,8 +35,13 @@ Building OpenCV from source using CMake, using the command line
#. Build OpenCV framework
#. Build OpenCV framework
.. code-block:: bash
.. code-block:: bash
cd ~/<my_working_directory>
cd ~/<my_working_directory>
python opencv/ios/build_framework.py ios
python opencv/ios/build_framework.py ios
If everything's fine, after a few minutes you will get ~/<my_working_directory>/ios/opencv2.framework. You can add this framework to your Xcode projects.
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.
Further Reading
=====================
You can find several OpenCV+iOS tutorials here :ref:`Table-Of-Content-iOS`
In this tutorial we will learn how to do basic image processing using OpenCV in iOS.
*Introduction*
==============
In *OpenCV* all the image processing operations are done on *Mat*. iOS uses UIImage object to display image. One of the thing is to convert UIImage object to Mat object. Below is the code to convert UIImage to Mat.
Once we obtain the Mat Object. We can do all our processing on Mat object, similar to cpp. For example if we want to convert image to gray, we can do it via below code.
.. code-block:: cpp
cv::Mat greyMat;
cv::cvtColor(inputMat, greyMat, CV_BGR2GRAY);
After the processing we need to convert it back to UIImage.
This tutorial explains how to process video frames using the iPhone's camera and OpenCV.
Prerequisites:
==================
* Xcode 4.3 or higher
* Basic knowledge of iOS programming (Objective-C, Interface Builder)
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 :ref:`iOS-Installation` to compile the framework manually. Once you have the framework, just drag-and-drop into XCode:
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:
.. code-block:: objc
:linenos:
//
// Prefix header for all source files of the 'VideoFilters' target in the 'VideoFilters' project
//
#import <Availability.h>
#ifndef __IPHONE_4_0
#warning "This project uses features only available in iOS SDK 4.0 and later."
#endif
#ifdef __cplusplus
#import <opencv2/opencv.hpp>
#endif
#ifdef __OBJC__
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#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:
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:
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:
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 __cpluscplus is defined.
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:
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.
|General| These tutorials are the bottom of the iceberg as they link together multiple of the modules presented above in order to solve complex problems.
|General| These tutorials are the bottom of the iceberg as they link together multiple of the modules presented above in order to solve complex problems.
...
@@ -189,4 +204,5 @@ As always, we would be happy to hear your comments and receive your contribution
...
@@ -189,4 +204,5 @@ As always, we would be happy to hear your comments and receive your contribution