We are free to choose among various setup tools, depending on our operating system and how much configuration we want to do. Let's take an overview of the tools for Windows, Mac, Ubuntu, and other Unix-like systems.

Finally, we need to make sure that Python and other processes can find the rest of OpenCV. Edit the system's Path variable and append ;<build_folder>/bin/Release (for a Visual Studio build) or ;<build_folder>/bin (for a MinGW build). Reboot your system.

Copy <build_folder>\lib\Release\cv2.pyd (from a Visual Studio build) or <build_folder>\lib\cv2.pyd (from a MinGW build) to C:\Python2.7\Lib\site-packages (assuming Python 2.7 is installed to the default location). Now, the Python installation can find part of OpenCV.

For Visual Studio or Visual C++ Express, open <build_folder>/OpenCV.sln . Select Release configuration and build. If you get build errors, double-check that Release configuration is selected.

Having configured our build system, we are ready to compile.

CMake might report that it has failed to find some dependencies. Many of OpenCV's dependencies are optional; so, do not be too concerned yet. If the build fails to complete or you run into problems later, try installing missing dependencies (often available as prebuilt binaries) and then rebuild OpenCV from this step.

If OpenNI is not installed, omit -D:WITH_OPENNI=ON . (In this case, depth cameras will not be supported.) If OpenNI and SensorKinect are installed to non-default locations, modify the command to include -D:OPENNI_LIB_DIR=<openni_install_destination>\Lib -D:OPENNI_INCLUDE_DIR=<openni_install_destination>\Include -D:OPENNI_PRIME_SENSOR_MODULE_BIN_DIR=<sensorkinect_install_destination>\Sensor\Bin .

Now, we are ready to configure our build. To understand all the options, we could read the code in <unzip_destination>\opencv\CMakeLists.txt . However, for this book's purposes, we only need to use the options that will give us a release build with Python bindings and, optionally, depth camera support via OpenNI and SensorKinect.

Open Command Prompt and make another folder where our build will go:

Download the self-extracting ZIP of OpenCV 2.4.3 from http://sourceforge.net/projects/opencvlibrary/files/opencv-win/2.4.3/OpenCV-2.4.3.exe/download . Run the self-extracting ZIP and, when prompted, enter any destination folder, which we will refer to as <unzip_destination> . A subfolder, <unzip_destination>\opencv , is created.

Optionally, download and install SensorKinect 0.93 from https://github.com/avin2/SensorKinect/blob/unstable/Bin/SensorKinect093-Bin-Win32-v5.1.2.1.msi?raw=true (32 bit). Alternatively, for 64-bit Python , use https://github.com/avin2/SensorKinect/blob/unstable/Bin/SensorKinect093-Bin-Win64-v5.1.2.1.msi?raw=true (64 bit).

Optionally, download and install OpenNI 1.5.4.0 from http://www.openni.org/wp-content/uploads/2012/12/OpenNI-Win32-1.5.4.0-Dev1.zip (32 bit). Alternatively, for 64-bit Python, use http://www.openni.org/wp-content/uploads/2012/12/OpenNI-Win64-1.5.4.0-Dev.zip (64 bit).

For MinGW get the installer from http://sourceforge.net/projects/mingw/files/Installer/mingw-get-inst/mingw-get-inst-20120426/mingw-get-inst-20120426.exe/download . When running the installer, make sure that the destination path does not contain spaces and that the optional C++ compiler is included. Edit the system's Path variable and append ;C:\MinGW\bin (assuming MinGW is installed to the default location.) Reboot the system.

For Microsoft Visual C++ Express 2010, get the installer from http://www.microsoft.com/visualstudio/eng/downloads . Reboot after installation is complete.

For Microsoft Visual Studio 2010, use any installation media you purchased. During installation, include any optional C++ components. Reboot after installation is complete.

Download and install Microsoft Visual Studio 2010, Microsoft Visual C++ Express 2010, or MinGW. Note that OpenCV 2.4.3 cannot be compiled with the more recent versions (Microsoft Visual Studio 2012 and Microsoft Visual Studio Express 2012).

Download and install CMake 2.8.9 from http://www.cmake.org/files/v2.8/cmake-2.8.9-win32-x86.exe . When running the installer, select either Add CMake to the system PATH for all users or Add CMake to the system PATH for current user .

Let's assume that we have already installed 32-bit Python 2.7, NumPy, and SciPy either from binaries (as described previously) or from source. Now, we can proceed with installing compilers and CMake, optionally installing OpenNI and SensorKinect, and then building OpenCV from source:

Windows does not come with any compilers or CMake. We need to install them. If we want support for depth cameras, including Kinect, we also need to install OpenNI and SensorKinect.

A final step is necessary if we want Python scripts to run using the new Python installation by default. Edit the system's Path variable and append ;C:\Python2.7 (assuming we installed Python 2.7 to the default location). Remove any previous Python paths, such as ;C:\Python2.6 . Log out and log back in (alternatively, reboot).

Copy <unzip_destination>\opencv\build\python\2.7\cv2.pyd to C:\Python2.7\Lib\site-packages (assuming we installed Python 2.7 to the default location). Now, the new Python installation can find OpenCV.

Download the self-extracting ZIP of OpenCV 2.4.3 from http://sourceforge.net/projects/opencvlibrary/files/opencv-win/2.4.3/OpenCV-2.4.3.exe/download . Run the self-extracting ZIP and, when prompted, enter any destination folder, which we will refer to as <unzip_destination> . A subfolder, <unzip_destination>\opencv , is created.

Here are the steps to set up 32-bit Python 2.7, NumPy, and OpenCV:

Now, under System variables , select Path and click on the Edit button. Make changes as directed. To apply the changes, click on all the OK buttons (until we are back in the main window of Control Panel ). Then, log out and log back in (alternatively, reboot).

On Windows XP, open the Start menu and go to Control Panel | System . Select the Advanced tab. Click on the Environment Variables button.

On Windows Vista/Windows 7/Windows 8, open the Start menu and launch Control Panel . Now, go to System and Security | System | Advanced system settings . Click on the Environment Variables button.

Some of the following steps refer to editing the system's Path variable. This task can be done in the Environment Variables window of Control Panel .

On Windows, OpenCV offers better support for 32-bit Python than 64-bit Python. Even if we are building from source, I recommend using 32-bit Python. Fortunately, 32-bit Python works fine on either 32-bit or 64-bit editions of Windows.

The precompiled version of OpenCV does not offer support for depth cameras.

If we want support for depth cameras including Kinect, we should first install OpenNI and SensorKinect, which are available as precompiled binaries with installation wizards. Then, we must build OpenCV from source.

Windows does not come with Python preinstalled. However, installation wizards are available for precompiled Python, NumPy, SciPy, and OpenCV. Alternatively, we can build from source. OpenCV's build system uses CMake for configuration and either Visual Studio or MinGW for compilation.

Making the choice on Mac OS X Snow Leopard, Mac OS X Lion, or Mac OS X Mountain Lion

Some versions of Mac come with Python 2.7 preinstalled. However, the preinstalled Python is customized by Apple for the system's internal needs. Normally, we should not install any libraries atop Apple's Python. If we do, our libraries might break during system updates or, worse, might conflict with preinstalled libraries that the system requires. Instead, we should install standard Python 2.7 and then install our libraries atop it.

For Mac, there are several possible approaches to obtaining standard Python 2.7, NumPy, SciPy, and OpenCV. All approaches ultimately require OpenCV to be compiled from source using Xcode Developer Tools. However, depending on the approach, this task is automated for us by third-party tools in various ways. We will look at approaches using MacPorts or Homebrew. These tools can potentially do everything that CMake can do, plus they help us resolve dependencies and separate our development libraries from the system libraries.

Tip I recommend MacPorts, especially if you want to compile OpenCV with depth camera support via OpenNI and SensorKinect. Relevant patches and build scripts, including some that I maintain, are ready-made for MacPorts. By contrast, Homebrew does not currently provide a ready-made solution for compiling OpenCV with depth camera support.

Before proceeding, let's make sure that the Xcode Developer Tools are properly set up:

Download and install Xcode from the Mac App Store or http://connect.apple.com/. During installation, if there is an option to install Command Line Tools, select it. Open Xcode and accept the license agreement. A final step is necessary if the installer did not give us the option to install Command Line Tools. Go to Xcode | Preferences | Downloads and click on the Install button next to Command Line Tools. Wait for the installation to finish and quit Xcode.

Now we have the required compilers for any approach.

Using MacPorts with ready-made packages We can use the MacPorts package manager to help us set up Python 2.7, NumPy, and OpenCV. MacPorts provides Terminal commands that automate the process of downloading, compiling, and installing various pieces of open source software (OSS). MacPorts also installs dependencies as needed. For each piece of software, the dependencies and build recipe are defined in a configuration file called a Portfile . A MacPorts repository is a collection of Portfiles. Starting from a system where Xcode and its Command Line Tools are already set up, the following steps will give us an OpenCV installation via MacPorts: Download and install MacPorts from http://www.macports.org/install.php. If we want support for the Kinect depth camera, we need to tell MacPorts where to download some custom Portfiles that I have written. To do so, edit /opt/local/etc/macports/sources.conf (assuming MacPorts is installed to the default location). Just above the line rsync://rsync.macports.org/release/ports/ [default] , add the following line: http://nummist.com/opencv/ports.tar.gz Save the file. Now, MacPorts knows to search for Portfiles in my online repository first and, then, the default online repository. Open Terminal and run the following command to update MacPorts: $ sudo port selfupdate When prompted, enter your password. Now (if we are using my repository), run the following command to install OpenCV with Python 2.7 bindings and support for depth cameras including Kinect: $ sudo port install opencv +python27 +openni_sensorkinect Alternatively (with or without my repository), run the following command to install OpenCV with Python 2.7 bindings and support for depth cameras excluding Kinect: $ sudo port install opencv +python27 +openni Dependencies, including Python 2.7, NumPy, OpenNI, and (in the first example) SensorKinect, are automatically installed as well. By adding +python27 to the command, we are specifying that we want the opencv variant (build configuration) with Python 2.7 bindings. Similarly, +openni_sensorkinect specifies the variant with the broadest possible support for depth cameras via OpenNI and SensorKinect. You may omit +openni_sensorkinect if you do not intend to use depth cameras or you may replace it with +openni if you do intend to use OpenNI-compatible depth cameras but just not Kinect. To see the full list of available variants before installing, we can enter: $ port variants opencv Depending on our customization needs, we can add other variants to the install command. For even more flexibility, we can write our own variants (as described in the next section). Also, run the following command to install SciPy: $ sudo port install py27-scipy The Python installation's executable is named python2.7 . If we want to link the default python executable to python2.7 , let's also run: $ sudo port install python_select $ sudo port select python python27

Using MacPorts with your own custom packages With a few extra steps, we can change the way that MacPorts compiles OpenCV or any other piece of software. As previously mentioned, MacPorts' build recipes are defined in configuration files called Portfiles. By creating or editing Portfiles, we can access highly configurable build tools, such as CMake, while also benefitting from MacPorts' features, such as dependency resolution. Let's assume that we already have MacPorts installed. Now, we can configure MacPorts to use custom Portfiles that we write: Create a folder somewhere to hold our custom Portfiles. We will refer to this folder as <local_repository> . Edit the file /opt/local/etc/macports/sources.conf (assuming MacPorts is installed to the default location). Just above the line rsync://rsync.macports.org/release/ports/ [default] , add this line: file://<local_repository> For example, if <local_repository> is /Users/Joe/Portfiles , add: file:///Users/Joe/Portfiles Note the triple slashes. Save the file. Now, MacPorts knows to search for Portfiles in <local_repository> first and, then, its default online repository. Open Terminal and update MacPorts to ensure that we have the latest Portfiles from the default repository: $ sudo port selfupdate Let's copy the default repository's opencv Portfile as an example. We should also copy the directory structure, which determines how the package is categorized by MacPorts. $ mkdir <local_repository>/graphics/ $ cp /opt/local/var/macports/sources/rsync.macports.org/release/ports/graphics/opencv <local_repository>/graphics Alternatively, for an example that includes Kinect support, we could download my online repository from http://nummist.com/opencv/ports.tar.gz, unzip it and copy its entire graphics folder into <local_repository> : $ cp <unzip_destination>/graphics <local_repository> Edit <local_repository>/graphics/opencv/Portfile . Note that this file specifies CMake configuration flags, dependencies, and variants. For details on Portfile editing, go to http://guide.macports.org/#development. To see which CMake configuration flags are relevant to OpenCV, we need to look at its source code. Download the source code archive from http://sourceforge.net/projects/opencvlibrary/files/opencv-unix/2.4.3/OpenCV-2.4.3.tar.bz2/download , unzip it to any location, and read <unzip_destination>/OpenCV-2.4.3/CMakeLists.txt . After making any edits to the Portfile, save it. Now, we need to generate an index file in our local repository so that MacPorts can find the new Portfile: $ cd <local_repository> $ portindex From now on, we can treat our custom opencv just like any other MacPorts package. For example, we can install it as follows: $ sudo port install opencv +python27 +openni_sensorkinect Note that our local repository's Portfile takes precedence over the default repository's Portfile because of the order in which they are listed in /opt/local/etc/macports/sources.conf .

Using Homebrew with ready-made packages (no support for depth cameras) Homebrew is another package manager that can help us. Normally, MacPorts and Homebrew should not be installed on the same machine. Starting from a system where Xcode and its Command Line Tools are already set up, the following steps will give us an OpenCV installation via Homebrew: Open Terminal and run the following command to install Homebrew: $ ruby -e "$(curl -fsSkLraw.github.com/mxcl/homebrew/go)" Unlike MacPorts, Homebrew does not automatically put its executables in PATH . To do so, create or edit the file ~/.profile and add this line at the top: export PATH=/usr/local/bin:/usr/local/sbin:$PATH Save the file and run this command to refresh PATH : $ source ~/.profile Note that executables installed by Homebrew now take precedence over executables installed by the system. For Homebrew's self-diagnostic report, run: $ brew doctor Follow any troubleshooting advice it gives. Now, update Homebrew: $ brew update Run the following command to install Python 2.7: $ brew install python Now, we can install NumPy. Homebrew's selection of Python library packages is limited so we use a separate package management tool called pip , which comes with Homebrew's Python: $ pip install numpy SciPy contains some Fortran code, so we need an appropriate compiler. We can use Homebrew to install the gfortran compiler: $ brew install gfortran Now, we can install SciPy: $ pip install scipy To install OpenCV on a 64-bit system (all new Mac hardware since late 2006), run: $ brew install opencv Alternatively, to install OpenCV on a 32-bit system, run: $ brew install opencv --build32 Tip Downloading the example code You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com . If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.