安装 opencv
版本为 4.2
一、前置条件
1. gcc安装
shell
yum install gcc
2. 安装cmake
注意安装 opencv-4.2
版本,需要 cmake-3.5.1
及其以上,安装脚本如下:
shell
#!/bin/bash
# 设置下载和安装目录
DOWNLOAD_DIR="/tmp/cmake_download"
INSTALL_DIR="/opt/cmake"
# 创建目录
mkdir -p $DOWNLOAD_DIR
mkdir -p $INSTALL_DIR
yum install -y gcc-c++ wget
# 下载 CMake 3.5.1 源码
cd $DOWNLOAD_DIR
wget http://www.cmake.org/files/v3.5/cmake-3.5.1.tar.gz
tar -zxvf cmake-3.5.1.tar.gz
cd cmake-3.5.1
# 编译和安装 CMake
./configure --prefix=$INSTALL_DIR
make
make install
# 设置环境变量
echo "export PATH=$INSTALL_DIR/bin:\$PATH" >> /etc/profile
echo "export MANPATH=$INSTALL_DIR/share/man:\$MANPATH" >> /etc/profile
source /etc/profile
# 清理临时文件
rm -rf $DOWNLOAD_DIR
二、安装opencv
1. 安装依赖
shell
sudo yum install -y epel-release
sudo yum install -y gtk2-devel
sudo yum install -y libpng-devel
sudo yum install -y jasper-devel
sudo yum install -y openexr-devel
sudo yum install -y libwebp-devel
sudo yum install -y libjpeg-turbo-devel
sudo yum install -y libtiff-devel
sudo yum install -y tbb-devel eigen3-devel
sudo yum install -y boost boost-thread boost-devel
sudo yum install -y libv4l-devel
2. 下载 opencv
官网下载即可:https://opencv.org/
3. 编译opencv
shell
cd opencv/
mkdir build && cd build
# 编译 opencv
cmake -D CMAKE_BUILD_TYPE=Release -D OPENCV_GENERATE_PKGCONFIG=ON -D CMAKE_INSTALL_PREFIX=/usr/local ..
make
shell
make -j 8
sudo make install
4. 环境变量配置
打开文件 /etc/ld.so.conf
,在最后一行加上 include /usr/local/lib64
运行ldconfig,使添加的库路径生效
shell
sudo ldconfig
编辑 /etc/profile
,在文件末尾添加
export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib64/pkgconfig
生效环境变量
shell
source /etc/profile
在命令行中输入如下命令,检测opencv是否安装成功
shell
pkg-config opencv4 --modversion
参考文件: