使用DirectShow采集图像
Wikipedia,自由的百科全书
您也可使用hardy_ai编写的ARFrameGrabber类
本文档介绍的CCameraDS类调用采集函数可直接返回IplImage,使用更方便,且集成了DirectShow,勿需安装庞大的DirectX/Platform SDK。
目录 |
[编辑]
下载
[编辑]
文档
CCameraDS中有如下函数:
- CCameraDS()
- 构造函数
- CCameraDS()
- 析构函数
- bool OpenCamera(int nCamID, bool bDisplayProperties=true)
- 打开摄像头,nCamID指定打开哪个摄像头,取值可以为0,1,2,...。bDisplayProperties指示是否自动弹出摄像头属性页。
- void CloseCamera()
- 关闭摄像头,析构函数会自动调用这个函数
- static int CameraCount()
- 返回摄像头的数目。可以不用创建CCameraDS实例,采用int c=CCameraDS::CameraCount();得到结果。
- int GetWidth()
- 返回图像宽度。
- int GetHeight()
- 返回图像高度
- IplImage * QueryFrame()
- 抓取一帧,返回的IplImage不可手动释放!返回图像数据的为BGR模式的Top-down(第一个字节为左上角像素),即IplImage::origin=0(IPL_ORIGIN_TL)
[编辑]
例程
////////////////////////////////////////////////////////////////////// // Video Capture using DirectShow // Author: Shiqi Yu (shiqi.yu@gmail.com) // Thanks to HardyAI@OpenCV China // Last modification: April 18, 2008 ////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////// // 使用说明: // 1. 将CameraDS.h CameraDS.cpp以及目录DirectShow复制到你的项目中 // 2. 菜单 Project->Settings->Settings for:(All configurations)->C/C++->Category(Preprocessor)->Additional include directories // 设置为 DirectShow/Include // 3. 菜单 Project->Settings->Settings for:(All configurations)->Link->Category(Input)->Additional library directories // 设置为 DirectShow/Lib ////////////////////////////////////////////////////////////////////// #include "camerads.h" #include <highgui.h> #include <stdio.h> int main() { int cam_count; //获取摄像头数目 cam_count = CCameraDS::CameraCount(); if(cam_count==0) return -1; //打开第一个摄像头 CCameraDS camera; if(! camera.OpenCamera(0)) { fprintf(stderr, "Can not open camera.\n"); return -1; } cvNamedWindow("camera"); while(1) { //获取一帧 IplImage *pFrame = camera.QueryFrame(); //显示 cvShowImage("camera", pFrame); if (cvWaitKey(20) == 'q') break; } camera.CloseCamera(); //可不调用此函数,CCameraDS析构时会自动关闭摄像头 cvDestroyWindow("camera"); return 0; }


