Getting Started with OpenCV

We will be using OpenCV 2.2 for this course. The sections below descibehow to compile and install the library for your operating system.
In addition to the sample programs that come with the OpenCV source code, you will also have the library reference manual handy:

Windows

To use OpenCV with Python, you will first need to install a Python interpreter and the numpy package. Download and install the 32-bit Windows installer for Python 2.7.1 from http://www.python.org/getit/. Then, download and install the 32-bit numpy module from http://sourceforge.net/projects/numpy/files/NumPy/1.5.1/numpy-1.5.1-win32-superpack-python2.7.exe/download.

To compile OpenCV programs using C or C++, you will need to install Microsoft Visual C++ 2010 Express from http://www.microsoft.com/express/Downloads/#2010-Visual-CPP.

Finally for either Python or C/C++, you will need to download precompiled libraries for OpenCV 2.2 from http://sourceforge.net/projects/opencvlibrary/files/opencv-win/2.2/OpenCV-2.2.0-win32-vs2010.exe/download. When prompted by the installer, be sure to add the OpenCV installation to your system path for all users.

Once the OpenCV install finishes, you need to tell Python how to find the OpenCV module. One way to do this is to drop a file named cv.pth into C:\Python27\Lib\site-packages containing the single line
C:\OpenCV2.2\Python2.7\lib\site-packages
...assuming you installed both Python and OpenCV to their default locations.

You may want to install CMake in order to avoid the chore of creating Visual Studio projects for the example code from the course website. To do so, download and install the CMake Win32 installer from http://www.cmake.org/cmake/resources/software.html. I don't yet have the Windows examples building with CMake, but I expect to shortly.

Here is a simple project for Visual Studio that builds a simple OpenCV program for displaying an image. Also, you can look at the equivalent Python script. To make your own projects, you can simply modify the project above by renaming it and adding your own source files; alternatively, you will need to perform the following steps by right-clicking on the program in the Project Explorer, and adding some settings:
  • Under "C/C++ -> General", you will want to add the OpenCV folders to "Additional Include Directories". I add both the general OpenCV include folder as well as the opencv folder underneath it.

    C++ General Settings

  • Under "C/C++ -> Preprocessor", I add the definition _USE_MATH_DEFINES to "Preprocessor Definitions" so that I can use constants like M_PI in my programs.

    C++ Preprocessor settings

  • Under "Linker -> General", I add the OpenCV library directory to "Additional Library Directories".

    Linker General settings

  • Under "Linker -> Input" I add the library files found in the OpenCV library directory to "Additional Dependencies". For the "Debug" configuration, I use the files ending in "220d.lib", and for "Release" configuration, I use the files ending in "220.lib".

    Linker Input settings
Also, see this page for more information about using Visual Studio with OpenCV.

Ubuntu Linux v10.x

Since OpenCV 2.2 is not yet in the stable releases, we will have to compile it from source. Fortunately all of its dependencies can be downloaded through the built-in package manager.

You can install all of the dependencies via the command
sudo apt-get install g++ cmake python-dev python-numpy libgtk2.0-dev libavformat-dev libswscale-dev libdc1394-22-dev
Next, download the current OpenCV source distribution from http://sourceforge.net/projects/opencvlibrary/files/opencv-unix/2.2/OpenCV-2.2.0.tar.bz2/download

In the terminal, change to the directory where you downloaded the package, and unarchive it with the command
tar -xjf OpenCV-2.2.0.tar.bz2
change to the OpenCV-2.2.0 directory that was created when you unarchived the package, and type
mkdir release
cd release
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D BUILD_PYTHON_SUPPORT=ON ..
Once this process completes, you can type
make
sudo make install
to install OpenCV. Libraries will be installed in /usr/local/lib, C/C++ headers are in /usr/local/include/opencv and /usr/local/include/opencv2, and the Python module is installed to /usr/local/lib/python2.6/site-packages. You will need to add that directory to your Python path if you want to use the OpenCV Python module. One way to do this is to add the line
export PYTHONPATH=/usr/local/lib/python2.6/site-packages
to the end of your .bashrc file.

This tarball contrains a makefile and C++ source for a simple OpenCV program for displaying an image. Or, try the equivalent Python script.

Mac OS X

The easiest way to install OpenCV is through MacPorts, an open-source package management system. To do this, you must first install the XCode Developer Kit, which can be downloaded from Apple (free, but requires registration via email). Alternatively, if you still have the OS installation discs, it can be found on one of them (in a folder/installer called "Additional Packages" or similar).

Once you have MacPorts setup, you can install OpenCV by going into the Terminal application and typing
sudo port install opencv +python26
Your OpenCV libraries will be installed into /opt/local/lib, the C/C++ header files will be in /opt/local/include/opencv and /opt/local/include/opencv2, and the Python interpreter installed at /opt/local/bin/python2.6 will be setup for the OpenCV Python module.

Here is a tarball for a simple OpenCV program that displays an image, and the equivalent Python script.
Last modified: Thursday, August 8, 2013, 8:01 PM