• Open Source Computer Vision Library

使用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)

例程

  1.  
  2. //////////////////////////////////////////////////////////////////////
  3. // Video Capture using DirectShow
  4. // Author: Shiqi Yu (shiqi.yu@gmail.com)
  5. // Thanks to HardyAI@OpenCV China
  6. // Last modification: April 18, 2008
  7. //////////////////////////////////////////////////////////////////////
  8.  
  9.  
  10. //////////////////////////////////////////////////////////////////////
  11. // 使用说明:
  12. // 1. 将CameraDS.h CameraDS.cpp以及目录DirectShow复制到你的项目中
  13. // 2. 菜单 Project->Settings->Settings for:(All configurations)->C/C++->Category(Preprocessor)->Additional include directories
  14. // 设置为 DirectShow/Include
  15. // 3. 菜单 Project->Settings->Settings for:(All configurations)->Link->Category(Input)->Additional library directories
  16. // 设置为 DirectShow/Lib
  17. //////////////////////////////////////////////////////////////////////
  18.  
  19. #include "camerads.h"
  20. #include <highgui.h>
  21. #include <stdio.h>
  22.  
  23. int main()
  24. {
  25. int cam_count;
  26.  
  27. //获取摄像头数目
  28. cam_count = CCameraDS::CameraCount();
  29. printf("There are %d cameras.\n", cam_count);
  30.  
  31. if(cam_count==0)
  32. return -1;
  33.  
  34. //打开第一个摄像头
  35. CCameraDS camera;
  36.  
  37. if(! camera.OpenCamera(0))
  38. {
  39. fprintf(stderr, "Can not open camera.\n");
  40. return -1;
  41. }
  42.  
  43.  
  44. cvNamedWindow("camera");
  45. while(1)
  46. {
  47. //获取一帧
  48. IplImage *pFrame = camera.QueryFrame();
  49.  
  50. //显示
  51. cvShowImage("camera", pFrame);
  52.  
  53. if (cvWaitKey(20) == 'q')
  54. break;
  55. }
  56. camera.CloseCamera(); //可不调用此函数,CCameraDS析构时会自动关闭摄像头
  57.  
  58. cvDestroyWindow("camera");
  59. return 0;
  60. }
  61.  
Views
Personal tools