o2co2 发表于 2012-8-5 23:46:05

OpenCV2.4使用摄像头和视频

我的博客: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;
}
}




vigiles 发表于 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);
就可以显示了嘛
页: [1]
查看完整版本: OpenCV2.4使用摄像头和视频