OpenCV is the most popular and advanced code library for Computer Vision related applications today, spanning from many very basic tasks (capture and pre-processing of image data) to high-level algorithms (feature extraction, motion tracking, machine learning). It is free software and provides a rich API in C, C++, Java and Python. Other wrappers are available. The library itself is platform-independent and often used for real-time image processing and computer vision.
1.移除先前安装的ffmpeg和x264
1 sudo apt-get -qq remove ffmpeg x264 libx264-dev
2.安装依赖项
1 sudo apt-get -qq install libopencv-dev build-essential checkinstall cmake pkg-config yasm libjpeg-dev libpng-dev libtiff5-dev libjasper-dev libavcodec-dev libavformat-dev libswscale-dev libdc1394-22-dev libxine-dev libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev libv4l-dev python-dev python-numpy libtbb-dev libqt4-dev libgtk2.0-dev libfaac-dev libmp3lame-dev libopencore-amrnb-dev libopencore-amrwb-dev libtheora-dev libvorbis-dev libxvidcore-dev x264 v4l-utils ffmpeg cmake qt5-default checkinstall
3.下载并解压OpenCV
1 wget -O OpenCV-$version.zip http://sourceforge.net/projects/opencvlibrary/files/opencv-unix/$version/opencv-"$version".zip/download
2 unzip OpenCV-$version.zip 3 cd opencv-$version
4.编译源码,构建OpenCV
1 mkdir build
2 cd build
3 cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_TBB=ON -D BUILD_NEW_PYTHON_SUPPORT=ON -D WITH_V4L=ON -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D BUILD_EXAMPLES=ON -D WITH_QT=ON -D WITH_OPENGL=ON .. 4 make -j2 5 sudo make install 6 sudo sh -c 'echo "/usr/local/lib" > /etc/ld.so.conf.d/opencv.conf' 7 sudo ldconfig
5.测试OpenCV是否安装成功
转到opencv的样例代码目录(samples/c)下,
1 chmod +x build_all.sh
2 ./build_all.sh
3 ./facedetect "/usr/share/opencv/haarcascades/haarcascade_frontalface_alt.xml" lena.jpg
运行结果:
编译OpenCV程序:
1 #!/bin/bash
2 echo "compiling $1" 3 if [[ $1 == *.c ]] 4 then 5 gcc -ggdb `pkg-config --cflags opencv` -o `basename $1 .c` $1 `pkg-config --libs opencv`; 6 elif [[ $1 == *.cpp ]] 7 then 8 g++ -ggdb `pkg-config --cflags opencv` -o `basename $1 .cpp` $1 `pkg-config --libs opencv`; 9 else 10 echo "Please compile only .c or .cpp files" 11 fi 12 echo "Output file => ${1%.*}"
将上面代码保存成shell脚本,在每次编译程序时调用即可。
如果在编译过程中出现如下错误:
1 [ 14%] Building CXX object modules/core/CMakeFiles/opencv_core.dir/src/system.cpp.o 2 /home/test/autobuild/instance-0/output/build/opencv-2.4.10/modules/core/src/system.cpp: In function '(static initializers for /home/test/autobuild/instance-0/output/build/opencv-2.4.10/modules/core/src/system.cpp)': 3 /home/test/autobuild/instance-0/output/build/opencv-2.4.10/modules/core/src/system.cpp:280:10: error: inconsistent operand constraints in an 'asm' 4 make[3]: *** [modules/core/CMakeFiles/opencv_core.dir/src/system.cpp.o] Error 1
可参考:https://github.com/Itseez/opencv/commit/ea50be0529c248961e1b66293f8a9e4b807294a6
http://www.cnblogs.com/liushaobo/p/4373956.html