2014年8月21日木曜日

OpenCV for iOS を試してみた

OpenCV for iOS を試した際のメモ

各バージョン
iOS : v7.1.2
Xcode : v5.1.1
OpenCV for iOS : v2.4.9

まずOpenCV公式ページからOpenCV for iOSをダウンロードします。

ダウンロードしたモノを解凍すると opencv2.framework が出てきます、これを使っていきます。

ここのページOpenCV iOS - Video Processingを参考に進めていきます。

テンプレートはSingle View Appricationを使いました。
作成できたら、先ほどダウンロードし解凍した opencv2.framework を作成したプロジェクトのFrameworksフォルダに入れておきます。
今回使用するフレームワークは以下のモノになります。opencv2.freamework 以外のものは Linked Frameworks and Libraries から追加してください。

使用するフレームワーク
  • opencv2.freamework
  • Accelerate.freamework
  • AssetsLibrary.freamework
  • AVFoundation.freamework
  • CoreGraphics.freamework
  • CoreImage.freamework
  • CoreMedia.freamework
  • CoreVideo.freamework
  • QuartzCore.freamework
  • UIKit.freamework
  • Foundation.freamework


次に <プロジェクト名>-Prefix.pch
の内容に以下を追記します。

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


追記した後の内容が以下のもになります。


//<プロジェクト名>-Prefix.pch

//
//  Prefix header
//
//  The contents of this file are implicitly included at the beginning of every source file.
//

#import <Availability.h>

#ifndef __IPHONE_5_0
#warning \"This project uses features only available in iOS SDK 5.0 and later.\"
#endif

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

#ifdef __OBJC__
    #import <UIKit/UIKit.h>
    #import <Foundation/Foundation.h>
#endif


ユーザインターフェース

次に Main.storyboard に UIImageView と UIButton2つ を配置します。下の図のように配置します。


対応するViewControllerにUIImageViewはIBOutletsとUIButton2つはIBActionsで追加して接続してください。したモノが下の図になります。


カメラの追加

ビューコントローラにカメラコントローラを追加し、ビューがロードされたときに、それを初期化するらしいです。
ViewController.hに以下の内容を記述


//ViewController.h

#import <UIKit/UIKit.h>
#import <opencv2/highgui/cap_ios.h>

@interface OFTViewController : UIViewController <CvVideoCameraDelegate>

@end


ビューコントローラはCvVideoCameraDelegateプロトコルを実装しなければならないし、ビデオカメラへのデリゲートとして設定する必要があります。
らしいので#import <opencv2/highgui/cap_ios.h>のインポートと CvVideoCameraDelegate でデリゲートを通す。


ViewController.mに以下の内容を記述

//ViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.videoCamera = [[CvVideoCamera alloc] initWithParentView:_image];
    self.videoCamera.delegate = self;
    self.videoCamera.defaultAVCaptureDevicePosition = AVCaptureDevicePositionFront;
    self.videoCamera.defaultAVCaptureSessionPreset = AVCaptureSessionPreset352x288;
    self.videoCamera.defaultAVCaptureVideoOrientation = AVCaptureVideoOrientationPortrait;
    self.videoCamera.defaultFPS = 30;
    self.videoCamera.grayscaleMode = NO;
}


self . videoCamera . delegate = self ; でデリゲートを通している。


このケースでは、カメラを初期化し、各フレームをレンダリングするための標的としてのImageViewを提供する。CvVideoCameraは基本的にAVFoundationのラッパーなので、プロパティとしてAVFoundationカメラオプションの一部provie。たとえば私たちはフロントカメラを使用したい、(ビデオカメラは、通常、あなたは肖像画のアプリケーションを設計する際に、転置データ、その結果、横長モードで出力)352×288のビデオサイズやビデオの向きを設定。

プロパティdefaultFPSは、カメラのFPSを設定します。処理は、所望のFPSよりも小さい速い場合、フレームが自動的に削除されます。
プロパティ grayscaleMode=YES の場合、「YUV(YpCbCr 4:2:0)」、grayscaleMode=NO の32ビットBGRAを出力する。

らしい適当翻訳。

画像処理

ViewController.mに以下を記述。

//ViewController.m

#pragma mark - Protocol CvVideoCameraDelegate
#ifdef __cplusplus
- (void)processImage:(cv::Mat&)images;
{
    // Do some OpenCV stuff with the image
    cv::Mat image_copy;
    cv::cvtColor(images, image_copy, CV_BGRA2BGR);
    
    // invert image
    bitwise_not(image_copy, image_copy);
    cv::cvtColor(image_copy, images, CV_BGR2BGRA);
}
#endif


色の反転の画像処理を行っています。

ここでCV::Matをを使っていることに注意してください。
ViewController.m のコンパイルの設定を変更する必要があります(ここで少し間取りました)。
ViewController.m の Utilities の Identity and Typeの項目(右側にあるよ)のTypeを Objective-C++ Sourceに変更。


それと Build Phases の Compile Sources の ViewController.m の Compiler Flags を -x objective-c++ にする必要があります。


カメラ ON OFF

ボタンを押すと、次のコードは、適切にUIを接続と仮定して、カメラを起動・終了します。

//カメラ開始
- (IBAction)startOpencv:(id)sender {
      [self.videoCamera start];
}
//カメラ終了
- (IBAction)stopOpenCV:(id)sender {
    [self.videoCamera stop];
}


起動に成功するとインカメラが起動し反転した映像が映っていると思います。


最後に久しぶりにiOSのコードを触ったのでコードの書き方は正しいか怪しいです。


参考にしたページは以下になります。
OpenCV iOS - Video Processing
openCV 2.4.8 iOS 7.0 framework linker errors

0 件のコメント:

コメントを投稿