OpenCV on OS X Sierra

Dennis Walsh
3 min readApr 26, 2017

When I first began experimenting with OpenCV, I started with an iOS Augmented Reality tutorial. This was an old tutorial from 2014, written in Objective-C. I wanted to use Swift as much as possible, so I re-wrote the tutorial using Swift 3.0. I got this working in Swift for the most part, barring some minor issues with sound and the explosion animation that I am not yet able to resolve (you can find the source here:OpenCVSwift). I wanted to experiment more with OpenCV in the areas of feature detection and tracking, but in doing so, I ran into some crashes in my Objective-C++ files on certain OpenCV method calls. In order to determine if this was an issue with my Objective-C++/Swift code I decided to create a new in Xcode project using C++. This way I could run pure C++ code in Xcode and see if I get the same results.

When developing for iOS installing OpenCV is as easy as using Cocoapods. Just setup a pod file and bridging header and you are off and running.

Target ‘OpenCVSwift’ do
use_frameworks!
#pod ‘OpenCV’
pod ‘OpenCV2-contrib’
end

The same is not quite true for OS X, but we do have HomeBrew as a package manager for OS X. We need to install OpenCV and configure Xcode manually. There are a lot of tutorials and video tutorials available to help with this process. I sifted through quite a few before I got my Xcode project up and running. What follows are the steps that I used, the issues I encountered and how I resolved my issues. Hopefully this may help someone else interested in working with OpenCV in Xcode.

Installing OpenCV
There are a couple of ways to install OpenCV, one is by using HomeBrew. You can find the OpenCV package by running the brew search command, and then running the brew install command. You can also download the OpenCV source files and compile them using CMake. Instructions on how to do this can be found here. I opted for using HomeBrew.

brew search opencv
brew install homebrew/science/opencv3

This is the basic OpenCV installation. You might want/need to use the OpenCV’s extra modules which contain additional source code for things like feature detection and tracking. You can install those by specifing additional options during the installation.

brew install homebrew/science/opencv3 --with-contrib

I ran into some issues when installing the additional modules, certain files could not be found.

dyld: Library not loaded: @rpath/libopencv_calib3d.3.2.dylib.

I uninstalled OpenCV using HomeBrew and then ran brew cleanup. I then reinstalled OpenCV using HomeBrew with the extra modules, which seems to have solved the issue. This stackoverflow post suggested copying the files, however, I think Tim Smith’s comment regarding a missing/corrupt symlink was actually my issue.

You then need to configure Xcode to find OpenCV’s header files, libraries, and set the linker flags. In the build settings in Xcode you need to update four items.

· Include Always Search User Paths should be set to YES

· Header Search Paths should point to the location of your OpenCV implementation, this is typically usr/local/include or if you used HomeBrew then /usr/local/Cellar/opencv3/3.2.0/include

· Library Search Paths should point to the location of your OpenCV implementation, this is typically usr/local/lib or if you used HomeBrew then /usr/local/Cellar/opencv3/3.2.0/lib

· Other Linker Flags should be set to include the list of flags for you version of OpenCV. You can get a complete list by running the pkg-config command:
pkg-config --cflags --libs opencv

If you installed OpenCV using HomeBrew you might need to specify the location of the opencv.pc file:
pkg-config --cflags --libs /path/to/opencv.pc

For my version of OpenCV the location of opencv.pc was at the following location:
pkg-config --cflags --libs /usr/local/Cellar/opencv3/3.2.0/lib/pkgconfig/opencv.pc

For my version of OpenCV, the list of linker flags was as follows:
-lopencv_shape -lopencv_stitching -lopencv_objdetect -lopencv_superres -lopencv_videostab -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_video -lopencv_photo -lopencv_ml -lopencv_imgproc -lopencv_flann -lopencv_core

One additional thing I discovered when running OpenCV 3.2 on OS X Sierra 10.12.3, is that I had to include a call to std::waitKey(10) when reading a camera capture, otherwise the webcam window would not open. Many of the OpenCV tutorials seem to use older versions of OpenCV and do not include this method call. I’m not sure if this is an OS X issue or an OpenCV 3.2 issue.

Sources
How to install OpenCV 3 on Mac
Install OpenCV 3 on Yosemite
OpenCV Project in Xcode Tutorial Mac

--

--

Dennis Walsh

iOS Developer working with Swift and Objective-C Worked @X-Team, @Verys, LLC, @Netbrains, Inc., @Mirth Inc.