CAPTUREView.cpp
上传用户:wzoulong
上传日期:2022-07-30
资源大小:1796k
文件大小:6k
源码类别:

OpenCV

开发平台:

Visual C++

  1. // CAPTUREView.cpp : implementation of the CCAPTUREView class
  2. //
  3. #include "stdafx.h"
  4. #include "CAPTURE.h"
  5. #include "CAPTUREDoc.h"
  6. #include "CAPTUREView.h"
  7. #include "cv.h"
  8. #include "cxcore.h"
  9. #include "cvaux.h"
  10. #include "highgui.h"
  11. #include "cvcam.h"
  12. #ifdef _DEBUG
  13. #define new DEBUG_NEW
  14. #undef THIS_FILE
  15. static char THIS_FILE[] = __FILE__;
  16. #endif
  17. /////////////////////////////////////////////////////////////////////////////
  18. // CCAPTUREView
  19. IMPLEMENT_DYNCREATE(CCAPTUREView, CView)
  20. BEGIN_MESSAGE_MAP(CCAPTUREView, CView)
  21. //{{AFX_MSG_MAP(CCAPTUREView)
  22. ON_COMMAND(ID_SHOW_VIDEO, OnShowVideo)
  23. ON_COMMAND(ID_CAPTURE_VIDEO, OnCaptureVideo)
  24. //}}AFX_MSG_MAP
  25. // Standard printing commands
  26. ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
  27. ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
  28. ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
  29. END_MESSAGE_MAP()
  30. /////////////////////////////////////////////////////////////////////////////
  31. // CCAPTUREView construction/destruction
  32. CCAPTUREView::CCAPTUREView()
  33. {
  34. // TODO: add construction code here
  35. }
  36. CCAPTUREView::~CCAPTUREView()
  37. {
  38. }
  39. BOOL CCAPTUREView::PreCreateWindow(CREATESTRUCT& cs)
  40. {
  41. // TODO: Modify the Window class or styles here by modifying
  42. //  the CREATESTRUCT cs
  43. return CView::PreCreateWindow(cs);
  44. }
  45. /////////////////////////////////////////////////////////////////////////////
  46. // CCAPTUREView drawing
  47. void CCAPTUREView::OnDraw(CDC* pDC)
  48. {
  49. CCAPTUREDoc* pDoc = GetDocument();
  50. ASSERT_VALID(pDoc);
  51. // TODO: add draw code for native data here
  52. }
  53. /////////////////////////////////////////////////////////////////////////////
  54. // CCAPTUREView printing
  55. BOOL CCAPTUREView::OnPreparePrinting(CPrintInfo* pInfo)
  56. {
  57. // default preparation
  58. return DoPreparePrinting(pInfo);
  59. }
  60. void CCAPTUREView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  61. {
  62. // TODO: add extra initialization before printing
  63. }
  64. void CCAPTUREView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  65. {
  66. // TODO: add cleanup after printing
  67. }
  68. /////////////////////////////////////////////////////////////////////////////
  69. // CCAPTUREView diagnostics
  70. #ifdef _DEBUG
  71. void CCAPTUREView::AssertValid() const
  72. {
  73. CView::AssertValid();
  74. }
  75. void CCAPTUREView::Dump(CDumpContext& dc) const
  76. {
  77. CView::Dump(dc);
  78. }
  79. CCAPTUREDoc* CCAPTUREView::GetDocument() // non-debug version is inline
  80. {
  81. ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CCAPTUREDoc)));
  82. return (CCAPTUREDoc*)m_pDocument;
  83. }
  84. #endif //_DEBUG
  85. /////////////////////////////////////////////////////////////////////////////
  86. // CCAPTUREView message handlers
  87. void CCAPTUREView::OnShowVideo() 
  88. {
  89. // TODO: Add your command handler code here
  90. //显示图像
  91. /*
  92. IplImage* img = cvLoadImage("E:\pic1.bmp");
  93. cvNamedWindow("image", CV_WINDOW_AUTOSIZE);
  94. cvShowImage("image", img);
  95. cvWaitKey(0);
  96. cvReleaseImage(&img);
  97. cvDestroyWindow("image");
  98. */
  99. //显示视频
  100. cvNamedWindow("Example2", CV_WINDOW_AUTOSIZE);
  101. CvCapture* capture = cvCreateFileCapture("E:\video1.avi");
  102. IplImage* frame;
  103. while (1)
  104. {
  105. frame = cvQueryFrame(capture);
  106. if (!frame)
  107. {
  108. break;
  109. }
  110. cvShowImage("Example2", frame);
  111. char c = cvWaitKey(33);
  112. if (c==27)
  113. {
  114. break;
  115. }
  116. }
  117. cvReleaseCapture(&capture);
  118. cvDestroyWindow("Example2");
  119. }
  120. void CCAPTUREView::OnCaptureVideo() 
  121. {
  122. // TODO: Add your command handler code here
  123. IplImage* pFrame = NULL; 
  124. // IplImage* pFrImg = NULL; 
  125. // IplImage* pBkImg = NULL; 
  126. CvCapture* pCapture = NULL; 
  127. IplImage* imgGray = NULL;
  128. IplImage* imgGrayBack = NULL;
  129. pCapture = cvCaptureFromAVI("E:\video1.avi") ;
  130. //标定视频
  131. int nFrameCount=0;
  132. nFrameCount=cvGetCaptureProperty(pCapture,CV_CAP_PROP_FRAME_COUNT);
  133. //获得帧数
  134. printf("%d",nFrameCount);
  135. // for (i=0;i<nFrameCount;i++) 
  136. cvNamedWindow("show", CV_WINDOW_AUTOSIZE);
  137. int i;
  138. for (i=0; i<nFrameCount; i++)
  139. //  i++;
  140. pFrame = cvQueryFrame(pCapture); //抓取一帧
  141. if (i%25 == 1)
  142. //每5帧捕获一次
  143. {
  144. CString strFileName("E:\video1\");
  145. //去e盘下面建一个文件夹,叫video1
  146. CString num;
  147. num.Format("%d", i);
  148. strFileName += num;
  149. strFileName += ".bmp";
  150. const char* e = strFileName.GetBuffer(strFileName.GetLength());
  151. imgGray = cvCreateImage(cvSize(pFrame->width, pFrame->height), IPL_DEPTH_8U, 1);
  152. imgGrayBack = cvCreateImage(cvSize(pFrame->width, pFrame->height), IPL_DEPTH_8U, 1);
  153. cvCvtColor(pFrame, imgGray, CV_BGR2GRAY);
  154. // cvConvertImage(imgGray, imgGrayBack, CV_CVTIMG_FLIP);
  155. cvFlip(imgGray, imgGrayBack, 0);
  156. cvShowImage("show", imgGrayBack);
  157. cvSaveImage(e, imgGrayBack);
  158. }
  159. char c = cvWaitKey(40);
  160. if (c==27)//ESC退出循环
  161. {
  162. break;
  163. }
  164. cvReleaseCapture(&pCapture);
  165. cvDestroyWindow("show");
  166. /*
  167. cvNamedWindow("aaa");
  168. cvShowImage("aaa",pFrame);
  169. //RGB转换成亮度
  170. IplImage* origin_rgb = NULL ;//定义rgb空间的存储
  171. IplImage* origin_ycc = NULL ;//定义转换成YCrCb空间的存储
  172. IplImage* lumi = NULL ;//定义亮度分量的存储空间
  173. origin_rgb = cvCreateImage(cvSize(pFrame->width, pFrame->height), IPL_DEPTH_8U, 3 );
  174. origin_ycc = cvCreateImage(cvSize(pFrame->width, pFrame->height), IPL_DEPTH_8U, 3 );
  175. lumi = cvCreateImage(cvSize(pFrame->width, pFrame->height), IPL_DEPTH_8U, 1 );
  176. cvCopy(pFrame,origin_rgb) ;
  177. cvCvtColor(origin_rgb,origin_ycc,CV_RGB2YCrCb) ;
  178. cvSplit(origin_ycc, lumi, NULL, NULL, NULL ); //获取Y亮度分量
  179. cvNamedWindow("Origin_rgb");
  180. cvMoveWindow("Origin_rgb",0,0);
  181. cvShowImage("Origin_rgb",origin_rgb);
  182. cvNamedWindow("origin_ycc");
  183. cvMoveWindow("origin_ycc",360,0);
  184. cvShowImage("origin_ycc",origin_ycc);
  185. cvWaitKey(-1) ;
  186. cvDestroyWindow("Origin_rgb"); 
  187. cvDestroyWindow("origin_ycc"); 
  188. cvReleaseImage(&origin_rgb); 
  189. cvReleaseImage(&origin_ycc); 
  190. //cvReleaseImage(&pFrame);
  191. cvReleaseCapture(&pCapture); 
  192. */
  193. }