Installing OpenCV on MacOS X

From NUI Group Community Wiki

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

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

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:

[code]fink install libname[/code]

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.

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:

[code]cvs -d:pserver:anonymous@opencvlibrary.cvs.sourceforge.net:/cvsroot/opencvlibrary login[/code]

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

[code]cvs -z3 -d:pserver:anonymous@opencvlibrary.cvs.sourceforge.net:/cvsroot/opencvlibrary co -P opencv[/code]

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:

[code]cd opencv[/code]

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

[code]mkdir build; cd build[/code]

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

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

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.

[code]./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[/code]

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:

[code]make sudo make install[/code]


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:

[code]/usr/bin/install -c -m 644 'opencv.pc' '/usr/local/lib/pkgconfig/opencv.pc'[/code]

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

[code]sudo -s[/code]

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:

[code]export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig[/code]

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:

[code]g++ -bind_at_load `pkg-config --cflags opencv` morphology.c -o morphology `pkg-config --libs opencv`[/code]

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

[code]./morphology[/code]

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

Imported from old wiki