OpenCV 3.2.0 Setup on iOS

Gone are the days of needing a dedicated desktop to be able to utilize the power of image processing software. OpenCV is one of the most popular image processing libraries out there, so being able to use it on a mobile device only unlocks more of it's potential.

Most of the time the hardest part about a project is knowing how to get started. So here's a short how-to on getting the latest OpenCV (3.2.0 as of 4-20-2017) working on iOS.


  1. Make sure that you have Xcode installed and start up a project.

    • For this demo I chose a 'Single View Application'.

    • After that go ahead and name your project whatever you'd like.

    • Go ahead and save your project somewhere on your machine. One it is saved it will open Xcode.

    Congrats, you have a project initialized. This is when we can start setting up for OpenCV.

  2. The next step is easy, close Xcode.

  3. Although there are multiple ways to get OpenCV working, we're going to be utilizing Cocoapods so make sure that you have it installed on your machine. Type pod --version in a terminal and if a version number (latest version being 1.2.1) appears then congrats, you're all ready to go. If it does not then and you want to install it, or you want to update to the latest version then use the following command.

    sudo gem install cocoapods

    For those of you who don't know, CocoaPods are projects and libraries that have been pre-configured to run without having to mess with all the linking and compiling usually needed to get an external library or framework up and running. There are many CocoaPods available and many of them can save you huge amounts of time in your development cycle. Just visit website and search for what you need, chances are you'll find it.

  4. Next, open up a terminal and navigate to the directory that you saved the project to and initialize cocoa pods using the following command

    pod init

    This will create a file called Podfile which houses the configurations for your current project.

  5. This is where cocoapods show their true power, instead of setting up OpenCV manually, all we need to do is open up the pod file add and single line after the line that says # Pods for OpenCV_Demo

    pod 'OpenCV', '~> 3.2'
  6. Simple enough, now save that file and run the install. This will grab all the dependencies and do all the necessary setup to get the library installed correctly.

    pod install
  7. There will now be a .xcworkspace file (in the same directory) which is what you will be using instead of your original .xcodeproj file. Go ahead and navigate to the workspace file and open it, Congrats, you now have a cocoapod compatible Xcode Workspace with OpenCV installed, you can freely add and remove cocoapods to the Podfile and re-run the pod install command without the worry of messing up your existing code or other libraries.

  8. Now lets get some OpenCV code running. Even though the OpenCV library is linked up correctly you will still need to take a few steps to access it. This includes creating a bridge file and wrapper.
    Create a new group under your main OpenCV Demo (or whatever you named the project) folder and call it Wrappers (this step is not mandatory but I think it helps keep things organized). You do this by simply right clicking on the folder and clicking New Group. The final result should look like the following image.

  9. Next Right Click on the newly created Wrappers folder and click New File.... Select the Cocoa Touch Class

  10. Name the file OpenCVWrapper or something along those lines and then set the Language to Objective-C, click ok and Xcode should ask you if you want to create an Objective-C bridging header, say yes.

  11. Next lets write a function that returns a string containing the OpenCV version to verify that OpenCV is working.

  12. Open the OpenCV_Demo-Bridging-Header.h and add this line

    #import OpenCVWrapper.h
  13. In OpenCVWrapper.h add this interface

    @interface OpenCVWrapper : NSObject
        -(NSString *) openCVVersionString;
  14. In OpenCVWrapper.m add this implementation Note: It is important that the #import "OpenCVWrapper.h" is the last import line, just remember to always import the wrappers last, and you'll be set, not doing so will create macro errors in Xcode.

    #import <opencv2/opencv.hpp>
    #import "OpenCVWrapper.h"
    @implementation OpenCVWrapper
    -(NSString *) openCVVersionString
        return [NSString stringWithFormat:@"OpenCV Version %s", CV_VERSION];
  15. In your ViewController.swift modify your ViewDidLoad function to look like the following

    override func viewDidLoad() {
       // Do any additional setup after loading the view, typically from a nib.
       let t = OpenCVWrapper()
  16. Last step is to rename the OpenCVWrapper.m file to to allow it to support Objective-C++ and not just Objective-C

  17. Run the code and you should be rewarded with a version number printed to the console.


Congrats, you now have OpenCV 3.2.0 running on iOS!