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
sudo apt-get install python3-dev pip3 install numpy scipy matplotlib scikit-image scikit-learn ipython
https://opencv.org/releases.html
1)進入咱們的opencv
目錄,建立一個build
的文件夾並進入html
cd opencv
mkdir build
cd build
2)cmake配置python
cmake -D CMAKE_BUILD_TYPE=Release -D MAKE_INSTALL_PREFIX=/usr/local ..
3)等待漫長的編譯過程ios
make
4)安裝git
sudo make install
5)環境配置添加庫路徑ubuntu
sudo sh -c 'echo "/usr/local/lib" >> /etc/ld.so.conf.d/opencv.conf'
6)更新系統庫vim
sudo ldconfig
7)配置bashbash
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.cppui
#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文件google
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鍵上面的那個鍵),而不是單引號' '
在以前建立的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殘留沒有刪除乾淨,刪除乾淨重來便可。