Installing OpenCV on Mac OS X

From NUI Group Community Wiki

Jump to: navigation, search
Image:Spam.png This page is a duplicate of "Installing OpenCV on MacOS X". It contains a subset of the same information. Please attempt to merge information. If unnecessary, redirect or delete.

Prebuilt Version

there is a prebuilt version of OpenCV framwork for OSX ( info here: http://opencv.willowgarage.com/wiki/Mac_OS_X_OpenCV_Port )

you can download it directly from: http://www.ient.rwth-aachen.de/~asbach/OpenCV-Private-Framework-1.1.dmg

then mount the dmg and in Terminal

cd /Volumes/OpenCV\ Private\ Framework/
cp -R OpenCV.framework/ /Library/Frameworks/OpenCV.framework

et voila'! you have the framework installed.


Compiling yourself

Note! I tried these instructions with no luck, I then found this post and used macports (same as Darwinports I think) to install OpenCV directly.

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

Mac OSX 10.5 (and maybe Tiger) already comes with Python. If you still need it, 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. MacPorts is also a viable option to get these dependencies. You can find the installer at MacPorts.org.

Fink

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:

pkgconfig libjpeg libtiff libpng3 pango1-xft2-dev atk1 gtk+2-dev glib2-dev

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

MacPorts

If you are using MacPorts to install the dependencies, use the following command in terminal to install them. The names of the ports are slightly different in MacPorts compared to their respective name in Fink so don't worry. Again replace the "libname" with the name of the dependency you wish to install. Press enter. Enter your password when prompted.

sudo /opt/local/bin/port install libname

Dependency names:

zlib

pkgconfig

jpeg

libpng

tiff

gtk2 (Note: This will install other dependencies as well since it depends on quite a few others itself. Hence, the long waiting period)

To verify that all the necessary dependencies have been installed, run the following command in a terminal session and check the given list:

/opt/local/bin/port installed


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:anonymous@opencvlibrary.cvs.sourceforge.net:/cvsroot/opencvlibrary login

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

cvs -z3 -d:pserver:anonymous@opencvlibrary.cvs.sourceforge.net:/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:

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 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 used MacPorts to install the dependencies, run the configure script with the following command. I used this on my Intel MBP and it seemed to work whereas I wasn't able to get the command after this one to work. If you are installing OpenCV on OSX 10.4, change both of the "5" 's to "4u". If you plan on using mainly C/C++, omit the "--with-python" segment. - catchpatrick

../configure --with-gtk --without-carbon CFLAGS="-isysroot /Developer/SDKs/MacOSX10.5.sdk" CXXFLAGS="-isysroot /Developer/SDKs/MacOSX10.5.sdk" --enable-dependency-tracking CPPFLAGS="-I/opt/local/include" LDFLAG="-L/opt/local/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:

make
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:

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. Now run the following command to check that everything was installed okay. Pay attention because it will require some user input.

make check

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:

For Bash:

export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig
export LD_LIBRARY_PATH=/usr

For C-Shell:

setenv PKG_CONFIG_PATH /usr/local/lib/pkgconfig
setenv LD_LIBRARY_PATH /usr

Note: Add the "Bash" command to the ".bashrc" file in your home directory if you use bash or the "C-Shell" command to the ".cshrc" file if you use C-shell. This will prevent you from having to type in this command just to compile. Now close your current session and open a new one for these settings to take effect.

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, go to the samples directory (eg. /Users/<username>/opencv/samples/c or /opt/local/share/opencv/samples/c/) and 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:

./morphology

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.

References

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!

- Kyle

Mac How