This chapter deals with the basic installation steps and settings required to develop applications with the OpenCV library. Also, it introduces the essential concepts in order to use the Application Programming Interface (API) provided by the library and the basic datatypes supplied. This chapter includes a section with full examples of code that illustrate how to read/write images and video files, and access images from live cameras. These examples also show how to get access to live input from cameras connected to a computer.
OpenCV can be downloaded from http://opencv.org/, and is available for the most popular operating systems, such as Unix (Linux/Mac), Microsoft Windows (Windows), Android, and iOS. In this book, the last stable release (2.4.9) of OpenCV for Windows 7 (SP1) has been used. For Windows, this release comes in the form of a self-extracting archive (opencv-2.4.9.exe
), which should be extracted to the desired location (for example, OPENCV_SCR
for C:opencv-src
). It should be noted that in Windows it is strongly recommended to allocate the source and binaries at absolute paths without white spaces because errors might appear later.
After extracting the archive, the obtained files are organized in two subdirectories under OPENCV_SCR
: build
and sources
. The first one (build
) includes precompiled (binaries) versions with Microsoft Visual C++ compilers (MSVC, v. 10, 11, and 12) for 32- and 64-bit architectures (located in the x 86 and x 64 subdirectories respectively). The sources
subdirectory contains the source code of the OpenCV library. This code might be compiled with other compilers (for example, GNU g++).
Using the precompiled versions of OpenCV is the easiest option and only requires setting the location of OpenCV's dynamic libraries binaries (DLL files) in the Path environment variable. For instance, in our setup, this location could be OPENCV_SCR/build/x86/vc12/bin
where the binaries compiled with MS VC version 12 for the 32 bit architecture are located. Remember that changing the environment variables in Windows 7 (SP1) can be done on Advanced System Settings under Properties of My Computer. The Rapid Environment Editor tool (available at http://www.rapidee.com) provides a convenient way to change Path and other environment variables in Windows 7.
This chapter covers a detailed installation process of OpenCV on Windows 7 (SP1). For Linux and other operating systems, you can have a look at the OpenCV online documentation (OpenCV Tutorials, Introduction to OpenCV section) available at http://docs.opencv.org/doc/tutorials/tutorials.html.
The OpenCV distribution includes the source code of the library that can be compiled when a different binary version is required. One such situation comes when we need to use the Qt-based user interface functions available in OpenCV (which are not included in the precompiled versions). Besides, the build process (compilation) for the OpenCV library is required if our compiler (for example, GNU g++) doesn't match the precompiled version of the library.
The requirements that have to be met in order to compile OpenCV with Qt are as follows:
C:QtQt5.2.1Toolsmingw48_32in
).C:QtQt5.2.15.2.1mingw48_32in
).In this section, we illustrate the configuration steps for OpenCV with CMake, with the help of screenshots of the steps involved:
OPENCV_SCR
) and the build directory (OPENCV_BUILD
) can be set in the proper text fields in the CMake main window. Also, the checkboxes labeled as Grouped and Advanced should be marked in the CMake main window. We continue clicking on the Configure button. At this point, the tool prompts the user to specify the desired compiler and we choose MinGW Makefiles using the native compilers. If we choose the Specify native compilers option, it is possible to specify a particular location for the compiler and make tools. After clicking on the Finish button, the configuration step continues checking the settings of the system. The following screenshot shows the CMake window at the end of this preconfiguration process:Qt5Concurrent_DIR
, Qt5Core_DIR
, Qt5Gui_DIR
, Qt5OpenGL_DIR
, Qt5Test_DIR
, and Qt5Widgets_DIR
(refer to the following figure). In our setup, these directories can be found under C:/Qt/Qt5.2.1/5.2.1/mingw48_32/lib/cmake
.By clicking on the Configure button once, we obtain no further red entries and the configuration process is finally done, as shown in the following screenshot:
In the process just described, it is possible to change the configuration options as many times as desired before the generation step. Some other convenient options to be set are listed as follows:
The compilation should be launched from the console at the target directory (OPENCV_BUILD
) set during the configuration with CMake (that is, step 1 from the previous list). The command should be as follows:
OPENCV_BUILD>mingw32-make
This command launches a build process using the generated files by CMake. Compilation typically takes several minutes. If the compilation ends without errors, the installation continues with the execution of the following command:
OPENCV_BUILD>mingw32-make install
This command copies the OpenCV binaries to the following directory:
C:opencv-buildQtinstall
If something goes wrong during the compilation, we should return to CMake to change the options selected in the previous steps. Installation ends by adding the location of the library binaries (DLL files) to the Path environment variable. In our setup, this directory is located at OPENCV_BUILDinstallx64mingwin
.
To check the success of the installation process, it is possible to run some of the examples compiled along with the library (if the BUILD_EXAMPLES option was set with CMake). The code samples can be found at OPENCV_BUILDinstallx64mingwsamplescpp
.
The preceding screenshot shows the output window for the sample cpp-example-edge.exe
file, which demonstrates the Canny edge detection on the fruits.jpg
input file included with the source OpenCV distribution.
In the next section, we summarize the recipe used to set up OpenCV 2.4.9 in our Windows 7-x32 platform with Qt 5.2.1 (MinGW 4.8).
The whole process for setting up OpenCV can be done using the following steps:
C:QtQt5.2.1Toolsmingw48_32in
).C:QtQt5.2.15.2.1mingw48_32in
).OPENCV_SRC
directory.OPENCV_SCR
) and target (OPENCV_BUILD
) directories.Qt5Concurrent_DIR
, Qt5Core_DIR
, Qt5Gui_DIR
, Qt5OpenGL_DIR
, Qt5Test_DIR
, Qt5Widgets_DIR
. Then, click on Configure again.OPENCV_BUILD
directory and run the mingw32-make
command to start the compilation.mingw32-make install
on the command line.OPENCV_BUILDinstallx64mingwin
).To check the right installation of the OpenCV library, you can run some of the examples included at OPENCV_BUILDinstallx64mingwsamplescpp
.