主要參考:php
一、http://www.ozbotz.org/opencv-installation/html
二、http://www.ozbotz.org/opencv-install-troubleshooting/linux
開發環境:VMware下Ubuntu+OpenCV2.4.7git
安裝過程:web
To install and configure OpenCV 2.4.1, complete the following steps. The commands shown in each step can be copy and pasted directly into a Linux command line.shell
sudo apt-get remove ffmpeg x264 libx264-dev
ubuntu
sudo apt-get update
bash
sudo apt-get install build-essential checkinstall git cmake libfaac-dev libjack-jackd2-dev libmp3lame-dev libopencore-amrnb-dev libopencore-amrwb-dev libsdl1.2-dev libtheora-dev libva-dev libvdpau-dev libvorbis-dev libx11-dev libxfixes-dev libxvidcore-dev texi2html yasm zlib1g-dev
sudo apt-get install libgstreamer0.10-0 libgstreamer0.10-dev gstreamer0.10-tools gstreamer0.10-plugins-base libgstreamer-plugins-base0.10-dev gstreamer0.10-plugins-good gstreamer0.10-plugins-ugly gstreamer0.10-plugins-bad gstreamer0.10-ffmpeg
ide
sudo apt-get install libgtk2.0-0 libgtk2.0-dev
post
sudo apt-get install libjpeg8 libjpeg8-dev
cd ~
mkdir src
cd ~/src
wget ftp://ftp.videolan.org/pub/videolan/x264/snapshots/x264-snapshot-20120528-2245-stable.tar.bz2
tar xvf x264-snapshot-20120528-2245-stable.tar.bz2
cd x264-snapshot-20120528-2245-stable
./configure --enable-static
make
sudo make install
IMPORTANT: If you are running a 64-bit version of Ubuntu, you must configure x264 as shown in the following command:
./configure --enable-shared --enable-pic
The -shared and -pic options might also be required when you compile for some other architectures, such as ARM. You know you need these options if you get the following error when compiling OpenCV:
[ 25%] Building CXX object modules/highgui/CMakeFiles/opencv_highgui.dir/src/bitstrm.cpp.o
Linking CXX shared library ../../lib/libopencv_highgui.so
/usr/bin/ld: /usr/local/lib/libavcodec.a(avpacket.o): relocation R_X86_64_32S against `av_destruct_packet' can not be used when making a shared object; recompile with -fPIC
/usr/local/lib/libavcodec.a: could not read symbols: Bad value
cd ~/src
wget http://ffmpeg.org/releases/ffmpeg-0.11.1.tar.bz2
tar xvf ffmpeg-0.11.1.tar.bz2
cd ffmpeg-0.11.1
./configure --enable-gpl --enable-libfaac --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libtheora --enable-libvorbis --enable-libx264 --enable-libxvid --enable-nonfree --enable-postproc --enable-version3 --enable-x11grab
make
sudo make install
IMPORTANT: Just like with x264 in the previous step, you must configure ffmpeg with the -shared and -pic options if you are running a 64-bit version of Ubuntu or some other architectures, such as ARM.
./configure --enable-gpl --enable-libfaac --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libtheora --enable-libvorbis --enable-libx264 --enable-libxvid --enable-nonfree --enable-postproc --enable-version3 --enable-x11grab --enable-shared --enable-pic
cd ~/src
wget http://www.linuxtv.org/downloads/v4l-utils/v4l-utils-0.8.8.tar.bz2
tar xvf v4l-utils-0.8.8.tar.bz2
cd v4l-utils-0.8.8
make
sudo make install
cd ~/src
wget http://downloads.sourceforge.net/project/opencvlibrary/opencv-unix/2.4.2/OpenCV-2.4.2.tar.bz2
tar xvf OpenCV-2.4.2.tar.bz2
cd OpenCV-2.4.2/
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE ..
make
sudo make install
export LD_LIBRARY_PATH=/usr/local/lib
Add the command to your .bashrc file so that you don’t have to enter every time your start a new terminal. (注:我主要採用將export LD_LIBRARY_PATH=/usr/local/lib加入.bashrc file的方法,.bashrc file爲隱藏文件,在主用戶目錄下,可用shell命令:ls -al查看)
Alternatively, you can configure the system wide library search path. Using your favorite editor, add a single line containing the text /usr/local/lib
to the end of a file named /etc/ld.so.conf.d/opencv.conf
. In the standard Ubuntu install, the opencv.conf file does not exist; you need to create it. Using vi, for example, enter the following commands:
sudo vi /etc/ld.so.conf.d/opencv.conf
G
o
/usr/local/lib
<Esc>
:wq!
After editing the opencv.conf file, enter the following command:
sudo ldconfig /etc/ld.so.conf
.
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
export PKG_CONFIG_PATH
After completing the previous steps, your system should be ready to compile code that uses the OpenCV libraries. The following example shows one way to compile code for OpenCV(這裏最好先進行重啓):
g++ `pkg-config opencv --cflags` main.cpp -o main `pkg-config opencv --libs`
以上按照過程當中,可能會遇到其餘問題,許多均可參考《OpenCV Installation Troubleshooting Guide》,裏面有詳細的問題描述及解答,很是清楚。
最後g++編譯的代碼main.cpp以下:
#include <cv.h> #include <opencv/highgui.h> #include<stdio.h> #include<opencv2/opencv.hpp> using namespace cv; int main(int argc, char *argv[]) { CvCapture* pCapture = cvCreateCameraCapture(0); cvNamedWindow("Video", 1); while(1) { IplImage* pFrame=cvQueryFrame( pCapture ); if(!pFrame)break; cvShowImage("Video",pFrame); char c=cvWaitKey(33); if(c==27)break; } cvReleaseCapture(&pCapture); cvDestroyWindow("Video"); return 0; }
讀取攝像頭結果以下圖: