OpenCV on Mac OS X - Walkthrough
Posted: 07 May 2007 05:27 PM   [ Ignore ]
Joined  2007-04-28
Total Posts:  78
New Member

Hey guys, I know some of you were wanting to know how to install OpenCV in OS X. I promised a walkthrough a few days ago, but got busy so it has taken a little time to finish it. I plan on making an easier to read, PDF file of the walkthrough and post it for download on my web site. Sorry for the delay, and good luck.

Introduction and a Warning

OpenCV is very nice application that is required to run David Wallin’s Touchlib multitouch libraries in Mac OS X. I am writing this walkthrough for people wanting to install OpenCV on their Mac.

Before we get started, I would like to inform you that I am pretty much a newbie when it comes to programming, the command line, and the like. Therefore, if these instructions damage your computer in any way, I take absolutely no responsibility. Proceed at your own risk.

Anyway, now that I have gotten that out of the way, let’s begin.

Installing Xcode

The first thing I would do is install Xcode if you haven’t already. You can find Xcode on your OS X reinstallation disk. I’m not sure if it is necessary, but there are some OpenCV dependencies that are included on that disc - mainly gcc4.0 and X11. You can find these in the /Xcode Tools/Packages/ directory of the OS X disc. Installing them is fairly straightforward, just double click on gcc4.0.pkg and it will launch an installer. Do the same for X11SDK.pkg. I believe that if you are trying to compile OpenCV on an Intel Mac, you will also have to install MacOSX10.4u.pkg.

Installing Python

The easiest way to install Python would be to download the installer from here. Just run the installer and that should take care of everything.

Installing External Dependencies

There are several more external dependencies, mainly libraries, that you need to install. I used Fink to install them. It comes with an installer for Fink, so you won’t have to compile anything.

You could use the included FinkCommander app to install the necessary libraries, but I just used Terminal. To install a library in Fink through Terminal, open up a new shell and type in the following:

fink install libname

Replace “libname” with the name of whatever library you are installing. I am not 100 percent sure of all the libraries you need, but these are the ones I installed:


Again, some of these you may not need, but I would go ahead and install all of them just in case.

Downloading, Configuring, Compiling and Installing OpenCV

Now, we can finally get around to downloading the OpenCV source code. Copy the following command into a Terminal shell:

cvs login

It will ask for a password. Just hit enter. Now, copy this in:

cvs -z3 co -P opencv

This will download the source code into a directory in your home folder. It is about 100MB and may take a while depending on your connection speed. Once this is done, navigate to the new directory in Terminal:

cd opencv

Now, we finally get to configure the source code. It would be wise to make a build folder by typing in the following:

mkdir buildcd build

If you used Fink to install the external libraries, run the configure script with this command if you are on a PPC Mac:

../configure CPPFLAGS="-I/sw/include" LDFLAGS="-L/sw/lib"

If you are on an Intel Mac, type the command below. I have not tested this command myself so I have no idea if it works, though it was mentioned on the OpenCV wiki for OS X.

./configure --with-python CFLAGS="-isysroot /Developer/SDKs/MacOSX10.4u.sdk -arch ppc -arch i386"
CXXFLAGS="-isysroot /Developer/SDKs/MacOSX10.4u.sdk -arch ppc -arch i386" --disable-dependency-tracking

Now, after that is finished, it will tell you to run make. To save from typing in a command later on, copy this into Terminal:

sudo make install

After awhile, it will come back and you will probably notice that sudo make install has already been entered in for you. How convenient. Now, hit enter and type in your administrator password.

Now, it should come back with the last line reading something like this:

/usr/bin/install --m 644 'opencv.pc' '/usr/local/lib/pkgconfig/opencv.pc'

What you need to do for now is copy that command. Now, instead of pasting it into the prompt, type in:

sudo -s

It will ask you for your password. Just enter it in. Using the sudo command allows you to gain temporary access to all files on the system.

Now, paste in that snipet we copied a little earlier and hit enter. It may seem like nothing much has happened, but don’t worry.

Testing OpenCV

Okay, let’s test one of the sample apps. Copy baboon.jpg and morphology.c from the samples folder in Users/Username/opencv/samples/c/ into your home folder (Users/Username).

Open up Terminal, and type in:

export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig

When I had trouble getting the sample to compile, I had to run the above command before it would work. So running this first should help it compile. Now, run this command:

g++ -bind_at_load `pkg-config --cflags opencvmorphology.-o morphology `pkg-config --libs opencv`

This will compile the sample app. We can now run it by typing in:


If it launches a window with a picture of a baboon and a slider at the top, you’re in luck! It looks like you’ve successfuly installed OpenCV!

This guide was pretty much a hash of several tutorials on installing OpenCV on the Mac. You may not even have to install everything I’ve mentioned here. I have not completely tested these instructions myself, but I believe this is all I have done to get OpenCV running on my system.

If you need help, I probably won’t be able to help much, but go ahead and ask anyway. Check out the sites in the References section below if you are having trouble.


Mac OS X OpenCV Port - OpenCV Library Wiki - This is where I got most of the information in this walkthrough.

Christoph Seibert’s OpenCV on Mac OS X - This really helped me figure out what external libraries I was missing.

Several posts from the OpenCV Yahoo! Group helped me figure out my biggest problems in getting OpenCV working.

If there are any references I am missing, I apologize.

Thanks for reading this walkthrough and I hope it helped!

Posted: 19 May 2007 12:52 PM   [ Ignore ]   [ # 1 ]
Joined  2007-05-19
Total Posts:  6
New Member

I just compiled it on my intel mac (macbook pro). I followed the above (excellent) instructions mostly. One difference:

the configure command - be in your build dir (do this after mkdir build; cd build):

../configure --without-python CFLAGS="-isysroot /Developer/SDKs/MacOSX10.4u.sdk -arch i386 -I/sw/include" 
LDFLAGS="-L/sw/lib" CXXFLAGS="-isysroot /Developer/SDKs/MacOSX10.4u.sdk -arch i386 -I/sw/include" 

I did mine without python because I’m stuck on a specific install of python 2.4, can’t go to 2.5 yet, etc. Python bindings might be nice, but in the end I’ll be using c/c++ for speed anyway.

good luck

Posted: 19 May 2007 02:51 PM   [ Ignore ]   [ # 2 ]
Joined  2007-04-28
Total Posts:  78
New Member

Hey jibberia,

I’m glad that those instructions worked for you (for the most part, of course). I appreciate your updated configure command, I’m sure that will be a big help for many people.


Posted: 04 June 2007 12:18 PM   [ Ignore ]   [ # 3 ]
Joined  2007-06-01
Total Posts:  12
New Member

When you enter the “./configure” command and do not have the cross-compiler package installed as part of XCode, you will need to omit the both instances of the “-arch ppc” arguments, or you will receive an error ‘arch = prescott’ not valid.

Otherwise, excellent walkthrough!

Posted: 04 June 2007 12:45 PM   [ Ignore ]   [ # 4 ]
Joined  2007-04-28
Total Posts:  78
New Member

Thanks for the compliment, dandm. As I’ve said, I’m a newbie to all this stuff so I know firsthand that any tips on compiling OpenCV are helpful.

Now I just need to figure out how to get all of Touchlib working…