跨境派

跨境派

跨境派,专注跨境行业新闻资讯、跨境电商知识分享!

当前位置:首页 > 卖家故事 > 手把手带你在Linux上安装带GPU加速的opencv库(C++版本)

手把手带你在Linux上安装带GPU加速的opencv库(C++版本)

时间:2024-04-03 15:00:46 来源:网络cs 作者:言安琪 栏目:卖家故事 阅读:

标签: 版本  安装  把手 
阅读本书更多章节>>>>

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-numpysudo apt-get install libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev

出现下面类似错误E: unable to locate libjasper-dev,运行以下命令.

image-20240219112728179

sudo add-apt-repository "deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ xenial main multiverse restricted universe"sudo apt updatesudo 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 updatesudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 3B4FE6ACC0B21F32sudo apt updatesudo 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-devsudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-devsudo apt-get install build-essentialsudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-devsudo 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-devsudo apt-get install libxvidcore-dev libx264-dev # 处理视频所需的包sudo apt-get install libatlas-base-dev gfortran # 优化opencv功能sudo apt-get install ffmpeg

需要出现这面这行提示,说明依赖安装好了

image-20240219121027935

2.编译opencv

下载源码

git clone https://github.com/opencv/opencv.gitgit clone https://github.com/opencv/opencv_contrib.git

在opencv文件夹内新建build文件夹

image-20240219121817129

编译

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)

image-20240219122803384

其它地方默认

编译过程需要等待较长时间…………

发现报错,需要将opencv_contrib中的modules中的cudev文件夹复制到opencv/modules里

image-20240219122858497

cp -r /home/opencv_contrib/modules/cudev /home/opencv/modules/

继续编译image-20240219123324769

出现下列提示,说明该步骤成功

image-20240219123414607

执行一下两条命令,安装带gpu的opencv库

sudo make -j7sudo make install

3.配置环境

在/etc/ld.so.conf.d/文件夹下有一个opencv.conf,里面需要写入/usr/local/lib。

cd /etc/ld.so.conf.d/sudo touch opencv.confsudo sh -c 'echo "/usr/local/lib" > opencv.conf'

更新pkg-config:

sudo ldconfig

打开/etc/bash.bashrc文件,在最后两行加上这两句话

PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfigexport PKG_CONFIG_PATH

最后效果如图:

image-20240219130528143

执行这三句

source /etc/bash.bashrcsudo apt install mlocatesudo updatedb

4.测试opencv

cd 到/opencv/samples/cpp/example_cmake目录下,依次执行以下命令:

cmake .make./opencv_example

该测试需要电脑有摄像头,若启动摄像头看到了画面,说明安装成功;若未启动摄像头,但出现如下显示,也安装成功:

image-20240219131636517

测试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 linecmake_minimum_required(VERSION 3.1)# Define project nameproject(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 GUIfind_package(OpenCV REQUIRED)# Enable C++11set(CMAKE_CXX_STANDARD 11)set(CMAKE_CXX_STANDARD_REQUIRED TRUE)# Declare the executable target built from your sourcesadd_executable(ImageShow main.cpp)# Link your application with OpenCV librariestarget_link_libraries(ImageShow PRIVATE ${OpenCV_LIBS})

运行以下命令:

cmake .make./ImageShow

若出现下面信息,则说明Linux上成功安装带GPU加速的opencv库(C++版本)🎉🎉🎉

image-20240219134014110

5.参考资料

Ubuntu 20.04搭建OpenCV 4.5.0 & C++环境_opencv4 ubuntu20.04-CSDN博客

Linux安装OpenCV4(可选GPU加速) - 知乎 (zhihu.com)

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.kjpai.cn/gushi/2024-04-03/153349.html,文章来源:网络cs,作者:言安琪,版权归作者所有,如需转载请注明来源和作者,否则将追究法律责任!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。

文章评论