1. Install opencv for the entire system as root
This section refers to this article: https://zhuanlan.zhihu.com/p/118222087
Download dependencies
First download the dependencies required for opencv installation. If a dependency file is missing, the compilation process will not go wrong, but when using the opencv function, an error will be prompted and you can only recompile and install.
Create a new shell script to install dependency files in batches. The script is named download.sh:
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
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev liblapacke-dev
sudo apt-get install libxvidcore-dev libx264-dev
sudo apt-get install libatlas-base-dev gfortran
sudo apt-get install ffmpeg
Modify the permissions of download.sh:
sudo chmod 777 ./download.sh
Execute the script to install dependency files:
./download.sh
Note:
If you are installing from the command line on Ubuntu and viewing this blog using Windows, then you will not be able to copy the above dependency file contents to Ubuntu. You may create a new download.sh file in Windows. Modify the content of the script file and upload it to the Linux server for execution. However, this will cause an error when the sh file is run and bad interpreter: No such file or directory
an error will be reported.
This is because the saving format of text files under Windows is different from that under Unix. The carriage return character in Windows is '/r/n', but in Linux it is '/n'.
Therefore, you can take the following approach:
use mobaxterm and other software to connect to the server, use vim or the editor that comes with mobaxterm to edit the script file, do not use the windows editor.
Download the source code of opencv and opencv_contrib
The download address of opencv 4.2.0: https://github.com/opencv/opencv/archive/refs/tags/4.2.0.tar.gz
The download address of opencv contrib 4.2.0: https://github.com/ opencv/opencv_contrib/archive/refs/tags/4.2.0.tar.gz
If you need other versions of opencv, you can directly download the corresponding version from releases/tags under github. The link is as follows:
opencv: https://github.com/opencv/opencv/releases
opencv_contrib: https://github.com/opencv/opencv_contrib/tags
Install opencv
Directly decompress the two tar.gz files you just downloaded and go to a specific directory:
tar -zxvf opencv-4.2.0.tar.gz
tar -zxvf opencv_contirb-4.2.0.tar.gz
After decompression, you get two folders: opencv-4.2.0 and opencv_contirb-4.2.0.
Move the decompressed opencv_contrib-4.2.0 folder as a whole to the opencv-4.2.0 directory:
sudo cp -r opencv_contrib-4.2.0 opencv-4.2.0
Create a new build folder under opencv-4.2.0 and enter:
cd opencv-4.2.0
sudo mkdir build
cd build
Create a new shell script under build and execute the build command:
vim cmake_build.sh
The content of the script cmake_build.sh is as follows:
Just replace OPENCV_EXTRA_MODULES_PATH
the one in the file your_path
with the path where you store opencv.
#!/bin/bash
sudo cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local -D OPENCV_EXTRA_MODULES_PATH=/your_path/opencv-4.2.0/opencv_contrib-4.2.0/modules/ ..
Modify permissions and execute:
sudo chmod 777 ./cmake_build.sh
./cmake_build_sh
During the build process, the required files will be downloaded, but due to network reasons, some resource connections will time out and some files cannot be downloaded. A prompt will be displayed on the command line. If you try to build multiple times but fail to download successfully, directly Manually download the missing files prompted in the command line and copy them to the corresponding path.
For example, if the download of boostdesc_binboost_256.i times out, search for resources directly on Baidu, download the file locally, and upload it to the /your_path/opencv-4.2.0/opencv_contrib-4.2.0/modules/xfeatures2d/src/
path.
I would like to recommend that you go to this big guy that I refer to. He uploaded the files that need to be downloaded in the build to github. You can directly find the corresponding files to download, and give the big guy a star by the way.
.i files often missing when compiling OpenCv
If you can't find it here, just search it directly on Baidu. After uploading the missing files to the corresponding location, you can execute the cmake_build.sh script again.
Then use the make command in the build directory to compile:
sudo make -j32
sudo make install
-j32 means that make allows up to 32 compilation commands to be executed simultaneously, which can make more efficient use of CPU resources.
Use the following command to view the CPU information of the Linux system and determine the number after the -j parameter. If your system has enough cores, set this parameter as large as possible, otherwise it will take a long time to compile:
# 查看CPU信息
cat /proc/cpuinfo
# 查看物理CPU的个数
cat /proc/cpuinfo |grep "physical id"|sort |uniq|wc -l
# 查看逻辑CPU的个数
cat /proc/cpuinfo |grep "processor"|wc -l
# 查看CPU是几核的
cat /proc/cpuinfo |grep "cores"|uniq
After the installation is complete, use vim to create or modify files opencv.conf
:
sudo vim/etc/ld.so.conf.d/opencv.conf
Add the following content at the end of the file:
/usr/local/lib
Then execute the command to make the configuration take effect:
sudo ldconfig
Then modify bash/bashrc
the file:
sudo vim /etc/bash.bashrc
At the end of the file add:
export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
After saving the file, execute the following command:
source /etc/bash.bashrc
sudo updatedb
At this point the opencv configuration is completed.
Use the following command to check the version of opencv and verify whether the installation is successful.
pkg-config --modversion opencv4
2. Install opencv as a non-root user
Many times, when we are on a public server and do not have administrator rights, or we are afraid of damaging the server, we can install opencv only under the current user.
Download dependencies
The method of downloading dependencies when installing opencv using root method is the same as above, but the apt-get command is used here, which requires root permissions.
If you have administrator rights, you can use the administrator identity to install some dependent packages on the server, and then install opencv on an ordinary user.
If not, you can try installing opencv first to see if there are any problems.
Download the source code of opencv
opencv source code download address: https://opencv.org/releases/
Still download version 4.2.0:
Install
As above, unzip the downloaded source code:
tar -zxvf opencv-4.2.0.tar.gz
In the opencv-4.2.0 directory, create the build directory:
cd opencv-4.2.0
mkdir build
In the build directory, create cmake_build.sh
a script file:
cd build
vim cmake_build.sh
The content of the script is as follows. You only need to change the opencv installation path CMAKE_INSTALL_PREFIX
and opencv modules storage path OPENCV_EXTRA_MODULES_PATH
to your_path
your own opencv storage path:
#!/bin/bash
sudo cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/your_path/opencv-4.2.0 -D WITH_TBB=ON -D WITH_V4L=ON -D BUILD_TIFF=ON -D BUILD_EXAMPLES=ON -D WITH_OPENGL=ON -D WITH_EIGEN=ON -D WITH_CUDA=OFF -D WITH_CUBLAS=ON -D OPENCV_GENERATE_PKGCONFIG=ON -D OPENCV_EXTRA_MODULES_PATH=/your_path/opencv-4.2.0/opencv_contrib-4.2.0/modules/ ..
OPENCV_GENERATE_PKGCONFIG=ON
Control whether pkg_config is generated. If this command is not added in opencv4, pkgconfig will not be generated, which will result in the opencv file not being found after installation.
After compilation, use the following command to install:
make -j32
make install
After installation, add environment variables, /home/your_username/.bashrc
add the following text in , and replace your_path with the path where you store opencv:
export PKG_CONFIG_PATH=/your_path/opencv4.0.1/lib/pkgconfig
export LD_LIBRARY_PATH=/your_path/opencv4.0.1/lib
After adding:
source ~/.bashrc
Verify whether the installation is successful and check the version of opencv:
pkg-config --modversion opencv4
reference:
Root installation:
https://zhuanlan.zhihu.com/p/118222087
https://blog.csdn.net/m0_37797953/article/details/82013509
https://blog.csdn.net/Wangguang_/article/details/85762705
Non-root installation:
https://blog.csdn.net/qq_19655645/article/details/96132274
opencv resources:
https://opencv.org/releases/page/2/
https://github.com/Linfeng-Lee/OpenCV_boostdesc_vgg_file
https://gitee.com/zhu_hj/opencv
https://sourceforge.net/projects /opencvlibrary/files/
Other references:
https://blog.csdn.net/flyconley/article/details/104441691
https://www.cnblogs.com/xd502djj/archive/2011/02/28/1967350.html
Error troubleshooting:
https://blog.csdn.net/gggxin/article/details/3876743