iOS — OpenCV and Swift
Written by Boris Ohayon | August 24, 2016
Today, we learn how to use OpenCV in iOS and Swift 💪
OpenCV is a a library written in C++. That was no problem with Objective-C, but things are now a tiny bit more complicated with Swift, but with practice, using a C++ library in Swift is no problem. Here’s what we’re going to do :
We will need to write a little bit of Objective-C++, but for us Swift lovers, it’s no big deal. Shall we begin ?
Step 1 — Download the OpenCV framework for iOS
Head to http://opencv.org and download the latest version of the framework. I am currently using version 2.4.13.
Step 2 — Create a new iOS project
Create a new project, nothing new here. Select Swift as the language and you’re done.
Step 3 — Import OpenCV in the project
To import the library in the project, simply drag&drop the downloaded OpenCV file into the Project Navigator.
When this window appears, check Copy items if needed.
Step 4 — Create a bridging header
The library is now in our project but as explained before, we can’t communicate with it right away. We need a bridging header.
Create an Objective-C file by going to File > New > File (or ⌘N) and in iOS > Source, select Cocoa Touch Class.
Name it OpenCVWrapper, which is going to be an Objective-C class that subclasses NSObject.
As we are creating an Objective-C class in a Swift project, Xcode is smart and proposes to create a bridging header. Click on Create Bridging Header.
We now have created three files : OpenCVWrapper.h, OpenCVWrapper.m and <NameOfYourProject>-Bridging-Header.h :
Step 5 — Configure the bridging header
The bridging header tells our Swift code what Objective-C stuffs are available. OpenCVWrapper.h will be the door to our Objective-C code, so we add the following line to the bridging header :
Step 6 — Change to Objective-C++
In order to use the C++ framework, we have to change to Objective-C++. This is done by simply changing the extension of OpenCVWrapper.m to OpenCVWrapper.mm. We can then import the library in our .mm file :
Step 7 — Write a test method
To test things out, we’ll write a pretty simple method called isThisWorking. Declare the function in OpenCVWrapper.h inside the @interface block, and implement it in OpenCVWrapper.mm, inside the @implementation block.
We are going to write a simple Objective-C++ function, but to help us write less code, let’s use the namespace std (standard) that will allow us to print stuff in the console. Add this below the #import section.
Complete our function : make it print something in the console. Notice how we use C++ code in an Objective-C function 😜
Final Step — Call our method from Swift
In the ViewController.swift file created by default, we can create an object of class OpenCVWrapper, and we can easily call the methods declared in OpenCVWrapper.h. Here we can only call just one : isThisWorking, as it is the only method there.
When running, this outputs :
🤘 That’s it !
You are now free to use OpenCV in your Swift projects, create new methods, let your imagination go! 😎
Like what you read? Let’s hit the ❤️ button so that everybody can read it too!
But if you prefer ether, you can also use them
Want to support those tutorials? A bitcoin donation is always fun! 19MZUSLWszAdkaKVJNwrcVwgfvHxvqNMVU