使用DirectShow采集图像
Wikipedia,自由的百科全书
您也可使用hardy_ai编写的ARFrameGrabber类
本文档介绍的CCameraDS类调用采集函数可直接返回IplImage,使用更方便,且集成了DirectShow,勿需安装庞大的DirectX/Platform SDK。
本类只在Visual C++ 6.0下进行了测试
目录[隐藏] |
[
编辑]
下载
[
编辑]
可能存在的缺陷
在vc6.0中测试将此例程移植到mfc下时(特别是显示到控件上时),性能不如CvCapture;mfc下显示使用CvvImage对象的DrawToHDC方法。最明显的差别在cpu使用率上,即使都开到多线程中,DirectShow采集图像的方法仅仅显示cpu使用率就高达60%,拖动时能高达80%,且资源释放缓慢;但是采用CvCapture,即使带上一些图像处理步骤,cpu使用率也基本在50%以下。
[
编辑]
文档
CCameraDS中有如下函数:
-
CCameraDS()
- 构造函数
-
CCameraDS()
- 析构函数
-
bool OpenCamera(int nCamID, bool bDisplayProperties=true)
- 打开摄像头,nCamID指定打开哪个摄像头,取值可以为0,1,2,...。bDisplayProperties指示是否自动弹出摄像头属性页。
-
bool OpenCamera(int nCamID, bool bDisplayProperties=true, int nWidth=320, int nHeight=240)
- 打开摄像头,nCamID指定打开哪个摄像头,取值可以为0,1,2,...。bDisplayProperties指示是否自动弹出摄像头属性页。nWidth和nHeight设置的摄像头的宽和高,如果摄像头不支持所设定的宽度和高度,则返回false
-
void CloseCamera()
- 关闭摄像头,析构函数会自动调用这个函数
-
static int CameraCount()
- 返回摄像头的数目。可以不用创建CCameraDS实例,采用int c=CCameraDS::CameraCount();得到结果。
-
static int CameraName(int nCamID, char* sName, int nBufferSize);
- 根据摄像头的编号返回摄像头的名字
- nCamID: 摄像头编号
- sName: 用于存放摄像头名字的数组
- nBufferSize: sName的大小
- 可以不用创建CCameraDS实例,采用CCameraDS::CameraName();得到结果。
-
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 // flymanbox@OpenCV China (for his contribution to function CameraName, and frame width/height setting) // Last modification: April 9, 2009 // // // 使用说明: // 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 // 在vc++2005开发环境下的使用说明: // 1.将CameraDS.h CameraDS.cpp复制到你的项目中 // 2.将DirectShow复制到你的opencv根目录下,菜单 工具->选项->项目和解决方案->vc++目录,把..(你的opencv安装目录)/DirectShow/Include添加到 // “引用文件”中$(VCInstallDir)PlatformSDK/include和$(FrameworkSDKDir)include下面任意位置 // 3.菜单 工具->选项->项目和解决方案->vc++目录,把..(你的opencv安装目录)/DirectShow/Lib添加到“库文件”下面。也可参考使用说明3。 // #include "camerads.h" #include <highgui.h> #include <stdio.h> int main() { int cam_count; //仅仅获取摄像头数目 cam_count = CCameraDS::CameraCount(); printf("There are %d cameras./n", cam_count); //获取所有摄像头的名称 for(int i=0; i < cam_count; i++) { char camera_name[1024]; int retval = CCameraDS::CameraName(i, camera_name, sizeof(camera_name) ); if(retval >0) printf("Camera #%d's Name is '%s'./n", i, camera_name); else printf("Can not get Camera #%d's name./n", i); } if(cam_count==0) return -1; CCameraDS camera; //打开第一个摄像头 //if(! camera.OpenCamera(0, true)) //弹出属性选择窗口 if(! camera.OpenCamera(0, false, 320,240)) //不弹出属性选择窗口,用代码制定图像宽和高 { 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; }