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
sudo apt-get -qq remove ffmpeg x264 libx264-dev
2.安装依赖项
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--dev libxine-dev libgstreamer0.-dev libgstreamer-plugins-base0.-dev libv4l-dev python-dev python-numpy libtbb-dev libqt4-dev libgtk2.-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
wget -O OpenCV-$version.zip http://sourceforge.net/projects/opencvlibrary/files/opencv-unix/$version/opencv-"$version".zip/download
unzip OpenCV-$version.zip
cd opencv-$version
4.编译源码,构建OpenCV
mkdir build
cd build
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 ..
make -j2
sudo make install
sudo sh -c 'echo "/usr/local/lib" > /etc/ld.so.conf.d/opencv.conf'
sudo ldconfig
5.测试OpenCV是否安装成功
转到opencv的样例代码目录(samples/c)下,
chmod +x build_all.sh
./build_all.sh
./facedetect "/usr/share/opencv/haarcascades/haarcascade_frontalface_alt.xml" lena.jpg
运行结果:
编译OpenCV程序:
#!/bin/bash
echo "compiling $1"
if [[ $ == *.c ]]
then
gcc -ggdb `pkg-config --cflags opencv` -o `basename $ .c` $ `pkg-config --libs opencv`;
elif [[ $ == *.cpp ]]
then
g++ -ggdb `pkg-config --cflags opencv` -o `basename $ .cpp` $ `pkg-config --libs opencv`;
else
echo "Please compile only .c or .cpp files"
fi
echo "Output file => ${1%.*}"
将上面代码保存成shell脚本,在每次编译程序时调用即可。
如果在编译过程中出现如下错误:
[ %] Building CXX object modules/core/CMakeFiles/opencv_core.dir/src/system.cpp.o
/home/test/autobuild/instance-/output/build/opencv-2.4./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)':
/home/test/autobuild/instance-/output/build/opencv-2.4./modules/core/src/system.cpp::: error: inconsistent operand constraints in an 'asm'
make[]: *** [modules/core/CMakeFiles/opencv_core.dir/src/system.cpp.o] Error
可参考:https://github.com/Itseez/opencv/commit/ea50be0529c248961e1b66293f8a9e4b807294a6