Installing OpenCV with Xcode 4.3

Getting a bleeding edge OpenCV version working on a Macbook pro, with Xcode 4.3 already installed. Oh, with the command line tools installed, of course. That’s not standard anymore (nobody knows why), see e.g. here for more info.

So, at last I cannot postpone the day any longer. I’ve been dreading it for some time, but OpenCV will now really have to be installed on my new Macbook, so I can work on my totally cool new project. Of course, stubborn fool that I am, I’m not going to use MacPorts, no, I want the bleeding edge. So, first things first, I used the excellent svn tool Versions to basically do the equivalent of a:

svn -co

Reading the rather confusing agglomeration of what passes for a wiki at the official opencv pages, which has a dedicated but outdated section for installation on Mac. It’s clear that to install OpenCV from source, you’ll need cmake. Again, not wanting to use port as it tends to sow confusion to my libraries, I consulted the cmake installation page. Basically, there’s a binary dmg which you can download here.

I’m planning on using QT as the highgui backend, and for GUI building in general. So the next thing to do is install that, which really was a breeze.

Apparently OpenCV now works with Intel’s threading building blocks (TBB), so I grabbed the latest stable version. Also grabbed the source, but didn’t run the gmake.

Next in line: libjpeg and libpng. PNG is my favourite image format, but sometimes you just need to save jpgs. After reading about the libpng vulnerability and the changed private class structure at the libpng homepage, I decided to download version 1.5.9 from source forge. Annoyingly, libpng is compressed using xz, for which support is minimal. However, a fully functional pckg can be downloaded here. To unpack tarxball, do:

xz -d libpng-1.5.9.tar.xz
tar -xvf libpng-1.5.9.tar

Followed by a good old:

./configure --enable-shared --enable-static
sudo make
sudo make install

Libjpg always looks a little dodgy, but I got the latest version (jpegsrc.v8d.tar.gz) from the apparently official website. The same set of build commands used for libpng do the trick for libjpg.

That first sudo may not be necessary btw.

Oh, I had forgotten to install pkg-config. Again side-stepping macports, this time I needed yet another build/development tool, git. Well, I have heard good things about git, so why not give it a chance? Version was timestamped 19 March 2012, nice and fresh, and it comes as a convenient dmg. For those like me who have never used git, this is how you then do a ‘checkout’ of the pkg-config source:

git clone git://

Unfortunately, that didn’t seem to result in exactly what I needed. I found some alternative installation guidelines, and thus executed:

curl -o pkgconfig.tgz
tar -xvzf pkgconfig.tgz

sudo make
sudo make install

, which did the trick. Now, on to what’s hopefully the final preparation before we can actually give building OpenCV our first shot: ffmpeg. ffmpeg has also moved to git (it seems to be the new standard), so this is how to get the latest version:

git clone git:// ffmpeg. Being optimistic, I tried ./configure --enable-shared. This only led to the following error:

yasm not found, use --disable-yasm for a crippled build

Argh. I don’t want a crippled build! So, on to fetch me a yasm. This is another git-supported project, so this should work:

git clone git:// yasm

Drat, again no ./configure, and the INSTALL file says it should work. Hmmm. I’ve had this before, let’s see if I can figure out what the problem is. A that’s it, you should just run ./! But good grief, what’s this? I need to install FOUR more packages? Time for a cup of tea :( Here’s the autogen output:

You must have aclocal installed to compile yasm.
Download the appropriate package for your system,
or get the source from one of the GNU ftp sites
listed in

You must have autoconf installed to compile yasm.
Download the appropriate package for your system,
or get the source from one of the GNU ftp sites
listed in

You must have autoheader installed to compile yasm.
Download the appropriate package for your system,
or get the source from one of the GNU ftp sites
listed in

You must have automake installed to compile yasm.
Download the appropriate package for your system,
or get the source from one of the GNU ftp sites
listed in

I followed the commands from this blog, except that for each package I checked whether there were newer versions. There were. I used them. After that, build files for yasm were correctly there, and I could do a

./configure --prefix=/usr/local
sudo make install

without any problems. So, back to ffmpeg (I feel like i’m stuck in Inception, many layers deep):

./configure --enable-shared --prefix=/usr/local
sudo make install

And done. Now following the instructions of point 2 of the opencv installation for mac page:

# make a separate directory for buildingmkdir buildcd buildcmake -G "Unix Makefiles" ..Now, I really would like to add the Eigen library. So I got that from the Eigen library webpage.

Here, then, is my cmake command:


But ai ai ai, gcc cannot even compile a simple test program! It’s too late now to figure this out, so on to the -G “Unix Makefiles” generator:

cmake -G “Unix “Makefile -D CMAKE_INSTALL_PREFIX=/usr/local -D USE_TBB=ON -D USE_EIGEN=ON -D USE_QT=ON ..

In fact, this didn’t build entirely. I had an error at 91% built:

Generating pyopencv_generated_funcs.h, pyopencv_generated_func_tab.h, pyopencv_generated_types.h, pyopencv_generated_type_reg.h, pyopencv_generated_const_reg.h
/bin/sh: -c: line 0: syntax error near unexpected token `('

As I don’t intend to use python interfacing anyway I turned it off, using ccmake. Unfortunately I couldn’t find the cmake cmd line option anywhere.

So, there we are. In total I had to install 15 programs. Each was build from source, and to be honest, no major problems were encountered.

Testing to see if it works using the following code on my Macbook:

#include “iostream”
#include “
#include “

using namespace cv;

int main(int argc, const char * argv[])

// insert code here...
std::cout << "Hello, World!\n";

VideoCapture cap(0); if(!cap.isOpened()) return -1;
Mat frame, edges; namedWindow("edges",1); for(;;)
cap >> frame;
cvtColor(frame, edges, CV_BGR2GRAY); GaussianBlur(edges, edges, Size(7,7), 1.5, 1.5); Canny(edges, edges, 0, 30, 3);
imshow("edges", edges);
if(waitKey(30) >= 0) break;
return 0;

This worked perfectly. I had to add the /usr/local/include path to my header search path, and /usr/local/lib to the link library search path, and add four dylibs to the project: libopencv_imgproc.dylib, libopencv_ml.dylib, libopencv_core.dylib and libopencv_highgui.dylib.

Compiling Matlab Mex Files on a Mac

So, I’ve finally received my super-hot new 15” MacBook Pro, and I’m super happy with it! Although slightly reluctant about upgrading to Lion and Xcode 4, I thought this would probably be the best moment to do it. I heard some bad stories about backwards compatibility issues of Lion, but heck, they’ve had plenty of time to sort that out, right? Wrong.

One of the things I need for my day-to-day research is the Matlab version of LibSVM. I use the version provided by UC Berkeley, which is based on LibSVM 3.0-1 but includes the Histogram Intersection Kernel, useful for my work with LBPs and other histogram-based appearance descriptors. Note that LibSVM itself is now at version 3.11, but I haven’t been able to find out if there are any significant changes between 3.0-1 and 3.11 that would warrant me changing over.

Anyway, after downloading and unpacking the LibSVM files, I tried to set up my fresh Matlab installation to use the correct compiler. But what’s that? After running

mex -setup

I got the following error:

/Applications/ line 305: gcc-4.2: command not found

What the??? No gcc? How’s that possible? I had just installed the latest shiny Xcode, everything should be a-ok! But noooo, apparently Apple believes that real developers don’t need command line tools anymore. They have removed the command line utilities from their standard Xcode install as of version 4.2. As I have version 4.3 installed, that means me. And installing it is hidden quite well. Here’s how to get them (back): in Xcode: Preferences --> Downloads With "Components" selected, you will see a list of downloadable components. Look for Command Line Tools, and click on install.

Great. That’s it, gcc is back. Good old gcc. So, we’re fine, right? Surely Matlab will work fine now, right? Wrong again. First of all, for some obscure reason, Apple has done away with the /Developer directory. Instead, it’s found in /Applications/
MacOSX.platform/Developer/. I suspect it always was in a more complicated sounding directory than /Developer, and /Developer just used to be a symlink to that. For some reason, it is no more. But hey, I’m root on this machine, so let’s just bring it back:

sudo ln -s /Applications/
MacOSX.platform/Developer/ /Developer

And that’s that. Or so I hoped. Matlab expects gcc-4.2. Xcode used to ship with that, but no more. Instead, it ships with a similar compiler (gcc-4.2 front-end to LLVM), that Matlab can use IFF you download and apply a patch from the Mathworks. They actually do a very good deal describing the Mex woes Mac users will encounter when ‘upgrading’ to Xcode 4.3.

Now, mex -setup finally works, and we can move onto building LibSVM. Unfortunately, this doesn’t work nicely either. First of all, the make.m file provided with LibSVM expects .obj files, instead of .o files, and secondly, it doesn’t support 64 bit arrays. And we want those, my machine learning training arrays are huge! In order to get LibSVM compiled on your 64-bit Matlab version on a Mac, I thus changed the commands in make.m to:

mex -largeArrayDims -O -c svm.cppmex -largeArrayDims -O -c svm_model_matlab.cmex -largeArrayDims -O svmtrain.c svm.obj svm_model_matlab.omex -largeArrayDims -O svmpredict.c svm.obj svm_model_matlab.omex -largeArrayDims -O libsvmread.cmex -largeArrayDims -O libsvmwrite.c

And voila, LibSVM finally build.

OpenCV on Mac

I’m in an ongoing battle to compile the trunk of OpenCV on my Mac (Snow Leopard). Due to compatibility issues, I want to build it for 32 bit. Which has caused some problems. First of all, when compiling it complains of the following:

warning: in /usr/local/lib/libdc1394.dylib, file was built for x86_64which is not the architecture being linked (i386)

Fair enough, if you want to keep things 32-bit, you need to have all components 32 bit. So, although the website of libdc1394 does not provide that much detail, I managed to find out how to compile libdc1394-2.x for 32 bit:

./configure CFLAGS=-m32 CPPFLAGS=-m32

Compiling OpenCV came a lot further this time, but still not all the way. Apparently there was a problem with my Python installation. Now, I don’t intend to use OpenCV with Python, so the easiest option was to use ccmake to configure the OpenCV installation without Python. This time around, building worked like a charm.