Converted tabs to spaces.
This commit is contained in:
@@ -17,32 +17,32 @@ 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:
|
||||
|
||||
.. image:: images/xcode_hello_ios_framework_drag_and_drop.png
|
||||
.. image:: images/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:
|
||||
|
||||
.. code-block:: objc
|
||||
:linenos:
|
||||
:linenos:
|
||||
|
||||
//
|
||||
// Prefix header for all source files of the 'VideoFilters' target in the 'VideoFilters' project
|
||||
//
|
||||
//
|
||||
// Prefix header for all source files of the 'VideoFilters' target in the 'VideoFilters' project
|
||||
//
|
||||
|
||||
#import <Availability.h>
|
||||
#import <Availability.h>
|
||||
|
||||
#ifndef __IPHONE_4_0
|
||||
#warning "This project uses features only available in iOS SDK 4.0 and later."
|
||||
#endif
|
||||
#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 __cplusplus
|
||||
#import <opencv2/opencv.hpp>
|
||||
#endif
|
||||
|
||||
#ifdef __OBJC__
|
||||
#import <UIKit/UIKit.h>
|
||||
#import <Foundation/Foundation.h>
|
||||
#endif
|
||||
#ifdef __OBJC__
|
||||
#import <UIKit/UIKit.h>
|
||||
#import <Foundation/Foundation.h>
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
@@ -53,23 +53,23 @@ 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:
|
||||
|
||||
.. image:: images/xcode_hello_ios_viewcontroller_layout.png
|
||||
.. image:: images/xcode_hello_ios_viewcontroller_layout.png
|
||||
|
||||
|
||||
Make sure to add and connect the IBOutlets and IBActions to the corresponding ViewController:
|
||||
|
||||
.. code-block:: objc
|
||||
:linenos:
|
||||
:linenos:
|
||||
|
||||
@interface ViewController : UIViewController
|
||||
{
|
||||
IBOutlet UIImageView* imageView;
|
||||
IBOutlet UIButton* button;
|
||||
}
|
||||
@interface ViewController : UIViewController
|
||||
{
|
||||
IBOutlet UIImageView* imageView;
|
||||
IBOutlet UIButton* button;
|
||||
}
|
||||
|
||||
- (IBAction)actionStart:(id)sender;
|
||||
- (IBAction)actionStart:(id)sender;
|
||||
|
||||
@end
|
||||
@end
|
||||
|
||||
|
||||
Adding the Camera
|
||||
@@ -78,37 +78,37 @@ Adding the Camera
|
||||
We add a camera controller to the view controller and initialize it when the view has loaded:
|
||||
|
||||
.. code-block:: objc
|
||||
:linenos:
|
||||
:linenos:
|
||||
|
||||
#import <opencv2/highgui/cap_ios.h>
|
||||
using namespace cv;
|
||||
#import <opencv2/highgui/cap_ios.h>
|
||||
using namespace cv;
|
||||
|
||||
|
||||
@interface ViewController : UIViewController
|
||||
{
|
||||
...
|
||||
CvVideoCamera* videoCamera;
|
||||
}
|
||||
...
|
||||
@property (nonatomic, retain) CvVideoCamera* videoCamera;
|
||||
@interface ViewController : UIViewController
|
||||
{
|
||||
...
|
||||
CvVideoCamera* videoCamera;
|
||||
}
|
||||
...
|
||||
@property (nonatomic, retain) CvVideoCamera* videoCamera;
|
||||
|
||||
@end
|
||||
@end
|
||||
|
||||
.. code-block:: objc
|
||||
:linenos:
|
||||
:linenos:
|
||||
|
||||
- (void)viewDidLoad
|
||||
{
|
||||
[super viewDidLoad];
|
||||
// Do any additional setup after loading the view, typically from a nib.
|
||||
- (void)viewDidLoad
|
||||
{
|
||||
[super viewDidLoad];
|
||||
// Do any additional setup after loading the view, typically from a nib.
|
||||
|
||||
self.videoCamera = [[CvVideoCamera alloc] initWithParentView:imageView];
|
||||
self.videoCamera.defaultAVCaptureDevicePosition = AVCaptureDevicePositionFront;
|
||||
self.videoCamera.defaultAVCaptureSessionPreset = AVCaptureSessionPreset352x288;
|
||||
self.videoCamera.defaultAVCaptureVideoOrientation = AVCaptureVideoOrientationPortrait;
|
||||
self.videoCamera.defaultFPS = 30;
|
||||
self.videoCamera.grayscale = NO;
|
||||
}
|
||||
self.videoCamera = [[CvVideoCamera alloc] initWithParentView:imageView];
|
||||
self.videoCamera.defaultAVCaptureDevicePosition = AVCaptureDevicePositionFront;
|
||||
self.videoCamera.defaultAVCaptureSessionPreset = AVCaptureSessionPreset352x288;
|
||||
self.videoCamera.defaultAVCaptureVideoOrientation = AVCaptureVideoOrientationPortrait;
|
||||
self.videoCamera.defaultFPS = 30;
|
||||
self.videoCamera.grayscale = NO;
|
||||
}
|
||||
|
||||
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).
|
||||
|
||||
@@ -143,7 +143,7 @@ Additionally, we have to manually add framework dependencies of the opencv frame
|
||||
* Foundation
|
||||
|
||||
|
||||
.. image:: images/xcode_hello_ios_frameworks_add_dependencies.png
|
||||
.. image:: images/xcode_hello_ios_frameworks_add_dependencies.png
|
||||
|
||||
|
||||
Processing frames
|
||||
@@ -152,35 +152,35 @@ 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:
|
||||
|
||||
.. code-block:: objc
|
||||
:linenos:
|
||||
:linenos:
|
||||
|
||||
@interface ViewController : UIViewController<CvVideoCameraDelegate>
|
||||
@interface ViewController : UIViewController<CvVideoCameraDelegate>
|
||||
|
||||
|
||||
|
||||
.. code-block:: objc
|
||||
:linenos:
|
||||
:linenos:
|
||||
|
||||
- (void)viewDidLoad
|
||||
{
|
||||
...
|
||||
self.videoCamera = [[CvVideoCamera alloc] initWithParentView:imageView];
|
||||
self.videoCamera.delegate = self;
|
||||
...
|
||||
}
|
||||
- (void)viewDidLoad
|
||||
{
|
||||
...
|
||||
self.videoCamera = [[CvVideoCamera alloc] initWithParentView:imageView];
|
||||
self.videoCamera.delegate = self;
|
||||
...
|
||||
}
|
||||
|
||||
|
||||
.. code-block:: objc
|
||||
:linenos:
|
||||
:linenos:
|
||||
|
||||
#pragma mark - Protocol CvVideoCameraDelegate
|
||||
#pragma mark - Protocol CvVideoCameraDelegate
|
||||
|
||||
#ifdef __cplusplus
|
||||
- (void)processImage:(Mat&)image;
|
||||
{
|
||||
// Do some OpenCV stuff with the image
|
||||
}
|
||||
#endif
|
||||
#ifdef __cplusplus
|
||||
- (void)processImage:(Mat&)image;
|
||||
{
|
||||
// Do some OpenCV stuff with the image
|
||||
}
|
||||
#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.
|
||||
@@ -193,18 +193,18 @@ From here you can start processing video frames. For example the following snipp
|
||||
|
||||
|
||||
.. code-block:: objc
|
||||
:linenos:
|
||||
:linenos:
|
||||
|
||||
- (void)processImage:(Mat&)image;
|
||||
{
|
||||
// Do some OpenCV stuff with the image
|
||||
Mat image_copy;
|
||||
cvtColor(image, image_copy, CV_BGRA2BGR);
|
||||
- (void)processImage:(Mat&)image;
|
||||
{
|
||||
// Do some OpenCV stuff with the image
|
||||
Mat image_copy;
|
||||
cvtColor(image, image_copy, CV_BGRA2BGR);
|
||||
|
||||
// invert image
|
||||
bitwise_not(image_copy, image_copy);
|
||||
cvtColor(image_copy, image, CV_BGR2BGRA);
|
||||
}
|
||||
// invert image
|
||||
bitwise_not(image_copy, image_copy);
|
||||
cvtColor(image_copy, image, CV_BGR2BGRA);
|
||||
}
|
||||
|
||||
|
||||
Start!
|
||||
@@ -213,14 +213,14 @@ 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:
|
||||
|
||||
.. code-block:: objc
|
||||
:linenos:
|
||||
:linenos:
|
||||
|
||||
#pragma mark - UI Actions
|
||||
#pragma mark - UI Actions
|
||||
|
||||
- (IBAction)actionStart:(id)sender;
|
||||
{
|
||||
[self.videoCamera start];
|
||||
}
|
||||
- (IBAction)actionStart:(id)sender;
|
||||
{
|
||||
[self.videoCamera start];
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user