OpenCV2.3使用摄像头和视频

来源:互联网 发布:h5动画源码 编辑:程序博客网 时间:2024/05/15 12:21
现在市面上得书都用的是老版本的Opencv1的内容,早以前不适合了,新版只能自己研究了。
先上代码。

http://blog.sina.com.cn/s/blog_a3e7ccd6010105yn.html
//使用摄像头 
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 by retrieve() .
  • 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;
}
}

0 0
原创粉丝点击