opencv基本上是搞CV必備套件之一了,支援的語言也非常多,但是安裝起來有點麻煩。
安裝官方給的opencv依賴包
- GCC 4.4.x or later
- CMake 2.6 or higher
- Git
- GTK+2.x or higher, including headers (libgtk2.0-dev) # 控制opencv GUI
- pkg-config
- Python 2.6 or later and Numpy 1.5 or later with developer packages (python-dev, python-numpy)
- ffmpeg or libav development packages: libavcodec-dev, libavformat-dev, libswscale-dev
- [optional] libtbb2 libtbb-dev
- [optional] libdc1394 2.x
- [optional] libjpeg-dev, libpng-dev, libtiff-dev, libjasper-dev, libdc1394-22-dev
第一步驟:先把所有的依賴都給安裝了
$ sudo apt-get install build-essential
$ sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
$ sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev # 處理圖像所需的包
$ sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev liblapacke-dev
$ sudo apt-get install libxvidcore-dev libx264-dev # 處理視訊所需的包
$ sudo apt-get install libatlas-base-dev gfortran # 優化opencv功能
$ sudo apt-get install ffmpeg
第二步驟:去官網下載下傳opencv3.4.1
官網位址:https://opencv.org/releases/ 下載下傳完成之後直接解壓将opencv3.4.1放在home目錄下
$cd opencv-3.4.1
$mkdir build
$cd build
第三步驟:編譯安裝
生成編譯檔案變革指定路徑:
$
cmake -
D CMAKE_BUILD_TYPE
=
Release
-
D CMAKE_INSTALL_PREFIX
=
/
usr
/
local ..
這裡将目标安裝到/usr/local這個路徑下
編譯:
$ make
這裡可能要等很長的時間(安裝的時候本人遇到了一個問題,詳見連接配接:
https://blog.csdn.net/Chris_zhangrx/article/details/85245610
)
and a TIF problem
解決TIF問題:
$sudo apt-get autoremove libtiff5-dev
sudo apt-get install lib4(此時按tab鍵補全)
安裝:
$make install
看到這個界面說明opencv安裝沒有問題了
第四步驟:設定相關的配置
1.用sudo打開ld.so.conf檔案:
$sudo gedit /etc/ld.so.conf
在檔案最後中加上一行 /usr/local/lib
2、運作:
$sudo ldconfig
3、修改bash.bashrc檔案:
$sudo vim /etc/bash.bashrc
4、在檔案末尾加入:
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
export PKG_CONFIG_PATH
5、最後執行:
$source /etc/bash.bashrc
至此所有的配置都已經完成,下面用一個小程式測試一下:
cd到opencv-3.4.1/samples/cpp/example_cmake目錄下,可以看到這個目錄裡官方已經給出了一個cmake的example,我們可以拿來測試下,按順序執行:
$cmake .
$make
$./opencv_example
即可看到打開了攝像頭,在左上角有一個hello opencv,即表示配置成功。
opencv-contrib的安裝
從官方網站下載下傳對應版本的opencv-contrib,解壓并放置在opencv的目錄下,下載下傳位址在開頭
進入opencv下的build檔案夾
$cd opencv/build
編譯
$sudo cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local -D OPENCV_EXTRA_MODULES_PATH=../opencv_contrib/modules/ ..
參考連結:https://blog.csdn.net/li_wen01/article/details/71641408
https://blog.csdn.net/GSH_Hello_World/article/details/80424829
https://blog.csdn.net/evil_xue/article/details/85081005
https://blog.csdn.net/xiangxianghehe/article/details/78780269