The use of Computer Vision in mobile apps has grown by leaps and bounds over the years, thanks in part to the sensors and powerful processors that keep making their way into mobile devices. Image processing has evolved from offloading the processing of images to remote servers, to simple apps used to scan barcodes or images of checks, to apps that can do real-time face or text detection and other complex image processing tasks on the device.
If you’ve ever researched how to implement things like image tracking, image recognition, or programmatic image filtering in your app then you’ve probably found that one of the most used and talked about libraries is OpenCV. OpenCV is an Open Source Computer Vision library that is widely used in industry and academia for complex real-time image and video processing. According to their website, OpenCV has a user community of more than 47,000 and an estimated 14 million downloads. It’s used for everything from interactive art to mines inspection, from stitching maps on the web to advanced robotics.
Even if you’ve heard of OpenCV or considered using OpenCV for a project, you may not know how easy it is to integrate the library into native mobile apps for Android and iOS. Using this native integration, mobile apps can perform real-time processing, shape recognition, filtering, etc., right on the device with no internet connection or third-party tools.
In this blog post, we will run through how to install and use OpenCV on both Android and iOS, and provide example apps to demonstrate the possibilities for the technology.
OpenCV on iOS
Since OpenCV is originally written in C/C++, the library lends itself well to the iOS platform, which is built on Objective-C (another extension of original C code). Even still, the process can be tough to get your head around and the documentation online is sparse. The main steps to integration on iOS are:
Installation (using CocoaPods or building the framework locally)
Creating an Objective-C Wrapper
Using the OpenCV library methods
Installation
There are two options for installing and linking the OpenCV library with your iOS project: using CocoaPods or building manually from source.
Using CocoaPods:
Install CocoaPods via these instructions.
In your Podfile, add the line pod ‘OpenCV’ to add OpenCV as a new dependency:
3. In Terminal, cd to your application directory and run pod install.
4. Open the project workspace (.xcworkspace not .xcodeproj) in Xcode. You should see a Pods directory with OpenCV included in the list:
Building Manually:
If your project does not use CocoaPods, you can still bring in the OpenCV framework manually by building from source. Follow these instructions to build the framework, then link it with your Xcode project like this:
Select the project Navigator in the left hand panel and click on project name.
Under the TARGETS click on Build Phases. Expand Link Binary With Libraries option.
Click on Add Others and go to directory where
opencv2.framework
is located and click open.
Creating an Objective-C Wrapper
In order to access C/C++ code in our Objective-C/Swift environment, we must create a “wrapper” file that abstracts the OpenCV library behind relevant Objective-C method calls:
Create a header file called “OpenCVWrapper.h”.
In this file, add interface method(s) for what your app will need to use OpenCV for.
3. Create a new Objective-C file called “OpenCVWrapper”. After it is created, change the file extension from .m to .mm.
4. In this .mm