The blogger needs to install opencv on the ubuntu20.04 system. He has installed opencv many times on 18.04 before, and he is still familiar with the computer vision open source library. This time, the installation records the detailed process, so that later students can avoid detours.
Installation Tutorial
1. Command line installation
sudo apt install libopencv-dev
Didn't expect that? Only one command line is needed to install opencv. It will automatically download and install the required library files. Here it shows that there are 149 required, and the lower right corner shows that it will take 2 hours and 11 minutes. Students who have plenty of time and are afraid of trouble can choose this method to install .
2. Source code installation
1. Download the source code
Click to enter the official website of opencv and download the Sources compressed package. Here I recommend version 3.4.5, which is safe and stable. Of course, it will not affect you if you choose other versions.
2. Install dependencies
sudo apt-get install build-essential libgtk2.0-dev libjpeg-dev libtiff5-dev libopenexr-dev libtbb-dev
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libgtk-3-dev libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev pkg-config
In fact, OpenCV has many dependencies, but we don't use all the functions, so the above dependencies are enough for normal scientific research projects.
3. cmake analysis
Unzip the source code. Here I unzip it to the home directory, then create a build folder, and perform cmake analysis. During the cmake phase, I will check whether the dependencies are installed and adjust my own functions.
cd opencv-3.4.5/
mkdir build
cd build
cmake ..
After the cmake check is complete, it will display that the configuration and generation have ended.
4, make compile
sudo make -j4
You can use -j8 to enable 8 threads for compilation (the parameter after -j is the number of threads used) to speed up compilation. This process is relatively long, and various warning prompts may appear in the middle, but as long as the final 100% is OK.
5. make install installation
sudo make install
The installation speed is very fast, and the installation can be completed for you in 5 seconds.
6. Configure environment variables
1. Modify the /etc/ld.so.conf file
sudo gedit /etc/ld.so.conf
Add a line to the file include /usr/local/lib
, /usr/local is the default installation path of opencv, which tells the system to find the opencv library file in the lib directory in the future.
Enter the command to make conf take effect:
sudo ldconfig
2. Modify the bash.bashrc file
sudo gedit /etc/bash.bashrc
Add at the end of the file:
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
export PKG_CONFIG_PATH
Then source makes bash effective:
source /etc/bash.bashrc
3. Verify the installation
Enter the command to view the opencv version information:
pkg-config opencv --modversion
The version is displayed, indicating that the installation is successful!