The modern guide to openCV on iOS

Disclaimer: I shall only be covering image processing, not video. Also, I would expect a basic understanding of openCV from the dear reader.

So, I was working on a project last semester. A basic Computer Vision project that involved finding out the number of yarns in a fabric in 2 directions. A secondary goal of the project involved doing this on an iOS app. Naturally, after looking at all the possible libraries, we settled on what appeared to be the most cross platform library: OpenCV.

Boy. Were we wrong…

The desktop python app was easier than I expected. Other than the mathematics and the algorithms we decided to use, programming was relatively painless( building and installing the openCV beta on a mac and later a raspberry pi, however was not. I’ll save that rant for another story).

Now onto business. Here’s the bad news: the openCV iOS framework has not been updated since iOS 5. Thankfully, it’s updated for ARC. And since the library itself is written in C++, so simple namespace calling should work.

So, I went over to the openCV iOS page(at the time of writing this article, the user tutorial page 404ed on me :/ ).

For iOS 8+, we need to do 2 things. First, add a prefix-pch file. This tutorial should give you a good idea. PS: Don’t forget to add it to the prefix header in your project’s build settings!!

Secondly, change the extensions of your ViewController.m files to .mm so it can use C++ functions.

Now, onto the Hello world project. Cool, they have a few functions to convert Mats to UIIMages and….

Well, 💩.

Ah, basically a lot of namespace errors and stuff. Nothing really logically wrong with the code. Here, I’ll post the fixed versions here.

Cool? Great. Now I’ll post some basic examples of basic functions.


Thresholding

RGB to Black and White.

And finally, edge detection

For an entire working XCode project, refer here

PS: In case there’s anything more you’d like me to demo, comment/respond below.

PPS: I’m looking for a job(I’m open to relocating)! DMs open on Twitter.