Using OpenCV in a Swift Project.
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.
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.
- 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.
3. Drag and drop opencv2.framework downloaded earlier in your favorite project folder. That’s how it looks in my project:
Don’t forget to check opencv2.framework in dependency, it should be there
4. To test our future functionality we will use any of two famous images. This is Mandrill and his best friend Lena(or Lenna?).
You can google to find more about this two folks and why we are using them:
We will use objective-c++ wrapper to use OpenCV c++ library.
- 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.
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.
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.
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 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++.
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.
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.
Easy, yeah? You can find the full source here: Link to Github repo pharosproduction/Swift-with-OpenCV
Thanks for reading!