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.