- Qt 5 and OpenCV 4 Computer Vision Projects
- Zhuo Qingliang
- 1342字
- 2021-06-24 13:59:19
Building and installing OpenCV from the source
In the preceding section, we installed a dummy slot for the blur action that does nothing but prints a simple message. Now, we are going to rewrite the implementation of that slot to do the real blurring work.
As we mentioned in the preceding sections, we will be using the OpenCV library, and, more precisely, the latest version (4.0) of it, to edit the images. So, before we start with our code, we will install the latest version of the OpenCV library and include it in our project.
OpenCV is a set of libraries, tools, and modules that contain classes and functions that are required for building computer vision applications. Its release files can be found on the release page of its official website: https://opencv.org/releases.html. Another thing we need to know is that OpenCV uses a modern build tool called CMake to construct its building system. This means that we must have CMake installed on our operating system to build OpenCV from source, and at least version 3.12 of CMake is required, so please ensure that your version of CMake is set up properly.
In our book, we will try our best to keep the use of these tools simple and clear.
The OpenCV release page looks as follows:

We can click the Sources link to download the ZIP package of its source to the local disk and then unzip it. We will build OpenCV by using CMake in a Terminal, so, we will open a Terminal and change its work directory to the directory of the unzipped source. Also, OpenCV doesn't allow you to build directly in the root directory of its source tree, so we should create a separate directory to build it.
Here are the instructions we used to build OpenCV in the Terminal:
$ cd ~/opencv-4.0.0 # path to the unzipped source
$ mkdir release # create the separate dir
$ cd release
$ cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=$HOME/programs/opencv ..
# ... output of cmake ...
# rm ../CMakeCache.txt if it tells you are not in a separate dir
$ make
# ... output of make ...
$ make install
The cmake ... line reads the CMakeLists.txt file in the root directory of the unzipped source and generates a makefile. The CMAKE_BUILD_TYPE variable we pass to the cmake command using -D specifies that we build OpenCV in the RELEASE mode. Likewise, the CMAKE_INSTALL_PREFIX variable specifies the path to where the OpenCV library will be installed. Here, I install OpenCV to $HOME/programs/opencv, that is, /home/kdr2/programs/opencv—you can change the value of CMAKE_INSTALL_PREFIX to change the destination directory if you so desire. After the cmake command ends in success, a file named Makefile will be generated. With the makefile, now we can run make and make install to compile and install the library.
If all the preceding instructions go well, your OpenCV version will be installed correctly. You can check this by browsing the installation directory:
$ ls ~/programs/opencv/
bin include lib share
$ ls ~/programs/opencv/bin/
opencv_annotation opencv_interactive-calibration opencv_version
opencv_visualisation setup_vars_opencv4.sh
$ ls -l ~/programs/opencv/lib/
# ...
lrwxrwxrwx 1 kdr2 kdr2 21 Nov 20 13:28 libopencv_core.so -> libopencv_core.so.4.0
lrwxrwxrwx 1 kdr2 kdr2 23 Nov 20 13:28 libopencv_core.so.4.0 -> libopencv_core.so.4.0.0
-rw-r--r-- 1 kdr2 kdr2 4519888 Nov 20 12:34 libopencv_core.so.4.0.0
# ...
lrwxrwxrwx 1 kdr2 kdr2 24 Nov 20 13:28 libopencv_imgproc.so -> libopencv_imgproc.so.4.0
lrwxrwxrwx 1 kdr2 kdr2 26 Nov 20 13:28 libopencv_imgproc.so.4.0 -> libopencv_imgproc.so.4.0.0
-rw-r--r-- 1 kdr2 kdr2 4714608 Nov 20 12:37 libopencv_imgproc.so.4.0.0
# ... output truncated
OpenCV is a modular library. It consists of two types of modules—the main modules and the extra modules.
The main modules are included in OpenCV by default when we build it from source, and they contain all of the core OpenCV functionality, along with the modules that are used for image processing tasks, filtering, transformation, and many more capabilities.
The extra modules include all of the OpenCV functionalities that are not included in the OpenCV library by default, and they mostly include additional computer vision-related functionalities.
If we look back at the contents of the lib directory under the OpenCV install path when we checked whether our OpenCV was installed correctly, we will find many files named in the pattern of libopencv_*.so*. Typically, each of these files corresponds to an OpenCV module. For instance, the libopencv_imgproc.so file is the imgproc module, which is used for image processing tasks.
Now that we have the OpenCV library installed, it's time to include it in our Qt project. Let's open our Qt project file, ImageEditor.pro, and add the following lines to it:
unix: !mac {
INCLUDEPATH += /home/kdr2/programs/opencv/include/opencv4
LIBS += -L/home/kdr2/programs/opencv/lib -lopencv_core -l opencv_imgproc
}
The unix: !mac directive means to use the configuration in the brackets next to it on any UNIX-like system except macOS. I am using this directive because I am using Debian GNU/Linux. The directives inside the brackets are the crucial part of importing the OpenCV library in the following lines:
- The first line tells the compiler where the header files of OpenCV that we will use in our code lie by updating the value of INCLUDEPATH.
- The second line tells the linker which OpenCV modules (shared objects) our application should link against, and where to find them. More concretely, -lopencv_core -l opencv_imgproc means that we should link our application against libopencv_core.so and libopencv_imgproc.so, and -L... means that the linker should find these lib files (shared objects) under the /home/kdr2/programs/opencv/lib directory.
On macOS or Windows, OpenCV is built and linked in another way, but not in each library file of a module. In that situation, all the modules are linked to one library, called opencv_world. We can pass -DBUILD_opencv_world=on to CMake to achieve the same effect on Linux:
# on mac
$ ls -l
-rwxr-xr-x 1 cheftin staff 25454204 Dec 3 13:47 libopencv_world.4.0.0.dylib
lrwxr-xr-x 1 cheftin staff 27 Dec 3 13:36 libopencv_world.4.0.dylib -> libopencv_world.4.0.0.dylib
lrwxr-xr-x 1 cheftin staff 25 Dec 3 13:36 libopencv_world.dylib -> libopencv_world.4.0.dylib
# on Linux with -D BUILD_opencv_world=on
$ ls -l
lrwxrwxrwx 1 kdr2 kdr2 22 Nov 29 22:55 libopencv_world.so -> libopencv_world.so.4.0
lrwxrwxrwx 1 kdr2 kdr2 24 Nov 29 22:55 libopencv_world.so.4.0 -> libopencv_world.so.4.0.0
-rw-r--r-- 1 kdr2 kdr2 57295464 Nov 29 22:09 libopencv_world.so.4.0.0
Building OpenCV in this way simplifies the linker options when we compile our source—we don't need to give a module list to the linker as we did with -lopencv_core -lopencv_imgproc. Telling the linker to link against opencv_world is enough. For macOS and Windows, we can put the following code into ImageEditor.pro:
unix: mac {
INCLUDEPATH += /path/to/opencv/include/opencv4
LIBS += -L/path/to/opencv/lib -lopencv_world
}
win32 {
INCLUDEPATH += c:/path/to/opencv/include/opencv4
LIBS += -lc:/path/to/opencv/lib/opencv_world
}
Although this way is easier, this book still uses separate modules to let you get a deep insight into the OpenCV modules we are learning about and using.
Qmake provides you with another way of configuring the third-party library, that is, through pkg-config, which is a facility for maintaining the meta information of libraries. Unfortunately, according to https://github.com/opencv/opencv/issues/13154, OpenCV deprecated the support of pkg-config starting from version 4.0. This means that we need to use a more direct and flexible way to configure OpenCV in our Qt project rather than using the pkg-config way.