Using OpenCV in a Swift project

All of you know OpenCV is one of the best computer vision frameworks. It’s free, it has huge community support and it has amazing tools inside. What about using OpenCV on Apple devices or maybe in a brand new server side project? It’s quite straightforward to add the latest OpenCV library to your Xcode project and use it with swift.


PREPARE

  1. First of all download the latest(or maybe outdated if you want) version of OpenCV framework from here. Current latest version is 3.2.0

2. Create a new project. Let’s assume it is Single View Application.

Create your first project

3. Drag and drop opencv2.framework downloaded earlier in your favorite project folder. That’s how it looks in my project:

Xcode file tree with OpenCV inside

Don’t forget to check opencv2.framework in dependancy, it should be there

opencv2.framework

4. To test our future functionality we will use any of two famous images. This is Mandrill and his best friend Lena(or Lenna?).

This is Lena and Mandrill

USE IT

We will use objective-c++ wrapper to use OpenCV c++ library.

  1. First of all get rid of Main.storyboard and sample ViewController.swift. We will not use both of them. Don’t forget to remove it from Deployment Info section of your project settings.
No main interface here

2. Create a new objective-c class OpenCVWrapper which is a subclass of NSObject. Then XCode will ask you to create bridging header. Say Yes. We need to link objective-c classes with swift and this is our glue.

create bridging header

If you’re going to change bridging header’s path to some other directory, you will need to fix path to header after you move it. It’s really easy, just type “bridging” in project’s build settings and you will find it.

Bridging header location

And add wrapper header to bridging header.

3. Next we should create all required UI because we want to test our new opencv cool stuff on our new friend Mandrill(we will take Mandrill rather than Lena just because he’s less sexy and a bit more funnier). I will not provide any user interface creation step here because it’s not relevant to this article. You can find working code on Github.

4. So far so good. We have our objective-c wrapper class which is PONSO(Plain Old NSObject) and can be natively used in swift classes. The only one small thing we should change is to rename it from .m to .mm so it’s now Objective-C++.

.mm file
Objective-C++

5. Now we can start with OpenCV calls. We will add 4 methods. One for a public access to our class and 3 private. Be sure you have set opencv.hpp import in .mm BEFORE header’s import. Otherwise it will not work. Also note we’re using cv namespace here. That’s to avoid of writing cv::Mat and their friends every time you’re calling something from OpenCV. Also it’s a good idea to get rid of documentation warnings and add c++ namespaces.

.h
.mm

6. Now we can use hassle-free wrapper in our swift code and it will work as expected.

I will explain basic OpenCV stuff we have used in this project later in the next article.

iPhone simulator and OpenCV

DONE

Easy, yeah? You can find full source here: Link to Github repo pharosproduction/Swift-with-OpenCV

And of course you’re welcome to hire us at Pharos Production Inc.

Thanks for reading!

Show your support

Clapping shows how much you appreciated Dmytro Nasyrov’s story.