在Swift iOS中使用OpenCV
在我的xcode项目中添加OpenCV 2框架后,我尝试搜索samlpes或教程以便与swift集成。
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?
OpenCV是一个用C ++编写的框架。 Apple的参考告诉我们
OpenCV is a framework written in C++. Apple's reference tell us that
您无法直接将C ++代码导入Swift。相反,为C ++代码创建一个Objective-C或C包装器。
You cannot import C++ code directly into Swift. Instead, create an Objective-C or C wrapper for C++ code.
所以你不能在swift项目中直接导入和使用OpenCV,但这实际上并不坏,因为你(需要)继续使用框架的C ++语法,这在整个网络上都有很好的文档。
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.
那么怎么做你继续吗?
- 创建一个新的Objective-C ++类(.h, .mm )来调用C ++ OpenCV
- 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 (使用文件 - >新建... Objective-C向导并将.m文件重命名为.mm)
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
作为创建新的替代方案诸如OpenCVWrapper.h / mm示例之类的类可以使用Objective-C类别来扩展具有OpenCV功能的现有Objective-C类。例如UIImage + OpenCV类别:
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
参见 https://github.com/foundry/OpenCVSwiftStitch/blob/master/SwiftStitch/UIImage%2BOpenCV.mm
-
更新 Bridging-Header 通过导入我们新创建的包装器(
#importOpenCVWrapper.h
)来使您创建的所有Objective-C ++类可供Swift使用
Update the Bridging-Header to make all Objective-C++ classes you created available to Swift by importing our newly created wrappers (
#import "OpenCVWrapper.h"
)
在Swift文件中使用你的包装器:
Use your wrapper in your Swift files:
让image = UIImage(命名为:image.jpeg )
let processedImage = OpenCVWrapper.processImageWithOpenCV(image)
let image = UIImage(named: "image.jpeg") let processedImage = OpenCVWrapper.processImageWithOpenCV(image)
桥中包含的所有Objective-C ++类标题可直接从Swift获得。
All Objective-C++ classes included in the bridge header are available directly from Swift.