转自:http://blog.csdn.net/android_lee/article/details/8080053
OpenCV官网的安装教程好像写的很全面,window,linux,macOS各种环境的都写了,VS2010,Eclipse,Codeblocks各种IDE的也都写了,但是好像每个都没写明白,所以在这里写一个简明的教程,环境是ubuntu11.04,openCV 2.3,编译器G++,编辑器vim。
1. 安装openCV2.3的依赖包,如果只用其基本功能用:
sudo apt-get install build-essential libgtk2.0-dev libavcodec-dev libavformat-dev libjpeg62-dev libtiff4-dev cmake libswscale-dev libjasper-dev
想用其所有功能,用以下命令:
sudo apt-get install build-essential sudo apt-get install cmake sudo apt-get install pkg-config sudo apt-get install libpng12-0 libpng12-dev libpng++-dev libpng3 sudo apt-get install libpnglite-dev libpngwriter0-dev libpngwriter0c2 sudo apt-get install zlib1g-dbg zlib1g zlib1g-dev sudo apt-get install libjasper-dev libjasper-runtime libjasper1 sudo apt-get install pngtools libtiff4-dev libtiff4 libtiffxx0c2 libtiff-tools sudo apt-get install libjpeg8 libjpeg8-dev libjpeg8-dbg libjpeg-prog sudo apt-get install ffmpeg libavcodec-dev libavcodec52 libavformat52 libavformat-dev sudo apt-get install libgstreamer0.10-0-dbg libgstreamer0.10-0 libgstreamer0.10-dev sudo apt-get install libxine1-ffmpeg libxine-dev libxine1-bin sudo apt-get install libunicap2 libunicap2-dev sudo apt-get install libdc1394-22-dev libdc1394-22 libdc1394-utils sudo apt-get install swig sudo apt-get install libv4l-0 libv4l-dev sudo apt-get install python-numpy
2. 点击这里下载 OpenCV 2.3或者在terminal里执行以下命令
wget http://sourceforge.net/projects/opencvlibrary/files/opencv-unix/2.2/OpenCV-2.3.1a.tar.bz2
3. 解压到home目录~/OpenCV-2.3.1(当然也可以是别的地方)
tar -xvf OpenCV-2.3.1a.tar.bz2
4. 创建一个发布opencv的目录,比如~/OpenCV-2.3.1/release
$ cd OpenCV-2.3.1 $ mkdir release $ cd release
5. 执行以下命令:
$ cmake .. $ make $ sudo make install
6. 打开home下的./bashrc
$ gedit .bashrc
在文件最后加入以下内容:
export LD_LIBRARY_PATH=/home/your_user_name/OpenCV-2.3.1/release/lib export PKG_CONFIG_PATH=/home/your_user_name/OpenCV-2.3.1/release/lib/pkgconfig alias gcv="g++ `pkg-config --cflags opencv` `pkg-config --libs opencv`"
7. 把openCV里的例子编译以下:
$ cd /home/your_user_name/OpenCV-2.3.1/samples/c $ chmod a+x build_all.sh $ ./build_all.sh
8. 由于我们已经在bashrc里创建了别名gcv,所以可以使用以下命令编译:
$ gcv hellocv.cpp
hellocv.cpp 一个简单的例子:
- <span style="color:#339900">#include "opencv2/imgproc/imgproc.hpp"</span>
- <span style="color:#339900">#include "opencv2/highgui/highgui.hpp"</span>
- <span style="color:#0000ff">using</span> <span style="color:#0000ff">namespace</span> cv<span style="color:#008080">;</span>
- <span style="color:#0000ff">using</span> <span style="color:#0000ff">namespace</span> std<span style="color:#008080">;</span>
- <span style="color:#0000ff">int</span> main<span style="color:#008000">(</span><span style="color:#0000ff">int</span> argc, <span style="color:#0000ff">char</span><span style="color:#000040">*</span> argv<span style="color:#008000">[</span><span style="color:#008000">]</span><span style="color:#008000">)</span>
- <span style="color:#008000">{</span>
- Mat img <span style="color:#000080">=</span> imread<span style="color:#008000">(</span><span style="color:#FF0000">"lena.jpg"</span><span style="color:#008000">)</span><span style="color:#008080">;</span>
- <span style="color:#0000ff">if</span><span style="color:#008000">(</span><span style="color:#000040">!</span>img.<span style="color:#007788">data</span><span style="color:#008000">)</span>
- <span style="color:#0000ff">return</span> <span style="color:#000040">-</span><span style="color:#0000dd">1</span><span style="color:#008080">;</span>
- namedWindow<span style="color:#008000">(</span><span style="color:#FF0000">"Lena"</span>, CV_WINDOW_AUTOSIZE<span style="color:#008000">)</span><span style="color:#008080">;</span>
- imshow<span style="color:#008000">(</span><span style="color:#FF0000">"Lena"</span>, img<span style="color:#008000">)</span><span style="color:#008080">;</span>
- waitKey<span style="color:#008000">(</span><span style="color:#008000">)</span><span style="color:#008080">;</span>
- <span style="color:#0000ff">return</span> <span style="color:#0000dd">0</span><span style="color:#008080">;</span>
- <span style="color:#008000">}</span>