Ubuntu16.04安装opencv-3.4.2
Posted systemcall
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Ubuntu16.04安装opencv-3.4.2相关的知识,希望对你有一定的参考价值。
原文链接:
https://m.oldpan.me/archives/ubuntu-install-opencv-from-source
第一步:更新我们的系统
sudo apt-get update sudo apt-get upgrade
第二步:安装所有依赖库
# 首先我们先移除系统中已经存在的依赖,这一部必须要做 sudo apt-get remove x264 libx264-dev # 然后安装我们需要的依赖 sudo apt-get install build-essential checkinstall cmake pkg-config yasm sudo apt-get install git gfortran sudo apt-get install libjpeg8-dev libjasper-dev libpng12-dev # 下面根据版本选择安装 # Ubuntu 14.04 sudo apt-get install libtiff4-dev # Ubuntu 16.04 sudo apt-get install libtiff5-dev sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libdc1394-22-dev sudo apt-get install libxine2-dev libv4l-dev sudo apt-get install libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev sudo apt-get install qt5-default libgtk2.0-dev libtbb-dev sudo apt-get install libatlas-base-dev sudo apt-get install libfaac-dev libmp3lame-dev libtheora-dev sudo apt-get install libvorbis-dev libxvidcore-dev sudo apt-get install libopencore-amrnb-dev libopencore-amrwb-dev sudo apt-get install x264 v4l-utils
sudo apt-get install qt5-default # 可选安装 sudo apt-get install libprotobuf-dev protobuf-compiler sudo apt-get install libgoogle-glog-dev libgflags-dev sudo apt-get install libgphoto2-dev libeigen3-dev libhdf5-dev doxygen
第三步:安装python依赖(可选)
sudo apt-get install python3-dev pip3 install numpy scipy matplotlib scikit-image scikit-learn ipython
第四步:下载opencv-3.4.2
https://opencv.org/releases.html
第五步:安装opencv-3.4.2
1)进入我们的opencv
目录,创建一个build
的文件夹并进入
cd opencv
mkdir build
cd build
2)cmake配置
cmake -D CMAKE_BUILD_TYPE=Release -D MAKE_INSTALL_PREFIX=/usr/local ..
3)等待漫长的编译过程
make
4)安装
sudo make install
5)环境配置添加库路径
sudo sh -c ‘echo "/usr/local/lib" >> /etc/ld.so.conf.d/opencv.conf‘
6)更新系统库
sudo ldconfig
7)配置bash
sudo vim /etc/bash.bashrc //在末尾添加 PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig export PKG_CONFIG_PATH
保存退出,然后执行如下命令使得配置生效
source /etc/bash.bashrc //激活配置然后更新database sudo updatedb
第六步:测试
1)在home下新建文件夹进入并新建名称为opencv_test.cpp
#include <iostream> #include <opencv2/opencv.hpp> using namespace cv; using namespace std; int main(int argc, char** argv) { if ( argc != 2 ) { cout << "Need to load a picture..." << endl; return -1; } Mat image; image = imread( argv[1], 1 ); if ( image.empty() ) { cout<<"No image data!"<< endl; return -1; } namedWindow("Display Image", WINDOW_AUTOSIZE ); imshow("Display Image", image); waitKey(0); return 0; }
2)建立一个CMakeLists.txt文件
cmake_minimum_required(VERSION 2.8) project( DisplayImage ) find_package( OpenCV REQUIRED ) add_executable( opencv_test opencv_test.cpp ) target_link_libraries( opencv_test ${OpenCV_LIBS} )
3)在home目录中放一张测试的图片,例如test.jpg,分别执行
cmake .
make
./opencv_test ~/test.jpg
图片显示成功!
在opencv_test.cpp文件创建成功后,或可直接使用如下命令生成可执行程序
g++ `pkg-config opencv --cflags` opencv_test.cpp -o opencv_test `pkg-config opencv --libs`
注意:这里是` `(Tab键上面的那个键),而不是单引号‘ ‘
卸载源码安装的opencv
在之前创建的build目录中执行
sudo make uninstall
如果卸载不干净,则执行下面的命令:
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*
在编译过程中可能遇到的问题
Scanning dependencies of target opencv_cudafilters [ 45%] Building CXX object modules/cudafilters/CMakeFiles/opencv_cudafilters.dir/src/filtering.cpp.o cc1plus: warning: /home/prototype/Downloads/opencv-3.4.0/build/modules/cudafilters/precomp.hpp.gch/opencv_cudafilters_RELEASE.gch: not used because ‘OPENCV_TRAITS_ENABLE_DEPRECATED‘ is defined [-Winvalid-pch] cc1plus: warning: /home/prototype/Downloads/opencv-3.4.0/build/modules/cudafilters/precomp.hpp.gch/opencv_cudafilters_Release.gch: not used because ‘OPENCV_TRAITS_ENABLE_DEPRECATED‘ is defined [-Winvalid-pch] [ 45%] Linking CXX shared library ../../lib/libopencv_cudafilters.so [ 45%] Built target opencv_cudafilters Makefile:162: recipe for target ‘all‘ failed make: *** [all] Error 2
遇到上面的问题,一般是之前OpenCV残留没有删除干净,删除干净重来即可。
以上是关于Ubuntu16.04安装opencv-3.4.2的主要内容,如果未能解决你的问题,请参考以下文章
ubuntu 16.04安装cv2, opencv3,无需编译
ubuntu18.04安装opencv3.4.3遇到问题解决方法
ubuntu 14.04 编译opencv-3.4.2 报错: /usr/bin/ld: /usr/local/lib/libavformat.a(allformats.o): relocation