OpenCV (Open Source Computer Vision Library) is a library of programming functions mainly for real-time computer vision.
But its installation can be very tricky in an environment like Linux, so let’s follow a correct installation process.
Download the sources
The most recent release at the time of writing is 4.5.1.
Create a tmp folder for all the archives:
mkdir ~/opencv4.5-tmp && cd ~/opencv4.5-tmp
Download the OpenCV sources and the opencv-contrib sources:
wget https://github.com/opencv/opencv/archive/4.5.1.zip -O opencv.zip
wget https://github.com/opencv/opencv_contrib/archive/4.5.1.zip -O opencv_contrib.zip
Unzip both archives:
unzip opencv.zip
unzip opencv_contrib.zip
Move the files to simpler directory names:
mv opencv-4.5.1/ opencv
mv opencv_contrib-4.5.1/ opencv_contrib
Build and install
Make a build directory:
cd opencv && mkdir build && cd build
Copy and run the following command (install cmake first if it is not available on your system):
cmake -D CMAKE_BUILD_TYPE=DEBUG \
-D CMAKE_INSTALL_PREFIX=~/opencv4.5-custom \
-D OPENCV_EXTRA_MODULES_PATH=~/opencv4.5-tmp/opencv_contrib/modules \
-D OPENCV_GENERATE_PKGCONFIG=ON \
-D BUILD_EXAMPLES=ON ..
Make the project:
make -j4
Install OpenCV:
sudo make install
Ensure it is updated in the library storage:
sudo ldconfig
Configure a C++ project to work with OpenCV
Open your editor of choice (vim in my case), create a folder ~/projects/HelloOpenCV, and put your code in main.cpp.
First, let’s try to compile the application with g++ as usual:
g++ -Wall -o main main.cpp
We see that it cannot find our library headers.
For that we need to provide the path to the headers and the linker flags. The best way to find them all in one place is the pkg-config utility. Remember we provided an additional argument to our cmake generation? So let’s execute the following:
export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/home/parallels/opencv4.5-custom/lib/pkgconfig
pkg-config --cflags --libs opencv4
Now add all the flags to the compilation command:
g++ -Wall -o main main.cpp $(pkg-config --cflags --libs opencv4)
Or a more explicit version for our particular sample:
g++ -Wall -o main main.cpp \
-I/home/parallels/opencv4.5-custom/include/opencv4 \
-L/home/parallels/opencv4.5-custom/lib \
-lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_core