歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> 在Linux下配置OpenCV環境

在Linux下配置OpenCV環境

日期:2017/2/28 15:42:42   编辑:Linux教程

配了一天的OpenCV環境,終於能編譯運行程序了,我的系統是Ubuntu 12.04LTS ,下載的OpenCV版本是目前最新的OpenCV 2.4.2

1、准備好源碼,可以直接下載,也可以svn弄下來

要准備的東東就是上網下載個Linux版的OpenCV啦,zip格式的。解壓到一個地方,我放到機子的地方是/home/star/apps/裡面。

如今的目錄狀態是:/home/star(這是我的用戶名啊,和你不一樣)/apps(這是我習慣放程序的地方,神碼pdf閱讀器就是放這的)/OpenCV/(這裡就好多OpenCV的文件,下面要在裡面cmake的)

2、下載OpenCV所需要的依賴文件

sudo -sH 變成超人

然後狂apt-get install。。。。

具體install什麼,就看下面的鏈接啦,如果是新手的話,建議全部都install啊。。不然就會有最低下的錯誤,而且弄了好久都不知到怎麼回事。

http://opencv.willowgarage.com/wiki/InstallGuide%20%3A%20Debian

3、編譯OpenCV

回到步驟1的那個OpenCV目錄,新建一個叫release的文件夾,然後在裡面cmake,具體也可參考上面的鏈接

    cd ~/opencv # the directory containing INSTALL, CMakeLists.txt etc.
    mkdir release
    cd release
    cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D BUILD_PYTHON_SUPPORT=ON -D BUILD_EXAMPLES=ON ..

看到上面的cmake命令沒有!!!搞定的話第一小步就完成了

然後就第二小步: make

這一步非常久啊。。。很久很久很久。。。。。。

然後就很輕松的: make install

4、小配置

這一步關乎你的程序能不能找到那些include,和lib。

大家可以參考這篇文章~~很後才找到的。。。為什麼沒有早點發現呢。。。走了好多冤枉路啊= =

http://www.samontab.com/web/2010/04/installing-opencv-2-1-in-ubuntu/

下面的是從這個鏈接copy的。。。人家圖文並茂,都懶得翻譯了。。怕翻譯錯了。。。

Now you have to configure the library. First, open the opencv.conf file with the following code:

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

Add the following line at the end of the file(it may be an empty file, that is ok) and then save it:

1 /usr/local/lib

Run the following code to configure the library:

1 sudo ldconfig

Now you have to open another file:

1 sudo gedit /etc/bash.bashrc

Add these two lines at the end of the file and save it:

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

Finally, open a new console, restart the computer or logout and then login again. OpenCV will not work correctly until you do this.


5、寫程序!!!

在自己的工作目錄裡面,新建DisplayImage.cpp

然後從這個地方,copy一下源代碼

http://docs.opencv.org/doc/tutorials/introduction/linux_gcc_cmake/linux_gcc_cmake.html

然後就編譯了,編譯的方法有三種:

1)直接gcc

2)用cmake建makefile然後make一下

3)IDE法,傳說有個萬能IDE叫eclipse。。。。

第一種,

g++ `pkg-config --cflags opencv` -o hello hello.cpp `pkg-config --libs opencv`

http://stackoverflow.com/questions/11532963/cant-compile-opencv-in-linux

給個鏈接出來,是要告訴你,libs要放在後面啊。。不然會出錯滴~~

第二種,

建一個CMakeLists.txt的東西,輸入下面的東西

project( DisplayImage )
find_package( OpenCV REQUIRED )
add_executable( DisplayImage DisplayImage )
target_link_libraries( DisplayImage ${OpenCV_LIBS} )
然後。。。還是看這篇文章。。。http://docs.opencv.org/doc/tutorials/introduction/linux_gcc_cmake/linux_gcc_cmake.html

cmake之後,就有個可執行文件,然後就能顯示圖片啦~~~(怎麼又是Lena。。。。)

我遇到的問題:

沒有編譯錯誤,但運行程序的時候出現下面這個錯誤

OpenCV ERROR: Unspecified error (The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Carbon support)

意思是,你木有某些東西的支持,建議你安裝什麼libgtk2.0(這個忘了是什麼),和pkg-config。

然後我就安裝了libgtk神馬的啊。然後運行了還不行。(請耐心看下去,最下面解決了~~)

有個GG說看這篇文章可以搞定http://mathiasirwans.blogspot.com/2007/07/my-experience-with-ubuntu-610-opencv-10.html

但是我看了還搞不定。。。

後來深思了出錯提示,在安裝了上面所說缺少的東西之後,重新cmake+make+make install後,終於搞定了~~~(因為make的過程很痛苦,所以我之前一直回避make。。這次證實我真錯了。。。)

Copyright © Linux教程網 All Rights Reserved