ubuntu安装、卸载opencv以及报错解决措施

一、安装opencv

1、安装依赖库

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install build-essential libgtk2.0-dev libavcodec-dev libavformat-dev libjpeg-dev libswscale-dev 
sudo apt-get install libtiff4-dev
sudo apt-get install libgtk2.0-dev
sudo apt-get install pkg-config

注意:这里libtiff的版本是4,好多教程默认安装的是libtiff5,如果直接sudo apt-get install libtiff-dev不加版本,会默认安装5的版本,我第一次安装opencv3.4.6的时候,在make的时候会报错:

~~对‘TIFFReadRGBAStrip@LIBTIFF_4.0’未定义的引用;

2、下载opencv

下载地址:https://opencv.org/releases/
点击Sources进行下载自己需要的版本

进入到解压的文件夹中:

在opencv3文件夹下新建build文件夹

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

3、配置环境

用gedit打开/etc/ld.so.conf

sudo gedit /etc/ld.so.conf.d/opencv.conf 

在文件中加上一行 /usr/loacal/lib

/usr/loacal/lib

其中/user/loacal是opencv安装路径也就是makefile中指定的安装路

更新系统:

sudo ldconfig

修改bash.bashrc文件:

sudo gedit /etc/bash.bashrc 

在文件末尾加入:

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

然后在命令行中输入

pkg-config opencv --modversion

4、检验

在命令行中输入如下命令:

pkg-config opencv --modversion

二、运行opencv中出现的报错

1、在跑相关代码时报错:

OpenCV Error: Unspecified error (The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Carbon support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script) in cvNamedWindow, file /home/nick/.Apps/opencv/modules/highgui/src/window.cpp, line 516

意思就是提示我libgtk2.0-dev和pkg-config这两个依赖没装,但是之前明明已经装了?

解决办法:

安装aptitude

sudo apt-get install aptitude

然后再安装libgtk2.0-dev

sudo aptitude install libgtk2.0-dev

 查看是否已安装libgtk2.0-dev

sudo aptitude search libgtk2.0-dev
//结果应该像这样:
i  libgtk2.0-dev              - development files for the GTK+ library 
p  libgtk2.0-dev:i386         - development files for the GTK+ library

然后重新编译opencv:

进入opencv安装目录:
cd /home/user_name/OpenCv
mkdir Release
cd Release
//cmake
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_TBB=ON -D BUILD_NEW_PYTHON_SUPPORT=ON -D WITH_V4L=ON -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D BUILD_EXAMPLES=ON -D WITH_QT=ON -D WITH_GTK=ON -D WITH_OPENGL=ON ..
//cmake的时候别忘了用 WITH_GTK=ON
//然后编译
make
sudo make install
//ok,问题解决

2、运行代码时报错:

~~对‘TIFFReadRGBAStrip@LIBTIFF_4.0’未定义的引用;

原因:开始安装依赖库的时候安装的是sudo apt-get install libtiff5-dev即tiff5的版本,我安装的opencv3.4.6需要的是tiff4,所以删除tiff5:

sudo apt-get remove libtiff5-dev

安装tiff4:

sudo apt-get install libtiff4-dev

后面再重新cmake和make一下(按照上面的方法和命令)

三、完全卸载opencv

1、进入opencv安装目录(/home下的那个目录)

sudo make uninstall
cd ..
sudo rm -r build

2、删除相关内容:

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*
sudo apt-get purge libopencv* 
sudo apt-get --purge remove opencv-doc opencv-data python-opencv
cd /usr
find . -name "*opencv*" | xargs sudo rm -rf
apt-get remove-doc opencv-data python-opencv

3、检查是否删除完

pkg-config opencv --libs
pkg-config opencv --modversion

你可能感兴趣的:(opencv,linux,ubuntu)