序言
最近刚刚申请了一个国家级的大创项目,需要用到opecv,在网上看了很多教程,发现实际配置中还是遇到了一些问题,在此整理一下安装过程,使用的环境是Centos7.7(1908)。
一、下载和安装
1、首先下载opencv压缩包,下载cmake压缩包,下面命令行中的”(version)“是你下载的压缩包对应版本,我用的opencv3.4.1,所以把”(version)“替换为”3.4.1“。
2、Centos7.7下依赖库安装
(1)cmake
tar -zxvf cmake-(version)-rc1.tar.gz(解压CMake源码包)
cd cmake-(version)-rc1(进入cmark的源码目录)
./bootstrap
gmake
gmake install
(2)essential
yum install gcc gcc-c++ kernel-devel
(3)gtk2.0
yum install git(安装git)
yum install libgnomeui-devel(安装必要文件包)
yum install gtk2 gtk2-devel gtk2-devel-docs(安装必要组件)
yum install gnome-devel gnome-devel-docs (安装可选组件)
(4)pkg-config
cd /opt/pkg wget http://pkgconfig.freedesktop.org/releases/pkg-config-(version).tar.gz
tar xvf pkg-config-(version).tar.gz
cd pkg-config-(version)
./configure --prefix=/usr/local/pkg-config --with-internal-glib
make
make instal
3、ffmpeg组件安装
主要依赖有 libavcodec-dev libavformat-dev libswscale-dev。
Step1-安装ffmpeg-devel
yum -y install epel-release(安装epel扩展源)
yum localinstall –nogpgcheck https://download1.rpmfusion.org/free/el/rpmfusion-free-release-7.noarch.rpm
yum localinstall –nogpgcheck https://download1.rpmfusion.org/nonfree/el/rpmfusion-nonfree-release-7.noarch.rpm
rpm –import http://li.nux.ro/download/nux/RPM-GPG-KEY-nux.ro
rpm -Uvh http://li.nux.ro/download/nux/dextop/el7/x86_64/nux-dextop-release-0-1.el7.nux.noarch.rpm
yum -y install ffmpeg ffmpeg-devel(安装ffmpeg )
Step2-编译源码ffmpeg,在官网上下载FFmpe源码包。
tar xvJf ffmpeg-4.1.tar.xz(解压)
makefile./configure --enable-libopencv --enable-swscale --enable-avresample--enable-gpl --enable-shared(生成)
make(并行编译)
make install(安装)
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
export PKG_CONFIG_PATH(添加ffmpeg到环境变量)
4、安装opencv其他依赖项
yum install python-devel numpy
yum install libdc1394-devel
yum install libv4l-devel
yum install gstreamer-plugins-base-devel
5、编译和安装opencv
Step1-opencv源码编译和安装
cd opencv
madir build
cd build
cmake -D WITH_TBB=ON -D WITH_EIGEN=ON ..
cmake -D BUILD_DOCS=ON -D BUILD_TESTS=OFF -D BUILD_PERF_TESTS=OFF -D BUILD_EXAMPLES=OFF ..
cmake -D WITH_OPENCL=OFF -D WITH_CUDA=OFF -D BUILD_opencv_gpu=OFF -D BUILD_opencv_gpuarithm=OFF -D BUILD_opencv_gpubgsegm=OFF -D BUILD_opencv_gpucodec=OFF -D BUILD_opencv_gpufeatures2d=OFF -D BUILD_opencv_gpufilters=OFF -D BUILD_opencv_gpuimgproc=OFF -D BUILD_opencv_gpulegacy=OFF -D BUILD_opencv_gpuoptflow=OFF -D BUILD_opencv_gpustereo=OFF -D BUILD_opencv_gpuwarping=OFF ..
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local
make
sudo make install
Step2-opencv配置与加载动态库
cd /etc/ld.so.conf.d(路径添加和加载)
/bin/bash -c 'echo "/usr/local/lib" > /etc/ld.so.conf.d/opencv.conf'(添加opencv编译产生的lib库路径到opencv.conf中)
ldconfig
gedit /etc/bashrc
(文件末尾加入以下片段)
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
export PKG_CONFIG_PATH
source /etc/bashrc(使配置生效)
updatedb(更新数据库)
Step3-pkg-config配置
将opencv.pc拷贝到pkg的默认路径下(/usr/lib/pkgconfig),如果没有此文件夹,可以新建一个/usr/local/lib/pkgconfig。
cp (opencv.pc的路径) /usr/lib/pkgconfig