1.安装依赖
sudo apt-get install build-essential cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
sudo apt-get install python-dev python-numpy python3-dev python3-numpy
sudo apt-get install libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev
出现下面类似错误E: unable to locate libjasper-dev
,运行以下命令.
sudo add-apt-repository "deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ xenial main multiverse restricted universe"
sudo apt update
sudo apt-get install build-essential cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
提示没有E: unable to locate libjasper-dev这个包,需要添加数字签名:
sudo add-apt-repository "deb http://security.ubuntu.com/ubuntu xenial-security main"
sudo apt-get update
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 3B4FE6ACC0B21F32
sudo apt update
sudo apt-get install libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev
检查一下有没有漏装依赖(非常重要,否则后续会失败)
sudo apt-get install libjpeg-dev libpng-dev libtiff-dev
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
sudo apt-get install build-essential
sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev # 处理图像所需的包
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev liblapacke-dev
sudo apt-get install libxvidcore-dev libx264-dev # 处理视频所需的包
sudo apt-get install libatlas-base-dev gfortran # 优化opencv功能
sudo apt-get install ffmpeg
需要出现这面这行提示,说明依赖安装好了
2.编译opencv
下载源码
git clone https://github.com/opencv/opencv.git
git clone https://github.com/opencv/opencv_contrib.git
在opencv文件夹内新建build文件夹
编译
cmake \
-D CMAKE_BUILD_TYPE=RELEASE \
-D OPENCV_GENERATE_PKGCONFIG=ON \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D OPENCV_EXTRA_MODULES_PATH= /home/opencv_contrib/modules \
-D CUDA_CUDA_LIBRARY=/usr/local/cuda/lib64/stubs/libcuda.so \
-D CUDA_ARCH_BIN=7.5 \
-D CUDA_ARCH_PTX="" \
-D WITH_CUDA=ON \
-D WITH_TBB=ON \
-D BUILD_NEW_PYTHON_SUPPORT=ON \
-D WITH_V4L=ON \
-D INSTALL_C_EXAMPLES=ON \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D BUILD_EXAMPLES=ON \
-D WITH_QT=ON \
-D WITH_OPENGL=ON \
-D ENABLE_FAST_MATH=1 \
-D CUDA_FAST_MATH=1 \
-D WITH_CUBLAS=1 \
-D WITH_NVCUVID=ON \
-D BUILD_opencv_cudacodec=OFF ..
-
-D OPENCV_EXTRA_MODULES_PATH= /home/opencv_contrib/modules
需要填写你自己的opencv_contrib路径 -
-D CUDA_ARCH_BIN=7.5
根据你的显卡去官网查看,我的显卡是2060,所以CUDA_ARCH_BIN=7.5(CUDA GPUs - Compute Capability | NVIDIA Developer)
其它地方默认
编译过程需要等待较长时间…………
发现报错,需要将opencv_contrib中的modules中的cudev文件夹复制到opencv/modules里
cp -r /home/opencv_contrib/modules/cudev /home/opencv/modules/
继续编译
出现下列提示,说明该步骤成功
执行一下两条命令,安装带gpu的opencv库
sudo make -j7
sudo make install
3.配置环境
在/etc/ld.so.conf.d/文件夹下有一个opencv.conf,里面需要写入/usr/local/lib。
cd /etc/ld.so.conf.d/
sudo touch opencv.conf
sudo sh -c 'echo "/usr/local/lib" > opencv.conf'
更新pkg-config:
sudo ldconfig
打开/etc/bash.bashrc文件,在最后两行加上这两句话
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
export PKG_CONFIG_PATH
最后效果如图:
执行这三句
source /etc/bash.bashrc
sudo apt install mlocate
sudo updatedb
4.测试opencv
cd 到/opencv/samples/cpp/example_cmake目录下,依次执行以下命令:
cmake .
make
./opencv_example
该测试需要电脑有摄像头,若启动摄像头看到了画面,说明安装成功;若未启动摄像头,但出现如下显示,也安装成功:
测试GPU功能
新建一个main.cpp文件,代码如下:
#include <iostream>
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include "opencv2/core/cuda.hpp"
using namespace cv;
using namespace std;
int main() {
int num_devices = cv::cuda::getCudaEnabledDeviceCount();
if (num_devices <= 0) {
std::cerr << "There is no device." << std::endl;
return -1;
}
int enable_device_id = -1;
for (int i = 0; i < num_devices; i++) {
cv::cuda::DeviceInfo dev_info(i);
if (dev_info.isCompatible()) {
enable_device_id = i;
}
}
if (enable_device_id < 0) {
std::cerr << "GPU module isn't built for GPU" << std::endl;
return -1;
}
cv::cuda::setDevice(enable_device_id);
std::cout << "GPU is ready, device ID is " << num_devices << "\n";
return 0;
}
新建一个CMakeLists.txt文件,代码如下
# cmake needs this line
cmake_minimum_required(VERSION 3.1)
# Define project name
project(ImageShow)
# Find OpenCV, you may need to set OpenCV_DIR variable
# to the absolute path to the directory containing OpenCVConfig.cmake file
# via the command line or GUI
find_package(OpenCV REQUIRED)
# Enable C++11
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)
# Declare the executable target built from your sources
add_executable(ImageShow main.cpp)
# Link your application with OpenCV libraries
target_link_libraries(ImageShow PRIVATE ${OpenCV_LIBS})
运行以下命令:
cmake .
make
./ImageShow
若出现下面信息,则说明Linux上成功安装带GPU加速的opencv库(C++版本)🎉🎉🎉
5.参考资料
Ubuntu 20.04搭建OpenCV 4.5.0 & C++环境_opencv4 ubuntu20.04-CSDN博客
Linux安装OpenCV4(可选GPU加速) - 知乎 (zhihu.com)文章来源:https://www.toymoban.com/news/detail-849379.html
Ubuntu 20.04搭建OpenCV 4.5.0 & C++环境_opencv4 ubuntu20.04-CSDN博客
Linux安装OpenCV4(可选GPU加速) - 知乎 (zhihu.com)
Ubuntu22.04安装opencv依赖包libjasper-dev出现unable to locate libjasper-dev的终极解决办法_ubuntu libjasper库-CSDN博客文章来源地址https://www.toymoban.com/news/detail-849379.html
到了这里,关于手把手带你在Linux上安装带GPU加速的opencv库(C++版本)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!