在 Swift iOS 中使用 OpenCV
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30908593/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using OpenCV in Swift iOS
提问by Kaushil Ruparelia
After adding the OpenCV 2 framework in my xcode project, I tried searching for samlpes or tutorials for integration with swift.
在我的 xcode 项目中添加 OpenCV 2 框架后,我尝试搜索与 swift 集成的示例或教程。
Are there any good tutorials for the same?
有没有同样好的教程?
回答by Tomas Camin
OpenCV is a framework written in C++. Apple's referencetell us that
OpenCV 是一个用 C++ 编写的框架。苹果的参考资料告诉我们
You cannot import C++ code directly into Swift. Instead, create an Objective-C or C wrapper for C++ code.
您不能将 C++ 代码直接导入 Swift。相反,为 C++ 代码创建一个 Objective-C 或 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.
所以你不能在一个 swift 项目中直接导入和使用 OpenCV,但这实际上一点也不差,因为你(需要)继续使用框架的 C++ 语法,它在网络上都有很好的记录。
So how do you proceed?
那么你如何进行呢?
- Create a new Objective-C++ class (.h, .mm) for calling C++ OpenCV
- 创建一个新的 Objective-C++ 类 (.h, .mm) 用于调用 C++ OpenCV
OpenCVWrapper.h
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)
OpenCVWrapper.mm(使用 File -> New... Wizard for Objective-C 并将 .m 文件重命名为 .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:
作为创建新类(例如示例 OpenCVWrapper.h/mm)的替代方法,您可以使用 Objective-C 类别来扩展具有 OpenCV 功能的现有 Objective-C 类。例如 UIImage+OpenCV 类别:
UIImage+OpenCV.h
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
UIImage+OpenCV.mm
See https://github.com/foundry/OpenCVSwiftStitch/blob/master/SwiftStitch/UIImage%2BOpenCV.mm
见https://github.com/foundry/OpenCVSwiftStitch/blob/master/SwiftStitch/UIImage%2BOpenCV.mm
Update the Bridging-Headerto make all Objective-C++ classes you created available to Swift by importing our newly created wrappers (
#import "OpenCVWrapper.h"
)Use your wrapper in your Swift files:
let image = UIImage(named: "image.jpeg") let processedImage = OpenCVWrapper.processImageWithOpenCV(image)
更新桥接报头,使您通过导入新创建的包装创建提供给斯威夫特所有的Objective-C ++类(
#import "OpenCVWrapper.h"
)在 Swift 文件中使用包装器:
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.
桥头文件中包含的所有 Objective-C++ 类都可以直接从 Swift 获得。
回答by foundry
I have a demo on github:
我在github上有一个演示:
https://github.com/foundry/OpenCVSwiftStitch
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.
它展示了如何从基于 Swift 的项目中使用 OpenCV 拼接器。openCV 方面的内容并不多,它实际上只是一个演示如何将各个部分组合在一起。
As Swift won't talk C++ directly, it uses a thin Objective-C++ wrapper to mediate between the iOS side and the openCV side.
由于 Swift 不会直接使用 C++,它使用一个瘦的 Objective-C++ 包装器在 iOS 端和 openCV 端之间进行调解。
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
回答这些问题:
我可以将 Swift 与 C++ 混合使用吗?像iOS 6 中的 Objective - C .mm 文件
库到 CAPTURE 全景图