MFC中显示Opencv的Mat 图像,视频

	// TODO:  在此添加额外的初始化代码
	namedWindow("view", WINDOW_AUTOSIZE);//这段程序是将opencv窗口父窗口设置为PictureContrlo,这里设置了四个窗口,
	HWND hWnd = (HWND)cvGetWindowHandle("view");//hWnd和hParent只能定义一次,多次使用,否则会出现异常的现象,窗口分离
	HWND hParent = ::GetParent(hWnd);
	::SetParent(hWnd, GetDlgItem(IDC_STATIC1)->m_hWnd);
	::ShowWindow(hParent, SW_HIDE);

	namedWindow("view1", WINDOW_AUTOSIZE);
	 hWnd = (HWND)cvGetWindowHandle("view1");
	 hParent = ::GetParent(hWnd);
	::SetParent(hWnd, GetDlgItem(IDC_STATIC2)->m_hWnd);
	::ShowWindow(hParent, SW_HIDE);

	namedWindow("view2", WINDOW_AUTOSIZE);
	 hWnd = (HWND)cvGetWindowHandle("view2");
	 hParent = ::GetParent(hWnd);
	::SetParent(hWnd, GetDlgItem(IDC_STATIC3)->m_hWnd);
	::ShowWindow(hParent, SW_HIDE);

	namedWindow("view3", WINDOW_AUTOSIZE);
	 hWnd= (HWND)cvGetWindowHandle("view3");
	 hParent = ::GetParent(hWnd);
	::SetParent(hWnd, GetDlgItem(IDC_STATIC4)->m_hWnd);
	::ShowWindow(hParent, SW_HIDE);
	Mat img;//这一段程序是将Mat中图像的大小转换为PictureControl对应大小,然后显示
	*(pCap) >> img;
	cvtColor(img,img,CV_RGB2GRAY);
	cv::Mat m_dst;
	CRect rect;
	GetDlgItem(IDC_STATIC1)->GetClientRect(&rect);
	cv::Rect dst(rect.left, rect.top, rect.right, rect.bottom);
	cv::resize(img, m_dst, cv::Size(rect.Width(), rect.Height()));
	imshow("view", m_dst);
	imshow("view1", m_dst);
	imshow("view2", m_dst);

根据网上的一些帖子,使用PictureControl控件显示opencv的图像,可以将opencv父窗口设置为PictureControl,并将PIctureControl隐藏。这种方法比较优雅。

你可能感兴趣的:(C++,mfc,oepncv)