Compiling OpenCV On Mac OS X 10.6

posted 24 Jan 2013 by Andy Maloney

A couple of years ago I needed to do some basic image processing and found OpenCV. OpenCV is a BSD-licensed library for digital image processing which implements several hundred computer vision algorithms. Unfortunately compiling it on the Mac was not straightforward—requiring Fink or MacPorts—and the one existing Mac framework was out of date and no longer maintained.

OpenCV Library

Fast forward to last week. I had another requirement for some image processing magic and I thought I’d check out OpenCV again. What a difference! It’s now really easy to compile and use on the Mac. I didn’t find a writeup online on how to compile on the Mac—the one on the OpenCV site is out of date—so I thought I’d write up a short summary for future reference and any Googlers out there in internet land.

Here’s how I did it…

I like to keep all my projects sandboxed, so I created a directory ~/dev/OpenCV/ for this project. You can use whatever you want. I’ll be referring to this as the project directory.

1) Download the source package (OpenCV-2.4.3.tar.bz2 at time of writing) from the OpenCV download page. “Un-archive” it in your project directory.

2) OpenCV requires CMake to build it, so if you don’t have that installed, make sure you install it. You’ll also need the command line tools, so make sure you install those when given the option.

3) In a Terminal window, change to the source directory and run cmake, specifying your project directory as the path prefix for installation:

1

2

cd/project/directory/OpenCV-2.4.3

cmake-DCMAKE_INSTALL_PREFIX:PATH=/project/directory.

If you want to use the Qt framework for the High-Level GUI library, you can specify the Qt installation by pointing at the qmake executable like this:

This will build and install OpenCV into the bin, include, lib, and share directories within the project directory.

That’s it for compiling and installing OpenCV! If you look in your project’s include directory, you’ll see the OpenCV headers and the project’s lib directory contains all the shared libs that make up OpenCV.

So now that we have the OpenCV libraries built, let’s look at building and running a simple example. (These are the files for the example:
OpenCV_MacOSX_Example.zip. They are also available at the end of this post.)

Here’s a simple program to load an image, process it with the Sobel image filter, and display the original and resulting images:

Oops. It can’t find our dynamic libraries because we haven’t told it where they are. Let’s fix that by specifying DYLD_LIBRARY_PATH (Note you’ll have to use your project directory and path to Qt here.):