OpenCV错误 - Core.hpp头必须编译为C ++ [英] OpenCV Error - Core.hpp header must be compiled as C++

查看:3415
本文介绍了OpenCV错误 - Core.hpp头必须编译为C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试过 Core.hpp,Base.hpp标头必须编译为C ++ 错误。
我将BITCODE设置为

#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 我在标题中包含 #importOpenCV.h

In my OpenCVSample.mm I have included #import "OpenCV.h in header.

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

推荐答案

您需要的是一个预编译头文件。在同一个文件夹中创建一个 PrefixHeader.pch 文件包含内容的.xcodeproj文件

What you need is a precompile header file. Create a PrefixHeader.pch file at the same folder with the .xcodeproj file with content:

#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

Then go to Project's Build Settings, search for Prefix Header and set it to PrefixHeader.pch.

此外,如果你想调用bridge MMOpenCVHelper.h 对于你的快速代码,它必须是Obje仅限ctive-C ++。将任何OpenCV或标准C ++放在 MMOpenCVHelper.mm 中。例如:

Additionally, if you want to call bridge MMOpenCVHelper.h to your swift code, it has to be Objective-C++ only. Put anything OpenCV, or standard C++, in MMOpenCVHelper.mm. For example:

MMOpenCVHelper.h

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

And 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不是很熟悉):

Later, in SomeSwiftFile.swift you can do (please verify this since I'm not very familiar with Swift):

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

这篇关于OpenCV错误 - Core.hpp头必须编译为C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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