I am working on a project where I need OpenCV, which is in Cpp. I successfully made the Cpp file, the wrapper, and everything is working fine. However I would now like to display some variables from the Cpp code in the UI. To do so, I made a second function in my Cpp file (which returns the variables I want) and a second wrapper for that function, however I get this error:
Code Block Undefined symbols for architecture arm64: "_OBJC_CLASS_$_dataAccessBridge", referenced from: objc-class-ref in ViewController.o ld: symbol(s) not found for architecture arm64 clang: error: linker command failed with exit code 1 (use -v to see invocation)
This is how my code looks like:
The cpp file, called imageProcessing.cpp:
Code Block #include "imageProcessing.hpp" using namespace cv; using namespace std; Mat image_processing::detect_line(Mat image) { /*colouring the bright parts of the image in red, just to make sure everything is working*/ Mat img; cvtColor(image, img, CV_RGB2GRAY); for(int i = 0; i < image.rows; i) { for(int j = 0; j < image.cols; j) { if (img.at<uchar>(i, j) > 127) { image.at<Vec3b>(i, j)[0] = 255; image.at<Vec3b>(i, j)[1] = 0; image.at<Vec3b>(i, j)[2] = 0; } } } return image; } string image_processing::dataAccess() { return "hello world from c++"; }
The hpp file, called imageProcessing.hpp:
Code Block #ifndef imageProcessing_hpp #define imageProcessing_hpp #include <stdio.h> #include <opencv2/opencv.hpp> #include <string> using namespace cv; using namespace std; class image_processing { public: Mat detect_line(Mat image); string dataAccess(); }; #endif /* imageProcessing_hpp */
The .h file of the first wrapper (which is working fine), called imageProcessingBridge.h:
Code Block #ifndef imageProcessingBridge_h #define imageProcessingBridge_h #import <Foundation/Foundation.h> #import <UIKit/UIKit.h> @interface imageProcessingBridge : NSObject - (UIImage *) imageProcessingIn: (UIImage *) image; @end #endif /* imageProcessingBridge_h */
The .mm file of the first wrapper, called imageProcessingBridge.mm:
Code Block #import <opencv2/opencv.hpp> #import <opencv2/imgcodecs/ios.h> #import <Foundation/Foundation.h> #import "imageProcessingBridge.h" #import "imageProcessing.hpp" @implementation imageProcessingBridge - (UIImage *) imageProcessingIn:(UIImage *)image { /* convert UI image to mat */ cv::Mat opencvImage; UIImageToMat(image, opencvImage, true); /* convert colorspace */ cv::Mat convertedColorSpaceImage; cv::cvtColor(opencvImage, convertedColorSpaceImage, CV_RGBA2RGB); /* Run image processing algorithm */ image_processing imageProcessing; cv::Mat processedImage = imageProcessing.detect_line(convertedColorSpaceImage); /* convert mat to UI image and return it to the caller */ return MatToUIImage(processedImage); } @end
The .h file of the second wrapper (which is not working), called dataAccessBridge.h:
Code Block #ifndef dataAccessBridge_h #define dataAccessBridge_h #import <Foundation/Foundation.h> #import <UIKit/UIKit.h> @interface dataAccessBridge : NSObject - (NSString *) dataAccesOut; @end #endif /* dataAccessBridge_h */
The .mm file of the second wrapper (which is not working), called dataAccessBridge.mm:
Code Block #import <Foundation/Foundation.h> #import "dataAccessBridge.h" #import "imageProcessing.hpp" @implementation dataAccessBridge - (NSString *) dataAccessOut { image_processing data_access; return [NSString stringWithCString:data_access.dataAccess().c_str() encoding:NSUTF8StringEncoding]; } @end
The ImageProcessingTest-Bridging-Header.h file:
Code Block #import "imageProcessingBridge.h" #import "dataAccessBridge.h"
How I call the functions in the ViewController.swift file:
Code Block let processed_image = imageProcessingBridge().imageProcessing(in: image) self.frameRate.text = dataAccessBridge().imageProcessing()
This last line of code also gives me this error:
Code Block Value of type 'dataAccessBridge' has no member 'imageProcessing'
But I would assume this is a result of the first error. I also notice that the icons next the dataAccessBridge.h and dataAccessBridge.mm file are greyed out.
What am I doing wrong? Or is it not possible to have 2 wrappers for the same Cpp file? In which case, how should I call the second function?
Thanks in advance