Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using OpenCV in Swift iOS

After adding the OpenCV 2 framework in my xcode project, I tried searching for samlpes or tutorials for integration with swift.

Are there any good tutorials for the same?

like image 634
Kaushil Ruparelia Avatar asked Jun 18 '15 07:06

Kaushil Ruparelia


People also ask

Can we use OpenCV in Swift?

In order to use C++ inside Objective C (OpenCV is written in C++ and C++ cannot interface directly with Swift), you need to change the file extension from OpenCVWrapper.

Does OpenCV work on iOS?

OpenCV (Open Source Computer Vision Library) is a free, open source computer vision library with thousands of useful algorithms. Written in optimized C/C+ +, OpenCV has C+ +, C, Python, MATLAB and Java interfaces and supports Windows, Linux, Mac OS, iOS and Android.

What is OpenCV in Swift?

OpenCV (open source computer vision library) is an open source computer vision and machine learning software library containing more than 2,500 optimized algorithms.


2 Answers

OpenCV is a framework written in C++. Apple's reference tell us that

You cannot import C++ code directly into Swift. Instead, create an Objective-C or C wrapper for C++ code.

so you cannot directly import and use OpenCV in a swift project, but this is actually not bad at all because you (need) continue to use the C++ syntax of the framework which is pretty well documented all over the net.

So how do you proceed?

  1. Create a new Objective-C++ class (.h, .mm) for calling C++ OpenCV

OpenCVWrapper.h

#import <UIKit/UIKit.h> #import <Foundation/Foundation.h>  @interface OpenCVWrapper : NSObject      + (UIImage *)processImageWithOpenCV:(UIImage*)inputImage;  @end 

OpenCVWrapper.mm (use the File -> New... Wizard for Objective-C and rename the .m file to .mm)

#include "OpenCVWrapper.h" #import "UIImage+OpenCV.h" // See below to create this  #include <opencv2/opencv.hpp>  using namespace cv; using namespace std;  @implementation OpenCVWrapper : NSObject      + (UIImage *)processImageWithOpenCV:(UIImage*)inputImage {         Mat mat = [inputImage CVMat];                  // do your processing here         ...                  return [UIImage imageWithCVMat:mat];     }  @end 

As an alternative to creating new classes such as the example OpenCVWrapper.h/mm you can use Objective-C categories to extend existing Objective-C classes with OpenCV functionality. For instance UIImage+OpenCV category:

UIImage+OpenCV.h

#import <UIKit/UIKit.h> #import <opencv2/opencv.hpp>  @interface UIImage (OpenCV)      //cv::Mat to UIImage + (UIImage *)imageWithCVMat:(const cv::Mat&)cvMat; - (id)initWithCVMat:(const cv::Mat&)cvMat;      //UIImage to cv::Mat - (cv::Mat)CVMat; - (cv::Mat)CVMat3;  // no alpha channel - (cv::Mat)CVGrayscaleMat;  @end 

UIImage+OpenCV.mm

See https://github.com/foundry/OpenCVSwiftStitch/blob/master/SwiftStitch/UIImage%2BOpenCV.mm

  1. Update the Bridging-Header to make all Objective-C++ classes you created available to Swift by importing our newly created wrappers (#import "OpenCVWrapper.h")

  2. Use your wrapper in your Swift files:

    let image = UIImage(named: "image.jpeg") let processedImage = OpenCVWrapper.processImageWithOpenCV(image) 

All Objective-C++ classes included in the bridge header are available directly from Swift.

like image 155
Tomas Camin Avatar answered Sep 29 '22 06:09

Tomas Camin


I have a demo on github:

https://github.com/foundry/OpenCVSwiftStitch

It shows how to use the OpenCV stitcher from a Swift-based project. There is not a lot to the openCV side, it's really just a demo of how to put the various pieces together.

As Swift won't talk C++ directly, it uses a thin Objective-C++ wrapper to mediate between the iOS side and the openCV side.

Made in answer to these questions:
Can I mix Swift with C++? Like the Objective - C .mm files
libraries to CAPTURE panorama in iOS 6

like image 38
foundry Avatar answered Sep 29 '22 05:09

foundry