I’ve recently gained interest in OpenCV. The problem with this is, that in most modern tutorials and walkthroughs it seems like the go-to language for working with this C-library is Python. I have an unreasonable antipathy towards this snake of a language and although I intend to fight through it some day in the future, at the moment I have more important stuff to deal with, so I intend to experiment with OpenCV in my own way – with a blend of noobie C++ and QT. In this quick note I’ll document for my (and whomever happens to land on this blog…) benefit the process of building a recent version of OpenCV on Ubuntu 20.04 and what arcane line to insert into the dark lord of K-prefixed apps – QtCreator.
Building the OpenCV
Install dependencies (I’m including python, because I really want to overcome my irrational feelings)
sudo apt-get install build-essential cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libdc1394-22-dev
Clone the repositories containing latest release, create a build folder
git clone https://github.com/opencv/opencv.git && git clone https://github.com/opencv/opencv_contrib.git && cd ./opencv && mkdir build && cd ./build
Set all the flags you expect to need
cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr/local -DBUILD_DOCS=ON -DBUILD_EXAMPLES=ON -DOPENCV_EXTRA_MODULES_PATH="~/opencv_contrib/modules" -DPYTHON3_EXECUTABLE=/usr/bin/python3.8 -DPYTHON_INCLUDE_DIR=/usr/include/python3.8 -DPYTHON_INCLUDE_DIR2=/usr/include/x86_64-linux-gnu/python3.8 -DPYTHON_LIBRARY=/usr/lib/x86_64-linux-gnu/libpython3.8.so -DPYTHON3_NUMPY_INCLUDE_DIRS=/usr/lib/python3.8/dist-packages/numpy/core/include/ -DOPENCV_GENERATE_PKGCONFIG=ON -DBUILD_opencv_apps=ON ..
Let it compile for a few minutes – opencv is pretty large beast, so it may take a while
After it’s done (took a few minutes on a desktop with an 8C/16T Ryzen 7 2700X, closer to an hour on an ultrabook with a 2C/4T i5), install it.
sudo make install
Including it into a QtCreator .pro file
Just add these two lines and you’re good to go.
INCLUDEPATH += /usr/local/include/opencv4 LIBS += -L/usr/local/lib -lopencv_core -lopencv_highgui -lopencv_imgproc -lopencv_objdetect -lopencv_imgcodecs -lopencv_videoio
Now you’re all ready to go play with some nice OpenCV in your QtCreator.