OpenCV是一個非常強大的計算機視覺方面的庫匿乃,支持C++牲迫、python、java等語言衡楞,是CV領(lǐng)域的機器學(xué)習(xí)常用的工具吱雏。平常的工作中涉及到視頻流的處理,就需要在編譯OpenCV的時候添加ffmpeg的支持。下面介紹如何在Ubuntu16.04中編譯安裝OpenCV (for python)坎背。
一替劈、安裝python3.5及numpy并配置為默認版本
1.安裝
# apt-get install python3.5
# apt-get install python3-dev
# apt-get instal python3-numpy
2.配置
利用update-alternatives將python3.5配置為默認版本,首先需要把兩個版本的python分別添加進去得滤。
# update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1
# update-alternatives --install /usr/bin/python python /usr/bin/python3.5 2
然后把python3.5設(shè)置為默認陨献。
# update-alternatives --config python
選擇python3.5對應(yīng)的號碼即可。在命令行輸入python發(fā)現(xiàn)版本號為3.5懂更,然后import numpy眨业,不報錯即證明numpy安裝成功。
二沮协、安裝ffmpeg
先到官網(wǎng)下載安裝包ffmpeg.org/download.html
這里推薦下載2.8版本(根據(jù)官方描述貌似2.8對Ubuntu16.04也就是Xenial Xerus兼容性做得最好)龄捡。
解壓、配置慷暂、編譯聘殖、安裝
解壓:
# tar -jxvf ffmpeg-2.8.14.tar.bz2
# cd ffmpeg-2.8.14
配置:
# ./config --enable-shared --prefix=/usr
到這里出現(xiàn)報錯:
yasm/nasm not found or too old. Use --disable-yasm for a crippled build.
If you think configure made a mistake, make sure you are using the latest version from
Git. If the latest version fails, report the problem to the ffmpeg-user@ffmpeg.org mailing
list or IRC #ffmpeg on irc.freenode.net. Include the log file "config.log" produced by
configure as this will help solve the problem.
發(fā)現(xiàn)沒有安裝yasm,所以安裝yasm行瑞。
# apt-get install yasm
重復(fù)配置的命令奸腺,然后編譯:
# make
會出現(xiàn)一些warning跟note,應(yīng)該不影響編譯血久,只要沒有報錯就OK突照。
安裝:
# make install
檢查是否安裝成功:
# ffmpeg -version
三、安裝OpenCV
1.安裝一些依賴
根據(jù)官方所說氧吐,需要安裝以下依賴:
- compiler
# apt-get install build-essential
- required
# apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
- optional
# apt-get install libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev
2.下載安裝OpenCV
OpenCV Download Page
選擇一個合適的版本點擊Sources下載源碼讹蘑,我選擇的是3.3.0版本。解壓后進入解壓目錄筑舅,輸入以下命令:
# mkdir build
# cd build
然后最最最重要的步驟——配置座慰,cmake的時候如果要編譯python庫、包含ffmpeg等等豁翎,就需要一系列的參數(shù):
# cmake -D CMAKE_BUILD_TYPE=Release -D WITH_FFMPEG=ON -D CMAKE_INSTALL_PREFIX=/usr/local PYTHON3_EXECUTABLE = /usr/bin/python3 PYTHON_INCLUDE_DIR = /usr/include/python3.5 PYTHON_INCLUDE_DIR2 = /usr/include/x86_64-linux-gnu/python3.5m PYTHON_LIBRARY = /usr/lib/x86_64-linux-gnu/libpython3.5m.so PYTHON3_NUMPY_INCLUDE_DIRS = /usr/lib/python3/dist-packages/numpy/core/include/ ..
有路徑不相同的請自行修改角骤。配置完之后注意要檢查FFMPEG一項后面一定要是YES,否則證明配置不成功心剥,這個時候繼續(xù)編譯并安裝的話是沒有ffmpeg的哦~
然后執(zhí)行以下命令:
# make -j4 //四核CPU所以是-j4
# make install
到這里邦尊,安裝過程就結(jié)束啦,可以用python import cv2 測試一下是否成功优烧。