libjpeg

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 http://code.opencv.org/svn/opencv/trunk

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 1.7.9.4 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://anongit.freedesktop.org/pkg-config

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

curl http://pkgconfig.freedesktop.org/releases/pkg-config-0.23.tar.gz -o pkgconfig.tgz
tar -xvzf pkgconfig.tgz

./configure
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://source.ffmpeg.org/ffmpeg.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://github.com/yasm/yasm.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 ./autogen.sh! 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 http://www.gnu.org/order/ftp.html

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 http://www.gnu.org/order/ftp.html

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 http://www.gnu.org/order/ftp.html

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 http://www.gnu.org/order/ftp.html

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

cmake -G "Xcode" -D CMAKE_INSTALL_PREFIX=/usr/local -D USE_TBB=ON -D USE_EIGEN=ON -D USE_QT=ON ..

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 “
cv.h”
#include “
highgui.h”

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.