Installing OpenCv on Ubuntu18.04 system
Article directory
Preface
Install OpenCv on Ubuntu18.04 system.
1. Installation environment dependencies
Open a new terminal and enter the following instructions in order to configure OpenCv environment dependencies.
sudo apt-get install build-essential
sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev
注:安装libdc1394-22-dev时会出现报错(无法定位 libjasper-dev),打开新终端并依次输入以下命令即可解决该问题。
sudo add-apt-repository "deb http://security.ubuntu.com/ubuntu xenial-security main"
sudo apt update
sudo apt install libjasper1 libjasper-dev
2. Download OpenCv source code
Open the following URL, select the required OpenCv version, download the Sources format file, decompress it after the download is complete, and place the decompressed file in the system "home directory".
https://opencv.org/releases/
3. Install OpenCv
Create a build folder in the decompressed OpenCv folder, enter the build folder and open a new terminal under the folder, enter the following instructions to compile and install.
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_GTK=ON -D OPENCV_GENERATE_PKGCONFIG=YES ..
sudo make -j8 //此处采用8个CPU核心进行编译
sudo make install
sudo gedit /etc/bash.bashrc
注:
1. CMAKE_INSTALL_PREFIX 是opencv的安装地址 默认安装在 usr/local’
2. CMAKE_BUILD_TYPE 是opencv安装的版本,Release和Debug两种可选,默认安装Release
3. OPENCV_ENABLE_NONFREE 是否使用部分被申请了专利的算方法 这里选True的话就可以使用了
4. OPENCV_GENERATE_PKGCONFIG 强烈建议开启这个 设置为ON OPENCV_GENERATE_PKGCONFIG 因为opencv4默认不生成.pc文件,所以加上这句用于生成opencv4.pc文件,支持pkg-config功能。opencv4版本及以上 这里用ON。
Add the following content at the end of the file opened in the above steps and save it to update the system environment variables.
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
export PKG_CONFIG_PATH
Activate the newly added environment variable.
sudo updatedb
source /etc/bash.bashrc
Open the Opencv configuration file and add the lib path at the end of the file, save and exit.
sudo gedit /etc/ld.so.conf.d/opencv.conf //打开配置文件
renew
sudo ldconfig
4. Check whether the installation is successful
Enter in the terminal:
mkdir build
cd ./build
cmake ..
make
./opencv_example
If it opens normally, the installation is successful!