Friday, March 2, 2012

How to install OpenCV on Mac OS X Lion to work with XCode

I have listed some easy and straight-forward steps below to get OpenCV library working with XCode on Mac OS X Lion without installing unnecessary bulky software on your Mac.

1. Installing CMake

i. CMake is essential to build OpenCV on UNIX. Get CMake Mac OS X binary (dmg) from http://www.cmake.org/cmake/resources/software.html. This comes with a GUI installer, making the installation process easy.

ii. At the end of the CMake installation it will ask whether or not to put CMake into /usr/bin and into PATH environment variable, select Yes and finish the installation. 

iii. Open the Terminal (Launch Pad -> Utilities -> Terminal) and make sure that CMake is successfully installed by entering the following command. It should output the CMake version you have installed.
cmake -version


2. Installing OpenCV

i. Download the latest version of OpenCV for UNIX. At the time of writing this is version 2.3.1 and can be downloaded from http://sourceforge.net/projects/opencvlibrary/files/opencv-unix/2.3.1/ 

ii. Open the Terminal and change directory to the place where you downloaded the OpenCV distribution tarball. e.g. cd ~/Downloads

iii. Execute the following commands one by one. Each command should complete without errors.

tar xvf OpenCV-2.3.1a.tar.bz2 
cd OpenCV-2.3.1 
mkdir build 
cd build 
cmake -G "Unix Makefiles" .. 
make 
sudo make install 

iv. Above commands will build OpenCV into your /usr/local/ directory. 


3. Testing a Sample Program with XCode

i.) Create a new Command Line Tool with a name of your choice. Set the type as C++ (See the screenshot below).


 ii.) After the new project is opened in XCode, click on the project name. Click Build Settings > All.

iii.) Search for 'Header Search Paths', set the value of Header Search Paths to /usr/local/include

 

iv.) Right click on the project name and select Add File To....

 

v.) A new file selector window will open. Press '/' key to open Go to folder box. Type /usr/local/lib there and hit Return.

vi.) Select the following files from /usr/local/lib and add them to your project. 

libopencv_core.2.3.1.dylib
libopencv_highgui.2.3.1.dylib

v.) Open main.cpp and put the following code there. This code simply reads an image and displays it in a new window. Make sure you put the actual path of your image in the code.

#include <iostream>
#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;

int main (int argc, const char * argv[])
{
    Mat img = imread("/Users/sadeep/Desktop/image.jpg"); //Change the image path here.
    if (img.data == 0) {
        cerr << "Image not found!" << endl;
        return -1;
    }
    namedWindow("image", CV_WINDOW_AUTOSIZE);
    imshow("image", img);
    waitKey();
}

vi.) Click Product -> Build. This should show the "Build Successful" message.

vii.) Click Product -> Run. Your image will open in a new window which will close upon a key press.

Happy coding with OpenCV!