in Code

Compiling OpenCV On Mac OS X 10.6

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

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:

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:

There are many other options you can pass to cmake. To see what options are available, add -L to the command line:

4) Run make to build and install it

Or if you want to run multiple jobs in parallel to compile it faster:

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:

This program takes one argument on the command line which should be the path to an image. (Note that this program is just an example and doesn’t do any error checking. Please do error checking.)

To make life easier, here’s a simple Makefile to build it:

Note that you’ll have to change SANDBOX_DIR to point at your project directory so it can find the headers and libraries when compiling and linking.

I put both of these files in a new directory, <project directory>/OpenCV_Example, opened a Terminal window, and ran make:

This creates an executable named testMe. So now we just need an image to test with. I grabbed this one of Ed White First American Spacewalker from the NASA image archives, resized it a bit, and put it in the directory alongside the testMe program.

Ed White - First American Spacewalker

Ed White – First American Spacewalker

Now we can run it to see what we get:

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.):

Now try running the test program again:

It should open two windows:

OpenCV Example - Original Image

OpenCV Example – Original Image

OpenCV Example - Sobel Applied

OpenCV Example – Sobel Applied

Success! Now you have a working OpenCV installation on Mac OS.

Go forth and create!

Project Files: OpenCV_MacOSX_Example.zip

Leave a Reply for Marek Mechura Cancel Reply

Write a Comment

Comment

  1. Thanks for this tutorial! $DYLD_LIBRARY_PATH fixed what Xcode can’t! Been struggling since the last night!

  2. Awesome post! Really appreciate the setup and was able to get this working with OpenCV 2.4.3 on Mac OSX 10.8.5 Nice to see a post that doesn’t force you to use XCode.