[转载]OpenCV2.3使用摄像头和视频
2016-05-26 20:45
281 查看
[转载]OpenCV2.3使用摄像头和视频
(2012-09-03 19:37:03)![](http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif)
转载▼
标签:转载 | 分类: 网站编程 |
现在市面上得书都用的是老版本的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 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; } }
通过 Wiz 发布
分享:
1
![](http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif)
喜欢
阅读(5116)┊
评论 (1)┊
收藏(0)
┊转载原文
┊
喜欢▼
┊打印┊举报
加载中,请稍候......
前一篇:2012年08月23日的日记
后一篇:[转载]说的对,买房和租房 买车和打车 郎咸平告诉你区别在那里,不愧是著名经济学家
评论
重要提示:警惕虚假中奖信息
[发评论]
jwzhangmin
我靠,我一直在寻找新版本的方法,结果还是高手在博客呀!
OpenCV 获取摄像头,新建窗口显示摄像头视频
结合Leaning OpenCV 第二个例子 显示一个视屏文件 写了一下 获取摄像头的代码为并且创建窗口显示的代码为:[cpp]
view plain
copy
print?
#include "stdafx.h"
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
int main( int argc, char** argv )
{
//声明IplImage指针
IplImage* pFrame = NULL;
//获取摄像头
CvCapture* pCapture = cvCreateCameraCapture(-1);
//创建窗口
cvNamedWindow("video", 1);
//显示视屏
while(1)
{
pFrame=cvQueryFrame( pCapture );
if(!pFrame)break;
cvShowImage("video",pFrame);
char c=cvWaitKey(33);
if(c==27)break;
}
cvReleaseCapture(&pCapture);
cvDestroyWindow("video");
}
#include "stdafx.h" #include <cv.h> #include <cxcore.h> #include <highgui.h> int main( int argc, char** argv ) { //声明IplImage指针 IplImage* pFrame = NULL; //获取摄像头 CvCapture* pCapture = cvCreateCameraCapture(-1); //创建窗口 cvNamedWindow("video", 1); //显示视屏 while(1) { pFrame=cvQueryFrame( pCapture ); if(!pFrame)break; cvShowImage("video",pFrame); char c=cvWaitKey(33); if(c==27)break; } cvReleaseCapture(&pCapture); cvDestroyWindow("video"); }
其中必须说一下几个函数用分割线框起来了
具体是这样的:
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
cvCreateCameraCapture
初始化从摄像头中获取视频CvCapture* cvCreateCameraCapture( int index );
index 要使用的摄像头索引。如果只有一个摄像头或者用哪个摄像头也无所谓,那使用参数-1应该便可以。
函数cvCreateCameraCapture给从摄像头的视频流分配和初始化CvCapture结构。目前在Windows下可使用两种接口:Video for Windows(VFW)和Matrox Imaging Library(MIL); Linux下也有两种接口:V4L和FireWire(IEEE1394)。
释放这个结构,使用函数cvReleaseCapture。
返回值为一个
CvCapture ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
CvCapture
视频获取结构typedef struct CvCapture CvCapture;
结构CvCapture 没有公共接口,它只能被用来作为视频获取函数的一个参数。
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
这个是一个很重要的结构 以后无论是读取已有视频还是从摄像头获取都必须用到它
然后的工作就是把视屏显示出来
[cpp]
view plain
copy
print?
while(1)
{
pFrame=cvQueryFrame( pCapture );
if(!pFrame)break;
cvShowImage("video",pFrame);
char c=cvWaitKey(33);
if(c==27)break;
}
while(1) { pFrame=cvQueryFrame( pCapture ); if(!pFrame)break; cvShowImage("video",pFrame); char c=cvWaitKey(33); if(c==27)break; }
在这里解释一下为什么显示视屏的时候要循环
进入While循环之后 开始读入视屏文件
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
cvQueryFrame函数
从摄像头或者文件中抓取并返回一帧IplImage* cvQueryFrame( CvCapture* capture );
capture 视频获取结构。
函数cvQueryFrame从摄像头或者文件中抓取一帧,然后解压并返回这一帧。这个函数仅仅是函数cvGrabFrame和函数cvRetrieveFrame在一起调用的组合。返回的图像不可以被用户释放或者修改。抓取后,capture被指向下一帧,可用cvSetCaptureProperty调整capture到合适的帧。
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
如果抓取帧为空 break 打破循环
否则将抓取的那一帧显示在创建的窗口上
cvShowImage("video",pFrame);
当前帧显示后
char c=cvWaitKey(33);
if(c==27)break;
我们等待33ms
如果用户触发了按键,将按键的ASCII值给C
如果C为ESC(ASCII 为27)循环退出
最后释放capture 和window
cvReleaseCapture(&pCapture);
cvDestroyWindow("video");
最后释放占用资源
显示运行结果
![](http://pic002.cnblogs.com/images/2011/141676/2011040920322015.jpg)
顶2踩1
上一篇OpenCV摄像头视频的获取
下一篇VS2010下配置 OpenCV2.2
相关文章推荐
- 使用VS+VisualGDB编译调试Linux程序
- 基于opencv的Gabor特征提取
- 旧版本Ubuntu添加源
- linux内核分析ELF文件分析实践报告
- linux内核分析程序破解实践报告
- Linux虚拟文件系统VFS简述
- halcon图片格式和opencv的IPLimage格式的转换
- Linux 之不同运维人员共用root 账户权限审计
- 【codeforces】#621B-Wet Shark and Bishops
- API Monitor简介(API监控工具)
- Linux C++ 开发简介(包括Linux守护线程)
- 123sdfdsfdsfsdf
- ActiveMQ笔记(5):JMX监控
- Linux目录作用
- 在linux下创建软链接(即目录映射)
- OPenGL生成圆柱文件
- nginx配置变量
- linux内实践核分析模块
- 【Linux】vim的配置
- 在VMware上安装CentOS-6.5 minimal - 安装VMware Tools