鉴于Raspberry pi自带摄像头,故可以方便得应用于计算机视觉相关领域,这里对于OpenCV的环境配置进行介绍。
—————————目前,Rasbian已包含2.3和2.4版本的OpenCV二进制发行包,我们不用再自己编译了——————————
sudo apt-get install libcv-dev
sudo apt-get install libopencv-dev
下面的包含上面的,但比上面的大25M,多包含了
libcvaux-dev libhighgui-dev libopencv-contrib-dev libopencv-photo-dev libopencv-stitching-dev libopencv-ts-dev libopencv-videostab-dev
OpenCV依赖于众多库,好在Debian已有二进制包发行,我们只需要执行以下命令安装即可
sudo apt-get install build-essential
sudo apt-get install cmake
sudo apt-get install libgtk2.0-dev
sudo apt-get install pkg-config
sudo apt-get install python-dev python-numpy
sudo apt-get install ffmpeg libavcodec-dev libavformat-dev libswscale-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev
——————-注释的分割线————————
libunicap2 libunicap2-dev
可以看到,以上包大部分是图像、视频解码器的相关库
可以通过以下命令下载老版本的OpenCV
wget http://sourceforge.net/projects/opencvlibrary/files/opencv-unix/2.3.1/OpenCV-2.3.1a.tar.bz2
然后解压缩
tar -xvjpf OpenCV-2.3.1a.tar.bz2
删除原压缩包
rm OpenCV-2.3.1a.tar.bz2
建立编译目录
cd OpenCV-2.3.1/
mkdir build
cd build
通过以下命令配置CMake文件
cmake -D CMAKE_BUILD_TYPE=RELEASE -D BUILD_EXAMPLES=ON -D CMAKE_INSTALL_PREFIX=/usr/local ..
其中,-D CMAKE_INSTALL_PREFIX用来指定安装目录,省略之后则安装到默认的 /usr 目录
或者,你可以直接使用CMake的图像界面。。。。。
cmake-gui ..
正式编译
make j4
一般推荐编译线程数与CPU核数量相同
经过漫长的等待(数小时),编译完成,安装
sudo make install
安装完成后,建议执行
sudo ldconfig
更新动态链接库列表
还可执行
pkg-config opencv --cflag --libs
验证编译链接参数。若提示找不到,则需要编辑
sudo nano /etc/ld.so.conf.d/opencv.conf
在其中添加
/usr/lib
等
在这个目录内有C语言Demo
cd ~/opencv/OpenCV-2.3.1/build/bin
其中,这些不需要摄像头
convexhull
kmeans
drawing
此外还可执行Python Demo
python ./minarea.py
python ./delaunay.py
python ./drawing.py
Raspberry pi的摄像头不能使用OpenCV自身的摄像头打开程序,而应该使用如下示例代码
#include
#include
#include
#include
#include
#include "raspicam_cv.h"
using namespace std;
void processCommandLine ( int argc,char **argv,raspicam::RaspiCam_Cv &Camera ) {
Camera.set ( CV_CAP_PROP_FRAME_WIDTH, getParamVal ( "-w",argc,argv,1280 ) );
Camera.set ( CV_CAP_PROP_FRAME_HEIGHT, getParamVal ( "-h",argc,argv,960 ) );
Camera.set ( CV_CAP_PROP_BRIGHTNESS,getParamVal ( "-br",argc,argv,50 ) );
Camera.set ( CV_CAP_PROP_CONTRAST ,getParamVal ( "-co",argc,argv,50 ) );
Camera.set ( CV_CAP_PROP_SATURATION, getParamVal ( "-sa",argc,argv,50 ) );
Camera.set ( CV_CAP_PROP_GAIN, getParamVal ( "-g",argc,argv ,50 ) );
if ( findParam ( "-gr",argc,argv ) !=-1 )
Camera.set ( CV_CAP_PROP_FORMAT, CV_8UC1 );
if ( findParam ( "-test_speed",argc,argv ) !=-1 )
doTestSpeedOnly=true;
if ( findParam ( "-ss",argc,argv ) !=-1 )
Camera.set ( CV_CAP_PROP_EXPOSURE, getParamVal ( "-ss",argc,argv ) );
}
int main ( int argc,char **argv ) {
raspicam::RaspiCam_Cv Camera;
processCommandLine ( argc,argv,Camera );
if ( !Camera.open() ) {
cerr<<"Error opening camera"<return -1;
}
cout<<"Connected to camera ="<int nCount=100;
for ( int i=0; iif ( i%5==0 )
cout<<"\r capturing ..."<"/"<std::flush;
if ( i%30==0 && i!=0 ) {
//save image
std::stringstream fn;
fn<<"image"<".ppm";
cv::imwrite ( fn.str(),image );
}
}
Camera.release();
}