CAPTUREView.cpp
资源名称:CAPTURE.rar [点击查看]
上传用户:wzoulong
上传日期:2022-07-30
资源大小:1796k
文件大小:6k
源码类别:
OpenCV
开发平台:
Visual C++
- // CAPTUREView.cpp : implementation of the CCAPTUREView class
- //
- #include "stdafx.h"
- #include "CAPTURE.h"
- #include "CAPTUREDoc.h"
- #include "CAPTUREView.h"
- #include "cv.h"
- #include "cxcore.h"
- #include "cvaux.h"
- #include "highgui.h"
- #include "cvcam.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CCAPTUREView
- IMPLEMENT_DYNCREATE(CCAPTUREView, CView)
- BEGIN_MESSAGE_MAP(CCAPTUREView, CView)
- //{{AFX_MSG_MAP(CCAPTUREView)
- ON_COMMAND(ID_SHOW_VIDEO, OnShowVideo)
- ON_COMMAND(ID_CAPTURE_VIDEO, OnCaptureVideo)
- //}}AFX_MSG_MAP
- // Standard printing commands
- ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
- ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
- ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CCAPTUREView construction/destruction
- CCAPTUREView::CCAPTUREView()
- {
- // TODO: add construction code here
- }
- CCAPTUREView::~CCAPTUREView()
- {
- }
- BOOL CCAPTUREView::PreCreateWindow(CREATESTRUCT& cs)
- {
- // TODO: Modify the Window class or styles here by modifying
- // the CREATESTRUCT cs
- return CView::PreCreateWindow(cs);
- }
- /////////////////////////////////////////////////////////////////////////////
- // CCAPTUREView drawing
- void CCAPTUREView::OnDraw(CDC* pDC)
- {
- CCAPTUREDoc* pDoc = GetDocument();
- ASSERT_VALID(pDoc);
- // TODO: add draw code for native data here
- }
- /////////////////////////////////////////////////////////////////////////////
- // CCAPTUREView printing
- BOOL CCAPTUREView::OnPreparePrinting(CPrintInfo* pInfo)
- {
- // default preparation
- return DoPreparePrinting(pInfo);
- }
- void CCAPTUREView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
- {
- // TODO: add extra initialization before printing
- }
- void CCAPTUREView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
- {
- // TODO: add cleanup after printing
- }
- /////////////////////////////////////////////////////////////////////////////
- // CCAPTUREView diagnostics
- #ifdef _DEBUG
- void CCAPTUREView::AssertValid() const
- {
- CView::AssertValid();
- }
- void CCAPTUREView::Dump(CDumpContext& dc) const
- {
- CView::Dump(dc);
- }
- CCAPTUREDoc* CCAPTUREView::GetDocument() // non-debug version is inline
- {
- ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CCAPTUREDoc)));
- return (CCAPTUREDoc*)m_pDocument;
- }
- #endif //_DEBUG
- /////////////////////////////////////////////////////////////////////////////
- // CCAPTUREView message handlers
- void CCAPTUREView::OnShowVideo()
- {
- // TODO: Add your command handler code here
- //显示图像
- /*
- IplImage* img = cvLoadImage("E:\pic1.bmp");
- cvNamedWindow("image", CV_WINDOW_AUTOSIZE);
- cvShowImage("image", img);
- cvWaitKey(0);
- cvReleaseImage(&img);
- cvDestroyWindow("image");
- */
- //显示视频
- cvNamedWindow("Example2", CV_WINDOW_AUTOSIZE);
- CvCapture* capture = cvCreateFileCapture("E:\video1.avi");
- IplImage* frame;
- while (1)
- {
- frame = cvQueryFrame(capture);
- if (!frame)
- {
- break;
- }
- cvShowImage("Example2", frame);
- char c = cvWaitKey(33);
- if (c==27)
- {
- break;
- }
- }
- cvReleaseCapture(&capture);
- cvDestroyWindow("Example2");
- }
- void CCAPTUREView::OnCaptureVideo()
- {
- // TODO: Add your command handler code here
- IplImage* pFrame = NULL;
- // IplImage* pFrImg = NULL;
- // IplImage* pBkImg = NULL;
- CvCapture* pCapture = NULL;
- IplImage* imgGray = NULL;
- IplImage* imgGrayBack = NULL;
- pCapture = cvCaptureFromAVI("E:\video1.avi") ;
- //标定视频
- int nFrameCount=0;
- nFrameCount=cvGetCaptureProperty(pCapture,CV_CAP_PROP_FRAME_COUNT);
- //获得帧数
- printf("%d",nFrameCount);
- // for (i=0;i<nFrameCount;i++)
- cvNamedWindow("show", CV_WINDOW_AUTOSIZE);
- int i;
- for (i=0; i<nFrameCount; i++)
- {
- // i++;
- pFrame = cvQueryFrame(pCapture); //抓取一帧
- if (i%25 == 1)
- //每5帧捕获一次
- {
- CString strFileName("E:\video1\");
- //去e盘下面建一个文件夹,叫video1
- CString num;
- num.Format("%d", i);
- strFileName += num;
- strFileName += ".bmp";
- const char* e = strFileName.GetBuffer(strFileName.GetLength());
- imgGray = cvCreateImage(cvSize(pFrame->width, pFrame->height), IPL_DEPTH_8U, 1);
- imgGrayBack = cvCreateImage(cvSize(pFrame->width, pFrame->height), IPL_DEPTH_8U, 1);
- cvCvtColor(pFrame, imgGray, CV_BGR2GRAY);
- // cvConvertImage(imgGray, imgGrayBack, CV_CVTIMG_FLIP);
- cvFlip(imgGray, imgGrayBack, 0);
- cvShowImage("show", imgGrayBack);
- cvSaveImage(e, imgGrayBack);
- }
- char c = cvWaitKey(40);
- if (c==27)//ESC退出循环
- {
- break;
- }
- }
- cvReleaseCapture(&pCapture);
- cvDestroyWindow("show");
- /*
- cvNamedWindow("aaa");
- cvShowImage("aaa",pFrame);
- //RGB转换成亮度
- IplImage* origin_rgb = NULL ;//定义rgb空间的存储
- IplImage* origin_ycc = NULL ;//定义转换成YCrCb空间的存储
- IplImage* lumi = NULL ;//定义亮度分量的存储空间
- origin_rgb = cvCreateImage(cvSize(pFrame->width, pFrame->height), IPL_DEPTH_8U, 3 );
- origin_ycc = cvCreateImage(cvSize(pFrame->width, pFrame->height), IPL_DEPTH_8U, 3 );
- lumi = cvCreateImage(cvSize(pFrame->width, pFrame->height), IPL_DEPTH_8U, 1 );
- cvCopy(pFrame,origin_rgb) ;
- cvCvtColor(origin_rgb,origin_ycc,CV_RGB2YCrCb) ;
- cvSplit(origin_ycc, lumi, NULL, NULL, NULL ); //获取Y亮度分量
- cvNamedWindow("Origin_rgb");
- cvMoveWindow("Origin_rgb",0,0);
- cvShowImage("Origin_rgb",origin_rgb);
- cvNamedWindow("origin_ycc");
- cvMoveWindow("origin_ycc",360,0);
- cvShowImage("origin_ycc",origin_ycc);
- cvWaitKey(-1) ;
- cvDestroyWindow("Origin_rgb");
- cvDestroyWindow("origin_ycc");
- cvReleaseImage(&origin_rgb);
- cvReleaseImage(&origin_ycc);
- //cvReleaseImage(&pFrame);
- cvReleaseCapture(&pCapture);
- */
- }