CentoOS 6.4 安装OpenCV2.4.7

其中包含ffmpeg和v4l2。為什要要先裝這兩個套件呢?
因為opencv 在使用讀取影像檔案和相機擷取就需要以上兩個套件
如果沒安裝opencv在測試影片或者相機輸入會不能用。如果你覺得無所謂,那就跳過安裝ffmpeg和v4l2

以下是紀錄依照順序紀錄的,有些步驟在記錄的時候可能漏掉,請多包涵

 

注意安裝前需要先裝gcc, g++, autoconf, cmake, pkgconfig
可以使用

 

yum install gcc autoconf libtool make cmake automake gcc gcc-c++
yum install git nasm pkgconfig zlib-devel

!!pkgconfig 記得使用

 

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

 才會抓到/usr/local/lib下的.pc設定檔,很多lib預設安裝在/usr/local/lib下

 

開啟自動設定路徑

vim /etc/ld.so.conf
最後加入/usr/local/lib
/sbin/ldconfig
或者

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib

 

1. Compile FFmpeg on CentOS 6.x

mkdir ~/ffmpeg_sources

建議一定要安裝x264,如果你要壓縮h264格式,x264需要yasm套件

Yasm

Yasm is an assembler used by x264 and FFmpeg.

cd ~/ffmpeg_sources
curl -O http://www.tortall.net/projects/yasm/releases/yasm-1.2.0.tar.gz
tar xzvf yasm-1.2.0.tar.gz
cd yasm-1.2.0
./configure 
make
make install
make distclean
. ~/.bash_profile

 

x264

H.264 video encoder.

cd ~/ffmpeg_sources
git clone --depth 1 git://git.videolan.org/x264
cd x264
./configure --enable-static
make
make install
make distclean

~~~~~~~~~~~~~~~~~~~~~~下面可选

 

libmp3lame

MP3 audio encoder.

Lame可有可無建議,都裝一裝,這是給mp3壓縮用的

cd ~/ffmpeg_sources
curl -L -O http://downloads.sourceforge.net/project/lame/lame/3.99/lame-3.99.5.tar.gz
tar xzvf lame-3.99.5.tar.gz
cd lame-3.99.5
./configure --disable-shared --enable-nasm
make
make install
make distclean

 

libfdk_aac

AAC audio encoder.

cd ~/ffmpeg_sources
git clone --depth 1 git://git.code.sf.net/p/opencore-amr/fdk-aac
cd fdk-aac
autoreconf -fiv
./configure --disable-shared
make
make install
make distclean

 

libopus

Opus audio decoder and encoder.

cd ~/ffmpeg_sources
curl -O http://downloads.xiph.org/releases/opus/opus-1.0.3.tar.gz
tar xzvf opus-1.0.3.tar.gz
cd opus-1.0.3
./configure --disable-shared
make
make install
make distclean

 

libogg

Ogg bitstream library. Required by libtheora and libvorbis.

cd ~/ffmpeg_sources
curl -O http://downloads.xiph.org/releases/ogg/libogg-1.3.1.tar.gz
tar xzvf libogg-1.3.1.tar.gz
cd libogg-1.3.1
./configure --disable-shared
make
make install
make distclean

 

libvorbis

Vorbis audio encoder. Requires libogg.

cd ~/ffmpeg_sources
curl -O http://downloads.xiph.org/releases/vorbis/libvorbis-1.3.3.tar.gz
tar xzvf libvorbis-1.3.3.tar.gz
cd libvorbis-1.3.3
./configure --disable-shared
make
make install
make distclean

 

libvpx

VP8/VP9 video encoder.

cd ~/ffmpeg_sources
git clone --depth 1 http://git.chromium.org/webm/libvpx.git
cd libvpx
./configure --disable-examples
make
make install
make clean

 

 ~~~~~~~~~~~~~~~~~~~~

zlib開發套件一定要

 

yum install zlib-devel

 如果要自己安裝要注意記得先用yum移除舊版,不然可能會跟舊版衝突

最後就是ffmpeg 預設安裝到/usr/local
如果不想要安裝到預設目錄記得在./configure 後加入 --prefix=[安裝目錄] ("$HOME/ffmpeg_build/“)

cd ~/ffmpeg_sources
git clone --depth 1 git://source.ffmpeg.org/ffmpeg
cd ffmpeg
./configure --extra-libs="-ldl" --enable-gpl --enable-nonfree --enable-libfdk_aac --enable-libmp3lame --enable-libopus --enable-libvorbis --enable-libvpx --enable-libx264 --enable-shared -–disable-yasm
make
make install
make distclean
hash -r
. ~/.bash_profile

 

最後透過yum安裝v4l2

 

yum install video4linux-devel

 

yum install libv4l-devel

 這要安裝因為opencv會參考pkg-config,這個套件換安裝libv4l2.pc設定套件資訊

 

Additional Libraries

libfreetype

Font rendering library. Required for the drawtext video filter.

# yum install freetype-devel

Add --enable-libfreetype to your ffmpeg ./configure.

 

libspeex

Speex audio decoder and encoder.

 

# yum install speex-devel

Add --enable-libspeex to your ffmpeg ./configure.

 

libtheora

Theora video encoder. Requires libogg.

cd ~/ffmpeg_sources
curl -O http://downloads.xiph.org/releases/theora/libtheora-1.1.1.tar.gz
tar xzvf libtheora-1.1.1.tar.gz
cd libtheora-1.1.1
./configure --disable-examples --disable-shared --disable-sdltest --disable-vorbistest
make
make install
make distclean

Add --enable-libtheora to your ffmpeg ./configure.

 

2. OpenCV Installation Guide

現在準備安裝opencv
這邊需要幾個套件

yum install swig sip

 gtk2.0 用來cvNameWindows顯示用

yum install gtk2-devel 

centos 5內建的python版本太舊建議安裝至少2.6
不過安裝不太容易
我是參考 >> HostUcan <<

wget http://www.python.org/ftp/python/2.6.7/Python-2.6.7.tgz
tar xzvf Python-2.6.7.tgz
./configure --prefix=/opt/python2.6 --with-threads --enable-shared --with-zlib=/usr/include --prefix=/opt/python2.6不見得要要的話下面指令要修改
make
make install

 /python2.6不見得要要的話下面指令要修改
make
make install

如果你照上面 --prefix=/opt/python2.6打則

sudo ln -s /opt/python2.6/lib/libpython2.6.so /usr/lib
sudo ln -s /opt/python2.6/lib/libpython2.6.so.1.0 /usr/lib

 
如果沒打則

sudo ln -s /usr/local/lib/libpython2.6.so /usr/lib
sudo ln -s /usr/local/lib/libpython2.6.so.1.0 /usr/lib

# Create necessary links and cache
sudo /sbin/ldconfig -v
記得重新登入

現在來開始安裝opencv

 

我是參考 OpenCV Installation Guide

tar jxvf opencv-2.4.7.tar.bz2
cd opencv-2.4.7
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D BUILD_PYTHON_SUPPORT=ON ..

 你應該要看到ffmpeg和v4l2是On如果沒看到,請檢查上面有沒有步驟少了
然後最後會產生Makefile檔。如果沒看到Makefile檔產生訊息也是錯了
make
make install
恭喜你安裝成功

 

你打pkg-config --cflags --libs opencv
你應該看到
-I/usr/local/include/opencv -I/usr/local/include  /usr/local/lib/libopencv_calib3d.so /usr/local/lib/libopencv_contrib.so /usr/local/lib/libopencv_core.so /usr/local/lib/libopencv_features2d.so /usr/local/lib/libopencv_flann.so /usr/local/lib/libopencv_gpu.so /usr/local/lib/libopencv_highgui.so /usr/local/lib/libopencv_imgproc.so /usr/local/lib/libopencv_legacy.so /usr/local/lib/libopencv_ml.so /usr/local/lib/libopencv_nonfree.so /usr/local/lib/libopencv_objdetect.so /usr/local/lib/libopencv_photo.so /usr/local/lib/libopencv_stitching.so /usr/local/lib/libopencv_ts.so /usr/local/lib/libopencv_video.so /usr/local/lib/libopencv_videostab.so

編譯程式可以直接在gcc後面加入 `pkg-config --cflags  opencv`
e.g.
gcc -c test `pkg-config --cflags  opencv` -o test.o
gcc test.o -o foo `pkg-config --libs opencv`
注意`是在鍵盤上esc下面那個,不是冒號右邊那個
在`XXXX`表示執行shell指令後回傳的值,它會自動替換掉上面剛剛一大串的指令執行結果

 

 

cmake -DBUILD_TIFF=ON -DBUILD_opencv_java=OFF -DWITH_CUDA=OFF -DWITH_IPP=ON -DWITH_TBB=ON -DWITH_EIGEN=ON -DWITH_GStreamer=OFF -DWITH_V4L=ON -DBUILD_TESTS=OFF -DBUILD_PERF_TESTS=OFF -DCMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/home/yaodezhong/env/local/ ..

你可能感兴趣的:(opencv)