知乎上有一个问题“在mfc框架中,有上面方法能直接将opencv2.0库中的Mat格式图片传递到Picture Control”中显示?
一直以来,我使用的方法都是shiqiyu在opencvchina上面提供的引入directshow,并且采用cvvimage和cameraDs的方法。这个方法虽然在xp/win7/win8下面都能够成果使用,但是一直以来我都没有动机去深入看一看这个方法。这次在知乎上面看到 jie wu 提出的“将Opencv窗口添加到PictureControl”中的方法,感到思路很好,进行了具体实现
http://pan.baidu.com/s/1nuixdhR
{
CFormView : :OnInitialUpdate();
GetParentFrame() - >RecalcLayout();
ResizeParentToFit();
//根据控件的大小设置初始帧的大小
CRect rect;
GetDlgItem(IDC_PBSRC) - >GetClientRect( &rect ); // 获取控件尺寸位置
m_lframe = Mat : :zeros(rect.Height(),rect.Width(),CV_8UC3);
GetDlgItem(IDC_PBSRC) - >GetClientRect( &rect );
m_rframe = Mat : :zeros(rect.Height(),rect.Width(),CV_8UC3);
//绑定Mat到Picturebox上去
namedWindow( "src",WINDOW_AUTOSIZE);
HWND hWnd = (HWND)cvGetWindowHandle( "src");
HWND hParnt = : :GetParent(hWnd);
: :SetParent(hWnd,GetDlgItem(IDC_PBSRC) - >m_hWnd);
: :ShowWindow(hParnt,SW_HIDE);
namedWindow( "dst",WINDOW_AUTOSIZE);
hWnd = (HWND)cvGetWindowHandle( "dst");
hParnt = : :GetParent(hWnd);
: :SetParent(hWnd,GetDlgItem(IDC_PBDEST) - >m_hWnd);
: :ShowWindow(hParnt,SW_HIDE);
}
{
CFormView : :OnSize(nType, cx, cy);
CWnd * pwndsrc = GetDlgItem(IDC_PBSRC);
CWnd * pwnddst = GetDlgItem(IDC_PBDEST);
//计算出长宽,这里的长宽是按照比例的,图像居中显示
int iblank = 15; //边界空余
int iwidth = cx / 2 -iblank * 2;
int iheight =( int)(iwidth * 0. 75);
if (pwndsrc - >GetSafeHwnd() && pwnddst - >GetSafeHwnd()){
pwndsrc - >MoveWindow(iblank,(cy -iheight) * 0. 4,iwidth,iheight);
pwnddst - >MoveWindow(cx / 2 +iblank,(cy -iheight) * 0. 4,iwidth,iheight);
}
}
{
if (src.empty())
return;
CRect rect;
Mat dst = src.clone();
GetDlgItem(ID) - >GetClientRect( &rect ); // 获取控件尺寸位置
if (dst.channels() == 1)
cvtColor(dst, dst, CV_GRAY2BGR);
resize(dst,dst,Size(rect.Width(),rect.Height()));
imshow( "src",dst);
}