关于opecv的gpu版本的源码编译安装

时间:2024-03-09 13:08:50

OpenCV下载安装

下载地址OpenCV官网,选择最新的4.9.0版本(如果下载速度太慢,复制链接地址,使用迅雷)

https://opencv.org/releases/

编译与安装

安装cmake
OpenCV需要使用cmake进行编译

	sudo apt-get install cmake

安装依赖

sudo apt-get install build-essential pkg-config libgtk2.0-dev libavcodec-dev libavformat-dev libjpeg-dev libswscale-dev libtiff5-dev

出现以下问题:

The following packages have unmet dependencies:
 libavcodec-dev : Depends: libavutil-dev (= 7:3.4.2-2) but it is not going to be installed
              Depends: libswresample-dev (= 7:3.4.2-2) but it is not going to be installed
 libavformat-dev : Depends: libavformat57 (= 7:3.4.2-2) but it is not going to be installed
               Depends: libavutil-dev (= 7:3.4.2-2) but it is not going to be installed
               Depends: libswresample-dev (= 7:3.4.2-2) but it is not going to be installed
 libgtk2.0-dev : Depends: libglib2.0-dev (>= 2.27.3) but it is not going to be installed
             Depends: libgdk-pixbuf2.0-dev (>= 2.21.0) but it is not going to be installed
             Depends: libpango1.0-dev (>= 1.20) but it is not going to be installed
             Depends: libatk1.0-dev (>= 1.29.2) but it is not going to be installed
             Depends: libcairo2-dev (>= 1.6.4-6.1) but it is not going to be installed
             Depends: libx11-dev (>= 2:1.0.0-6) but it is not going to be installed
             Depends: libxext-dev (>= 1:1.0.1-2) but it is not going to be installed
             Depends: libxinerama-dev (>= 1:1.0.1-4.1) but it is not going to be installed
             Depends: libxi-dev (>= 1:1.0.1-4) but it is not going to be installed
             Depends: libxrandr-dev (>= 2:1.2.99) but it is not going to be installed
             Depends: libxcursor-dev but it is not going to be installed
             Depends: libxfixes-dev (>= 1:3.0.0-3) but it is not going to be installed
             Depends: libxcomposite-dev (>= 1:0.2.0-3) but it is not going to be installed
             Depends: libxdamage-dev (>= 1:1.0.1-3) but it is not going to be installed
             Recommends: python (>= 2.4) but it is not going to be installed
 libjpeg-dev : Depends: libjpeg8-dev but it is not going to be installed
libswscale-dev : Depends: libavutil-dev (= 7:3.4.2-2) but it is not going to be installed
              Depends: libswscale4 (= 7:3.4.2-2) but 7:3.4.8-0ubuntu0.2 is to be installed
 libtiff5-dev : Depends: libtiff5 (= 4.0.9-5) but 4.0.9-5ubuntu0.4 is to be installed
E: Unable to correct problems, you have held broken packages.

使用aptitude

aptitude与 apt-get 一样,是 Debian 及其衍生系统中功能极其强大的包管理工具。与 apt-get 不同的是,aptitude在处理依赖问题上更佳一些。举例来说,aptitude在删除一个包时,会同时删除本身所依赖的包。这样,系统中不会残留无用的包,整个系统更为干净。

sudo aptitude install install build-essential pkg-config libgtk2.0-dev libavcodec-dev libavformat-dev libjpeg-dev libswscale-dev libtiff5-dev

运行后,不接受未安装方案,接受降级方案。

解压

unzip opencv-4.9.0

进入文件目录,创建build目录并进入

cd opencv-4.9.0/
mkdir build
cd build

使用cmake生成makefile文件

cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -DOPENCV_EXTRA_MODULES_PATH=../opencv_contrib-4.9.0/modules ..  -DWITH_CUDA=1  -D WITH_GTK=ON -D OPENCV_GENERATE_PKGCONFIG=YES ..

CMAKE_BUILD_TYPE=RELEASE:表示编译发布版本
CMAKE_INSTALL_PREFIX:表示生成动态库的安装路径,可以自定义
WITH_GTK=ON:这个配置是为了防止GTK配置失败:即安装了libgtk2.0-dev依赖,还是报错未安装
OPENCV_GENERATE_PKGCONFIG=YES:表示自动生成OpenCV的pkgconfig文件,否则需要自己手动生成。
编译

make -j8

-j8表示使用多个系统内核进行编译,从而提高编译速度,不清楚自己系统内核数的,可以使用make -j$(nproc)
如果编译时报错,可以尝试不使用多个内核编译,虽然需要更长的编译时间,但是可以避免一些奇怪的报错
安装

sudo make install

注:如果需要重新cmake,请先将build目录下的文件清空,再重新cmake,以免发生错误
环境配置

将OpenCV的库添加到系统路径

方法一:配置ld.so.conf文件

sudo vim /etc/ld.so.conf

在文件中加上一行 include /usr/local/lib,这个路径是cmake编译时填的动态库安装路径加上/lib
配置ld.so.conf文件

方法二:手动生成opencv.conf文件

sudo vim /etc/ld.so.conf.d/opencv.conf

是一个新建的空文件,直接添加路径,同理这个路径是cmake编译时填的动态库安装路径加上/lib

/usr/local/lib

以上两种方法配置好后,执行如下命令使得配置的路径生效

sudo ldconfig

配置系统bash
因为在cmake时,选择了自动生成OpenCV的pkgconfig文件,在/usr/local/lib/pkgconfig路径可以看到文件
opencv4.pc

确保文件存在,执行如下命令

sudo vim /etc/bash.bashrc

在文末添加

PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
export PKG_CONFIG_PATH

如下:

bash.bashrc

保存退出,然后执行如下命令使配置生效

source /etc/bash.bashrc

至此,Linux\Ubuntu18.04环境下OpenCV的安装以及配置已经全部完成,可以使用以下命令查看是否安装和配置成功

pkg-config --modversion opencv4
pkg-config --cflags opencv4
pkg-config --libs opencv4

最后添加软链接,确保C++编译没问题:

sudo mv /usr/local/include/opencv4  /usr/local/include/opencv
sudo ln -s /usr/local/include/opencv/opencv2   /usr/local/include/opencv2
sudo ln -s /usr/local/include/opencv/opencv2   /usr/local/include/opencv4