OpenCV & example

[Home]

If you read this article you probably ran across problems when compiling OpenCV on Mac OSX. In this tutorial I show you how to install the latest OpenCV source files to use OpenCV on your Mac in the IDE XCode. I give my credits to René Meusel who recently helped me out in solving this issue.

1. Download Required Additional Software

a) Subversion Client

A subversion software to grab the latest source files from the developers. I use the open source software svnX but there are many other great tools out there.

b) Build System

The OpenCV files need to be built first before you can use them in your XCode project. I used CMake which is an open source build system.

2. Get the Latest OpenCV Code from the Developers

Open svnX and go to the window called “Repositories”. Hit the little “plus” in the right corner and create a new repository.

We now have to tell the svnX where the code files are on the Internet. There are two versions of the OpenCV Code. One that is the latest version but haven’t been tested yet and one that is fully tested but a little bit less recent.

Path Tested Version: https://code.ros.org/svn/opencv/tags/latest_tested_snapshot

Path Non-Tested Most Recent Version: https://code.ros.org/svn/opencv/trunk/opencv

For now please take the tested version just to make sure nothing goes wrong.

In the freshly created repository enter any name you like in the “name”-field and add the path from above in the “path” field. No user name and password are required. I have many more repositories in my svnX, one for each project I do.

If you are done with entering the name and path, double click the new entry. Another window will open and after a few seconds you will see the latest changes that are made to the source files by the OpenCV developers (top) and the structure of the opencv source folder on the server (bottom).

Click the checkout button in the above screenshot. Afterwards decide in which folder on your computer you want to download the OpenCV Source files and hit the “Open” button. SvnX will now start to download (“checkout”) the OpenCV files which can take some time depending on your internet connection and the overall size of files on the OpenCV server.

If everything went fine with downloading you will have the uncompiled OpenCV files in the chosen folder.

We now need to compile the files and install them using CMake.

3. Build Files Using CMake

After you have downloaded CMake open it.

In the field “Where is the source code” choose the folder in which you downloaded the code files in the previous step e.g. MyOpenCVSVN/opencv/

In the field “Where to build the binaries” enter the path to a folder that lies within the source code folder e.g. MyOpenCVSVN/opencv/cmakeresultforxcode

After you are done hit the “Configure” button.

CMake will now ask you if it should create the binaries folder (if it’s not yet existing on your computer). Click Yes.

In the next window choose “Unix Makefiles” from the pull-down menu and hit the “done” button.

After this you will be returned to the main window and a list of options turnes up. Leave everything as it is. If you like to have some openCV examples later on check the “Build Examples” option.

Press again the “Configure” Button (because we changed an option) and then hit the “Generate” Button.

This will generate the files.

If everything went fine you should see something like this in your svn folder in the finder. The “cmakeresultforxcode” folder now contains the files we build by using Cmake.

In the next step we have to install these files.

4. Installing OpenCV

Open the “Terminal”. If you have never used Terminal before, go to spotlight and type “Terminal”. It should look like this:

Type in “cd”, make an empty space and then drag the binaries folder (in my case the “cmakeresultforxcode”) from the finder into the terminal. This will automatically give you the right path. If it’s not working for you, you can also type it in manually. Press enter after you are done.

Before you do the next step, please make sure that there are NO empty spaces in the path you entered in the previous step. If any folder of your entire path contains empty spaces it is very likely that you will get errors during the next steps (it happened to me and was really annoying). So do not name your folder “my projects/open cv/latest build” but “MyProjects/OpenCV/LatestBuild” or sth like this.

If you have no empty spaces in your path name, just go on with the next step:

Type the following line in terminal:

make -j8

Press enter.

This will start the build which can take a couple of minutes to finish. The process will look like this:

If you are done with this step enter the following line to actually install the code:

sudo make install

You will be asked to enter your mac admin password.

Again it will take some time until the process is finished and it should look like this:

Now we have installed OpenCV and are ready to create our first XCode OpenCV application. Hold on .

5. Setting up an OpenCV XCode Project

Start Xcode. Go to “File->New Project”.

Select “Command Line Utility” on the left side and “Standard Tool” on the right side. Hit the “choose” button and save the project anywhere you like on your computer.

After you have created the new project, we have to adjust several settings.

Go to “Project->Edit Project Settings”.

Select the “build”-tab.

Go to the section “Valid Architectures”.

Double click-it and remove all the ppc entries.

After you are done, go to the section “Header Search Paths”.

Double click the section. In the new window add a new entry. Check “Recursive” and enter “/usr/local/include”. Hit okay when you are done.

If you are done close the window and return to your project overview.

Click “Project->New Group”.

This will create a new folder on the left side in your project structure. Rename the folder e.g. to “OpenCVFrameworks”. We will put the required OpenCV libraries there in a second.

With the new folder selected, go to “Project->Add to Project”.

In the next window press “/” to get an additional pop-up for entering the folder name.

Enter “/usr/local/lib” to be directed to the folder where the OpenCV libraries are located (we built them using CMake and the Terminal).

Select all the libs you need for your project. If you want to make sure just select all of them.

(I ran the cmake process several times when I tried to get OpenCV running so I have some more libs there than there should be, just ignore them).

In the next window make sure that “copy items” is not checked and click the “Add” button.

The libs should now be linked in your project structure:

Now we are ready to create our first OpenCV Application.

Go to the “Source” folder in your XCode project structure and open the main.c

Put the header files you want to use in your project as the first lines e.g.

I just create a small programm that grabs the images from my built-in camera and displays it. Here’s the code for copy-paste:

//but the include lines here (see the screenshot)… I don’t now why wordpress is not able to display these signs.

#include aheaderfilefromscreenshot

#include anotherheaderfilefromscreenshot

int main()

{

cvNamedWindow(“Source”, 1);

CvCapture *camera=cvCreateCameraCapture(CV_CAP_ANY);

IplImage *img=cvQueryFrame(camera);

while(1) {

img=cvQueryFrame(camera);

cvFlip(img, img, 1);

cvShowImage(“Source”, img);

cvWaitKey(100);

}

cvReleaseImage(&img);

cvDestroyWindow(“Source”);

return 0;

}

When you are done with the code press the "Build and Go"-button and hopefully everything works.

If you went through this process once successfully, you can just reuse your XCode project by making copies of it and changing the project name. Please google how to change a project name in XCode, there are several good tutorials.

Stefanie Müller | Bismarckstrasse 28 | 10625 Berlin | Germany | stefaniemueller@acm.org