Using OpenCV in a Swift Project.

Computer Vision & Robotics A-Z Guide.

Dmytro Nasyrov
May 1, 2017 · 4 min read

Give us a message if you’re interested in Blockchain and FinTech software development or just say Hi at Pharos Production Inc.

Or follow us on Youtube to know more about Software Architecture, Distributed Systems, Blockchain, High-load Systems, Microservices, and Enterprise Design Patterns.

Pharos Production Youtube channel

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. The current latest version is 3.2.0

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

Image for post
Image for post
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:

Image for post
Image for post
Xcode file tree with OpenCV inside

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

Image for post
Image for post
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?).

Image for post
Image for post
Image for post
Image for post
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.
Image for post
Image for post
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 a bridging header. Say Yes. We need to link objective-c classes with swift and this is our glue.

Image for post
Image for post
create bridging header

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

Image for post
Image for post
Bridging header location

And add wrapper header to bridging header.

Image for post
Image for post

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 funnier). I will not provide any user interface creation step here because it’s not relevant to this article. You can find the 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++.

Image for post
Image for post
.mm file
Image for post
Image for post
Objective-C++

5. Now we can start with OpenCV calls. We will add 4 methods. One for 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 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.

Image for post
Image for post
.h
Image for post
Image for post
.mm

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

Image for post
Image for post

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

Image for post
Image for post
iPhone simulator and OpenCV

DONE

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

Thanks for reading!

Pharos Production

Software Development Company.

Dmytro Nasyrov

Written by

CTO and Founder@ Pharos Production Inc. — Blockchain and Fintech Software Development Company.

Pharos Production

Software Development Company. Fintech and Blockchain. Enterprise Solutions.

Dmytro Nasyrov

Written by

CTO and Founder@ Pharos Production Inc. — Blockchain and Fintech Software Development Company.

Pharos Production

Software Development Company. Fintech and Blockchain. Enterprise Solutions.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store