Installing and Using OpenCV 2.1.1 in Snow Leopard


Update 2011/07/26:

It is easier to install homebrew and then install opencv using it:
$ /usr/bin/ruby -e "$(curl -fsSL https://raw.github.com/gist/323731)"
$ brew install opencv
And you are done!
Now add the headers to your project as shown in step 3 and 4 :)


I use OpenCV very often for my projects. I like it because it has some interesting and very useful implementations. What I don't like is kind of messy and docs update are slow. Specially documentation for Macs is slow!

So I just followed some instructions from here, read some other info from here and realized opencv's new structure by looking at the installed files.

This post shows how to run the sample code I got from here

Step 1: Get and Configure OpenCV 2.1.1:


In the Terminal,
$ svn co https://code.ros.org/svn/opencv/trunk/opencv
$ cd opencv
$ mkdir build
$ cd build
$ cmake ..
Optionally you can configure some flags using cmake configure tool

Step 2: Build and Install OpenCV 2.1.1:

$ make -j8
$ sudo make install

And we are done!

Step 3: Create a Xcode Project:

(I am using Xcode 3.2.2)

In Xcode, create a Standard C++ project by doing: File>New Project...
Select "Application" from MacOS X left menu and "Command line tool" type: "C++ stdc".

Step 4: Use OpenCV 2.1.1


Add OpenCV 2.1.1 Headers Path

  • Select Project>Edit Project Settings...
  • Set Configuration to "All Configurations"
  • In the "Architectures" section, double-click "Valid Architectures" and remove all PPC architectures
  • In "Search Paths" section set "Header Search Paths" to /usr/local/include/
    (What we are doing is add the path where opencv's headers folder is)
  • Close the Project Info window


Add OpenCV 2.1.1 Libraries (Dylibs)

  • Select Project -> New Group and create a group called OpenCV Frameworks
  • With the new group selected, select Project -> Add to Project…
  • Press the "/" key to get the Go to the folder prompt
  • Enter /usr/local/lib
  • Select the libraries needed or all of them:
    libopencv_calib3d.2.1.1.dylib
    libopencv_contrib.2.1.1.dylib
    libopencv_core.2.1.1.dylib
    libopencv_features2d.2.1.1.dylib
    libopencv_highgui.2.1.1.dylib
    libopencv_imgproc.2.1.1.dylib
    libopencv_legacy.2.1.1.dylib
    libopencv_ml.2.1.1.dylib
    libopencv_objdetect.2.1.1.dylib
    libopencv_video.2.1.1.dylib
    


Uncheck "Copy resources" when  asked, importing a reference is enough.

Include OpenCV 2.1.1 headers in your code:


In prior versions of OpenCV we would have written:
#include "cv.h"; or depending on your header search path #include "opencv/cv.h"; but in OpenCV2.1.1 file structure is changed, so including headers like that will cause at least some warnings. (Because those headers are deprecated) So let's do it the new way:

#include "opencv2/imgproc/imgproc.hpp" //for image processing
#include "opencv2/highgui/highgui.hpp" //for GUI

You will need to include more headers if you are doing other stuff like machine learning, etc. If you are not sure what headers you should include just check directory:
/usr/local/include/opencv2/
You will realize that OpenCV 2.1.1 is now divided in modules!



Now build and Run and you should have your program running ;)
You can download my sample project from here.

I hope it helps ;)

4 comments :

FaB said...

Hi,
thx to your post and your demo project i got opencv running. (lots of examples are out there, but none of them worked for me -> Great Job!)

But now i want to include OpenCV within a Cocoa Application. I excatly followed your steps again, and it didint work out. I even get compiler errors.

Have you tried the same?

nacho4d said...

FaB I also had trouble... I just found a solution;).

First If you are using C++ APIs make sure your files are .mm and not .m when writing C++.

Second add -lstdc++ to Other Linker Flags in your project settings.

Third, this is the tricky part and I don't know why this works. I read that you need include opencv header BEFORE everything else so, look for your prefix.pch file and add #import "opencv/cv.h" before #import

Now you can work with OpenCV as usual and #import "opencv/opencv.hpp" in your .mm files if necessary.

I will update this post for Cocoa Apps later ;)

Unknown said...

Hey Nacho4d,

Thanks for putting this up, though I'm having an issue getting it to work. After the sudo make install command the installation stops at around 58% :(. I have no idea why either.

This is the error I am getting: [ 58%] Building CXX object modules/highgui/CMakeFiles/opencv_highgui.dir/src/cap.o
/Users/dhawannn/opencv/modules/highgui/src/cap.cpp: In function ‘CvCapture* cvCreateCameraCapture(int)’:
/Users/dhawannn/opencv/modules/highgui/src/cap.cpp:126: error: ‘CV_CAP_OPENNI’ was not declared in this scope
make[2]: *** [modules/highgui/CMakeFiles/opencv_highgui.dir/src/cap.o] Error 1
make[1]: *** [modules/highgui/CMakeFiles/opencv_highgui.dir/all] Error 2
make: *** [all] Error 2


Any ideas what all this means?

nacho4d said...

Hey naveen ;)

This post is kind of outdated, because there is opencv 2.2 out already! the easiest way to install it by using macports: $sudo port install opencv

This work is licensed under BSD Zero Clause License | nacho4d ®