Ubuntu16.04安装Opencv3.4.1教程

Posted shuqing-cxw

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Ubuntu16.04安装Opencv3.4.1教程相关的知识,希望对你有一定的参考价值。

一、编译安装

1.官网下载sources版本(For Linux)http://opencv.org/releases.html

2.解压:

1 unzip opencv-3.4.1.zip 

3.进入:

1 cd opencv-3.4.1.zip

4.安装cmake工具: 

1 sudo apt-get install cmake

5.安装依赖库:

1 sudo apt-get install build-essential libgtk2.0-dev libavcodec-dev libavformat-dev libjpeg.dev libtiff4.dev libswscale-dev libjasper-dev

6.创建编译目录(release)并进入:

1 mkdir release && cd release

7.cmake配置编译:

1 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 WITH_QT=ON -D WITH_OPENGL=ON ..

8.make编译:

1 make -j$(nproc)
2 // nproc是读取CPU的核心数量 

9.安装:

1 sudo make install

10.环境配置添加库路径(方式一):

1 sudo /bin/bash -c echo "/usr/local/lib" > /etc/ld.so.conf.d/opencv.conf

10.环境配置添加库路径(方式二):

1 sudo gedit /etc/ld.so.conf.d/opencv.conf 
2 //打开后可能是空文件,在文件内容最后添加
3 /usr/local/lib

11.更新系统库: 

1 sudo ldconfig

12.配置bash,执行如下命令

1 sudo gedit /etc/bash.bashrc 
2 //在末尾添加
3 PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig  
4 export PKG_CONFIG_PATH  

保存退出,然后执行如下命令使得配置生效

1 sudo source /etc/bash.bashrc
2 //激活配置然后更新database
3 sudo updatedb  

参考:https://blog.csdn.net/keith_bb/article/details/52685231

二、测试

在home下新建文件夹进入并新建名称为opencv_test.cpp

 1 #include <iostream>
 2 #include <opencv2/opencv.hpp>
 3 
 4 using namespace cv;
 5 using namespace std;
 6 
 7 int main(int argc, char** argv)
 8 {
 9 
10     if ( argc != 2 )
11     {
12         cout << "Need to load a picture..." << endl;
13         return -1;
14     }
15     Mat image;
16     image = imread( argv[1], 1 );
17     if ( image.empty() )
18     {
19         cout<<"No image data!"<< endl;
20         return -1;
21     }
22     namedWindow("Display Image", WINDOW_AUTOSIZE );
23     imshow("Display Image", image);
24     waitKey(0);
25     return 0;
26 }

建立一个CMakeLists.txt文件:

1 cmake_minimum_required(VERSION 2.8)
2 
3 project( DisplayImage )
4 
5 find_package( OpenCV REQUIRED )
6 
7 add_executable( opencv_test opencv_test.cpp )
8 
9 target_link_libraries( opencv_test ${OpenCV_LIBS} )

在桌面上放一张测试的图片,例如test.jpg,分别执行

1 cmake .
2 
3 make
4 
5 ./opencv_test test.jpg

即可显示图片

三、问题记录

1.Qt5Core编译错误

技术分享图片
  1 CMake Warning at cmake/OpenCVFindLibsGUI.cmake:18 (find_package):
  2 By not providing "FindQt5Core.cmake" in CMAKE_MODULE_PATH this project has
  3 asked CMake to find a package configuration file provided by "Qt5Core", but
  4 CMake did not find one.
  5 
  6 Could not find a package configuration file provided by "Qt5Core" with any
  7 of the following names:
  8 
  9 Qt5CoreConfig.cmake
 10 qt5core-config.cmake
 11 
 12 Add the installation prefix of "Qt5Core" to CMAKE_PREFIX_PATH or set
 13 "Qt5Core_DIR" to a directory containing one of the above files.  If
 14 "Qt5Core" provides a separate development package or SDK, be sure it has
 15 been installed.
 16 Call Stack (most recent call first):
 17 CMakeLists.txt:466 (include)
 18 
 19 CMake Warning at cmake/OpenCVFindLibsGUI.cmake:19 (find_package):
 20 By not providing "FindQt5Gui.cmake" in CMAKE_MODULE_PATH this project has
 21 asked CMake to find a package configuration file provided by "Qt5Gui", but
 22 CMake did not find one.
 23 
 24 Could not find a package configuration file provided by "Qt5Gui" with any
 25 of the following names:
 26 
 27   Qt5GuiConfig.cmake
 28   qt5gui-config.cmake
 29 
 30 Add the installation prefix of "Qt5Gui" to CMAKE_PREFIX_PATH or set
 31 "Qt5Gui_DIR" to a directory containing one of the above files.  If "Qt5Gui"
 32 provides a separate development package or SDK, be sure it has been
 33 installed.
 34 Call Stack (most recent call first):
 35 CMakeLists.txt:466 (include)
 36 
 37 
 38 CMake Warning at cmake/OpenCVFindLibsGUI.cmake:20 (find_package):
 39 By not providing "FindQt5Widgets.cmake" in CMAKE_MODULE_PATH this project
 40 has asked CMake to find a package configuration file provided by
 41 "Qt5Widgets", but CMake did not find one.
 42 
 43 Could not find a package configuration file provided by "Qt5Widgets" with
 44 any of the following names:
 45 
 46   Qt5WidgetsConfig.cmake
 47   qt5widgets-config.cmake
 48 
 49 Add the installation prefix of "Qt5Widgets" to CMAKE_PREFIX_PATH or set
 50 "Qt5Widgets_DIR" to a directory containing one of the above files.  If
 51 "Qt5Widgets" provides a separate development package or SDK, be sure it has
 52 been installed.
 53 
 54 Call Stack (most recent call first):
 55 
 56   CMakeLists.txt:466 (include)
 57 
 58 CMake Warning at cmake/OpenCVFindLibsGUI.cmake:21 (find_package):
 59 
 60   By not providing "FindQt5Test.cmake" in CMAKE_MODULE_PATH this project has asked CMake to find a package configuration file provided by "Qt5Test", but
 61 
 62   CMake did not find one.
 63 
 64   Could not find a package configuration file provided by "Qt5Test" with any
 65 
 66   of the following names:
 67 
 68 Qt5TestConfig.cmake
 69 qt5test-config.cmake
 70 
 71 Add the installation prefix of "Qt5Test" to CMAKE_PREFIX_PATH or set
 72 "Qt5Test_DIR" to a directory containing one of the above files.  If
 73 "Qt5Test" provides a separate development package or SDK, be sure it has
 74 been installed.
 75 Call Stack (most recent call first):
 76   CMakeLists.txt:466 (include)
 77 
 78 
 79 CMake Warning at cmake/OpenCVFindLibsGUI.cmake:22 (find_package):
 80   By not providing "FindQt5Concurrent.cmake" in CMAKE_MODULE_PATH this
 81   project has asked CMake to find a package configuration file provided by
 82   "Qt5Concurrent", but CMake did not find one.
 83 
 84   Could not find a package configuration file provided by "Qt5Concurrent"
 85   with any of the following names:
 86 
 87     Qt5ConcurrentConfig.cmake
 88     qt5concurrent-config.cmake
 89 
 90   Add the installation prefix of "Qt5Concurrent" to CMAKE_PREFIX_PATH or set
 91   "Qt5Concurrent_DIR" to a directory containing one of the above files.  If
 92   "Qt5Concurrent" provides a separate development package or SDK, be sure it
 93   has been installed.
 94   Call Stack (most recent call first):
 95   CMakeLists.txt:466 (include)
 96 
 97 qmake: could not exec /usr/lib/x86_64-linux-gnu/qt4/bin/qmake: No such file     or         directory
 98 CMake Error at /usr/share/cmake-2.8/Modules/FindQt4.cmake:1386 (message):
 99   Found unsuitable Qt version "" from NOTFOUND, this code requires Qt 4.x
100 Call Stack (most recent call first):
101   cmake/OpenCVFindLibsGUI.cmake:34 (find_package)
102   CMakeLists.txt:466 (include)
103 
104 -- Configuring incomplete, errors occurred!
105 See also "/home/*******/OpenCV/opencv-2.4.9/build/CMakeFiles/CMakeOutput.log".
106 See also "/home/*******/OpenCV/opencv-2.4.9/build/CMakeFiles/CMakeError.log".
Qt5Core-CMake did not find one

解决方式:

1 qt5-default:sudo apt-get install qt5-default

 参考地址:https://stackoverflow.com/questions/24378473/ubuntu-opencv-install-and-setup-qt5

以上是关于Ubuntu16.04安装Opencv3.4.1教程的主要内容,如果未能解决你的问题,请参考以下文章

ubuntu16.04安装opencv3.4.1及卸载,以及opencv与anaconda相关问题,import cv2,no module named cv2填坑指南

ubuntu16.04安装opencv3.4.1及卸载,以及opencv与anaconda相关问题,import cv2,no module named cv2填坑指南

Ubuntu 16.04 安装 Leptonica 1.75.3

Ubuntu16.04下安装opencv-3.4.1

Linux ubuntu16.04 安装opencv4教程(源码编译)

在ubuntu18.04一键安装opencv3.4.1的脚本