Ubuntu下opencv的安装与卸载


前言

Ubuntu上我们一般需要安装opencv来完成图像处理等程序操作,下面我以opencv3.4.0在Ubuntu16.04上的安装为例进行讲解。

1. 系统自带opencv

Ubuntu16.04系统资源库自带opencv2.4.8,如果不需要用到高级功能,这点即可。其下载方法如下:

1
sudo apt install libopencv-dev

判断系统上是否存在opencv,可输入:

1
pkg-config --modversion opencv

卸载方法为:

1
sudo apt remove libopencv-dev

2.官网下载安装opencv

2.1 安装opencv

  • Step1 卸载已有opencv

  • Step2 进入Opencv官网选择对应版本source下载。

  • Step3 在下载路径执行:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    if [ ! -d "opencv-3.4.0/" ];then
    unzip opencv-3.4.0.zip -d ./
    else
    echo "opencv package has been unzipped"
    fi

    sudo apt-get install cmake pkg-config build-essential libgtk2.0-dev libavcodec-dev libavformat-dev libjpeg.dev libtiff4.dev libswscale-dev libjasper-dev -y

    cd opencv-3.4.0/
    sudo mkdir build
    cd build
    sudo make clean
    cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local ..
    sudo make -j8
    sudo make install

    if [ 0 -eq $(cat /etc/ld.so.conf.d/opencv.conf | grep -c '/usr/local/lib') ] ;then
    echo '/usr/local/lib' >> /etc/ld.so.conf.d/opencv.conf
    fi

    if [ 0 -eq $(cat /etc/bash.bashrc | grep -c 'PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig') ] ;then
    echo 'PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig' >> /etc/bash.bashrc
    fi

    if [ 0 -eq $(cat /etc/bash.bashrc | grep -c 'export PKG_CONFIG_PATH') ] ;then
    echo 'export PKG_CONFIG_PATH' >> /etc/bash.bashrc
    fi

    sudo ldconfig
    sudo updatedb
    source /etc/bash.bashrc
  • Step4 如果安装过程中卡在ippicv这里,可以去这里下载,然后重命名为4e0352ce96473837b1d671ce87f17359-ippicv_2017u3_lnx_intel64_general_20170822.tgz,放入opencv-3.4.0/.cache/ippicv/下,重新安装。

2.2 卸载opencv

卸载方法如下,进入下载路径:

1
2
3
4
5
6
7
8
9
10
11
cd opencv-3.4.0/build
sudo make uninstall
cd ../..
sudo rm -rf opencv-3.4.0/
sudo rm -rf /usr/local/include/opencv2 /usr/local/include/opencv /usr/include/opencv /usr/include/opencv2 /usr/local/share/opencv /usr/local/share/OpenCV /usr/share/opencv /usr/share/OpenCV /usr/local/bin/opencv* /usr/local/lib/libopencv* /usr/local/lib/pkgconfig
sudo rm -rf /etc/ld.so.conf.d/opencv.conf
sudo sed -i '/PKG_CONFIG_PATH/d' /etc/bash.bashrc
sudo ldconfig
sudo updatedb
cd ..
source /etc/bash.bashrc

3.python版opencv

python版本的opencv可通过pip直接下载安装,pip下载源建议设置为国内源,即在当前用户HOME目录下新建一个.pip/pip.conf文件,写入:

1
2
3
4
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
[install]
trusted-host=pypi.tuna.tsinghua.edu.cn

然后下载安装python-opencv:

1
pip install opencv-python

卸载方式:

1
pip uninstall opencv-python

-------------本文结束感谢您的阅读-------------
坚持原创技术分享,您的支持将鼓励我继续创作!