CentOS7.8 安装 OpenCV

一、安装依赖

系统依赖:

sudo yum -y install epel-release
sudo yum -y install gcc gcc-c++
sudo yum -y install cmake cmake3
sudo yum -y install autoconf automake bzip2 bzip2-devel cmake freetype-devel gcc gcc-c++ git libtool make mercurial pkgconfig zlib-devel
sudo yum -y install python-devel numpy gtk2-devel libdc1394-devel libv4l-devel gstreamer-plugins-base-devel

安装:FFmpeg(在线安装):

sudo su          # 使用超级权限
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           # 查看安装情况

安装参考:https://linoxide.com/linux-how-to/install-ffmpeg-centos-7/
或者参考:https://commandnotfound.cn/linux/1/171/ffmpeg-%E5%91%BD%E4%BB%A4

安装:FFmpeg(离线安装):https://www.cnblogs.com/blackhumour2018/p/9431393.html

二、安装OpenCV

2.1 下载安装

OpenCV官网下载:https://opencv.org/releases.html
或者命令下载:wget https://github.com/opencv/opencv/archive/3.4.4.zip
安装位置:path/to /install,根据自己路径设置。不指定时,默认安装路径在 /usr/local/

unzip opencv-3.4.12.zip
cd opencv-3.4.12
mkdir build && cd build

cmake -D CMAKE_BUILD_TYPE=RELEASE \
    -D CMAKE_INSTALL_PREFIX=/usr/local \
    -D INSTALL_C_EXAMPLES=ON \
    -D INSTALL_PYTHON_EXAMPLES=ON \
    -D OPENCV_GENERATE_PKGCONFIG=ON \
    -D BUILD_EXAMPLES=ON ..
'''
-- Configuring done
-- Generating done
-- Build files have been written to: /home/`username`/opencv-3.4.12/build
 
'''

make -j$(nproc)			#有几个CPU就用几个CPU
sudo make install

2.2 配置OpenCV

  1. 添加库路径–执行:
    sudo ln -s /usr/local/lib64/pkgconfig/opencv.pc /usr/share/pkgconfig/
    sudo ldconfig

  2. 添加环境变量:
    sudo vi /etc/profile     打开此文件,在文件末尾另起行加入如下两行:(保存,退出)
    export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib64/pkgconfig
    export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib64
    source /etc/profile     立即生效

  3. pkg-config配置
    默认的pkg搜索链接路径/usr/lib/pkgconfig,需要将opencv.pc拷贝到pkg的默认路径下
    cp /usr/local/lib64/pkgconfig/opencv.pc /usr/lib/pkgconfig

  4. 测试

    cd samples/cpp/example_cmake/
    make
    
    '''
    g++ example.cpp -o example.o -c -Wall -I/usr/local/include/opencv -I/usr/local/include
    g++ example.o -o opencv_example -L/usr/local/lib64 -lopencv_dnn -lopencv_highgui -lopencv_ml -lopencv_objdetect -lopencv_shape -lopencv_stitching -lopencv_superres -lopencv_videostab -lopencv_calib3d -lopencv_videoio -lopencv_imgcodecs -lopencv_features2d -lopencv_video -lopencv_photo -lopencv_imgproc -lopencv_flann -lopencv_core -ldl -lm -lpthread -lrt
    
    '''
    ./opencv_example         # 执行生成的程序
    Built with OpenCV 3.4.1   # 在这里会卡一阵
    

安装opencv3.4脚本

opencv安装脚本:在刚安装完centos系统上安装
确保网络畅通,一气呵成。

#!/usr/bin/bash
echo "执行该脚本时,请确保下载完opencv-3.4.12.zip文件,该文件与脚本处于同一目录下。"
sleep 10
echo
echo

echo -e '\033[32m======= 安装系统依赖文件 ==========\033[0m'
sleep 2
echo
sudo yum -y install make cmake cmake3 zip unzip
sudo yum -y install gcc gcc-c++ kernel-devel gcc-essential gcc-gfortran
sudo yum -y install libgnomeui-devel
sudo yum -y install gtk2 gtk2-devel gtk2-devel-docs
sudo yum -y install libv4l-devel gstreamer-plugins-base-devel
sudo yum -y install autoconf automake bzip2 bzip2-devel freetype-devel 
sudo yum -y install git libtool mercurial pkgconfig zlib-devel
sudo yum -y install python-devel numpy libdc1394-devel 
sleep 2
echo



echo -e '\033[32m======= ffmpeg组件安装 ==========\033[0m'
sleep 2
echo
sudo yum -y install epel-release
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
sudo yum -y install ffmpeg ffmpeg-devel
ffmpeg -version
sleep 2
echo



echo
echo -e '\033[32m======= opencv安装 ==========\033[0m'
sleep 3
echo
echo
if [[ ! -f "/home/`username`/opencv-3.4.12.zip" ]]; then
    echo "文件不存在"
else
    echo "准备解压文件"
    echo
    echo
    unzip opencv-3.4.12.zip
    mv opencv-3.4.12 opencv
    cd opencv
    mkdir build && cd build
    echo "检查依赖项..."
    sleep 2
    cmake -D CMAKE_BUILD_TYPE=RELEASE \
        -D CMAKE_INSTALL_PREFIX=/usr/local \
        -D INSTALL_C_EXAMPLES=ON \
        -D INSTALL_PYTHON_EXAMPLES=ON \
        -D OPENCV_GENERATE_PKGCONFIG=ON \
        -D BUILD_EXAMPLES=ON ..
    if [ $? -ne 0 ]; then
        echo "依赖项异常"
    else
        echo "准备编译..."
        sleep 2
        make -j$(nproc)
        if [ $? -ne 0 ]; then
            echo "编译异常"
        else
            echo "准备安装..."
            sleep 2
            sudo make install
            if [ $? -ne 0 ]; then
                echo "安装异常"
            else
                echo "安装 success"
            fi
        fi
    fi
fi

echo
echo -e '\033[32m======= opencv配置与加载动态库 配置环境变量 ==========\033[0m'
echo
echo "/usr/local/lib64" >> /etc/ld.so.conf.d/opencv.conf
cat >> /etc/profile <<"EOF"
export PKG_CONFIG_PATH=/usr/local/lib64/pkgconfig:$PKG_CONFIG_PATH
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib64
EOF
source /etc/profile
ldconfig
echo "====查询OpenCV版本===="
pkg-config --modversion opencv
echo
echo
echo -e '\033[32m======= All done ==========\033[0m'
echo

最好是重启系统执行:reboot

编写测试文件:(我的在centos虚拟机中,只有终端,无可视化界面)
执行编译命令:

g++ test.cpp -o test `pkg-config opencv --cflags --libs`
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;

int main(){
    Mat image;
    image = imread("./test.png");
    if (image.empty()) {
        cout << "Error! Input image cannot be read...\n";
        return -1;
    }
    cout<<"图像宽为:"<<image.cols<<"\t高度为:"<<image.rows<<"\t通道数为:"<<image.channels()<<endl;
    return 0;
}

参考:
https://lvjianzhao.gitee.io/lvjianzhao/2020/07/03/Centos%207.6%E5%AE%89%E8%A3%85opencv/
https://linuxize.com/post/how-to-install-opencv-on-centos-8/

https://blog.csdn.net/whudee/article/details/93379780
https://blog.csdn.net/weixin_43470000/article/details/84962432

报错:【解决方法】
Package opencv was not found in the pkg-config search path.
Perhaps you should add the directory containing `opencv.pc’
to the PKG_CONFIG_PATH environment variable
No package ‘opencv’ found

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 1024 设计师:白松林 返回首页