OpenCV is an open source library for developing computer vision applications that run on Windows, Linux, Android, and macOS. It can be used in both academic and commercial applications under a BSD license that allows you to use, distribute, and adapt it freely. This recipe will show you how to install the library on your machine.
Installing the OpenCV library
Getting ready
When you visit the OpenCV official website at https://opencv.org/, you will find the latest release of the library, the online documentation, and many other useful resources concerning OpenCV.
How to do it...
The following steps will help take us through the installation, as follows:
- 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 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:\OpenCV4.0.0).
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 modules 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 Visual Studio 2013 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.
- To complete the installation process and build the OpenCV binaries, you need to use the CMake tool, available at https://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.
- Then, run it using the command line. Thereafter, it is easier to use CMake with its GUI (cmake-gui).
- 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 as shown in the following screenshot:
- 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:
- 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 click on Build Solution 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.
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 has been 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), and many more. 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 new branch that contains other library modules, which include non-free algorithms, non-stable modules, or experimental modules. This branch is on the opencv-contrib GitHub branch. When you compile your application, you will have to link your program with the libraries that contain the OpenCV functions you are using, linking it with the opencv-contrib folder.
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), it will look like the following code:
#include <opencv2/core/core.hpp> #include <opencv2/imgproc/imgproc.hpp> #include <opencv2/highgui/highgui.hpp>
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 emailed directly to you.
You might see an OpenCV code starting with the following command:
#include "cv.h"
This is because it used the old style before the library was restructured into modules and became compatible with older definitions.
There's more...
The OpenCV website at https://opencv.org/ contains detailed instructions on how to install the library. It also contains 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 GNU Lesser General Public License (LGPL) 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, that 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, macOS, 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 https://www.qt.io/developers/. 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 will have built by clicking on Build Project in the Qt menu:
You might get a few warnings, but these can be overlooked without consequences.
The OpenCV developer site
OpenCV is an open source project that welcomes user contributions. You can access the developer site at https://docs.opencv.org/. Among other things, you can access the currently developed version of OpenCV. The community uses Git as its 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 https://git-scm.com/.
See also
- The website of the author of this cookbook (www.laganiere.name) also presents step-by-step instructions on how to install the latest versions of the library.
- The There's more... section of the next recipe explains how to create an OpenCV project with Qt.
We've successfully learned how to install the OpenCV library. Now, let's move on to the next recipe!