一、完全卸载opencv
1. 先到opencv编译安装的目录下
sudo make uninstall
cd ..
sudo rm -r build
2.上面最后一行命令有的文件夹不存在会有提示,但是不影响,只要把已安装的opencv的include和lib删掉就行,也可以手动删除,总之删除干净即可。
sudo rm -r /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*
3.检查是否删除完
pkg-config opencv --libs
pkg-config opencv --modversion
4.清除干净
sudo apt-get autoremove opencv-doc opencv-data libopencv-dev libopencv2.4-java
libopencv2.4-jni python-opencv libopencv-core2.4 libopencv-gpu2.4 libopencv-ts2.4
libopencv-photo2.4 libopencv-contrib2.4 libopencv-imgproc2.4 libopencv-superres2.4
libopencv-stitching2.4 libopencv-ocl2.4 libopencv-legacy2.4 libopencv-ml2.4 libopencv-
video2.4 libopencv-videostab2.4 libopencv-objdetect2.4 libopencv-calib3d2.4
二、重新安装opencv
1.官网下载对应版本的source:https://opencv.org/releases/
2.更新安装依赖库
sudo apt-get install build-essential
sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev
3.编译并安装
cd opencv
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local ..
make -j
sudo make install
安装时间会比较长,要耐心等待!
三、配置环境变量
将opencv的库加入路径
sudo gedit /etc/ld.so.conf/opencv.conf
此时在打开的文件中加入/usr/loacl/lib
,保存退出
sudo ldconfig
sudo gedit /etc/bash.bashrc
此时在打开的文件末尾加入PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig export PKG_CONFIG_PATH
,保存退出
source /etc/bash.bashrc
4.检验
pkg-config opencv --modversion
至此opencv的安装就完成了
四、解决错误提示
1.因之前的版本没完全删除干净,会残留一些lib或者include导致重新安装时相应的目录文件不可用。参考上文中彻底删除opencv文件
2.安装依赖库时(第三句指令),发生错误
下列软件包有未满足的依赖关系:
libopencv-dev : 依赖: libopencv-calib3d2.4v5 但是它将不会被安装
依赖: libopencv-highgui2.4v5 但是它将不会被安装
依赖: libopencv-ml2.4v5 但是它将不会被安装
依赖: libopencv-objdetect2.4v5 但是它将不会被安装
依赖: libopencv-core-dev (= 2.4.9.1+dfsg-1.5ubuntu1xqms2~xenial1) 但是 2.4.9.1+dfsg-1.5ubuntu1.1 正要被安装
依赖: libopencv-ml-dev (= 2.4.9.1+dfsg-1.5ubuntu1xqms2~xenial1) 但是它将不会被安装
依赖: libopencv-imgproc-dev (= 2.4.9.1+dfsg-1.5ubuntu1xqms2~xenial1) 但是 2.4.9.1+dfsg-1.5ubuntu1.1 正要被安装
依赖: libopencv-video-dev (= 2.4.9.1+dfsg-1.5ubuntu1xqms2~xenial1) 但是它将不会被安装
依赖: libopencv-objdetect-dev (= 2.4.9.1+dfsg-1.5ubuntu1xqms2~xenial1) 但是它将不会被安装
依赖: libopencv-highgui-dev (= 2.4.9.1+dfsg-1.5ubuntu1xqms2~xenial1) 但是它将不会被安装
依赖: libopencv-calib3d-dev (= 2.4.9.1+dfsg-1.5ubuntu1xqms2~xenial1) 但是它将不会被安装
依赖: libopencv-flann-dev (= 2.4.9.1+dfsg-1.5ubuntu1xqms2~xenial1) 但是 2.4.9.1+dfsg-1.5ubuntu1.1 正要被安装
依赖: libopencv-features2d-dev (= 2.4.9.1+dfsg-1.5ubuntu1xqms2~xenial1) 但是 2.4.9.1+dfsg-1.5ubuntu1.1 正要被安装
E: 无法修正错误,因为您要求某些软件包保持现状,就是它们破坏了软件包间的依赖关系。
解决:源文件过时,添加新的源文件
deb http://cn.archive.ubuntu.com/ubuntu/ xenial main restricted universe multiverse
deb http://cn.archive.ubuntu.com/ubuntu/ xenial-security main restricted universe multiverse
deb http://cn.archive.ubuntu.com/ubuntu/ xenial-updates main restricted universe multiverse
deb http://cn.archive.ubuntu.com/ubuntu/ xenial-backports main restricted universe multiverse
然后执行sudo apt-get update即可。