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.

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.

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

pkgconfig

libjpeg

libpng3

libtiff

gtk+2-dev

glib2-dev

pango1-xft2-dev

atk1

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:

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.

Start X11, switch back to Terminal, then run the sample program and play with it:

./morphology

To get started with OpenCV programming, read the documentation, starting with the file /usr/local/share/opencv/doc/index.htm.

Quirks

When building your own projects, use the -bind_at_load flag of g++ when linking executables. You will get linker warnings otherwise (see Clément’s comment), and some parts may fail to work (the persistence functions cvLoad and cvSave in particular). In XCode, add this flag to the “Other Linker Flags” build setting.

On Tiger, you should probably use GCC 3.3 for your projects. OpenCV compiles with GCC 4.0 as far as I know, but FFmpeg 0.4.9-pre1 doesn’t, and I don’t know whether it’s a good idea to mix libraries compiled with different versions of GCC.

If you want to run cvtest on Tiger, go to the directory tests/cv/src/.libs and run it from there. tests/cv/src/cvtest is a wrapper generated by libtool, which mistakenly sets the DYLD_LIBRARY_PATH variable resulting in a library conflict that prevents the test from running.

Thanks for the in-depth tutorial of how to install opencv on MacOSX. I think I have managed to install it I just need to test it. I have downloaded a simple sample program that uses opencv but the makefile was for Windows. I was wondering if you could give me a little help in modifying it for the mac.

Wow! Thank you for porting OpenCV to OS X - I don't have time to attempt an install now (I have final exams until April 22nd) But when those are done I will definatly try your package. If I make any progress I will be sure to let you know. Thanx!

I'm having trouble with the port of OpenCV for OS X, using 10.3.9 and XCode 1.5. I got errors while attempting to compile ffmpeg (which I ignored with a -i in make to get it to install). But I also got errors with OpenCV in the compile phase, specifically I got a "internal link edit command failed" with libcv. Any idea what is wrong?

Thanks,
Chris Davis

sancho

BTW, I did get my problem fixed with Christoph's help. It seems that I had old versions of his version of OpenCV and his FFMpeg patch. After I downloaded new versions of these from this web site everything cleared up.
Thanks,
Chris Davis

Jacob

Thanks Christoph and others who contributed to this page. Getting OpenCV to work on the Mac was the part of my internship that I was dreading, but you made it almost painless.

Antonio (Italian student)

Thanks for this great tutorial! I managed to install OpenCV without a single problem following your instructions. I know this is probably a dumb question, but I can't build my XCode 2.0 projects that make use of OpenCV functions; I keep getting linker errors. I know that you said to: "use the -bind_at_load flag of g++ when linking executables", but how do I do that in XCode? Thanks, Antonio

Howdy.
I am trying to install opencv i am on Tiger. I follow all the directions and I have pkgconfig and i get this error during my config of opencv
checking for pow in -lm... yes
checking for pkg-config... no
configure: error: You have to install pkg-config to compile OpenCV.

Have you considered making a native port of OpenCV ? I've been myself playing with OpenCV and I've build cv, cvaux and cxcore as standalone frameworks without any problem, I'm now working on making highgui working, I need to make a new FireWire implementation and do all the window stuff but seems like it should be rather easy no?

Unfortunately, I don’t know an easy way to build the Python wrappers on Mac OS X at all. There’s not even a hard way that I could really recommend. The Python setup on Mac OS X is a little weird, and I’m not an expert in this matter.

Your instructions were invaluable for getting opencv set up and running on my mac, but face detection in particular is ridiculously slow--under 10 fps on my dual 2ghz G5. I realize it must be somewhat slower than on intel because opencv is intel-optimized code, but are other people getting these speeds? What can I do to improve this? I've tried messing around with the compiler flags a little bit-- -fastmath, -funroll-loops, -faltivec, and so on, with little improvement. Any advice would be greatly appreciated.

As is often the way of things, immediately after posting the above I realized that things were slow because I was using my full-resolution image, rather than scaling it down. Performance is quite adequate at 320x240.

Joel, thanks for the info. Out of curiosity: What was your original resolution?

I’ve only tried this on my 500 MHz G3, so I’m not in a good position to talk about performance. :-)

If you haven’t done so already, you could also try appending CXXFLAGS="-fast" to the 'configure' line for OpenCV. The dual processors won’t help, unfortunately. Some of the code is parallelized, but via OpenMP, which GCC doesn’t support.

Maybe there's a bright future where IPP and the Intel compiler are available for Intel Macs …

In fact the Intel compiler is <a href='http://weblog.infoworld.com/enterprisemac/archives/2006/02/the_intel_compi.html'>available already</a>, so I'm really excited about getting access to an Intel mac to see how performance changes.

2006-02-26 16:44

Guy Isely

Thanks a lot. You instructions worked perfectly for me. I'm using it for my computer vision class.

Hello,
first of all, thanks for this tutorial... I would have never managed to do this on my own.. Nevertheless, I am not able to use openCV on my powerbook, even on the "morphology-test" with the baloon image I get the following error-message:

"Package opencv was not found in the pkg-config search path.
Perhaps you should add the directory containing `opencv.pc'
to the PKG_CONFIG_PATH environment variable
No package 'opencv' found"

Where and what do I have to change? Thanks a lot in advance...

2006-06-22 10:21

James

Hi Tim, for your problem, you should add in your /etc/bashrc file, this line
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig

Not much out there for opencv on os x so I'm posting a question here. I managed to get it installed and compiled an extremely simple test--using cvLoadImage to open a jpg and then immediately using cvSaveImage. It runs fine, however, the output image is garbage. Thanks!

Package opencv was not found in the pkg-config search path.
Perhaps you should add the directory containing `opencv.pc'
to the PKG_CONFIG_PATH environment variable
No package 'opencv' found
I also have such a problem and James's method (on the above) doesn't work out for me. Thanks!

I'm having trouble compiling with my intel MacOS X 10.4.11 when I use gcc-3.3 and try to run the make file I'm getting
"installation problem cannot exec `cc1': No such file or directory".
After reading around I understand that gcc-3.3 doesn't work properly with intel MacOS X so I tried using gcc-4.0 and I got the following errors

I tried adding on --disable-mmx at the end of configure but still gives me the same error message.

I'm using an Intel-based Mac... any suggestions?

2008-10-08 03:07

Frank Foerster

Just installed newest version of ffmpeg via svn as described here: http://stephenjungels.com/jungels.net/articles/ffmpeg-howto.html
Worked out fine. Before I had the same problems as described by Danny. Tried to fix it with pathes for gcc4.x, but that neither worked out.
(I'm using a Mac Pro with Mac OS X 10.5.6)

2009-02-09 16:23

Janaka Prabhash

Hi,

This is regarding the issue raised by cyzh.

hello there, I have a toruble with compilation, I received this : configure:2310: error: C++ compiler cannot create executables Do you have any idea how to make this work correctly? Thanks

2007-10-20 14:39

I had the same kind of error in linux. I could solve it by installing g++.

I think there is some issue in compiling the c++ files. So, try installing the c++ compiler for your platform with reference to the config files and make files.

Bye

Prabhash

2009-02-25 05:58

Ankur

The current distribution of OpenCV (0.9.7 beta 5) does *NOT* work with GCC 4.0 or 4.1. Before compiling, you will have to run the configure script as follows:
CXXFLAGS=-fno-strict-aliasing ./configure .
GCC 4.0 will *NOT* (but 4.1 will) give *ANY* warning whatsoever that something is wrong (it is a bug of GCC 4.0), but it will *NOT* work, so you have been warned. If this option is not used with GCC 4.0 or 4.1, the resulting library has lots of errors as evidenced by failed tests in the test programs and the test programs segfaulting. Alternatively, you can also use the CVS version. The aliasing bugs are fixed in the CVS version.

Hi all:
I really need help with opencv with a PowerPC G4, OS X 10.5.8. I tried the build from willowgarage & everything run smooth. But, when I try comp. (g++ foo.cpp -I /usr/local/include/opencv - L /usr/local/lib) I get an annoying msg "ld: in /usr/local/lib, can't map file, errno=22;collect2: ld returned 1 exit status". Then, I wandered through Google. No hints. Then, I tried to install using MacPorts. Lots of time later, I got it done. Again, if I try any comp. (now with g++ -foo.cpp -I /opt/local/include/opencv -L /opt/local/bin) I got the same annoying damn error!
Any good soul can help me up, pleeeeease? Cheers.

2010-01-01 20:50

NK

very useful tutorial for installation of openCV on MAC.great job..thanks