极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 10228|回复: 2

OpenCV2.4使用摄像头和视频

[复制链接]
发表于 2012-8-5 23:46:05 | 显示全部楼层 |阅读模式
我的博客:http://www.zwmin.com
现在市面上得书都用的是老版本的Opencv1的内容,早以前不适合了,新版只能自己研究了。
先上代码。
//使用摄像头
void videoCapture1()
{
VideoCapture cap(0);
//设置摄像头
cap.set( CV_CAP_PROP_FRAME_WIDTH,640);
cap.set( CV_CAP_PROP_FRAME_HEIGHT,480 );
//确认是否成功打开摄像头
if(!cap.isOpened()){
cout<<"打开摄像头失败,退出";
exit(-1);
}
namedWindow( "Capture",CV_WINDOW_AUTOSIZE|CV_WINDOW_FREERATIO );
while (1)
{
Mat frame;
cap>>frame;
//各种处理
imshow( "Capture",frame);
if( waitKey(30)>=0 ) break;
}
}


使用VideoCapture来获取视频或者摄像头的图像。
//也可以这样获取
VideoCapture cap;
cap.open(0);


isOpened()可以返回摄像头打开是否正确。

主要是摄像头的设置,这个比较常用。
  • CV_CAP_PROP_POS_MSEC Current position of the video file in milliseconds.
  • CV_CAP_PROP_POS_FRAMES 0-based index of the frame to be decoded/captured next.
  • CV_CAP_PROP_POS_AVI_RATIO Relative position of the video file: 0 - start of the film, 1 - end of the film.
  • CV_CAP_PROP_FRAME_WIDTH Width of the frames in the video stream.
  • CV_CAP_PROP_FRAME_HEIGHT Height of the frames in the video stream.
  • CV_CAP_PROP_FPS Frame rate.
  • CV_CAP_PROP_FOURCC 4-character code of codec.
  • CV_CAP_PROP_FRAME_COUNT Number of frames in the video file.
  • CV_CAP_PROP_FORMAT Format of the Mat objects returned byretrieve() .
  • CV_CAP_PROP_MODE Backend-specific value indicating the current capture mode.
  • CV_CAP_PROP_BRIGHTNESS Brightness of the image (only for cameras).
  • CV_CAP_PROP_CONTRAST Contrast of the image (only for cameras).
  • CV_CAP_PROP_SATURATION Saturation of the image (only for cameras).
  • CV_CAP_PROP_HUE Hue of the image (only for cameras).
  • CV_CAP_PROP_GAIN Gain of the image (only for cameras).
  • CV_CAP_PROP_EXPOSURE Exposure (only for cameras).
  • CV_CAP_PROP_CONVERT_RGB Boolean flags indicating whether images should be converted to RGB.
  • CV_CAP_PROP_WHITE_BALANCE Currently unsupported
  • CV_CAP_PROP_RECTIFICATION Rectification flag for stereo cameras (note: only supported by DC1394 v 2.x backend currently)

opencv获取视频基本类似
//打开视频
void videoCapture2()
{
VideoCapture cap("file01.avi");
if (!cap.isOpened())
{
cout<<"视频打开错误,退出";
exit(-1);
}
namedWindow( "Capture",CV_WINDOW_AUTOSIZE|CV_WINDOW_FREERATIO );
while (1)
{
Mat frame;
cap>>frame;
//各种操作
if (frame.empty())break;
imshow( "Capture",frame );
if ( waitKey(30)>=0 )break;
}
}




回复

使用道具 举报

发表于 2012-8-7 07:02:50 | 显示全部楼层
好贴,支持!
回复 支持 反对

使用道具 举报

发表于 2012-8-7 10:08:04 | 显示全部楼层
2.4怎么是这个样子的。。只用过1。0和2.3
CvCapture *capture = cvCreateCameraCapture(0);
再来一句处理帧 frame = cvQueryFrame(capture);
就可以显示了嘛
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|联系我们|极客工坊 ( 浙ICP备09023225号 )

GMT+8, 2020-7-13 20:53 , Processed in 0.048726 second(s), 24 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表