2006-02-02

OpenCV on Mac OS X

Some time ago, I managed to compile Intel’s Open Computer Vision library on Mac OS X. In this article, I hope to explain how I got it to work. I’ll list the prerequisites, then explain how to compile and install OpenCV as well as a sample program to get you started. I’ll describe quirks in using OpenCV on the Mac as I stumble upon them. The description of the installation process assumes familiarity with using the command line in Terminal.

I welcome all your feedback and corrections or requests for help. Just send an e-mail, especially if you want to hear back from me, or use the comments link at the bottom right of this article.

Updates

2006-02-02
  • Added a “Quick Start” section explaining how to get one of the OpenCV samples running.
  • I don’t use “Panther” any more, but the instructions still apply.
  • Made a German translation of this article.
2005-09-30
With OpenCV 0.9.7, the tests don't get compiled by default. I’ve updated the instructions to take that into account.
2005-07-28
  • OpenCV 0.9.7 (beta5) is out. This removes the need for a patched distribution because the Linux version works on Mac OS X. This article stays valid since you still need some special configuration flags on OS X, and the FFmpeg patch stays useful as well. I’ve removed the download link for my patched distribution, but the file is still there. However, I urge you to update to 0.9.7 because it contains a lot of bug fixes and improvements.
  • I’ve updated the quirks section with a hint about setting -bind_at_load in XCode and a recommendation to use GCC 3.3.
2005-07-13
The instructions now apply to Mac OS X “Tiger” as well (but have a look at the quirks section if you want to run the tests). I had to update the FFmpeg patch again, so be sure to download the new version (no need to recompile on Panther, though). You can now check whether you have the current version by verifying that the files match the supplied MD5 checksums (if you’re using Safari, it will probably uncompress them for you, so there are separate checksums for the uncompressed files). I’m also using a newer version of Fink now on Panther, but that doesn’t really change anything. I’ve added the need for X11 to the prerequisites section. I was going to recommend using the current CVS version of OpenCV which obviates the need for a patched distribution, but last I checked, there was a bug preventing it from compiling cleanly on OS X.
2005-04-28
I’ve updated the patch for FFmpeg so that the libraries are named and installed in the correct way. The dynamic linker will now no longer complain that it cannot find them. (If you used my previous patch, remove libavcodec* and libavformat* from /usr/local/lib before trying the new version.) I’ve also updated the patched OpenCV source so that the installation involves one step less and the binaries are bound at load time (see the updated quirks section). The test binaries will now build as well, although two tests in cxcoretest fail for reasons unknown to me.
2005-02-15
Turns out the installation of FFmpeg wasn’t quite as straightforward as I thought—I had to run through this process on a Mac that didn’t have any developer tools installed. I’ve added some detailed installation instructions for FFmpeg now in the prerequisites section. Also, I added an item to the quirks section.
2005-02-14
Updated list of required Fink packages according to Tom’s comment (thanks Tom!)
2004-11-27
First published

Prerequisites

My Mac currently runs Mac OS X “Tiger” 10.4.4 with XCode 2.2, but I’ve also tested these instructions with Mac OS X “Panther” with XCode 1.5. I have installed Apple’s X11 (can be found on the OS install CDs/DVD as X11User.pkg) and X11 SDK (comes with XCode as X11SDK.pkg).

OpenCV depends on several tools and libraries. The easiest way to install them is probably via Fink, which is what I’m using (binary distribution, version 0.7.2 on Panther, 0.8.0 on Tiger). You will need to install the following packages (along with dependencies which Fink handles automatically):

You will also need FFmpeg, which isn’t available in Fink (at least not in the stable version). You can either try to use the version in the unstable branch of Fink (which I didn’t), or follow these instructions:

  1. Download the source for version 0.4.9-pre1 of FFmpeg (1.5 MB). I’m using that version since I don’t have time to track the CVS version, where the API might change so OpenCV stops working.
  2. Uncompress the source, switch to the source directory on the command line and apply my small patch (1 KB, MD5 checksum: 41b2d644b9d82f38d3a75c88d9054e4b for the compressed file, 4be5f172eefd5aa1945a2c35d3cda31f for the uncompressed file) (you might have to uncompress it yourself if your browser doesn’t already do so):
    patch -p1 < /path/to/ffmpeg-0.4.9-pre1-macosx.patch
    I derived this patch from the Fink package for 0.4.8, but it’s not as complete.
  3. Configure ffmpeg with the following command line:
    ./configure --cc=gcc-3.3 --enable-shared --enable-pthreads --disable-vhook
  4. Compile and install it (in /usr/local):
    make
    sudo make install

This will get you a version of FFmpeg without any optional capabilities or codecs, but I found it fulfills my needs so far.

Please note that I don’t use camera capturing and have no idea how to get it to work on Mac OS X.

Installation

  1. Download the Linux source distribution of OpenCV 0.9.7 (10 MB), uncompress it somewhere and switch to the source directory on the command line.
  2. Configure the source like this:
    ./configure --with-apps CPPFLAGS="-I/sw/include -I/usr/include/malloc" LDFLAGS=-L/sw/lib CC=gcc-3.3 CXX=g++-3.3
  3. Compile the source and install it in /usr/local:
    make
    sudo make install
  4. If you want to run the included tests, compile them with:
    make check

Quick Start

In order to get up and running, try compiling and running one of the samples in Terminal:

  1. Copy the files morphology.c and baboon.jpg from the samples directory to your home directory:
    cd ~
    cp /usr/local/share/opencv/samples/c/{morphology.c,baboon.jpg} .
  2. Compile the source code:
    g++ -bind_at_load `pkg-config --cflags opencv` morphology.c -o morphology `pkg-config --libs opencv`
  3. Start X11, switch back to Terminal, then run the sample program and play with it:
    ./morphology
  4. To get started with OpenCV programming, read the documentation, starting with the file /usr/local/share/opencv/doc/index.htm.

Quirks