在mfc中利用opencv打开摄像头并显示在窗口上

平台:vs2013+opencv3.1.0

本来以为利用mfc打开摄像头并且显示到窗口上应该是很简单的一件事情,但是没想到却遇到了一点麻烦,首先我是根据这篇博客做的http://blog.csdn.net/jiyanfeng1/article/details/9169019,感觉代码可行,但是运行以后却发现摄像头是打开了,可是摄像头抓取的内容却没有显示。所以就根据这篇博客选取了另外一种方法。

第一步:首先创建一个基于对话框的mfc应用程序,当然我们要在vs2013中配置好相应的opencv,opencv的下载和配置这里也给出一篇博客http://www.cnblogs.com/zangdalei/p/5339316.html,同时我们需要CvvImage类,可以参考上面第一篇论文吧相应的CvvImage.h,CvvImage.cpp文件添加到工程中,同时在对话框的cpp程序中添加上CvvImage.h

第二步:在对话框中添加一个picture控件和两个打开关闭按钮,可以参考第一篇博客,将相关头文件添加进去

#include "stdafx.h"
#include "VideoMFC.h"
#include "VideoMFCDlg.h"
#include "afxdialogex.h"

#include "cv.h"
#include "cxcore.h"
#include "highgui.h"
#include "opencv2/highgui/highgui.hpp"  
#include "opencv2/imgproc/imgproc.hpp"
#include 
#include
#include "CvvImage.h" 
using namespace cv;
using namespace std;
其次把下列变量声明为全局变量,这里主要是需要把VideoCapture cap声明为全局变量,因为我们需要用cap打开关闭摄像头。
CvCapture* capture;
CRect rect;
CDC *pDC;
HDC hDC;
CWnd *pwnd;

VideoCapture cap;
第三步:看一下打开摄像头按钮的代码:
void CVideoMFCDlg::OnBnClickedButton1()
{
	// TODO: Add your control notification handler code here

	Mat frame;
	cap.open(0);

		cap >> frame;

		CDC* pDC = GetDlgItem(IDC_ShowImage)->GetDC();
		HDC hDC = pDC->GetSafeHdc();
		IplImage img = frame;
		CvvImage cimg;
		cimg.CopyOf(&img);
		CRect rect;
		GetDlgItem(IDC_ShowImage)->GetClientRect(&rect);
		cimg.DrawToHDC(hDC, &rect);
		ReleaseDC(pDC);

		SetTimer(1, 10, NULL);
		//imshow("调用摄像头", frame);
		//waitKey(30);


}
这里也是需要重点说的,首先我们利用opencv的库函数打开摄像头并且获取到mat格式的图片,但是由于利用mfc显示摄像头需要用到CvvImage,所以我们需要首先将图片转到IplImage格式,再将图片转到CvvImage格式,然后将其显示到picture控件上。正如在上边第一篇博客写的,在控制台程序中,我们可以很简单的通过for(;;)的空循环来不停的实现获取摄像头的每一帧,但是我发现这么做在MFC里面是不可行的。一个是因为MFC是用户界面程序,如果这么写的话,所有的界面都会卡住,而且这么写的话其他的功能按钮就失去作用了。这里为了实现获取摄像头的每一帧,我们要通过设定一个时间事件,让每隔一定时间,比如20ms,就调用一个函数,通过这个时间调用来获取摄像头的帧。
OnTimer函数如下
void CVideoMFCDlg::OnTimer(UINT_PTR nIDEvent)
{
	// TODO: Add your message handler code here and/or call default
	
	Mat frame;
	//cap.open(0);

	cap >> frame;

	CDC* pDC = GetDlgItem(IDC_ShowImage)->GetDC();
	HDC hDC = pDC->GetSafeHdc();
	IplImage img = frame;
	CvvImage cimg;
	cimg.CopyOf(&img);
	CRect rect;
	GetDlgItem(IDC_ShowImage)->GetClientRect(&rect);
	cimg.DrawToHDC(hDC, &rect);
	ReleaseDC(pDC);


	CDialogEx::OnTimer(nIDEvent);
}
第四步:关闭摄像头,这里采取了比较巧妙的办法,我们只是将时间函数停掉,并且关闭摄像头就ok了
KillTimer(1);
cap.release();
窗口完整程序(就是那个带Dlg的cpp文件)如下

// VideoMFCDlg.cpp : implementation file
//

#include "stdafx.h"
#include "VideoMFC.h"
#include "VideoMFCDlg.h"
#include "afxdialogex.h"

#include "cv.h"
#include "cxcore.h"
#include "highgui.h"
#include "opencv2/highgui/highgui.hpp"  
#include "opencv2/imgproc/imgproc.hpp"
#include 
#include
#include "CvvImage.h" 
using namespace cv;
using namespace std;

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


CvCapture* capture;
CRect rect;
CDC *pDC;
HDC hDC;
CWnd *pwnd;

VideoCapture cap;

CvVideoWriter* writer = 0;



// CAboutDlg dialog used for App About

class CAboutDlg : public CDialogEx
{
public:
	CAboutDlg();

// Dialog Data
	enum { IDD = IDD_ABOUTBOX };

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

// Implementation
protected:
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialogEx(CAboutDlg::IDD)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()


// CVideoMFCDlg dialog




CVideoMFCDlg::CVideoMFCDlg(CWnd* pParent /*=NULL*/)
	: CDialogEx(CVideoMFCDlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);


	
}

void CVideoMFCDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CVideoMFCDlg, CDialogEx)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, &CVideoMFCDlg::OnBnClickedButton1)
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_BUTTON2, &CVideoMFCDlg::OnBnClickedButton2)
	ON_WM_CLOSE()
END_MESSAGE_MAP()


// CVideoMFCDlg message handlers

BOOL CVideoMFCDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		BOOL bNameValid;
		CString strAboutMenu;
		bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
		ASSERT(bNameValid);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	
	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	// TODO: Add extra initialization here
	//pwnd = GetDlgItem(IDC_ShowImage);
	//pwnd->MoveWindow(35,30,352,288);
    //pDC =pwnd->GetDC();
	//pDC =GetDC();
    //hDC= pDC->GetSafeHdc();
	//pwnd->GetClientRect(&rect);


	

	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CVideoMFCDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialogEx::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CVideoMFCDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast(dc.GetSafeHdc()), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialogEx::OnPaint();
	}
}

// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CVideoMFCDlg::OnQueryDragIcon()
{
	return static_cast(m_hIcon);
}



void CVideoMFCDlg::OnBnClickedButton1()
{
	// TODO: Add your control notification handler code here
	//AfxMessageBox("OK");
	//Mat mat = imread("222.jpg");

	
	double m_observe_z[8];
	Mat frame;
	cap.open(0);

	//for (int ii = 0; ii< 50;ii++)
	//{

		cap >> frame;

		CDC* pDC = GetDlgItem(IDC_ShowImage)->GetDC();
		HDC hDC = pDC->GetSafeHdc();
		IplImage img = frame;
		CvvImage cimg;
		cimg.CopyOf(&img);
		CRect rect;
		GetDlgItem(IDC_ShowImage)->GetClientRect(&rect);
		cimg.DrawToHDC(hDC, &rect);
		ReleaseDC(pDC);

		SetTimer(1, 10, NULL);
		//imshow("调用摄像头", frame);
		//waitKey(30);
	//}


}


//void CVideoMFCDlg::OnTimer(UINT nIDEvent)
//{
//	
//}


void CVideoMFCDlg::OnTimer(UINT_PTR nIDEvent)
{
	// TODO: Add your message handler code here and/or call default
	/************************************************************************/
	/* 显示摄像头                                                           */
	/************************************************************************/
	
	//for (int ii = 0; ii< 50;ii++)
	//{
	//VideoCapture cap;
	//double m_observe_z[8];
	Mat frame;
	//cap.open(0);

	cap >> frame;

	CDC* pDC = GetDlgItem(IDC_ShowImage)->GetDC();
	HDC hDC = pDC->GetSafeHdc();
	IplImage img = frame;
	CvvImage cimg;
	cimg.CopyOf(&img);
	CRect rect;
	GetDlgItem(IDC_ShowImage)->GetClientRect(&rect);
	cimg.DrawToHDC(hDC, &rect);
	ReleaseDC(pDC);


	CDialogEx::OnTimer(nIDEvent);
}


void CVideoMFCDlg::OnBnClickedButton2()
{
	// TODO: Add your control notification handler code here
	KillTimer(1);
	cap.release();
	//cvReleaseVideoWriter(&writer);
	//cvReleaseCapture(&capture);
	//CDC MemDC; 
	//CBitmap m_Bitmap1;
	//m_Bitmap1.LoadBitmap(IDB_BITMAP1); 
	//MemDC.CreateCompatibleDC(NULL);
	//MemDC.SelectObject(&m_Bitmap1);
	//pDC->StretchBlt(rect.left,rect.top,rect.Width(),rect.Height(),&MemDC,0,0,48,48,SRCCOPY); 
}


void CVideoMFCDlg::OnClose()
{
	// TODO: Add your message handler code here and/or call default
	cvReleaseCapture(&capture);
	CDialogEx::OnClose();
}
程序还没有进行优化,还有很多不完善的地方,欢迎大家能改正修改。


展开阅读全文

没有更多推荐了,返回首页