Friday, January 31, 2014

Tutorial: Installing OpenCV on a Mac (OS X 10.9.1 Mavericks)

Since I found it quite hard to get OpenCV up and running and most of the tutorials I found were outdated, I will update them to work on the latest releases of OpenCV and Mac OS X. In every post I will also tell you which versions I used because I always thought this was a missing information. Here I used:

OS: Mac OS X 10.9.1
Xcode: 5.0.2
OpenCV: 2.4.8.0
MacPorts: 2.2.1
cmake: 2.8.12_3

OpenCV is available from various sources. You can get it via package managers like Homebrew or MacPorts or you can build it yourself from source. It's much easier if you get it from any of the mentioned package managers but in the beginning I got some errors with that when I tried to compile tutorials. Manly because it is installed in a different location as if you build it yourself. As the OpenCV tutorials always refer to the standard path, I would recommend for beginners to build it from source so you don't have to change the code of the examples and tutorials. So, let's go.

Preparation

Install Xcode

First of all you need to have Xcode installed. Just get it from the Mac App Store. The actual Version is 5.0.2. In other tutorials you will always find that you need to install the "Command Line Tools" for Xcode. Well, maybe you needed it. But Xcode 5 comes with these tools already installed. So nothing to do here.
Before you continue, launch Xcode. When you start it the first time you have to agree to the license agreement. If you don't do this, the following will not work.

Download OpenCV

Download the OpenCV Libraries. For Mac you want to use the Unix version. You will get a .zip file - unzip it.

Get MacPorts

Just download the package from their site MacPorts-2.2.1-10.9-Mavericks
... and install it. For help, refer to the MacPorts Guide.

You can check if it was installed correctly by typing
 port version  
in the terminal. The terminal app is in the Utilities folder in your applications.

Get cmake

For this you want to use the previously installed MacPorts. So launch your terminal app again. Then type this in:
 sudo port install cmake   
This may take a while until everything is downloaded and installed. You can check your installation by typing this in your terminal:
 port installed   
This will show all your installed ports. You should see cmake there.

Build OpenCV

We finally have made all the necessary preparations and are ready to build OpenCV.

1.
Copy the unpacked folder of OpenCV to your desktop und rename it to "opencv".

2.
Open your terminal and type the following in:
 cd Desktop/opencv  
 mkdir build  
 cd build  
With these commands you go to your OpenCV folder on the desktop, create a new folder inside named "build" and enter this folder.

3.
Now we can build and install OpenCV:
 cmake -G "Unix Makefiles" ..  
 make -j8  
 sudo make install  

That's it. You just built OpenCV from source and installed it to /usr/local.

In another post I will guide you through the process of creating your first OpenCV project in Xcode.

Source: Tilo Mitra