1. 程式人生 > >Raspbian Ubuntu下安裝OpenCV2.4.9的詳細過程及碰到的問題和第一次程式的編譯除錯

Raspbian Ubuntu下安裝OpenCV2.4.9的詳細過程及碰到的問題和第一次程式的編譯除錯

1.下載若干依賴項     在開始安裝之前,最好更新樹莓派軟體源。如果更新時間太長,請參考博文修改軟體源網路地址——樹莓派學習筆記——修改樹莓派軟體源 sudo apt-get update     請依次安裝以下依賴項,這些必須安裝的依賴項來自於opencv官網的說明,在多數debian系統中都可以採用這種方式進行安裝。其實在樹莓派中build-essential已經預設安裝,但是cmake等其他內容仍需要手動安裝。 sudo apt-get install build-essential sudo apt-get install cmake sudo apt-get install libgtk2.0-dev
sudo apt-get install pkg-config sudo apt-get install python-dev python-numpy sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev 2.獲得原始碼     在樹莓派使用者目錄中建立opencv資料夾 mkdir opencv     通過sourceforge網站獲取opencv原始碼,本文選擇2.4.9版本。下載過程可能比較緩慢,請耐心等待。 wget http://sourceforge.net/projects/opencvlibrary/files/opencv-unix/2.4.9/opencv-2.4.9.zip/download opencv-2.4.9.zip
    解壓原始碼,獲得一個名為opencv-2.4.9的資料夾 unzip opencv-2.4.9.zip 3.編譯原始碼     進入opencv原始碼目錄,在該目錄中新建一個名為release的資料夾。 cd ~/opencv/opencv-2.4.9 mkdir release cd release     使用cmake工具在該資料夾中生成makefile檔案,指定opencv的安裝目錄為/usr/local,opencv的原始碼位於上一層目錄。 cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local ..
    編譯原始碼並安裝(編譯原始碼的時間非常的長,make之後我便睡去,第二天早上才執行install) make(這個過程至少半小時,需要耐心等待) sudo make install     更新搜尋動態連結庫

sudo ldconfig

****cmake 當中遇到的問題*****************

Ubuntu 上cmake編譯OpenCV,遇到一個錯誤:

usr/include/c++/6/cstdlib:75:25: fatal error: stdlib.h: 沒有那個檔案或目錄

很是奇怪,百度搜索了下,得知是GCC6的一個改動造成的。

自己的gcc是6.3.0

解決辦法:
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local .. -DENABLE_PRECOMPILED_HEADERS=OFF

****make 當中遇到的問題*****************

1、/home/pikachu/dev/opencv-2.4.9/modules/contrib/src/chamfermatching.cpp:In member function ‘void cv::ChamferMatcher::Matching::computeDistanceTransform(cv::Mat&, cv::Mat&, cv::Mat&, float, float, float)’:/home/pikachu/dev/opencv-2.4.9/modules/contrib/src/chamfermatching.cpp:969:30:error: the compiler can assume that the address of ‘annotate_img’ will never be NULL [-Werror=address]

if (&annotate_img!=NULL) { ^/home/pikachu/dev/opencv-2.4.9/modules/contrib/src/chamfermatching.cpp:1016:34:error: the compiler can assume that the address of ‘annotate_img’ will never be NULL [-Werror=address] if (&annotate_img!=NULL) { ^/home/pikachu/dev/opencv-2.4.9/modules/contrib/src/chamfermatching.cpp:In member function ‘cv::ChamferMatcher::Match* cv::ChamferMatcher::Matching::localChamferDistance(cv::Point, cv::Mat&, cv::Mat&, cv::ChamferMatcher::Template*, float)’:/home/pikachu/dev/opencv-2.4.9/modules/contrib/src/chamfermatching.cpp:1111:25:error: the compiler can assume that the address of ‘orientation_img’ will never be NULL [-Werror=address] if (&orientation_img!=NULL) { ^/home/pikachu/dev/opencv-2.4.9/modules/contrib/src/chamfermatching.cpp:1111:5:warning: nonnull argument ‘orientation_img’ compared to NULL [-Wnonnull-compare] if (&orientation_img!=NULL) { ^~/home/pikachu/dev/opencv-2.4.9/modules/contrib/src/chamfermatching.cpp:In member function ‘void cv::ChamferMatcher::Matching::computeDistanceTransform(cv::Mat&, cv::Mat&, cv::Mat&, float, float, float)’:/home/pikachu/dev/opencv-2.4.9/modules/contrib/src/chamfermatching.cpp:1016:17:warning: nonnull argument ‘annotate_img’ compared to NULL [-Wnonnull-compare] if (&annotate_img!=NULL) { ^~/home/pikachu/dev/opencv-2.4.9/modules/contrib/src/chamfermatching.cpp:969:13:warning: nonnull argument ‘annotate_img’ compared to NULL [-Wnonnull-compare] if (&annotate_img!=NULL) { ^~cc1plus: some warnings being treated as errorsmodules/contrib/CMakeFiles/opencv_contrib.dir/build.make:158: recipe for target 'modules/contrib/CMakeFiles/opencv_contrib.dir/src/chamfermatching.cpp.o' failedmake[2]: *** [modules/contrib/CMakeFiles/opencv_contrib.dir/src/chamfermatching.cpp.o] Error1CMakeFiles/Makefile2:3195: recipe for target 'modules/contrib/CMakeFiles/opencv_contrib.dir/all' failedmake[1]: *** [modules/contrib/CMakeFiles/opencv_contrib.dir/all] Error2

解決辦法:

 release/modules/contrib/CMakeFiles/opencv_contrib.dir/flags.make檔案 , 刪掉-Werror=address,然後重新 $make 解決完上面的問題就完成了OpenCV的交叉編譯了。 接下來編譯第一個程式:

首先:配置環境

將opencv的庫加入到路徑,從而讓系統可以找到

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

儲存退出

sudo source /etc/bash.bashrc  #使配置生效
(該步驟可能會報錯找不到命令,原因是source為root命令

sudo passwd root(設定root密碼,如果之前沒有設定過)

輸入新密碼

su(進入root許可權)

輸入密碼

source /etc/bash.bashrc

exit(退出root)


sudo updatedb #更新database

採取直接編譯的方式

g++ helloworld.cpp -o helloworld `pkg-config --cflags --libs opencv`(注意ESC下面的`,不是單引號)(注意後面的依賴庫)

編譯通過後再執行

./helloworld hello.jpg

即可看到圖片顯示,安裝完成。