opencv与mfc

Sometimes for simple demonstrations, mfc and opencv are often used. Now record the use of the two together.

One, first open VS2015, create an MFC project with the name opencv_mfc, select the dialog box type, and just follow the default operations for the rest.

##For example, display an image on the Button button and display image control:

Then open the dialog bar of the resource view and add controls to the dialog. Delete the two original default buttons, add a picture control, ID is IDC_PIC, and then add a button control, ID is IDC_BTN_OPEN, title is "open picture" to complete the above steps to get the following picture:

 

Second, switch to the Solution Explorer and add the following header files and declaration files in opencv_mfcDlg.h:

Create a new opencv attribute table in the project properties and configure the include path and library path.

Include the opencv header file in xxDlg.cpp:

#include "opencv2/opencv.hpp"
using namespace cv;

The above is the header file in OpenCV, and then add a member variable, mainly to get the size of the picture control:

CRect rect;

Third, add custom code to the OnInitDialog() function in the opencv_mfcDlg.cpp source file as follows:

//----------------------------【自定义代码处】--------------------------------------
	CWnd  *pWnd1 = GetDlgItem(IDC_PIC);//CWnd是MFC窗口类的基类,提供了微软基础类库中所有窗口类的基本功能。
	pWnd1->GetClientRect(&rect);//GetClientRect为获得控件相自身的坐标大小
	namedWindow("src1", WINDOW_AUTOSIZE);//设置窗口名
	HWND hWndl = (HWND)cvGetWindowHandle("src1");//hWnd 表示窗口句柄,获取窗口句柄
	HWND hParent1 = ::GetParent(hWndl);//GetParent函数一个指定子窗口的父窗口句柄
	::SetParent(hWndl, GetDlgItem(IDC_PIC)->m_hWnd);
	::ShowWindow(hParent1, SW_HIDE);
	Mat srcImg = imread("bk.jpg");
	resize(srcImg, srcImg, Size(rect.Width(), rect.Height()));
	imshow("src1", srcImg);
//----------------------------【自定义代码处】--------------------------------------

To put it simply, first get the size of the picture control, then use the namewindow function to create a window, then get its handle, and associate it with the picture control. Another picture is displayed later. If the picture is not displayed in the initialization, the program starts, and the picture control has a dark gray shadow, so a light-colored picture is added. (This step is not necessary)

Fourth, add a message response function to the button. Directly right-click the button in the resource view of the dialog box to add a message response function. The code is as follows:

void CcvMFCDlg::OnBnClickedBtnOpen()
{
	// TODO: 在此添加控件通知处理程序代码
	TCHAR szFilter[] = _T("图片文件 (*.jpg)|*.jpg|All Files (*.*)|*.*||");
	// 构造打开文件对话框   
	CFileDialog fileDlg(TRUE, _T("txt"), NULL, 0, szFilter, this);
	CString strFilePath;
 
	// 显示打开文件对话框   
	if (IDOK == fileDlg.DoModal())
	{
		// 如果点击了文件对话框上的“打开”按钮,则将选择的文件路径显示到编辑框里   
		strFilePath = fileDlg.GetPathName();
		USES_CONVERSION;
		char *s = T2A(strFilePath);
		Mat srcImg = imread(s);
		resize(srcImg, srcImg, Size(rect.Width(), rect.Height()));
		imshow("src1", srcImg);
	}
}

Step 3: When bk.jpg is not loaded, the window looks like this. The image area has a shadow. The last three lines of code in Step 3 use a picture consistent with the background to cover the shadow.

The effect after loading a picture:

 

Reference: MFC and openCV are used together to display pictures

Reference: Can't find the solution of cvGetWindowHandle()

Reference: [In MFC framework, OpenCV and MFC window binding method]

Encounter problems:

1、cvGetWindowHandle() is undefined

Solution: add on

#include<opencv2/highgui/highgui_c.h>

 

Guess you like

Origin blog.csdn.net/juluwangriyue/article/details/108886191