MFC_把OpenCV的图像显示在MFC的PictureControl控件

 2022-5-15更新:用目前最新的4.5.5测试无法显示,又换回4.5.1版本了


参考:OpenCV初探 —— 将OpenCV窗口链接在MFC的PictureControl控件中https://blog.csdn.net/OmuziO/article/details/120946216


头文件中添加:

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui_c.h>
#include<opencv2\imgproc\types_c.h>
using namespace cv;

 cpp的OnInitDialog()里添加:

/*嵌入OpenCV显示窗口*/
// CWnd:MFC窗口类的基类,IDC_ORIIMG:PictureControl的ID
CWnd* pWnd1 = GetDlgItem(IDC_STATIC_Pic);
// GetClientRect:获得控件大小
pWnd1->GetClientRect(&rect1);
// 设置opencv窗口名
namedWindow("src1", WINDOW_AUTOSIZE);
// 获取窗口句柄
HWND hWndl = (HWND)cvGetWindowHandle("src1");
// 获取父窗口句柄
HWND hParent1 = ::GetParent(hWndl);
// 设置opencv窗体与PictureControl控件绑定
::SetParent(hWndl, GetDlgItem(IDC_STATIC_Pic)->m_hWnd);
// 在指定窗口中显示
::ShowWindow(hParent1, SW_HIDE);

 显示图像到控件:

Mat image(pFrame->iHeight, pFrame->iWidth, CV_8UC1, (uchar*)imgData[index - 1]);//相机MONO8格式图像数据转换为OpenCV的Mat图像格式

// 设置图片大小与窗体匹配
resize(image_g, image_g, Size(rect1.Width(), rect1.Height()));
// 在窗口中显示图片
imshow("src1", image_g);

猜你喜欢

转载自blog.csdn.net/qq_36917144/article/details/123840857