OpenCV学习笔记之 ( 三 ) MFC显示Mat图片

来源:互联网 发布:oracle 数据字典 编辑:程序博客网 时间:2024/05/01 23:11

以下步骤参考以下链接 

http://blog.csdn.net/dcrmg/article/details/51913160 

原理及详解见上链接。

下面只讲步骤。   

一、打开VS2010,建立对话框的项目。




二、建立“PictureControl”,"Button"按钮,修改属性





三、在Show_Mat_ImageDlg.h添加如下头文件

#include <iostream>#include<opencv2/core/core.hpp>#include<opencv2/highgui/highgui.hpp>#include<opencv2/imgproc/imgproc.hpp>#include<string>using namespace std;using namespace cv;


四、在OnInitDialog初始化函数里添加代码

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

五、为“Open Image”按钮添加事件处理程序


六、添加如下代码:

CString picPath;   //定义图片路径变量  CFileDialog dlg(TRUE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT|OFN_ALLOWMULTISELECT,   NULL, this);   //选择文件对话框  if(dlg.DoModal() == IDOK)  {  picPath= dlg.GetPathName();  //获取图片路径  }  //CString to string  使用这个方法记得字符集选用“使用多字节字符”,不然会报错  string picpath=picPath.GetBuffer(0);      Mat image=imread(picpath);     Mat imagedst;  //以下操作获取图形控件尺寸并以此改变图片尺寸  CRect rect;  GetDlgItem(IDC_PIC_STATIC)->GetClientRect(&rect);  Rect dst(rect.left,rect.top,rect.right,rect.bottom);  resize(image,imagedst,cv::Size(rect.Width(),rect.Height()));   imshow("view",imagedst);


七,编译运行




0 0
原创粉丝点击