Book Image

OpenCV Computer Vision Application Programming Cookbook Second Edition

By : Robert Laganiere
Book Image

OpenCV Computer Vision Application Programming Cookbook Second Edition

By: Robert Laganiere

Overview of this book

Table of Contents (18 chapters)
OpenCV Computer Vision Application Programming Cookbook Second Edition
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Installing the OpenCV library


OpenCV is an open source library for developing computer vision applications that run on Windows, Linux, Android, and Mac OS. It can be used in both academic and commercial applications under a BSD license that allows you to freely use, distribute, and adapt it. This recipe will show you how to install the library on your machine.

Getting ready

When you visit the OpenCV official website at http://opencv.org/, you will find the latest release of the library, the online documentation, and many other useful resources on OpenCV.

How to do it...

From the OpenCV website, go to the DOWNLOADS page that corresponds to the platform of your choice (Unix/Windows or Android). From there, you will be able to download the OpenCV package. You will then need to uncompress it, normally under a directory with a name that corresponds to the library version (for example, in Windows, you can save the uncompressed directory under C:\OpenCV2.4.9). Once this is done, you will find a collection of files and directories that constitute the library at the chosen location. Notably, you will find the sources directory here, which contains all the source files. (Yes, it is open source!) However, in order to complete the installation of the library and have it ready for use, you need to undertake an additional step: generating the binary files of the library for the environment of your choice. This is indeed the point where you have to make a decision on the target platform that you will use to create your OpenCV applications. Which operating system should you use? Windows or Linux? Which compiler should you use? Microsoft VS2013 or MinGW? 32-bit or 64-bit? The Integrated Development Environment (IDE) that you will use in your project development will also guide you to make these choices.

Note that if you are working under Windows with Visual Studio, the executable installation package will, most probably, not only install the library sources, but also install all of the precompiled binaries needed to build your applications. Check for the build directory; it should contain the x64 and x86 subdirectories (corresponding to the 64-bit and 32-bit versions). Within these subdirectories, you should find directories such as vc10, vc11, and vc12; these contain the binaries for the different versions of MS Visual Studio. In that case, you are ready to start using OpenCV. Therefore, you can skip the compilation step described in this recipe, unless you want a customized build with specific options.

To complete the installation process and build the OpenCV binaries, you need to use the CMake tool, available at http://cmake.org. CMake is another open source software tool designed to control the compilation process of a software system using platform-independent configuration files. It generates the required makefiles or workspaces needed for compiling a software library in your environment. Therefore, you need to download and install CMake. You can then run it using the command line, but it is easier to use CMake with its GUI (cmake-gui). In the latter case, all you need to do is specify the folder containing the OpenCV library source and the one that will contain the binaries. You need to click on Configure in order to select the compiler of your choice and then click on Configure again.

You are now ready to generate your project files by clicking on the Generate button. These files will allow you to compile the library. This is the last step of the installation process, which will make the library ready to be used under your development environment. For example, if you have selected Visual Studio, then all you need to do is to open the top-level solution file that CMake has created for you (most probably, the OpenCV.sln file). You then issue the Build Solution command in Visual Studio. To get both a Release and a Debug build, you will have to repeat the compilation process twice, one for each configuration. The bin directory that is created contains the dynamic library files that your executable will call at runtime. Make sure to set your system PATH environment variable from the control panel such that your operating system can find the dll files when you run your applications.

In Linux environments, you will use the generated makefiles by running your make utility command. To complete the installation of all the directories, you also have to run a Build INSTALL or sudo make INSTALL command.

However, before you build the libraries, make sure to check what the OpenCV installer has installed for you; the built library that you are looking for might already be there, which will save you the compilation step. If you wish to use Qt as your IDE, the There's more... section of this recipe describes an alternative way to compile the OpenCV project.

How it works...

Since Version 2.2, the OpenCV library is divided into several modules. These modules are built-in library files located in the lib directory. Some of the commonly-used modules are as follows:

  • The opencv_core module that contains the core functionalities of the library, in particular, basic data structures and arithmetic functions

  • The opencv_imgproc module that contains the main image processing functions

  • The opencv_highgui module that contains the image and video reading and writing functions along with some user interface functions

  • The opencv_features2d module that contains the feature point detectors and descriptors and the feature point matching framework

  • The opencv_calib3d module that contains the camera calibration, two-view geometry estimation, and stereo functions

  • The opencv_video module that contains the motion estimation, feature tracking, and foreground extraction functions and classes

  • The opencv_objdetect module that contains the object detection functions such as the face and people detectors

The library also includes other utility modules that contain machine learning functions (opencv_ml), computational geometry algorithms (opencv_flann), contributed code (opencv_contrib), obsolete code (opencv_legacy), and gpu-accelerated code (opencv_gpu). You will also find other specialized libraries that implement higher-level functions, such as opencv_photo for computational photography and opencv_stitching for image-stitching algorithms. There is also a library module, called opencv_nonfree, which contains functions that have a potential limitation in use. When you compile your application, you will have to link your program with the libraries that contain the OpenCV functions you are using. Most likely, these will be the first three functions of the list given previously plus some of the others depending on the scope of your application.

All these modules have a header file associated with them (located in the include directory). A typical OpenCV C++ code will, therefore, start by including the required modules. For example (and this is the suggested declaration style):

#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>

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.

You might see an OpenCV code starting with the following command:

#include "cv.h"

This is because it uses the old style, before the library was restructured into modules. Finally, note that OpenCV will be restructured in the future; so, if you download a more recent version than 2.4, you will probably not see the same module subdivision.

There's more...

The OpenCV website at http://opencv.org/ contains detailed instructions on how to install the library. It also contains a complete online documentation that includes several tutorials on the different components of the library.

Using Qt for OpenCV developments

Qt is a cross-platform IDE for C++ applications developed as an open source project. It is offered under the LPGL open source license as well as under a commercial (and paid) license for the development of proprietary projects. It is composed of two separate elements: a cross-platform IDE called Qt creator and a set of Qt class libraries and development tools. Using Qt to develop C++ applications has the following benefits:

  • It is an open source initiative developed by the Qt community, which gives you access to the source code of the different Qt components

  • It is a cross-platform IDE, meaning that you can develop applications that can run on different operating systems, such as Windows, Linux, Mac OS X, and so on

  • It includes a complete and cross-platform GUI library that follows an effective object-oriented and event-driven model

  • Qt also includes several cross-platform libraries that help you to develop multimedia, graphics, databases, multithreading, web applications, and many other interesting building blocks useful for designing advanced applications

You can download Qt from http://qt-project.org/. When you install it, you will be offered the choice of different compilers. Under Windows, MinGW is an excellent alternative to the Visual Studio compilers.

Compiling the OpenCV library with Qt is particularly easy because it can read CMake files. Once OpenCV and CMake have been installed, simply select Open File or Project... from the Qt menu and open the CMakeLists.txt file that you will find under the sources directory of OpenCV. This will create an OpenCV project that you build using the Build Project Qt command.

You might get a few warnings, but these are without consequences.

The OpenCV developer site

OpenCV is an open source project that welcomes user contributions. You can access the developer site at http://code.opencv.org. Among other things, you can access the currently developed version of OpenCV. The community uses Git as their version control system. You then have to use it to check out the latest version of OpenCV. Git is also a free and open source software system; it is probably the best tool you can use to manage your own source code. You can download it from http://git-scm.com/.

See also

  • My website (www.laganiere.name) also presents step-by-step instructions on how to install the latest versions of the OpenCV library

  • The There's more... section of the next recipe explains how to create an OpenCV project with Qt