Ubuntu16.04卸载旧版OpenCV安装新版OpenCV3.4.11+contrib3.4.11

首先上传一份解决出错问题的opencv3.4.11源码 opencv3.4.11.

一、卸载已安装的opencv

0、安装opencv需要的一些库,能安装的尽量都安装上(预备)

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
 
sudo apt-get install --assume-yes libopencv-dev libdc1394-22 libdc1394-22-dev libjpeg-dev libpng12-dev libtiff5-dev libjasper-dev libavcodec-dev libavformat-dev libswscale-dev libxine2-dev libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev libv4l-dev libtbb-dev libqt4-dev libfaac-dev libmp3lame-dev libopencore-amrnb-dev libopencore-amrwb-dev libtheora-dev libvorbis-dev libxvidcore-dev x264 v4l-utils unzip
 
sudo apt-get install ffmpeg libopencv-dev libgtk-3-dev python-numpy python3-numpy libdc1394-22 libdc1394-22-dev libjpeg-dev libpng12-dev libtiff5-dev libjasper-dev libavcodec-dev libavformat-dev libswscale-dev libxine2-dev libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev libv4l-dev libtbb-dev qtbase5-dev libfaac-dev libmp3lame-dev libopencore-amrnb-dev
 
sudo apt-get install libgtk-3-dev
 
sudo apt-get -y install libgstreamer-plugins-base1.0-dev
 
sudo apt-get -y install libgstreamer1.0-dev
 
sudo apt-get -y install libavresample-dev
 
sudo apt-get -y install libgphoto2-dev
 
sudo apt-get install libopenblas-dev
 
sudo apt-get install doxygen
 

1、查看已安装的opencv版本

$ pkg-config --modversion opencv

2、卸载

 首先要找到当初安装opencv的build目录,进入该build目录执行卸载操作
$  sudo make uninstall
$  cd  ..
$  rm -r build
如果找不见该build目录,可以重新建立build目录安装对应版本,然后再执行上边卸载步骤。然后清理/usr中所有opencv相关项
$  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*
$  cd /usr
$  find . -name "*opencv*" | xargs sudo rm -rf
移除python相关

$ apt-get remove-doc opencv-data python-opencv
如果找不见当时的安装目录,可能被删除了,就需要重新安装一遍,安装之后就可以进行uninstall操作了!
https://sourceforge.net/projects/opencvlibrary/files/ 由上述网站下载对应版本
安装再重新uninstall

cmake ..
make -j4
sudo make install

3.OpenCV3.4.11+contrib3.4.11的安装:

(记得下载对应版本)
OpenCV:https://github.com/opencv/opencv/releases
contrib:https://github.com/opencv/opencv_contrib/tree/3.4.3
下载opencv,解压,同时下载解压并将contrib也放在文件夹下,

cd opencv-3.4.11
mkdir release
cd release

然后在新建的这个文件里运行cmake:

cmake -DCMAKE_BUILD_TYPE=Release \
-DOPENCV_GENERATE_PKGCONFIG=ON \
-DCMAKE_INSTALL_PREFIX=/usr/local \
-D OPENCV_EXTRA_MODULES_PATH=../opencv_contrib/modules ..

记得修改自己的contrib路径;
可能或出现ubuntu16.04 安装opencv IPPICV 和 face_landmark_model.dat下载不下来的问题解决。
根据该博客解决:

二、解决ippiv和face_landmark_model.dat及boostdesc_bgm.i问题

1. ippicv问题:

当安装opencv时卡在ippicv下载的地方,解决办法:
手动下载ippicv:
ippicv2015: ippicv2015.
ippicv2017: ippicv2017.

打开终端,输入
$ gedit 你的目录/opencv_path/opencv/3rdparty/ippicv/ippicv.cmake
#将ippicv.cmake文件下的
"https://raw.githubusercontent.com/opencv/opencv_3rdparty/${IPPICV_COMMIT}/ippicv/"
替换为
"file:///home/usrname/install/"这是你的本地ippicv文件目录
重新编译即可

2. 卡在face_landmark_model.dat下载问题:

手动下载:face_landmark_model.dat
终端输入
$ gedit 你的目录/opencv-3.4.0/opencv_contrib-3.4.0/modules/face/CMakeLists.txt
"https://raw.githubusercontent.com/opencv/opencv_3rdparty/${__commit_hash}/"替换为
"file:///home/usrname/install/"你的文件目录

3. make时的boostdesc_bgm.i错误

错误内容:
~/opencv_contrib/modules/xfeatures2d/src/boostdesc.cpp:673:20: fatal error: boostdesc_bgm.i: No such file or directory
解决办法:boostdesc_bgm等文件(https://raw.githubusercontent.com/opencv/opencv_3rdparty/8afa57abc8229d611c4937165d20e2a2d9fc5a12/face_landmark_model.dat)
下载上述文件放在:你的目录/opencv-3.4.11/.cache/xfeatures2d/src/下

4. make时的opencv2/xfeatures2d.hpp错误

错误内容:
opencv_contrib/modules/sfm/src/simple_pipeline.cpp:44:35: fatal error: opencv2/xfeatures2d.hpp: No such file or directory
解决办法:
在Opencv的CmakeLists.txt 中加入以下include语句:

INCLUDE_DIRECTORIES("/home/yourusername/Dependencies/opencv-3.4.9/opencv_contrib-3.4.9/modules/xfeatures2d/include")

重新编译

三、编译(可以使用上面打包好的源码)

运行camke

cmake -DCMAKE_BUILD_TYPE=Release \
-DOPENCV_GENERATE_PKGCONFIG=ON \
-DCMAKE_INSTALL_PREFIX=/usr/local \
-D OPENCV_EXTRA_MODULES_PATH=../opencv_contrib/modules ..

其中:OpenCV4默认不生成.pc文件,OPENCV_GENERATE_PKGCONFIG=ON才会生成(后面才可显示版本)
OPENCV_EXTRA_MODULES_PATH添加外部库
之后就是编译和安装了,我的电脑是四核八线程,可以运行make -j8,但我不想编译时影响我工作,所以用-j7,如果是四核可用make -j4。

make -j4
sudo make install

环境配置:

首先将OpenCV的库添加到路径,从而可以让系统找到打开opencv.conf ,打开后很可能是空白
sudo gedit /etc/ld.so.conf.d/opencv.conf
在文末添加

/usr/local/lib

保存后,执行如下命令使得刚才的配置路径生效
sudo ldconfig

接下来配置 bash
sudo gedit /etc/bash.bashrc
在文件最末尾添加

PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig  
export PKG_CONFIG_PATH  

保存,执行如下命令使得配置生效
source /etc/bash.bashrc
然后更新
sudo updatedb
到这里配置就完成了

测试

转到 opencv-4.1.0/sample/cpp/example_cmake 目录下,打开终端

运行

cmake .
make
./opencv_example
如果摄像头打开,左上角出现 Hello OpenCV 则证明安装成功

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页