Requirements

  • Ubuntu 18.04
  • Qt 5.12.0 LTS
  • OpenCV-4.0.0 or newer

Install OpenCV 4.0 from Native Building

I installed OpenCV-4.0.1, also with OpenCV’s extra modules (opencv_contrib). It may take ~half hour.

sudo apt-get install build-essential cmake unzip wget pkg-config
sudo apt-get install libjpeg-dev libpng-dev libtiff-dev
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
sudo apt-get install libxvidcore-dev libx264-dev
sudo apt-get install libgtk-3-dev
sudo apt-get install libatlas-base-dev gfortran
sudo apt-get install python3-dev
sudo apt install x264 libx264-dev ffmpeg
sudo apt-get install v4l-utils libtbb-dev
sudo apt-get install libvorbis-dev libxvidcore-dev

cd ~
wget -O opencv.zip https://github.com/Itseez/opencv/archive/4.0.1.zip
unzip opencv.zip

wget -O opencv_contrib.zip https://github.com/Itseez/opencv_contrib/archive/4.0.1.zip
unzip opencv_contrib.zip

cd opencv
mkdir build && cd build

cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D BUILD_opencv_python2=OFF -D BUILD_opencv_python3=OFF -D INSTALL_PYTHON_EXAMPLES=OFF -DBUILD_opencv_java=OFF -D INSTALL_C_EXAMPLES=ON -D WITH_OPENGL=ON -D OPENCV_EXTRA_MODULES_PATH=$HOME/opencv_contrib/modules -D -BUILD_EXAMPLES=ON ..

make -j6
sudo make install
sudo ldconfig

Configuration

Add the following code to the .pro file in Qt project.

INCLUDEPATH += /usr/local/include/opencv4
LIBS += -L/usr/local/lib -lopencv_core -lopencv_imgproc -lopencv_imgcodecs -lopencv_videoio -lopencv_highgui

For common case using, include the following header to the header flile:

#include "opencv2/opencv.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/core/core.hpp"

using namespace cv;

Now you can use OpenCV as usual in Qt.

–END–