在安装OpenCV前需要安装的软件包有
sudo apt-get install build-essential libgtk2.0-dev libjpeg-dev libtiff4-dev libjasper-dev libopenexr-dev cmake python-dev python-numpy python-tk libtbb-dev libeigen2-dev yasm libfaac-dev libopencore-amrnb-dev libopencore-amrwb-dev libtheora-dev libvorbis-dev libxvidcore-dev libx264-dev libqt4-dev libqt4-opengl-dev sphinx-common texlive-latex-extra libv4l-dev libdc1394-22-dev libavcodec-dev libavformat-dev libswscale-dev
2)下载最新版OpenCV
打开网页:http://sourceforge.net/projects/opencvlibrary
下载安装包OpenCV-2.4.2.tar.bz2
sudo tar jxvf OpenCV-2.4.2.tar.bz2 -C /usr/local/
cd /usr/local/
sudo mv OpenCV-2.4.2 opencv
cd opencv
mkdir release
cd release
cmake -D WITH_TBB=ON -D BUILD_NEW_PYTHON_SUPPORT=ON -D WITH_V4L=ON -DINSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -DBUILD_EXAMPLES=ON -D WITH_QT=ON -D WITH_OPENGL=ON ..
make
sudo make install
3)安装后的配置
添加库的路径
sudo gedit /etc/ld.so.conf.d/opencv.conf
添加内容
/usr/local/lib
在终端输入命令
sudo ldconfig
设置环境变量
sudo gedit /etc/bash.bashrc
在文件最后加入以下两行并保存:
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
export PKG_CONFIG_PATH
此时重启Ubuntu或重新登录账户,使得OpenCV安装生效。
4)测试OpenCV自带例程
编译程序:
cd /usr/local/opencv/samples/c
chmod +x build_all.sh
./build_all.sh
运行程序:
./facedetect --cascade="/usr/local/share/OpenCV/haarcascades/haarcascade_frontalface_alt.xml" --scale=1.5 lena.jpg
运行结果如下:
运行程序:
./facedetect --cascade="/usr/local/share/OpenCV/haarcascades/haarcascade_frontalface_alt.xml" --nested-cascade="/usr/local/share/OpenCV/haarcascades/haarcascade_eye.xml" --scale=1.5 lena.jpg
运行结果如下:
运行程序:
python /usr/local/opencv/samples/python2/turing.py
程序结果如下:
注意,此时,图片的显示窗口是Qt界面。你可以随意对图片进行放大,缩小,可以保存图片等。当你放大图片到一定值,你可以看到每个像素对应的RGB值。