Ubuntu16.04安装opencv3.4.1

前端之家收集整理的这篇文章主要介绍了Ubuntu16.04安装opencv3.4.1前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. 去官网下载opencv,在本教程中选用的时opencv3.4.1,其他版本的配置方法异曲同工。
    下载链接http://opencv.org/releases.html,选择sources版本
  2. 解压下载下来的zip包
  3. 进入到解压后的文件包中
  4. 安装依赖库和cmake ,如果提醒需要apt-get update,那就先sudo su进入root权限,再sudo apt-get update,然后在执行下面命令
  1. sudo apt-get install cmake
  1. sudo apt-get install build-essential libgtk2.0-dev libavcodec-dev libavformat-dev libjpeg.dev libtiff4.dev libswscale-dev libjasper-dev

5.安装完cmake之后执行命令,创建编译文件夹,不创建的会提示In-source builds are not allowed.

  1. mkdir my_build_dir
  2. cd my_build_dir

6.cmake一下

  1. cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local ..

7.执行命令,漫长的编译过程

期间可能会出现一些ERROR; 下面是我遇上的及解决办法.

首先如果以前有装了ffmpeg,先到ffmpeg的目录下uninstall,否则opencv安装会有冲突

  1. moc: Cannot open options file specified with @
  2. Usage: moc [options] <header-file>
  3. -o<file> write output to file rather than stdout
  4. -I<dir> add dir to the include path for header files
  5. -E preprocess only; do not generate Meta object code
  6. -D<macro>[=<def>] define macro,with optional definition
  7. -U<macro> undefine macro
  8. -i do not generate an #include statement
  9. -p<path> path prefix for included file
  10. -f[<file>] force #include,optional file name
  11. -nn do not display notes
  12. -nw do not display warnings
  13. @<file> read additional options from file
  14. -v display version of moc
  15. make[2]: *** [modules/highgui/src/moc_window_QT.cxx] Error 1
  16. make[1]: *** [modules/highgui/CMakeFiles/opencv_highgui.dir/all] Error 2
  17. make: *** [all] Error 2

解决办法:If there is an accented character in the path name to the source files you may get this error. Move the source directory to a directory so that there is no accented characters in the path name. Then restart the compilation.

因为我放在了有中文的路径下,后来挪到没中文的路径下就不会报这个错误了;

  1. Traceback (most recent call last):
  2. File "/media/stefan/A050780B5077E706/_raspi/opencv/sources/modules/java/generator/gen_java.py",line 1559,in <module>
  3. generator.gen(srcfiles,module,dstdir)
  4. File "/media/stefan/A050780B5077E706/_raspi/opencv/sources/modules/java/generator/gen_java.py",line 1061,in gen
  5. self.gen_class(ci)
  6. File "/media/stefan/A050780B5077E706/_raspi/opencv/sources/modules/java/generator/gen_java.py",line 1460,in gen_class
  7. for fi in ci.getAllMethods():
  8. File "/media/stefan/A050780B5077E706/_raspi/opencv/sources/modules/java/generator/gen_java.py",line 824,in getAllMethods
  9. result.extend([fi for fi in sorted(self.methods) if fi.isconstructor])
  10. TypeError: unorderable types: FuncInfo() < FuncInfo()
  11. modules/java/CMakeFiles/opencv_java.dir/build.make:86: recipe for target 'modules/java/photo+CalibrateCRF.java' Failed
  12. make[2]: *** [modules/java/photo+CalibrateCRF.java] Error 1
  13. CMakeFiles/Makefile2:6282: recipe for target 'modules/java/CMakeFiles/opencv_java.dir/all' Failed
  14. make[1]: *** [modules/java/CMakeFiles/opencv_java.dir/all] Error 2
  15. Makefile:137: recipe for target 'all' Failed
  16. make: *** [all] Error 2

解决办法:

  1. cmake -D CMAKE_BUILD_TYPE=RELEASE -D WITH_QT=ON -D WITH_OPENGL=ON -D WITH_TBB=ON -D CMAKE_INSTALL_PREFIX=$(python3 -c "import sys; print(sys.prefix)") -D BUILD_EXAMPLES=ON -D PYTHON_EXECUTABLE=$(which python3) -D BUILD_opencv_java=OFF BUILD_opencv_test_java=OFF ..

8.执行命令

  1. sudo make install

9.sudo make install 执行完毕后OpenCV编译过程就结束了,接下来就需要配置一些OpenCV的编译环境首先将OpenCV的库添加到路径,从而可以让系统找到

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

执行此命令后打开的可能是一个空白的文件,不用管,只需要在文件末尾添加

  1. /usr/local/lib

10.执行如下命令使得刚才的配置路径生效

  1. sudo ldconfig

11.配置bash

  1. sudo gedit /etc/bash.bashrc

在最末尾添加

  1. PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
  2. export PKG_CONFIG_PATH

保存,执行如下命令使得配置生效

  1. source /etc/bash.bashrc

更新

  1. sudo updatedb
12.至此所有的配置都已经完成

下面用一个小程序测试一下

找到
cd到opencv-3.4.1/samples/cpp/example_cmake目录下
我们可以看到这个目录里官方已经给出了一个cmake的example我们可以拿来测试下

按顺序执行

  1. cmake .
  2. make
  3. ./opencv_example
即可看到打开了摄像头,在左上角有一个hello opencv 即表示配置成功

猜你在找的Ubuntu相关文章