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.
  • I don’t use “Panther” any more, but the instructions still apply.
  • Made a German translation of this article.
2005-09-30
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.
2005-02-14
Updated list of required Fink packages according to Tom’s comment (thanks Tom!)
2004-11-27
First published

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

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:

  1. Download the source for version 0.4.9-pre1 of FFmpeg (1.5 MB). I’m using that version since I don’t have time to track the CVS version, where the API might change so OpenCV stops working.
  2. 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.
  3. Configure ffmpeg with the following command line:
    ./configure --cc=gcc-3.3 --enable-shared --enable-pthreads --disable-vhook
  4. Compile and install it (in /usr/local):
    make
    sudo make install

This will get you a version of FFmpeg without any optional capabilities or codecs, but I found it fulfills my needs so far.

Please note that I don’t use camera capturing and have no idea how to get it to work on Mac OS X.

Installation

  1. Download the Linux source distribution of OpenCV 0.9.7 (10 MB), uncompress it somewhere and switch to the source directory on the command line.
  2. Configure the source like this:
    ./configure --with-apps CPPFLAGS="-I/sw/include -I/usr/include/malloc" LDFLAGS=-L/sw/lib CC=gcc-3.3 CXX=g++-3.3
  3. Compile the source and install it in /usr/local:
    make
    sudo make install
  4. If you want to run the included tests, compile them with:
    make check

Quick Start

In order to get up and running, try compiling and running one of the samples in Terminal:

  1. Copy the files morphology.c and baboon.jpg from the samples directory to your home directory:
    cd ~
    cp /usr/local/share/opencv/samples/c/{morphology.c,baboon.jpg} .
  2. Compile the source code:
    g++ -bind_at_load `pkg-config --cflags opencv` morphology.c -o morphology `pkg-config --libs opencv`
  3. Start X11, switch back to Terminal, then run the sample program and play with it:
    ./morphology
  4. To get started with OpenCV programming, read the documentation, starting with the file /usr/local/share/opencv/doc/index.htm.

Quirks

Comments

Tom Stepleton

I have a minimal Fink setup on my machine. I found that I also needed these libraries to add GTK support for my OpenCV build:

glib2-dev, pango1-xft2-dev, atk1

Also, I haven't been able to get it to recognize that ffmpeg is installed. The configure script flunks it on the test of whether avcodec_decode_video is in -lavcodec.

Jay

I had to add /sw/bin to my path for configure to work.

Steve Manley

I mucked around with this for a week or so before giving up, this gives me new hope I can do work directly from my powerbook on the roads.

Thanks VERY much.

Steve

keteracel

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.

Here it is:

CC = gcc #opencv includes INCLUDE = ..\include\opencv #ipl includes INCLUDE2 = c:\usr2\includes

testCanny.exe: $(CC) testCanny.c -otestCanny.exe -I$(INCLUDE) -I$(INCLUDE2) ..\lib\cv.lib

Thanks,

Connor Dickie

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!

Clément (frenc student)

Thank you for porting OpenCV to OS X and for the tutorial.

But I've got always error with the dynamic lybrary

gcc -I/sw/include/opencv/ -L/sw/lib -lcxcore -lcv -lcvaux -lhighgui kalman.c -o kalman [...] ld: warning can't open dynamic library: libavcodec.dylib (checking for undefined symbols may be affected) (No such file or directory, errno = 2) ld: warning suggest use of -bind_at_load, as lazy binding may result in errors or different symbols being used symbol _locale_charset used from dynamic library /sw/lib/libiconv.2.dylib(localcharset.o) not from earlier dynamic library /sw/lib/libintl.1.dylib(localcharset.lo) ld: Undefined symbols: _av_free _avcodec_alloc_context _avcodec_close [...] _img_convert

Thanx for your help.

Chris Davis

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

worked perfectly

Chris Davis

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

jennie

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.

but i have it! this is the config line i am useing

./configure --with-apps CPPFLAGS="-I/sw/include -I/usr/include/malloc -I/sw/bin" LDFLAGS="-L/sw/lib -bind_at_load" CC=gcc-3.3 CXX=g++-3.3

any clue to where i am going wrong? thanks

Jen

Jean-Baptiste LE STANG

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?

Franklin

SWIG configuration problems on tiger, opencv .9.7, ffmpeg .4.9

the configuration script is throwing the following error: Build wrappers for - Python yes SWIG is at No swig detected. Use existing files.

Build demo apps yes

i see on a website that the old versions of swig have preprocessor parsing problems.

can anyone help?

Christoph Seibert

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.

Joel

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.

2006-02-04 17:04

Joel

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.

2006-02-08 07:57

Christoph Seibert

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 …

2006-02-08 09:28

Joel

The original was 640x480. In my tests nothing above 320x240 got acceptable performance (acceptable meaning >= 30 fps).

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.

2006-02-27 06:58

Edward Scott

thanks for the instructions, they work great. However, I'm having trouble running the morphology test. The g++ compiles, but when i try to run it, i get

(Opening&Closing window:1566): Gtk-WARNING **: cannot open display:

any idea what this means?

2006-06-13 09:23

Christoph Seibert

Edward, you need to set the DISPLAY variable in Terminal with:

export DISPLAY=:0.0

I’ll have to add that to the instructions one of these days.

2006-06-13 14:56

Tim

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

It will work

James

2006-06-28 01:10

Kelly

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!

2006-09-03 03:10

maddama

To jennie

This should help:

$ export PKG_CONFIG="/sw/lib/pkgconfig"

2006-10-27 19:45

Keith

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!

2007-01-14 03:17

Phoebe

I found to get the PKG_CONFIG_PATH to take, had to do a

sudo apachectl graceful

2007-03-02 18:22

Anna

Anyone knows how to get it working with the camcorder & firewire?

Thanks.

2007-06-02 00:09

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

Doug

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

In file included from avcodec.h:14, from common.c:28: common.h:67: error: array type has incomplete element type common.h:71: error: array type has incomplete element type

Any suggestions would be great.

Doug

2007-11-27 06:46

Nik

@Doug: you should add in --disable-mmx at the end of configure line.

2008-04-21 00:04

xav

Hello, I have some troubles with functions of the ml lib. Build, it 's ok. But for execution, there is this kind of error: Symbol not found: __ZTV11CvStatModel. Do you have the same pb?

2008-07-11 10:00

jera

wow, thanks, christoph... nice work. i translated some points of your tutorial to portuguese and posted it in my blog. i hope you see no problems with this. =]

2008-09-04 19:58

Danny Kim

Hello, I'm trying to get the ffmpeg make working and it's keep on giving me the same error posted by Doug,

In file included from avcodec.h:14, from common.c:28: common.h:67: error: array type has incomplete element type common.h:71: error: array type has incomplete element type

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.

2009-09-15 11:19

Dechang Xu

I am not able to use openCV on my powerbook pro, 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"

According to "http://blog.csdn.net/bloghome/archive/2008/04/20/2309590.aspx"

I get successed as :

g++ -I/usr/local/include/opencv -L/usr/local/lib -lcxcore -lcv -lhighgui -lcvaux -lml morphology.c -o morpholog

Just for reference

2009-10-16 21:12

José Miranda

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

2010-01-25 10:40

Trackbacks are closed for this story.

Comments are closed for this story.