OpenCV environment construction

Today, I'm going to build an OpenCV environment on my notebook Ubuntu 20.04. The recording process is as follows.

1. Start

1.1 installing dependent Libraries

# Install, compile, download and other tools
# Build essential is used to download gcc, g + + and other compilation tool chains
sudo apt-get install build-essential cmake git pkg-config wget unzip
# Install dependency libraries. Many dependency libraries have been found on the Internet. I don't know the specific role. Download new ones when I see them
# opencv's highgui is based on GTK2 0
sudo apt-get install  libgtk2.0-dev libavcodec-dev libavformat-dev libjpeg-dev libtiff5-dev libswscale-dev libavresample-dev
sudo apt-get install libtbb2 libtbb-dev
sudo apt-get install -y zlib1g-dev libwebp-dev libpng-dev libopenexr-dev libgdal-dev
sudo apt-get install -y libdc1394-22-dev libavcodec-dev libavformat-dev libswscale-dev libtheora-dev libvorbis-dev libxvidcore-dev libx264-dev yasm libopencore-amrnb-dev libopencore-amrwb-dev libv4l-dev libxine2-dev
sudo apt-get install -y libtbb-dev libeigen3-dev
sudo apt-get install -y python3-dev python3-tk python3-numpy
sudo apt-get install ffmpeg libxvidcore-dev libx264-dev libatlas-base-dev gfortran
# Installation is required to compile the document
sudo apt install texlive doxygen
# gflags Google's command line parsing tool
sudo apt install *gflags*
sudo apt install *glog*
# tesseract is an OCR related library
sudo apt install *tesseract*
# VTK is an image library. It is installed near 1GB of controls. It should not be used. Install it first
sudo apt install *vtk*
# Looking at the log, I found that there is a bs4 module. Here, use pip to install it
sudo apt install python3-pip
pip install bs4
# It is a video stream related library, * gstreamer * will fail to install and conflict
sudo sudo apt-get install libgstreamer*
# When installing 'libjasper dev', the prompt cannot be located. Processing method:
sudo add-apt-repository "deb http://security.ubuntu.com/ubuntu xenial-security main"
sudo apt update
sudo apt upgrade
sudo apt install libjasper1 libjasper-dev

1.2 compilation

# Download decompression source code
wget -O opencv.zip https://github.com/opencv/opencv/archive/master.zip
wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/master.zip
unzip -q opencv.zip
unzip -q opencv_contrib.zip
mkdir build; cd build
# In a few minutes, the process will connect to the Internet to download some files. The download may fail, which will not affect the success of the configuration
# If the download fails, you can download it first and then copy it. Refer to [2.2 download files during configuration]
# Compile and install, enable QT support, enable py3 support, set the installation path to / opt/opencv, and generate opencv4 PC files, compiling third-party modules
cmake \
-D BUILD_DOCS=ON \
-D BUILD_opencv_python3=yes \
-D BUILD_opencv_python2=no \
-D PYTHON3_EXECUTABLE=/usr/bin/python3.8 \
-D PYTHON3_INCLUDE_DIR=/usr/include/python3.8 \
-D PYTHON3_LIBRARY=/usr/lib/x86_64-linux-gnu/libpython3.8.so.1.0 \
-D PYTHON3_NUMPY_INCLUDE_DIRS=/usr/lib/python3/dist-packages/numpy/core/include \
-D PYTHON3_PACKAGES_PATH=/usr/lib/python3/dist-packages \
-D PYTHON_DEFAULT_EXECUTABLE=/usr/bin/python3 \
-D WITH_QT=ON \
-D Qt5_DIR=/opt/Qt5.14.2/5.14.2/gcc_64/lib/Qt5 \
-D WITH_OPENGL=ON \
-D CMAKE_INSTALL_PREFIX=/opt/opencv \
-D OPENCV_GENERATE_PKGCONFIG=ON \
-D OPENCV_EXTRA_MODULES_PATH=../opencv_contrib-master/modules \
../opencv-master
# 20 minutes for the pure system and 30 minutes for the virtual machine. Do not delete the compilation folder after installation. When you open new features in the future, you can add compilation in this folder, which will be faster
make -j8 > log.txt 2> log_err.txt

1.3 installation

# 1 minute, I installed it in / opt. At present, this directory is root, so sudo installation is required
sudo make install
# Add dynamic library to system environment, PKG_CONFIG_PATH will be used by PKG config to provide opencv information at compile time
sudo bash -c 'echo /opt/opencv/lib > /etc/ld.so.conf.d/opencv.conf'
sudo ldconfig
echo "export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/opt/opencv/lib/pkgconfig" >> ~/.bashrc
. ~/.bashrc

2. Episode

2.1 generate pkgconfig file

At the beginning, I didn't know that opencv4 compilation would not generate the pc file used by PKG config. After compilation and installation, I used the following steps to remedy it and added - dopencv during configuration_ GENERATE_ Pkgconfig = on parameter. Because the compiled temporary files are all there, make -j8 soon ends and is generated smoothly/ unix-install/opencv4.pc, sudo make install. reference resources Compile and install opencv under linux to generate OpenCV pc.

2.2 downloading files during configuration

Later, it was found that during configuration, cmake would download some files from the Internet and visit https://raw.githubusercontent.com , the domain name is inaccessible. I learned that the / etc/hosts file needs to be modified. After modification, only some files are successfully downloaded. Finally, I learned that it can be downloaded through Xunlei offline download. Baidu search Thunderbolt offline web version ,CMakeDownloadLog.txt file has the address of the file to be downloaded. Fill in the address in Xunlei for seconds, and then copy it to In the cache directory, change the name to check value - file name. When the configuration is executed again, the files to be downloaded are ready. reference resources raw.githubusercontent cannot be accessed,OpenCV compiling CUDA module_ landmark_ model. Solutions to dat download failure.

2.3 Qt5 not detected

Because my Qt5 is installed by myself, although - dwith is specified_ QT = on, but the configuration result shows that QT is not found. Check cmakecache Txt found because Qt5_DIR is not defined, so - dqt5 is added_ DIR=/opt/Qt5. 14.2/5.14. 2/gcc_ 64 / lib / Qt5 is enough. reference resources CMake Qt5 when configuring VTK_ Solution of dir-notfound.

2.4 QT does not support OpenGL

Qt5 detected, but the display does not support OpenGL, found cmakelist Txt WITH_OPENGL option, add - DWITH_OPENGL is enough.

3. Operation example

reference resources Read video, read camera.

3.1 c++

main.cpp

#include "opencv2/opencv.hpp"

int main()
{
	cv::VideoCapture cap;
	cv::Mat frame0, frame1;
	uchar c;

	cap.open(0); // Turn on the camera
	if(!cap.isOpened())
		return -1;

	while(1)
	{
		cap.read(frame0); // Read one frame of image
		if(frame0.empty())
			continue;
		cv::Canny(frame0, frame1, 30, 100); // Edge detection processing
		cv::imshow("Camera", frame1); // display frame
		c = cv::waitKey(20); // Delay 20ms and wait for the key to be pressed
		if(c != 0xFF) // If a key is pressed, exit
			break;
	}

	cap.release();
	cv::destroyAllWindows();

	return 0;
}

Compile run

# PKG config reads opencv4 PC gets the header file path of opencv4 and all dynamic libraries
g++ main.cpp `pkg-config opencv4 --cflags --libs`
./a.out

3.2 qt

To use opencv in qmake, you need to add the header file and Library of OpenCV to the project as follows. reference resources Use PKG config output to link libraries to QT projects .

Modify the pro file and add the following two lines

CONFIG += link_pkgconfig
PKGCONFIG += opencv4

4. Reference

linux opencv Library folder, linux Installation and configuration of c + + opencv Library
Ubuntu16.04 install opencv3 four point two
The solution to the failure to locate the package libjasper dev
Installation in Linux
Installation in Linux
Compile and install opencv under linux to generate OpenCV pc
Read video, read camera
Use PKG config output to link libraries to QT projects
Install opencv from the source code and use Python 3 to generate the interface of O penCV python (CV2)
Installing and using Gstreamer under Linux
raw.githubusercontent cannot be accessed
OpenCV compiling CUDA module_ landmark_ model. Solutions to dat download failure
Installation and use of Python BS4 Library
CMake Qt5 when configuring VTK_ Solution of dir-notfound

Keywords: Linux OpenCV

Added by Ghulam Yaseen on Mon, 27 Dec 2021 11:16:10 +0200