Ubuntu 18.04.5 LTS source code to compile and install OpenCV 4.5.1

Hits: 0

When using TensorRT to accelerate [yolov5] , it appears during make. fatal error: opencv2/dnn/dnn.hpp: No such file or directoryIt seems that there is a problem with the [opencv] dnn module, so I reinstalled and installed the reference tutorial .

1. Install dependencies

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

(Although these are installed, various NOT FOUNDs may appear later!!!)

2. Clone OpenCV and OpenCV contrib repository source code (note that the two versions should be consistent)

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

If it is a zip file downloaded directly, the opencv_contrib [source code] will remove the version number in the decompressed folder, otherwise the opencv_contrib folder will not be found when opencv is compiled later.

3. Create a temporary build directory

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

CMake settings

    -D CMAKE_INSTALL_PREFIX=/usr/local \
    -D OPENCV_EXTRA_MODULES_PATH=~/opencv_build/opencv_contrib/modules \

After the end you can see the following output

If there are various NOT FOUNDs in the process, you can refer to this blog post to install the packages that cannot be found, or directly find what needs to be installed on Baidu/Google

Could not find OpenBLAS [resolve reference]

4. Start compiling

make -j48

Change the following numbers according to your own processor -j. If you don’t know, you can use the command to nprocview

If the following error occurs during compilation

It is recommended that CMake add a parameter at the end -D WITH_FFMPEG=OFF, which may be caused by the problem that I installed ffmpeg before

Compiled successfully

5. Installation

sudo make install

6. Verify

pkg-config --modversion opencv4  # output 4.5.1
python3 -c "import cv2; print(cv2.__version__)" #output 4.5.1

You may also like...

Leave a Reply

Your email address will not be published.