Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import opencv2 framework in iOS Swift

Tags:

ios

opencv

swift

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:

enter image description here

This is the structure of the framework after the framework is added to the project:

enter image description here

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:

enter image description here

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?

like image 533
TERACytE Avatar asked Jul 19 '14 16:07

TERACytE


People also ask

Can we use OpenCV in Swift?

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.

What is OpenCV in Swift?

OpenCV (open source computer vision library) is an open source computer vision and machine learning software library containing more than 2,500 optimized algorithms.


2 Answers

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

like image 199
whitneyland Avatar answered Oct 21 '22 02:10

whitneyland


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.

like image 29
MaddTheSane Avatar answered Oct 21 '22 04:10

MaddTheSane