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
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.


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.

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

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.

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


  1. Thanks for this tutorial, it helped me out. One thing to mention, you need to make sure your install is in a folder that does not have a space in the name. So no "\my folder\openCV". This last part had me for a while.

  2. During the last command i got this error message : fatal error: 'opencl_kernels.hpp' file not found
    #include "opencl_kernels.hpp"

  3. Thank for this tutorial. It's very helpful for me.

  4. Just compiled on OSX 10.9.2 using homebrew and it worked just fine. Nice tutorial.

  5. Some explanation of the commands in step 3 would be helpful.

  6. bernds-air:build bernd$ cmake -G "Unix Makefiles"
    CMake Error: The source directory "/Users/bernd/opencv/build" does not appear to contain CMakeLists.txt.
    Specify --help for usage, or press the help button on the CMake GUI.

    1. Same error with me

    2. You have to write the 2 dots after : cmake -G "Unix Makefiles" ..

  7. Any idea what causes this:

    In file included from /Users/benjamin/Desktop/opencv/modules/highgui/src/cap_ffmpeg.cpp:45:
    /Users/benjamin/Desktop/opencv/modules/highgui/src/cap_ffmpeg_impl.hpp:1446:5: error: use of undeclared identifier 'avformat_free_context'
    1 error generated.
    make[2]: *** [modules/highgui/CMakeFiles/opencv_highgui.dir/src/cap_ffmpeg.cpp.o] Error 1
    make[2]: *** Waiting for unfinished jobs....
    make[1]: *** [modules/highgui/CMakeFiles/opencv_highgui.dir/all] Error 2

  8. While entering "make - j8" command on terminal it returns "No targets specified and no makefile found. Stop" . Any suggestions to overcome come this problem? Thanks in advance