as the basis for our instructions
- Let's open the most recent release of opencv to the date of this video capturing: https://github.com/opencv/opencv/releases/tag/4.5.1
- Create a tmp folder for all archives:
mkdir ~/opencv4.5-tmp && cd ~/opencv4.5-tmp
- We need to download opencv sources:
wget https://github.com/opencv/opencv/archive/4.5.1.zip -O opencv.zip
- We need to download opencv-contrib sources:
wget https://github.com/opencv/opencv_contrib/archive/4.5.1.zip -O opencv_contrib.zip
- Unzip the opencv files:
unzip opencv.zip
- Unzip the opencv-contrib files:
unzip opencv_contrib.zip
- Move the files to simple directories:
mv opencv-4.5.1/ opencv
- Move opencv-contrib files to simple directories:
mv opencv_contrib-4.5.1/ opencv_contrib
- Make build directory:
cd opencv && mkdir build && cd build
- Copy and run the following command. Install cmake if it is not available on the 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 WITH_QT=OFF \
-D WITH_GTK=ON \
-D BUILD_EXAMPLES=ON ..
-
Make the project:
make -j4
-
Install opencv:
sudo make install
-
Ensure that it is updated in the library storage:
sudo ldconfig
-
Open your editor of choice (VSCode in my case)
-
Create a folder "~/projects/HelloOpenCV"
-
Paste the
main.cpp
code to themain.cpp
-
Firstly, let's try to compile our 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 path to the headers and linker flags. The best way to find them all in one place is to use the utility module pkg-config. 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
-
Add all flags to 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