Ubuntu上安装opencv-cuda加速

前言

OpenCV的全称是Open Source Computer Vision Library,是一个跨平台的计算机视觉库。是由英特尔公司发起并参与开发,以BSD许可证授权发行,可以在商业和研究领域中免费使用。可用于开发实时的图像处理、计算机视觉以及模式识别程序。
Ubuntu上安装opencv-cuda加速_第1张图片
OpenCV的主要目标是:

  • 为推进机器视觉的研究,提供一套开源且优化的基础库。不重造轮子。
  • 提供一个共同的基础库,使得开发人员的代码更容易阅读和转让,促进了知识的传播。
  • 透过提供不需要开源或免费的软件许可,促进商业应用软件的开发。
  • OpenCV现在也集成了对CUDA的支持。

安装

最好的安装指导文档还是参考官方给出的(以Linux版本安装为例):
Installation in Linux

依赖环境

  • GCC 4.4.x or later
  • CMake 2.8.7 or higher
  • Git
  • GTK+2.x or higher, including headers (libgtk2.0-dev)
  • pkg-config
  • Python 2.6 or later and Numpy 1.5 or later with developer packages (python-dev, python-numpy)
  • ffmpeg or libav development packages: libavcodec-dev, libavformat-dev, libswscale-dev
  • [optional] libtbb2 libtbb-dev
  • [optional] libdc1394 2.x
  • [optional] libjpeg-dev, libpng-dev, libtiff-dev, libjasper-dev, libdc1394-22-dev
  • [optional] CUDA Toolkit 6.5 or higher

上述部分安装包能被以下命令安装:

[compiler] sudo apt-get install build-essential
[required] sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
[optional] sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev

如果需要CUDA支持,那么就必须提前安装配置好CUDA环境:

-------软硬件信息--------------------
操作系统:Ubuntu18.04
Nvidia 显卡:RTX 2080Ti
Nvidia 驱动版本:440.82
CUDA 版本: 10.1
Python 版本:3.6.10
-----------------------------------

下载Video_Codec_SDK

本文采用Video_Codec_SDK_9.1.23,需要自己根据环境选择。
下载Video_Codec_SDK_9.1.23,拷贝头文件和动态链接库到CUDA相应目录下:

cp ./Video_Codec_SDK_9.1.23/Lib/linux/stubs/x86_64/*  /usr/local/cuda-10.1/lib64/stubs
cp ./Video_Codec_SDK_9.1.23/include/cuviddec.h /usr/local/cuda/include

下载OpenCV源码

从官网下载或者使用Git下载:
(1)官网下载
Ubuntu上安装opencv-cuda加速_第2张图片
(2)Git下载(推荐)

cd ~/<my_working_directory>
git clone https://github.com/opencv/opencv.git
git clone https://github.com/opencv/opencv_contrib.git

编译和安装

cd ~/opencv
mkdir build
cd build

使用Cmake配置编译选项:

cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_C_COMPILER=/usr/bin/gcc \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D INSTALL_C_EXAMPLES=OFF \
-D WITH_TBB=ON \
-D BUILD_opencv_cudacodec=ON \
-D ENABLE_FAST_MATH=1 \
-D CUDA_FAST_MATH=1 \
-D WITH_CUDA=ON \
-D WITH_CUBLAS=1 \
-D WITH_FFMPEG=ON \
-D WITH_V4L=ON \
-D WITH_QT=OFF \
-D WITH_OPENGL=ON \
-D WITH_GSTREAMER=ON \
-D OPENCV_GENERATE_PKGCONFIG=ON \
-D OPENCV_PC_FILE_NAME=opencv.pc \
-D OPENCV_ENABLE_NONFREE=ON \
-D OPENCV_PYTHON3_INSTALL_PATH=/home/ubuntu/conda/envs/pytorch/lib/python3.6/site-packages \
-D WITH_NVCUVID=ON \
-D BUILD_opencv_python3=yes \
-D OPENCV_EXTRA_MODULES_PATH=/home/ubuntu/installs/opencv_contrib/modules \
-D PYTHON2_EXECUTABLE=/home/ubuntu/conda/envs/pytorch/bin/python \
-D PYTHON3_EXECUTABLE=/home/ubuntu/conda/envs/pytorch/bin/python \
-D PYTHON_INCLUDE_DIR=/home/ubuntu/conda/envs/pytorch/include/python3.6m \
-D PYTHON_INCLUDE_DIR2=/home/ubuntu/conda/envs/pytorch/include/python3.6m \
-D PYTHON2_LIBRARY=/home/ubuntu/conda/envs/pytorch/lib/libpython3.6m.so \
-D PYTHON3_LIBRARY=/home/ubuntu/conda/envs/pytorch/lib/libpython3.6m.so \
-D PYTHON3_NUMPY_INCLUDE_DIRS=/home/ubuntu/conda/envs/pytorch/lib/python3.6/site-packages/numpy/core/include \
-D BUILD_EXAMPLES=ON \
-D WITH_CUDNN=ON \
-D OPENCV_DNN_CUDA=ON \
-D CUDA_ARCH_BIN=7.5 ..

注:要开启CUDA加速支持,需要在cmake选项中打开相应开关:

-D BUILD_opencv_cudacodec=ON
-D WITH_CUDA=ON
-D WITH_CUDNN=ON
-D OPENCV_DNN_CUDA=ON
-D CUDA_ARCH_BIN=7.5

其中CUDA_ARCH_BIN是GPU的算力等级,根据自己的显卡型号在NVIDIA官网查看。
Ubuntu上安装opencv-cuda加速_第3张图片

编译:

make -j8

其中8是线程数,根据自己的机器CPU情况给定,也可直接make

安装:

sudo make install

更新动态库:

sudo ldconfig

快速测试

bin目录下有大量已经编译好的示例程序,可以使用:

cd ~/<my_working_directory>/opencv/build/bin
opencv_version

Ubuntu上安装opencv-cuda加速_第4张图片

CUDA加速测试

Python:
测试cv2.cuda_GpuMat()

import numpy as np
import cv2 as cv
npTmp = np.random.random((1024, 1024)).astype(np.float32)
npMat1 = np.stack([npTmp,npTmp],axis=2)
npMat2 = npMat1
cuMat1 = cv.cuda_GpuMat()
cuMat2 = cv.cuda_GpuMat()
cuMat1.upload(npMat1)
cuMat2.upload(npMat2)

C++:
(1)测试cv::cudacodec::createVideoReader()

cd ~/<my_working_directory>/opencv/samples/gpu
g++ -ggdb video_reader.cpp -o video_reader `pkg-config --cflags --libs opencv`
./video_reader test.mp4
Results:
CPU : Avg : 1.75547 ms FPS : 569.65 Frames 1054
GPU : Avg : 1.01719 ms FPS : 983.104 Frames 1054

(2)测试cv::cudacodec::createVideoWriter()

cd ~/<my_working_directory>/opencv/samples/gpu
g++ -ggdb video_writer.cpp -o video_writer `pkg-config --cflags --libs opencv`
./video_writer test.mp4

运行报错:

OpenCV was built without CUDA Video encoding support

查看源码,发现代码开头宏定义有下面这一句:

#if defined(HAVE_OPENCV_CUDACODEC) && defined(_WIN32)

也就是说不支持Linux系统下运行。
尝试注释掉defined(_WIN32)后重新编译:

#if defined(HAVE_OPENCV_CUDACODEC)// && defined(_WIN32)
g++ -ggdb video_writer.cpp -o video_writer `pkg-config --cflags --libs opencv`

再次运行,报错如下:

Device 0:  "GeForce RTX 2080 Ti"  11019Mb, sm_75, Driver/Runtime ver.10.20/10.10
Read 1 frame
Frame Size : 720x1280
Open CPU Writer
Open CUDA Writer
terminate called after throwing an instance of 'cv::Exception'
  what():  OpenCV(4.4.0-dev) /home/ubuntu/installs/opencv/modules/core/include/opencv2/core/private.cuda.hpp:112: error: (-213:The function/feature is not implemented) The called functionality is disabled for current build or platform in function 'throw_no_cuda'

Aborted (core dumped)

去Opencv官网查看相关内容,发现cv::cudacodec::VideoWriter确实只支持Windows平台:

Ubuntu上安装opencv-cuda加速_第5张图片
所以暂时只能用GPU来解码视频了,经过测试,确实能比CPU解码快不少。

Ubuntu上安装opencv-cuda加速_第6张图片

参考资料

[1] OpenCV维基百科
[2] OpenCV安装包下载
[3] OpenCV Tutorials -> Introduction to OpenCV -> Installation in Linux
[4] GitHub issue: Failed to build OpenCV 4.0.1 with CUDA 10 10.0 #13897
[5] NVIDIA Video Codec SDK
[6] Accelerate OpenCV 4.2.0 – build with CUDA and python bindings
[7] GitHub issue: Error when use cv::cudacodec::VideoReader by opencv_cudacodec with cuda 10.1, ubuntu18.04 #17798
[8] How to use OpenCV’s “dnn” module with NVIDIA GPUs, CUDA, and cuDNN
[9] OpenCV CUDA implementations for background subtraction with Python
[10] OpenCV cv::cudacodec::VideoWriter Class Reference
[11] GitHub: cudawarped/opencv-experiments
[12] How to install OpenCV 4.2.0 with CUDA 10.1 on Ubuntu 20.04 LTS (Focal Fossa)
[13] Accelerating OpenCV with CUDA streams in Python
[14] Accelerating OpenCV 4 – build with CUDA 10.0, Intel MKL + TBB and python bindings in Windows
[15] Accelerate OpenCV 4.4.0 – build with CUDA and python bindings
[16] Compiling OpenCV with CUDA support
[17] GitHub: NeerajGulia/python-opencv-cuda
[18] 知乎 - Linux安装OpenCV4(可选GPU加速)

你可能感兴趣的:(#,音视频处理,#,OpenCV,#,C/C++,linux,cuda,gpu,opencv)