Install OpenCV on Ubuntu 20.04

1. Install OpenCV from the Ubuntu source repository

OpenCV is available in the Ubuntu 20.04 software repositories. To install it, run:

sudo apt update
sudo apt install libopencv-dev python3-opencv

The above command will install all necessary packages to run OpenCV:

Verify the installation by importing cv2the module and printing the OpenCV version:

python3 -c "import cv2; print(cv2.__version__)"

output:

4.2.0

2. Install OpenCV from source

1. Install build tools and all dependent packages:

sudo apt install build-essential cmake git pkg-config libgtk-3-dev \
    libavcodec-dev libavformat-dev libswscale-dev libv4l-dev \
    libxvidcore-dev libx264-dev libjpeg-dev libpng-dev libtiff-dev \
    gfortran openexr libatlas-base-dev python3-dev python3-numpy \
    libtbb2 libtbb-dev libdc1394-22-dev libopenexr-dev \
    libgstreamer-plugins-base1.0-dev libgstreamer1.0-dev

2. Clone all OpenCV and OpenCV contrib sources

mkdir ~/opencv_build && cd ~/opencv_build
git clone https://github.com/opencv/opencv.git
git clone https://github.com/opencv/opencv_contrib.git

3. After the download is complete, create a temporary build directory and switch to this directory

cd ~/opencv_build/opencv
mkdir -p build && cd build

4. Use the CMake command to configure the OpenCV build

cmake -D CMAKE_BUILD_TYPE=RELEASE \
    -D CMAKE_INSTALL_PREFIX=/usr/local \
    -D INSTALL_C_EXAMPLES=ON \
    -D INSTALL_PYTHON_EXAMPLES=ON \
    -D OPENCV_GENERATE_PKGCONFIG=ON \
    -D OPENCV_EXTRA_MODULES_PATH=~/opencv_build/opencv_contrib/modules \
    -D BUILD_EXAMPLES=ON ..

The output will be as follows:

-- Configuring done
-- Generating done
-- Build files have been written to: /home/vagrant/opencv_build/opencv/build

5. Start the compilation process

make -j8
# 8是处理器核心个数,你可以输入nproc找到

6. Install OpenCV

sudo make install

output:

4.6.0

7. Verify

pkg-config --modversion opencv4

output

4.6.0

3. Points to note when you choose to install OpenCV4.2.0 lower than 4.6.0

Copy the contents of the i folder to /opencv_build/opencv_contrib-/modules/xfeatures2d/src/

Copy the contents of the data folder to /opencv_build/opencv/.cache/data/

make -j8 error

solve:

Copy the three files above to /opencv_build/opencv_contrib/modules/xfeatures2d/test, and modify test_features2d.cpp and test_rotation_and_scale_invariance.cpp in the /opencv_build/opencv_contrib/modules/xfeatures2d/test directory (both files must be changed) header file

recompile 

Link: https://download.csdn.net/download/qq_58060770/86838394

Guess you like

Origin blog.csdn.net/qq_58060770/article/details/127553911