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.
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.
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 -d:pserver:email@example.com:/cvsroot/opencvlibrary login
It will ask for a password. Just hit enter. Now, copy this in:
cvs -z3 -d:pserver:firstname.lastname@example.org:/cvsroot/opencvlibrary 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:
Now, we finally get to configure the source code. It would be wise to make a build folder by typing in the following:
mkdir build; cd 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 -c -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:
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.
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:
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 opencv` morphology.c -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!