MFC中基于OpenCV实现Picture Control控件成像方法有两种,一种是OpenCV2.2以前版本的绘制,另外一种是OpenCV2.2以后版本的绘制。
(1)在OpenCV2.2之前的版本利用CvvImage.h,CvvImage.cpp中的DrawToHDC函数实现。
(2)在OpenCV2.2之后的版本利用namedwindow创建窗户与控件的窗口子父绑定实现。
1.利用CvvImage.h,CvvImage.cpp中的实现
capture = cvCreateCameraCapture(0);//读入摄像头
if (capture==0)
{
AfxMessageBox("读入摄像头错误");
return;
}
frame = cvQueryFrame(capture);//获取摄像头视频帧
pDc = GetDlgItem(IDC_PIC_STATIC)->GetDC();//获得Picture control控件CDC指针
GetDlgItem(IDC_PIC_STATIC)->GetClientRect(rect);//获得Picture control控件区域大小
hDc = pDc->GetSafeHdc();//获得Picture control控件的DC句柄
CvvImage m_CvvImage;
m_CvvImage.CopyOf(frame,1);//深拷贝
m_CvvImage.DrawToHDC(hDc,&rect);//将视频帧绘制到Picture control控件
ReleaseDC(pDc);
SetTimer(1,25,NULL);
}
void CFull_Video_GenerationDlg::OnTimer(UINT_PTR nIDEvent)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
frame = cvQueryFrame(capture);
pDc = GetDlgItem(IDC_PIC_STATIC)->GetDC();
GetDlgItem(IDC_PIC_STATIC)->GetClientRect(rect);
hDc = pDc->GetSafeHdc();
CvvImage m_CvvImage;
m_CvvImage.CopyOf(frame,1);
m_CvvImage.DrawToHDC(hDc,&rect);
ReleaseDC(pDc);
CDialogEx::OnTimer(nIDEvent);
}
2.利用namedwindow窗口绑定实现
void CFull_Video_GenerationDlg::OnBnClickedPlay()
{
// TODO: 在此添加控件通知处理程序代码
VideoCapture Matcapture(0);
GetDlgItem(IDC_PIC_STATIC)->GetClientRect(rect);//获取Picture control控件区域大小
namedWindow("aaa",CV_WINDOW_NORMAL);//创建窗口 正常显示
resizeWindow("aaa",rect.Width(),rect.Height());//调整窗口大小为Picture control控件区域大小
HWND hWnd = (HWND)cvGetWindowHandle("aaa");//获得创建的窗口句柄
HWND hParent = ::GetParent(hWnd);//获得其父句柄
::SetParent(hWnd, GetDlgItem(IDC_PIC_STATIC)->m_hWnd);//设置创建窗口的父窗口为Picture control控件
::ShowWindow(hParent, SW_HIDE);//将父窗口及其子窗口进行隐藏
while (1)
{
Matcapture>>srcImage;
imshow("aaa",srcImage);
if (waitKey(10)>=0)
{
break;
}
}
}