当前位置:   article > 正文

亲测:Ubuntu 20.04上安装OpenCV 5.x with CUDA_ubuntu20 opencv cudacodec::videoreader

ubuntu20 opencv cudacodec::videoreader

一、安装依赖库

首先安装更新和升级你的系统:

sudo apt update
sudo apt upgrade
  • 1
  • 2

然后,安装所需的库:

通用工具:

sudo apt install build-essential cmake pkg-config unzip yasm git checkinstall
  • 1

图像 IO 库:

sudo apt install libjpeg-dev libpng-dev libtiff-dev
  • 1

视频/音频库 - FFMPEG、GSTREAMER、x264 等:

sudo apt install libavcodec-dev libavformat-dev libswscale-dev libavresample-dev
sudo apt install libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev
sudo apt install libxvidcore-dev x264 libx264-dev libfaac-dev libmp3lame-dev libtheora-dev 
sudo apt install libfaac-dev libmp3lame-dev libvorbis-dev
  • 1
  • 2
  • 3
  • 4

OpenCore - 自适应多速率窄带 (AMRNB) 和宽带 (AMRWB) 语音编解码器:

sudo apt install libopencore-amrnb-dev libopencore-amrwb-dev
  • 1

相机编程接口库:

sudo apt-get install libdc1394-22 libdc1394-22-dev libxine2-dev libv4l-dev v4l-utils
cd /usr/include/linux
sudo ln -s -f ../libv4l1-videodev.h videodev.h
cd ~
  • 1
  • 2
  • 3
  • 4

来自 OpenCV highghui 模块的图形用户功能的 GTK 库:

sudo apt install libgtk-3-dev
  • 1

python3的Python库:

python3-pip
sudo -H pip3 install -U pip numpy
sudo apt install python3-testresources
  • 1
  • 2
  • 3

用于 CPU 的并行库 C++:

sudo apt install libtbb-dev
  • 1

OpenCV 的优化库:

sudo apt install libatlas-base-dev gfortran
  • 1

可选库:

sudo apt install libprotobuf-dev protobuf-compiler
sudo apt install libgoogle-glog-dev libgflags-dev
sudo apt install libgphoto2-dev libeigen3-dev libhdf5-dev doxygen
  • 1
  • 2
  • 3

二、安装OpenCV

在Ubuntu上编译安装 opencv3,官方的下载及编译方法:

# Install minimal prerequisites (Ubuntu 18.04 as reference)
sudo apt update && sudo apt install -y cmake g++ wget unzip

cd ~
mkdir opencv && cd opencv

# Download and unpack sources
wget -O opencv.zip https://github.com/opencv/opencv/archive/5.x.zip
wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/5.x.zip
unzip opencv.zip
unzip opencv_contrib.zip

# Create build directory and switch into it
mkdir -p build && cd build

# Configure
cmake -DOPENCV_EXTRA_MODULES_PATH=../opencv_contrib-5.x/modules ../opencv-5.x
# Build
cmake --build .
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

为了添加CUDA及GPU,作者采用的配置命令,如下:

cmake \
-D CMAKE_INSTALL_PREFIX=/home/angie/Programs/OpenCV/install \
-D CMAKE_BUILD_TYPE=RELEASE \
-D WITH_CUDA=ON \   #使用CUDA
-D WITH_CUDNN=ON \
-D OPENCV_DNN_CUDA=ON \
-D CUDA_ARCH_BIN=8.6 \  #指定GPU算力,在NVIDIA官网查询,也可在jtop中查询
-D CUDA_GENERATION=Pascal \
-D WITH_CUBLAS=1 \
-D CUDA_FAST_MATH=1 \
-D WITH_V4L=ON \
-D WITH_QT=ON \   #使用QT支持
-D WITH_OPENGL=ON \
-D WITH_TBB=ON \
-D WITH_GTK=ON \
-D WITH_FFMPEG=1 \
-D ENABLE_FAST_MATH=1 \
-D BUILD_opencv_python2=OFF \
-D BUILD_opencv_python3=ON \
-D BUILD_opencv_java=OFF \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D INSTALL_C_EXAMPLES=ON \
-D BUILD_EXAMPLES=OFF \
-DOPENCV_GENERATE_PKGCONFIG=ON \  #用于生成opencv.pc文件,支持pkg-config功能,没有这一项的话需要自己手动生成
-DOPENCV_EXTRA_MODULES_PATH=../opencv_contrib-5.x/modules \
../opencv-5.x 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

实际命令如下:

cmake -DCMAKE_INSTALL_PREFIX=/home/angie/Programs/OpenCV/install -DCMAKE_BUILD_TYPE=RELEASE -DWITH_CUDA=ON -DWITH_CUDNN=ON -DOPENCV_DNN_CUDA=ON -DCUDA_ARCH_BIN=8.6 -DWITH_CUBLAS=1 -DCUDA_FAST_MATH=1 -DWITH_V4L=ON -DWITH_QT=ON -DWITH_OPENGL=ON -DWITH_TBB=ON -DWITH_GTK=ON -DWITH_FFMPEG=1 -DENABLE_FAST_MATH=1 -DBUILD_opencv_python2=OFF -DBUILD_opencv_python3=ON -DINSTALL_PYTHON_EXAMPLES=ON -DINSTALL_C_EXAMPLES=ON -DOPENCV_GENERATE_PKGCONFIG=ON -DOPENCV_EXTRA_MODULES_PATH=../opencv_contrib-5.x/modules ../opencv-5.x 
  • 1

由于OpenCV 5.X支持使用CUDA对DNN模块进行加速计算,所以这里配置CUDA;在此之前需要自行配置好NVIDIA显卡的驱动与CUDA;

可修改以下语句进行配置,参数为电脑显卡的计算能力.

 CUDA_ARCH_BIN='8.6'   #对应GTX 3070显卡
  • 1

计算能力对应表:
CUDA GPUs | NVIDIA Developer
https://developer.nvidia.com/cuda-gpus

配置好之后,采用如下命令进行编译并安装到指定目录:

# Build && Install
cmake --build . --target install
  • 1
  • 2

等待很长时间的编译之后,在目录“/home/angie/Programs/OpenCV/install”可以找到安装的opencv。

三、环境配置

环境配置过程:
1)添加库路径

打开文件进行编辑:

sudo gedit /etc/ld.so.conf.d/opencv.conf
  • 1

输入下面的内容,然后保存并退出

/home/angie/Programs/OpenCV/install/lib
  • 1

在这里插入图片描述
并输入命令:

sudo ldconfig
  • 1

2)添加环境变量
打开文件进行编辑:

sudo vim /etc/profile
  • 1

在末尾加入:

export PKG_CONFIG_PATH=/home/angie/Programs/OpenCV/install/lib/pkgconfig:$PKG_CONFIG_PATH
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/angie/Programs/OpenCV/install/lib
export OpenCV_DIR=/home/angie/Programs/OpenCV/install/lib/cmake/opencv5
  • 1
  • 2
  • 3

在这里插入图片描述
3)更新环境变量

source /etc/profile
  • 1

4)重启,然后输入命令

pkg-config --modversion opencv           # 查看linux下的opencv安装版本
  • 1

在这里插入图片描述

注意:前文编译选项“-DOPENCV_GENERATE_PKGCONFIG=ON” ,该选项可以自动生成opencv5.pc文件,支持pkg-config功能。
在这里插入图片描述

没有这一项的话需要自己手动生成,首先直接输入命令:

pkg-config --modversion opencv5
  • 1

如果显示版本号则说明配置是正确的。

如果报了如下错误:

Package opencv was not found in the pkg-config search path.
Perhaps you should add the directory containing `opencv.pc'
to the PKG_CONFIG_PATH environment variable
No package 'opencv' found
  • 1
  • 2
  • 3
  • 4

说明缺失了opencv.pc这个配置信息文件,解决方法就是添加这个文件然后将其导入到环境变量中。具体操作如下:
1)首先创建opencv5.pc文件(需要注意它的路径信息)

cd /home/angie/Programs/OpenCV/install
sudo mkdir pkgconfig
cd pkgconfig
sudo touch opencv5.pc
  • 1
  • 2
  • 3
  • 4

2)然后在opencv5.pc中添加以下信息
注意:这些信息需要与自己安装opencv时的库路径对应

# Package Information for pkg-config

prefix=/home/angie/Programs/OpenCV/install
exec_prefix=${prefix}
libdir=${exec_prefix}/lib
includedir=${prefix}/include/opencv5

Name: OpenCV
Description: Open Source Computer Vision Library
Version: 5.0.0
Libs: -L${exec_prefix}/lib -lopencv_gapi -lopencv_stitching -lopencv_alphamat -lopencv_aruco -lopencv_barcode -lopencv_bgsegm -lopencv_bioinspired -lopencv_ccalib -lopencv_cudabgsegm -lopencv_cudafeatures2d -lopencv_cudaobjdetect -lopencv_cudastereo -lopencv_dnn_objdetect -lopencv_dnn_superres -lopencv_dpm -lopencv_face -lopencv_freetype -lopencv_fuzzy -lopencv_hdf -lopencv_hfs -lopencv_img_hash -lopencv_intensity_transform -lopencv_line_descriptor -lopencv_mcc -lopencv_quality -lopencv_rapid -lopencv_reg -lopencv_rgbd -lopencv_saliency -lopencv_sfm -lopencv_structured_light -lopencv_phase_unwrapping -lopencv_superres -lopencv_cudacodec -lopencv_surface_matching -lopencv_videostab -lopencv_cudaoptflow -lopencv_optflow -lopencv_cudalegacy -lopencv_calib -lopencv_cudawarping -lopencv_wechat_qrcode -lopencv_xfeatures2d -lopencv_shape -lopencv_ximgproc -lopencv_xobjdetect -lopencv_objdetect -lopencv_xphoto -lopencv_photo -lopencv_cudaimgproc -lopencv_cudafilters -lopencv_cudaarithm -lopencv_xstereo -lopencv_tracking -lopencv_video -lopencv_stereo -lopencv_highgui -lopencv_datasets -lopencv_3d -lopencv_videoio -lopencv_text -lopencv_imgcodecs -lopencv_features2d -lopencv_dnn -lopencv_plot -lopencv_ml -lopencv_imgproc -lopencv_flann -lopencv_core -lopencv_cudev
Libs.private: -lm -lpthread -lcudart_static -ldl -lrt -lnppc -lnppial -lnppicc -lnppidei -lnppif -lnppig -lnppim -lnppist -lnppisu -lnppitc -lnpps -lcublas -lcudnn -lcufft -L-L/usr/local/cuda-11.8 -llib64 -L-L/usr/lib -lx86_64-linux-gnu
Cflags: -I${includedir}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

3)保存退出,然后将文件导入到环境变量

export PKG_CONFIG_PATH=/home/angie/Programs/OpenCV/install/lib/pkgconfig
  • 1

刚才配置编译和安装目录的时候,没有设置为缺省的/usr/local目录, 而是设置了其他的目录(比如/home/angie/Programs/OpenCV/install这个目录),则有可能会在后面在应用的过程中遇到一个问题,就是使用cmake命令编译某个工程项目时,导致cmake找不到opencv。解决方法也很简单,先使用指令搜索OpenCVConfig.cmake文件的路径:

locate OpenCVConfig.cmake
  • 1

结果如下:

/home/angie/Programs/OpenCV/build/OpenCVConfig.cmake
/home/angie/Programs/OpenCV/build/unix-install/OpenCVConfig.cmake
/home/angie/Programs/OpenCV/install/lib/cmake/opencv5/OpenCVConfig.cmake
/home/angie/Programs/OpenCV/opencv-5.x/cmake/templates/OpenCVConfig.cmake.in
  • 1
  • 2
  • 3
  • 4

然后,在CmakeLists.txt中添加:

set(OpenCV_DIR /home/angie/Programs/OpenCV/install/lib/cmake/opencv5/OpenCVConfig.cmake)
  • 1

或者,输入下面的语句:

export OpenCV_DIR=/home/angie/Programs/OpenCV/install/lib/cmake/opencv5
  • 1

再或者,如前文所示,将其直接加入到“/etc/profile”文件中。
在这里插入图片描述经过这样处理后,然后再执行cmake命令,就可以找到OpenCVConfig.cmake的路径了。

四、测试

新建DisplayImage.cpp文件,并添加:

#include <stdio.h>
#include <opencv2/opencv.hpp>
using namespace cv;
int main(int argc, char** argv )
{
    if ( argc != 2 )
    {
        printf("usage: DisplayImage.out <Image_Path>\n");
        return -1;
    }
    Mat image;
    image = imread( argv[1], 1 );
    if ( !image.data )
    {
        printf("No image data \n");
        return -1;
    }
    namedWindow("Display Image", WINDOW_AUTOSIZE );
    imshow("Display Image", image);
    waitKey(0);
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

再新建一个CMakeLists.txt文件,添加配置:

cmake_minimum_required(VERSION 2.8)
project( DisplayImage )
#set(OpenCV_DIR /home/angie/Programs/OpenCV/install/lib/cmake/opencv5/)
find_package( OpenCV REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )
add_executable( DisplayImage DisplayImage.cpp )
target_link_libraries( DisplayImage ${OpenCV_LIBS} )
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

在当前目录下,新建新文件夹build并进入,完成编译:

mkdir build && cd build
cmake ..
cmake --build .
  • 1
  • 2
  • 3

准备lena照片,进行测试:

./DisplayImage lena.jpeg
  • 1

得到的结果:
在这里插入图片描述说明环境配置成功。

dense_flow

下载并编译dense_flow:

sudo apt install libboost-all-dev libzip-dev 
git clone --recursive http://github.com/yjxiong/dense_flow
cd dense_flow/
mkdir build && cd build
cmake ..
cmake --build .   # 或者:make -j48
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

过程中,需要修改源文件中calib3d.hpp的路径。
再进行测试:

./extract_gpu -f=test.avi -x=tmp/flow_x -y=tmp/flow_y -i=tmp/image -b=20 -t=1 -d=0 -s=1 -o=dir
  • 1

五、Jetson嵌入式设备

关于 CUDA_GENERATION ,对于常见的几个嵌入式设备的CUDA架构如下:

Jetson Nano: Maxwell
Jetson TX2: Pascal
Xavier NX: Volta
AGX Xavier: Volta
  • 1
  • 2
  • 3
  • 4

以下参数是针对jetson的优化

cmake -D CMAKE_BUILD_TYPE=RELEASE \
        -D CMAKE_INSTALL_PREFIX=/usr/local \
        -D OPENCV_EXTRA_MODULES_PATH=../opencv_contrib/modules \
        -D EIGEN_INCLUDE_PATH=/usr/include/eigen3 \
        -D WITH_CUDA=ON \
        -D CUDA_ARCH_BIN=5.3 \
        -D CUDA_ARCH_PTX="" \
        -D WITH_CUDNN=ON \
        -D WITH_CUBLAS=ON \
        -D ENABLE_FAST_MATH=OFF \
        -D CUDA_FAST_MATH=ON \
        -D OPENCV_DNN_CUDA=ON \
        -D ENABLE_NEON=ON \
        -D WITH_QT=OFF \
        -D WITH_OPENMP=ON \
        -D WITH_OPENGL=ON \
        -D BUILD_TIFF=ON \
        -D WITH_FFMPEG=ON \
        -D WITH_GSTREAMER=ON \
        # -D WITH_TBB=ON \
        # -D BUILD_TBB=ON \
        -D BUILD_TESTS=OFF \
        -D WITH_EIGEN=ON \
        -D WITH_V4L=ON \
        -D WITH_LIBV4L=ON \
        -D OPENCV_ENABLE_NONFREE=ON \
        -D BUILD_EXAMPLES=ON \
        -D INSTALL_C_EXAMPLES=ON \
        -D INSTALL_PYTHON_EXAMPLES=ON \
        -D PYTHON_DEFAULT_EXECUTABLE=$(which python3) \
        -D BUILD_NEW_PYTHON_SUPPORT=ON \
        -D BUILD_opencv_python3=TRUE \
        -D OPENCV_GENERATE_PKGCONFIG=ON \
        -D WITH_GTK_2_X=ON ..
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34

cmake配置中 -D ENABLE_FAST_MATH=1 会导致莫名的错误,建议去掉;

-D CUDA_GENERATION=Pascal 对于 -D CUDA_ARCH_BIN=‘7.2’ 来说是多余的,建议去掉。

附录A: Anaconda环境(未经测试)

若采用Ananconda的Python3环境,则需要再添加以下配置命令:

-D PYTHON_DEFAULT_EXECUTABLE=/home/thhicv/anaconda3/envs/mmact/bin/python3 \
-D PYTHON3_INCLUDE_DIR=/home/thhicv/anaconda3/envs/mmact/include/python3.7m \
-D PYTHON3_LIBRARY=/home/thhicv/anaconda3/envs/mmact/lib/libpython3.7m.so \
-D PYTHON3_PACKAGES_PATH=/home/thhicv/anaconda3/envs/mmact/lib/python3.7 \
-D PYTHON_EXECUTABLE=/home/thhicv/anaconda3/envs/mmact/lib/python3.7 \
-D WITH_LAPACK=OFF \
-D OPENCV_PYTHON3_INSTALL_PATH=/home/thhicv/anaconda3/envs/mmact/lib/python3.7/site-packages \
-D PYTHON3_NUMPY_INCLUDE_DIRS=/home/thhicv/anaconda3/envs/mmact/lib/python3.7/site-packages/numpy/core/include
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

cmake 命令中需要说明的一些,下面的参数是获得Anaconda的路径,不用自己去找路径

CMAKE_INSTALL_PREFIX=$(python3 -c "import sys; print(sys.prefix)") 
#得到路径:/home/cv/SoftWare/Anaconda3/envs/fcos
-D PYTHON3_EXECUTABLE=$(which python3) 
#得到路径:/home/cv/SoftWare/Anaconda3/envs/fcos/bin/python3
-DPYTHON3_INCLUDE_DIR=$(python3 -c "from distutils.sysconfig import get_python_inc; print(get_python_inc())") 
#得到路径:/home/cv/SoftWare/Anaconda3/envs/fcos/include/python3.6m
-D PYTHON3_PACKAGES_PATH=$(python3 -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())")
#得到路径:/home/cv/SoftWare/Anaconda3/envs/fcos/lib/python3.6/site-packages
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

参考

  1. Installation in Linux
  2. OpenCV configuration options reference
  3. 在Ubuntu 20.04上安装OpenCV with CUDA,denseflow环境,使用GPU加速光流
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/代码探险家/article/detail/1002704
推荐阅读
相关标签
  

闽ICP备14008679号