Using OpenCV in Swift iOS

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

Are there any good tutorials for the same?

+47
ios image-processing opencv swift
Jun 18 '15 at 7:04
source share
2 answers

OpenCV is a framework written in C ++. Apple link let us know 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 fast project, but actually it’s not at all bad because you (need) continue to use C ++ syntax for a framework that is pretty well documented throughout the network.

So how do you go on?

  • Create a new Objective-C ++ class (.h, .mm ) to call C ++ OpenCV

OpenCVWrapper.h

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

OpenCVWrapper.mm (use 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 OpenCVWrapper.h / mm example, you can use the Objective-C categories to extend existing Objective-C classes with OpenCV functions. For example, the category UIImage + OpenCV:

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 Bridging-Header to create all Objective-C ++ classes that you created for Swift by importing newly created wrappers ( #import "OpenCVWrapper.h" )

  2. Use your wrapper in your Swift files:

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

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

+61
Jun 18 '15 at 7:44
source share

I have a demo on github:

https://github.com/foundry/OpenCVSwiftStitch

It shows how to use an OpenCV sticker from a Swift-based project. For the openCV side, there aren't many, it's really just a demonstration of how to assemble the different parts.

Since Swift will not speak C ++ directly, it uses the thin Objective-C ++ shell to mediate between the iOS side and the openCV side.

Made in response to these questions:
Is it possible to mix Swift with C ++? Like Objective files - C.mm

for CAPTURE panorama in iOS 6

+33
Jun 18 '15 at 8:36
source share



All Articles