OpenCV错误 - Core.hpp头必须编译为C ++

时间:2017-09-19 05:39:16

标签: ios swift opencv

我尝试过Core.hpp, Base.hpp header must be compiled as C++错误。 我将BITCODE设置为NO

#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>

@interface OpenCV : NSObject
    /// Converts a full color image to grayscale image with using OpenCV.
+ (nonnull UIImage *)cvtColorBGR2GRAY:(nonnull UIImage *)image;
+ (cv::Mat)cvMatFromAdjustedUIImage:(UIImage *)image;
+ (cv::Mat)cvMatFromUIImage:(UIImage *)image;
+ (cv::Mat)cvMatGrayFromUIImage:(UIImage *)image;
+ (cv::Mat)cvMatGrayFromAdjustedUIImage:(UIImage *)image;
@end

在我的OpenCVSample.mm我已将#import "OpenCV.h添加到标题中。

Git:https://github.com/n1tesh/OpenCV-Demo

1 个答案:

答案 0 :(得分:1)

您需要的是预编译头文件。使用.xcodeproj文件在同一文件夹中创建PrefixHeader.pch个文件,内容为:

#ifndef PrefixHeader_pch
#define PrefixHeader_pch

#ifdef __cplusplus
#include "opencv2/opencv.hpp"
// include other opencv2 headers if needed.
#endif

#endif /* PrefixHeader_pch */

然后转到Project的Build Settings,搜索Prefix Header并将其设置为PrefixHeader.pch

此外,如果要将桥接MMOpenCVHelper.h调用到swift代码,则必须仅使用Objective-C ++。在MMOpenCVHelper.mm中放置任何OpenCV或标准C ++。例如:

<强> MMOpenCVHelper.h

#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>

// These two headers god to mm file
// #include <opencv2/imgproc/imgproc.hpp>
// #include <opencv2/highgui/highgui.hpp>


@interface MMOpenCVHelper : NSObject
+ (UIImage *) applyOpenCVTo: (UIImage*) uiImage;

// these methods have to be in mm file, hence private
// + (UIImage *)UIImageFromCVMat:(cv::Mat)cvMat;
// + (cv::Mat)cvMatFromAdjustedUIImage:(UIImage *)image;
// + (cv::Mat)cvMatFromUIImage:(UIImage *)image;
// + (cv::Mat)cvMatGrayFromUIImage:(UIImage *)image;
// + (cv::Mat)cvMatGrayFromAdjustedUIImage:(UIImage *)image;


@end

MMOpenCVHelper.mm

#import "MMOpenCVHelper.h"
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>

@implementation MMOpenCVHelper

// implementation of public methods:
+ (UIImage *) applyOpenCVTo: (UIImage*) uiImage{
    cv::Mat cvFrame = [MMOpenCVHelper cvMatFromAdjustedUIImage: uiImage];
    // do something with cvFrame using OpenCV
    cv::Mat ret = cvFrame.clone();

    return [MMOpenCVHelper UIImageFromCVMat: ret];
}

// Implementations of the conversion functions:
+ (UIImage *)UIImageFromCVMat:(cv::Mat)cvMat{
    // do something here
    return uiImage;
}

+ (cv::Mat)cvMatFromAdjustedUIImage:(UIImage *)image;
+ (cv::Mat)cvMatFromUIImage:(UIImage *)image;
+ (cv::Mat)cvMatGrayFromUIImage:(UIImage *)image;
+ (cv::Mat)cvMatGrayFromAdjustedUIImage:(UIImage *)image;
@end

稍后,在SomeSwiftFile.swift你可以做(​​请验证这个,因为我对Swift不是很熟悉):

let UIImage * processedImage = MMOpenCVHelper.applyOpenCVTo(uiImage);