2

I have tried Core.hpp, Base.hpp header must be compiled as C++ error. I have BITCODE set as 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

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

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

Nitesh
  • 1,564
  • 2
  • 26
  • 53

1 Answers1

1

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 */

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

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:

#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

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

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

let UIImage * processedImage = MMOpenCVHelper.applyOpenCVTo(uiImage);
Quang Hoang
  • 146,074
  • 10
  • 56
  • 74