Ubuntu16.04下安装opencv3.3和opencv_contrib(命令行及cmake-gui安装)及卸载

下载opencv3.3.1及opencv-3.3.contrib

opencv3.3.1

https://github.com/opencv/opencv/releases/tag/3.3.1
选择source code(tar.gz)

opencv_contrib

https://github.com/opencv/opencv_contrib/releases/tag/3.3.1
选择source code(tar.gz)

安装相关依赖包

CTRL+T打开终端,在解压opencv之前输入以下命令,安装相关依赖包,一般装前三个命令就行,一定要确认GTK2.0已经装好

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 build-essential qt5-default ccache libv4l-dev libavresample-dev  libgphoto2-dev libopenblas-base libopenblas-dev doxygen  openjdk-8-jdk pylint libvtk6-dev

sudo apt-get install pkg-config

注意

这儿一定要确定pkg-config和libgtk2.0-dev已经装上,不然后续写程序用了imshow会报错,查了一下只能重装opencv才能解决咯

解压,编译

解压opencv3.3.1及opencv-3.3.contrib
并将opencv3.3.1放到opencv目录下,将opencv-3.3.contrib放到opencv-3.3.1目录下
最后在opencv3.3.1目录下创建一个build文件夹


cd opencv-3.3.1

sudo mkdir build                              #新建文件夹build

进入build目录

执行cmake生成makefile文件

cd build  

sudo cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local -D OPENCV_EXTRA_MODULES_PATH=/home/opencv/opencv-3.3.1/opencv_contrib-3.3.1/modules/ ..

OPENCV_EXTRA_MODULES_PATH就是你 opencv_contrib-3.3.1下面的modules目录,且后面有两个…不能缺少

生成完毕之后,提示如下:

--   Install path:                  /usr/local
-- 
--   cvconfig.h is in:              /home/user/opencv-3.3.1/build
-- -----------------------------------------------------------------
-- 
-- Configuring done
-- Generating done
-- Build files have been written to: /home/user/opencv-3.3.1/build

cmake完毕之后,再进行make,在build文件夹下进行make:

sudo make -j16    	#16线程编译,这个线程数依据个人电脑配置而定
sudo make install

成功之后再

make install

链接库共享

编译安装完毕之后,为了让你的链接库被系统共享,让编译器发现,需要执行管理命令ldconfig

sudo ldconfig -v

cmake-gui安装opencv及contrib

sudo apt-get install cmake-qt-gui

在提取好的opencv3.3目录下创建build文件夹

mkdir build
cd build 
cmake-gui ..

然后会弹出cmake-gui界面
与windows下的opencv安装其实是一样的
先填好路径再configure

这是我之后换的3.4.2版本(别介意,安装方法都是一样的)
Ubuntu16.04下安装opencv3.3和opencv_contrib(命令行及cmake-gui安装)及卸载_第1张图片

这儿需要做以下几步

勾上Nonfree模块

Ubuntu16.04下安装opencv3.3和opencv_contrib(命令行及cmake-gui安装)及卸载_第2张图片

找到opencv_extra_modules_path

在后面的values中添加opencv-contrib中modules的路径,一定要选中modules文件夹
Ubuntu16.04下安装opencv3.3和opencv_contrib(命令行及cmake-gui安装)及卸载_第3张图片

不装cuda的话可以把cuda的勾去了

Ubuntu16.04下安装opencv3.3和opencv_contrib(命令行及cmake-gui安装)及卸载_第4张图片

防止出现.GTK2.0的错误,勾上

Ubuntu16.04下安装opencv3.3和opencv_contrib(命令行及cmake-gui安装)及卸载_第5张图片

在cmake_build_type处填上RELEASE

Ubuntu16.04下安装opencv3.3和opencv_contrib(命令行及cmake-gui安装)及卸载_第6张图片
然后GENERATE

其中可能会遇到IPPICV的问题,一定要确认其装好了,按文末方法解决即可

然后在build下打开终端,最好用root权限执行以下命令,不然容易报错,输入

make  -j16
sudo make install

环境变量配置

然后配置环境变量即可

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

添加以下内容

/usr/local/lib

配置库

sudo ldconfig

更改环境变量

sudo gedit /etc/bash.bashrc

在文件后添加

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

保存退出,重新开启终端来使配置生效。

到此,安装和配置的整个过程都完成了!

问题

其间可能会遇到许多问题

很多依赖项,如果遇到问题,一个个谷歌百度,一定要学会看控制台输出的命令错误在哪,
比如缺失文件啥的

  • ippicv的问题

在下面网站下载对应名称的ippicv文件
https://github.com/opencv/opencv_3rdparty/tree/ippicv/master_20170822/ippicv

保存路径随意,一般是自动放在默认的下载路径/home/user/下载

修改opencv里相关配置文件

打开终端,输入
gedit /home/user/opencv_source/opencv/3rdparty/ippicv/ippicv.cmake #记得user换成自己的用户名

将47行的

"https://raw.githubusercontent.com/opencv/opencv_3rdparty/${IPPICV_COMMIT}/ippicv/"

改为从网站中下载下来的文件的本地路径:

 "file:///home/user/下载/" #(根据自己的路径填写)
  • vtk is not found

    sudo apt-get install libproj-dev

以及下列问题

The imported target "vtkRenderingPythonTkWidgets" references the file
   "/usr/lib/x86_64-linux-gnu/libvtkRenderingPythonTkWidgets.so"
but this file does not exist.  Possible reasons include:
* The file was deleted, renamed, or moved to another location.
* An install or uninstall procedure did not complete successfully.
* The installation package was faulty and contained
   "/usr/lib/cmake/vtk-6.2/VTKTargets.cmake"
but not all the files it references.

-- The imported target "vtk" references the file
   "/usr/bin/vtk"
but this file does not exist.  Possible reasons include:
* The file was deleted, renamed, or moved to another location.
* An install or uninstall procedure did not complete successfully.
* The installation package was faulty and contained
   "/usr/lib/cmake/vtk-6.2/VTKTargets.cmake"
but not all the files it references.

解决办法:

sudo apt install python-vtk

cmake could not find atlas
解决办法:
cmake -DBLAS=open …

或者直接不管,比如下列问题,因为不需要用上java和matlab,所以就先不管它们

cmake could not find jin(missing: JAVA_AWT_LIBRARY JAVA_JVM_LIBRARY JAVA_INCLUDE_PATH JAVA_INCLUDE_PATH)

cmake could not find matlab

又比如在下载tiny-dnn的时候卡住,用不上神经网络的话可以不装,让它卡一会,等等就可以configure done了

再比如还有以下错误提示

file cannot create directory: /usr/local/include/opencv2.  Maybe need
  administrative privileges.

Makefile:148: recipe for target 'install' failed
make: *** [install] Error 1

解决办法:
千万要用sudo ,而不能只用make install,不然就报错

sudo make install

另外可能

modules/viz/CMakeFiles/opencv_test_viz.dir/build.make:233: recipe for target 'bin/opencv_test_viz' failed
make[2]: *** [bin/opencv_test_viz] Error 1
CMakeFiles/Makefile2:5627: recipe for target 'modules/viz/CMakeFiles/opencv_test_viz.dir/all' failed
make[1]: *** [modules/viz/CMakeFiles/opencv_test_viz.dir/all] Error 2
make[1]: *** Waiting for unfinished jobs...

解决办法:

sudo apt-get autoremove libtiff5-dev

sudo apt-get install libtiff5-dev

另外可能有一些文件需要更高权限

更改文件夹和子文件夹权限

打开终端进入你需要修改的目录
然后执行下面这条命令

chmod 777 * -R

opencv卸载

1.查看已安装的版本

pkg-config --modversion opencv

2.进入安装opencv的目录的build文件夹

cd /home/***/opencv/build
sudo make uninstall
cd  ..
sudo rm -r build

3.清理 /usr 中所有 opencv 相关项

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*
cd /usr
find . -name "*opencv*" | xargs sudo rm -rf
 
cd /home/***
chmod a+x /home/***/opencv
rm -r /home/***/opencv

4.再查看版本

pkg-config --modversion opencv

此时显示opencv已经not found了

你可能感兴趣的:(软件安装)