在 IOS Swift 项目中使用 OpenCV 进行视频处理 [英] Video processing with OpenCV in IOS Swift project

查看:77
本文介绍了在 IOS Swift 项目中使用 OpenCV 进行视频处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用桥接头(将 Swift 连接到 Objective C)和 Objective C 包装器(将 Objective C 连接到 C++)将 opencv 集成到 Swift IOS 项目中.使用这种方法,我可以从 Swift 代码中传递单个图像,在 C++ 文件中分析它们并将它们取回.

I've integrated opencv in Swift IOS project using bridging header (to connect Swift to Objective C) and a Objective C wrapper (to connect Objective C to C++). Using this method I can pass single images from the Swift code, analyse them in the C++ files and get them back.

我看到opencv提供了CvVideoCamera对象,可以与 Objective C UIViewController 集成.

但由于我的 UIViewController 是用 Swift 编写的,我想知道这是否也可能?

But since my UIViewController are written in Swift I've wondered if this is possible as well?

推荐答案

这是在我有机会自己玩这个之后对我的初始答案的更新.是的,可以将 CvVideoCamera 与用 Swift 编写的视图控制器一起使用.如果您只是想使用它在您的应用中显示来自相机的视频,那真的很简单.

This is an update to my initial answer after I had a chance to play with this myself. Yes, it is possible to use CvVideoCamera with a view controller written in Swift. If you just want to use it to display video from the camera in your app, it's really easy.

#import 通过桥接头.然后,在您的视图控制器中:

#import <opencv2/highgui/cap_ios.h> via the bridging header. Then, in your view controller:

class ViewController: UIViewController, CvVideoCameraDelegate {
...
    var myCamera : CvVideoCamera!
    override func viewDidLoad() {
        ...
        myCamera = CvVideoCamera(parentView: imageView)
        myCamera.delegate = self
        ...
    }
}

ViewController 实际上不能符合 CvVideoCameraDelegate 协议,但是 CvVideoCamera 没有委托就不能工作,所以我们解决了这个问题通过声明 ViewController 来采用该协议而不实现其任何方法.这将触发编译器警告,但来自摄像头的视频流将显示在图像视图中.

The ViewController cannot actually conform to the CvVideoCameraDelegate protocol, but CvVideoCamera won't work without a delegate, so we work around this problem by declaring ViewController to adopt the protocol without implementing any of its methods. This will trigger a compiler warning, but the video stream from the camera will be displayed in the image view.

当然,您可能希望实现 CvVideoCameraDelegate 的(仅)processImage() 方法以在显示视频帧之前对其进行处理.你不能在 Swift 中实现它的原因是因为它使用 C++ 类型,Mat.

Of course, you might want to implement the CvVideoCameraDelegate's (only) processImage() method to process video frames before displaying them. The reason you cannot implement it in Swift is because it uses a C++ type, Mat.

因此,您需要编写一个Objective-C++ 类,其实例可以设置为相机的委托.该 Objective-C++ 类中的 processImage() 方法将由 CvVideoCamera 调用,并依次调用 Swift 类中的代码.下面是一些示例代码片段.在 OpenCVWrapper.h 中:

So, you will need to write an Objective-C++ class whose instance can be set as camera's delegate. The processImage() method in that Objective-C++ class will be called by CvVideoCamera and will in turn call code in your Swift class. Here are some sample code snippets. In OpenCVWrapper.h:

// Need this ifdef, so the C++ header won't confuse Swift
#ifdef __cplusplus
#import <opencv2/opencv.hpp>
#endif

// This is a forward declaration; we cannot include *-Swift.h in a header.
@class ViewController;

@interface CvVideoCameraWrapper : NSObject
...
-(id)initWithController:(ViewController*)c andImageView:(UIImageView*)iv;
...
@end

在包装器实现中,OpenCVWrapper.mm(它是一个 Objective-C++ 类,因此是 .mm 扩展名):

In the wrapper implementation, OpenCVWrapper.mm (it's an Objective-C++ class, hence the .mm extension):

#import <opencv2/highgui/cap_ios.h>
using namespace cv;
// Class extension to adopt the delegate protocol
@interface CvVideoCameraWrapper () <CvVideoCameraDelegate>
{
}
@end
@implementation CvVideoCameraWrapper
{
    ViewController * viewController;
    UIImageView * imageView;
    CvVideoCamera * videoCamera;
}

-(id)initWithController:(ViewController*)c andImageView:(UIImageView*)iv
{
    viewController = c;
    imageView = iv;

    videoCamera = [[CvVideoCamera alloc] initWithParentView:imageView];
    // ... set up the camera
    ...
    videoCamera.delegate = self;

    return self;
}
// This #ifdef ... #endif is not needed except in special situations
#ifdef __cplusplus
- (void)processImage:(Mat&)image
{
    // Do some OpenCV stuff with the image
    ...
}
#endif
...
@end

然后将 #import "OpenCVWrapper.h" 放在桥接头中,Swift 视图控制器可能如下所示:

Then you put #import "OpenCVWrapper.h" in the bridging header, and the Swift view controller might look like this:

class ViewController: UIViewController {
...
    var videoCameraWrapper : CvVideoCameraWrapper!

    override func viewDidLoad() {
        ...
        self.videoCameraWrapper = CvVideoCameraWrapper(controller:self, andImageView:imageView)
        ...
    }

请参阅 https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html 关于前向声明和 Swift/C++/Objective-C 互操作.网上有很多关于 #ifdef __cplusplusextern "C" 的信息(如果你需要的话).

See https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html about forward declarations and Swift/C++/Objective-C interop. There is plenty of info on the web about #ifdef __cplusplus and extern "C" (if you need it).

processImage() 委托方法中,您可能需要与某些 OpenCV API 进行交互,您还必须为此编写包装器.您可以在其他地方找到一些相关信息,例如这里:在 Swift iOS 中使用 OpenCV

In the processImage() delegate method you will likely need to interact with some OpenCV API, for which you will also have to write wrappers. You can find some info on that elsewhere, for example here: Using OpenCV in Swift iOS

2019 年 9 月 3 日更新

应社区要求,见评论,示例代码已放在 GitHub 上 https://github.com/aperedera/opencv-swift-examples.

At the community request, see comments, the sample code has been placed on GitHub at https://github.com/aperedera/opencv-swift-examples.

此外,在撰写本文时,OpenCV iOS 框架的当前版本不再允许 Swift 代码使用声明 CvVideoCameraDelegate 协议的标头(现在位于 videoio/cap_ios.h 中),因此您不能仅将其包含在桥接头中并声明视图控制器符合协议以在您的应用中简单地显示相机视频.

Also, the current, as of this writing, version of the OpenCV iOS framework no longer allows Swift code to use the header (now it's in videoio/cap_ios.h) that declares the CvVideoCameraDelegate protocol, so you cannot just include it in the bridging header and declare the view controller to conform to the protocol to simply display camera video in your app.

这篇关于在 IOS Swift 项目中使用 OpenCV 进行视频处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆