I downloaded the latest pre-built opencv2.framework from the OpenCV SourceForge page. then in Xcode6-Beta3, I added opencv2.framework as a required linked framework under the "General" tab of my Swift project settings:
This is the structure of the framework after the framework is added to the project:
If this was a Objective-C project I could add the following import statement:
#ifdef __cplusplus
#import <opencv2/opencv.hpp>
#endif
In my ViewController.swift file, if I do something similar I get a "no such module" error:
I tried the following variations that result in an error:
import opencv2/opencv.hpp
import opencv2/opencv
import opencv
import "opencv2/opencv.hpp"
import <opencv2/opencv>
What is the correct way to import opencv in to my Swift project?
In order to use C++ inside Objective C (OpenCV is written in C++ and C++ cannot interface directly with Swift), you need to change the file extension from OpenCVWrapper.
OpenCV (open source computer vision library) is an open source computer vision and machine learning software library containing more than 2,500 optimized algorithms.
It's easiest if you start with a simple project all ready to compile and run out of the box.
This xcode project works with Swift, XCode 6.1, OpenCV 2, and is MIT licensed: http://whitneyland.com/2014/10/opencv-swift.html
Here's one I haven't tried yet but there are so few still it's worth looking at: https://github.com/foundry/OpenCVSwiftStitch
You will need to create a bridge from C++ to Objective C or plain C. After that, you will need to include/import the bridge's header into an "Objective-C Bridging Header", which Xcode should have generated when you added a Swift source file in an existing project.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With