OpenCV Installation Guide
Overview:
If you want the latest 2.0 release, get it here for Linux or Windows
If you want the latest code, get it from our from out SVN server by:
svn co https://code.ros.org/svn/opencv/trunk/opencv
Mac users can read some details below, but mainly see how to install on Mac OS X.
- If you need detailed help, for Linux and some for Windows, read below:
Prerequisites
Common Prerequisites
- C/C++ compiler (OpenCV is known to work with VS2005, VS2008,
including Express Editions, and MinGW on Windows, GCC 4.x on Linux, MacOSX and other Unix-like systems).
CMake 2.6 or later. CMake is now the only method of building OpenCV SVN snapshots.
(Optional) Subversion client (e.g. command-line svn tool, SmartSVN, Tortoise SVN)
(Optional) Python 2.5.x or 2.6.x (Python 3.x support status is unknown),
- including the development files: Python.h, etc. in order to build Python wrappers
(Optional) SWIG 1.3.30 or later to build Python & Octave wrappers
- (in order to build Octave wrappers you will need at least SWIG 1.3.36).
(Optional) IPP 5.1 or later. OpenCV does not require IPP, but can be configured to
- use IPP to make some functions run faster.
(Optional) LiveTeX distribution (or MiKTeX on Windows) to build the up-to-date OpenCV reference manual
Extra Prerequisites (Linux/BSD/other Unix-like OSes)
- pkg-config. It is used at the configuration stage and also simplifies the further use of OpenCV itself.
(Optional) gtk+ 2.x and the related packages (glib, gthread etc.).
- This is the GUI toolkit of choice for highgui on OSes other than Windows and MacOSX.
(Optional) libjpeg, libtiff, libjasper, libpng and zlib. Install any of those with the associated
- development packages (libjpeg-dev etc. on Debian/Ubuntu) to be able to read/write the corresponding image formats.
(Optional) ffmpeg, libgstreamer, libxine, unicap, libdc1394 2.x (or libdc1394 1.x + libraw1394).
You should have some/all of these packages installed (together with associated development packages) to add video capturing, video decoding and video encoding capabilities to highgui. The output of the cmake will show you, which of the packages have been detected and will be used in highgui. (Unable full video support with FFMPEG). For example, on Ubuntu 8.10 all the necessary ffmpeg files can be installed using the following command:
sudo apt-get install libavformat-dev libswscale-dev
(Optional) Octave 2.9 or later, together with SWIG 1.3.36 or later, in order to build Octave wrappers.
Extra Prerequisities (MacOSX)
Xcode 3.1 or later. It does not only include the required C++ compiler, but also Quicktime and Carbon frameworks that make highgui on MacOSX functional. Besides, CMake can generate Xcode projects by the command "ccmake -G Xcode", so you can build OpenCV conveniently from within the IDE. Additional information can be found on the Mac_OS_X_OpenCV_Port page.
Getting the latest stable OpenCV version
- Windows: download the executable installation package for Windows and run it
- Linux/MacOSX/other operating systems: download the source tarball and unpack it
Getting the cutting-edge OpenCV from SourceForge SVN repository
- Launch SVN client and checkout either
the current OpenCV snapshot from here: http://code.ros.org/svn/opencv/trunk
or the latest tested OpenCV snapshot from here: http://code.ros.org/svn/opencv/tags/latest_tested_snapshot
- In Linux/MacOSX it can be done using command line, e.g.:
cd ~/<my_working_directory> svn co https://code.ros.org/svn/opencv/trunk
Building OpenCV from source using CMake
Create a temporary directory, which we denote as <cmake_binary_dir>, where you want to put the generated Makefiles, VisualStudio, Xcode or Eclipse etc. project files as well the object files and output binaries. You can do it using CMake GUI.
- If you use CMake GUI, execute "Configure" to do the initial configuration, then adjust any options, then press "Configure" again and then press "Generate". For the best performance, and if your compiler and the platform supports it, it is recommended to turn on SSE2 optimization (on Mac and Windows it is turned on by default) and OpenMP support. Also, if you want to build Python wrappers, samples or the reference manual in PDF, you should explicitly turn it on.
If you are using command line, enter the <cmake_binary_dir> and type
cmake [<some optional parameters...>] <path to the OpenCV source directory>
For example, if you downloaded the project to ~/projects/opencv, you can do the following:cd ~/projects/opencv # the directory containing INSTALL, CMakeLists.txt etc. mkdir release cd release cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D BUILD_PYTHON_SUPPORT=ON ..
- that will generate makefiles for the Release configuration and will put them in ~/projects/opencv/release directory.
- Another example for Windows users (assuming the .exe extracted files to C:\OpenCV2.0\)
cd C:\OpenCV2.0 # the directory containing INSTALL, CMakeLists.txt etc. mkdir release cd release cmake -D:CMAKE_BUILD_TYPE=RELEASE C:\OpenCV2.0 Note that the use of the colon after the -D is required on Windows Vista (include if cmake is giving errors on other Windows distro's as well). If you are using MS Visual Studio cmake exits with an error message "error PRJ0003: Error spawning 'cmd.exe', make sure that you have the correct VC++ Executable Directories set (see here).
Using IPP. If you have IPP installed on your machine and want to build OpenCV with IPP support, check if IPP is turned on in CMake configuration options and if it was detected. First, look at the CMake output. If it prints
USE IPP: NO
It means that IPP was not turned on or it was not detected. In this case turn it on (USE_IPP=ON) and pass the correct path to IPP shared libraries (IPP_PATH=<...>), like in the example below. While OpenCV currently uses static IPP libraries, it derives their path from the supplied path to the shared/dynamic IPP libraries.
cmake -D:CMAKE_BUILD_TYPE=RELEASE -D:USE_IPP=ON -D:IPP_PATH="C:\Program Files\Intel\IPP\6.1.031\ia32\bin" C:\OpenCV2.0
- (It's also easy to do the same using CMake GUI)
- If you did everything right, you will see the following in the CMake output:
USE IPP: <ipp_path>
- If there are multiple IPP versions installed on the machine (not necessarily all of them are in the system path) and you want to use the particular one, different from what CMake has found, just specify the correct IPP_PATH.
If you generated project files for VisualStudio, Xcode, Eclipse etc., run the respective IDE, open the OpenCV top-level project/solution/workspace etc. and build it.
- If you generated makefiles, then enter the created temporary directory and run make/nmake utility. Then you can optionally run "sudo make install" (Linux, MacOSX).
- If you did not run "make install", you should let your system know where to find the generated libraries.
In Windows you should add <cmake_binary_dir>/bin/debug and <cmake_binary_dir>/bin/release
to the system path (My Computer--[Right button click]-->Properties->Advanced->Environment Variables->Path).
In Linux you should add <cmake_binary_dir>/lib[/debug|/release] to /etc/ld.so.conf or to LD_LIBRARY_PATH, e.g.:
export LD_LIBRARY_PATH=~/projects/opencv/release/lib:$LD_LIBRARY_PATH sudo ldconfig
In MacOSX you should add <cmake_binary_dir>/lib[/debug|/release] to DYLD_LIBRARY_PATH.
- Note that the step is not needed to run OpenCV samples, because:
- on Windows both OpenCV DLLs and the samples are placed into the same directory
- on Linux/MacOSX CMake embeds the correct library paths into the executables
Testing OpenCV
- You may turn on "BUILD_EXAMPLES" in CMake GUI or run cmake with "-D BUILD_EXAMPLES=ON" option, then it will include short demo samples to the build. Note that some of them need image files from the original source directory, you may just copy them to the bin directory.
You can also run tests correctness tests <cmake_binary_dir>/bin[/debug|/release]/cxcoretest and .../cvtest.
Testing Python/Octave wrappers
- As long as you build Python/Octave wrappers and installed them, you can just enter opencv/samples/swig_python or opencv/samples/octave and run the samples, e.g.
python delaunay.py
Building your own code that uses OpenCV
- If you use CMake, you can write your own CMakeLists.txt script and use the provided FindOpenCV.cmake file there,
- just like you would use FindJPEG.cmake etc. Please, refer to CMake documentation.
- You can add opencv include, lib and bin subdirectory to your IDE settings.
- For hand-written Makefiles you can specify the necessary paths and options manually, e.g.:
g++ -o my_example my_example.cpp -I<opencv_source_dir>/include[/opencv] \ -L<cmake_binary_dir>/lib -lcxcore -lcv -lcvaux -lhighgui -lml
- Finally, you can use pkg-config and the provided opencv.pc.
- Make sure that opencv.pc is found by pkg-config:
pkg-config opencv --libs # should print something like -lcxcore -lcv ...
- You can now use it in your Makefiles or build scripts, e.g.:
g++ -o my_example my_example.cpp `pkg-config opencv --cflags --libs`
- Make sure that opencv.pc is found by pkg-config:
Building on Windows using MinGW 3.4.5
If you get compile errors to do with __exchange_and_add it is due to the wrong definition of CV_XADD. It can be fixed by:
Open cxoperations.hpp (found in <Open CV base dir>\include\opencv)
- Find this section (lines 67-68 in 2.0):
#else #include <bits/atomicity.h> #if __GNUC__ >= 4 And update it to reference a new definition, __MINGW32__ (defined by the MinGW compiler, see http://predef.sourceforge.net/precomp.html#sec31):
#else #include <bits/atomicity.h> #if __GNUC__ >= 4 || __MINGW32__
Up-to-date version of this manual can be found here: http://opencv.willowgarage.com/wiki/InstallGuide