MFC显示MAT图片(打开文件或摄像头)

基本参数:
  • opencv获取 mat图片
  • Picture Control的ID标识 IDC_STATIC2
方法1:文件读入

	CStatic* pwnd = (CStatic*)GetDlgItem(IDC_STATIC2);
	CRect rect;
	pwnd->GetWindowRect(rect);
	pwnd->ModifyStyle(0xf, SS_BITMAP | SS_CENTERIMAGE);
		pwnd->SetBitmap((HBITMAP)::LoadImage(NULL, _T("F:\\biancheng\\VS2019\\mymfc\\My_MFCApp\\pic\\2.BMP"),// 添加资源号或本地文件名		
		IMAGE_BITMAP,                                  // 装载位图IMAGE_CURSOR光标IMAGE_ICON图标		
		rect.Width(),                                     // 宽度0为默认大小	0	
		rect.Height(),                                    // 高度像素为单位    0
		LR_CREATEDIBSECTION | LR_DEFAULTSIZE | LR_LOADFROMFILE));
方法2:摄像头输入
		using namespace cv;

		cv::VideoCapture cam(0);
	
		cv::Mat frame;
	
		cam >> frame;
	
		CRect rect;

		GetDlgItem(IDC_STATIC)->GetClientRect(&rect);  // 获取图片控件矩形框

		// 缩放Mat,以适应图片控件大小

		//cv::resize(frame, frame, cv::Size(rect.Width(), rect.Height()));



		// 转换格式 ,便于获取BITMAPINFO

		switch (frame.channels())

		{

		case 1:

			cv::cvtColor(frame, frame, CV_GRAY2BGRA); // GRAY单通道

			break;

		case 3:

			cv::cvtColor(frame, frame, CV_BGR2BGRA);  // BGR三通道

			break;

		default:

			break;

		}



		int pixelBytes = frame.channels() * (frame.depth() + 1); // 计算一个像素多少个字节

		// 制作bitmapinfo(数据头)

		BITMAPINFO bitInfo;

		bitInfo.bmiHeader.biBitCount = 8 * pixelBytes;

		bitInfo.bmiHeader.biWidth = frame.cols;

		bitInfo.bmiHeader.biHeight = -frame.rows;   //注意"-"号(正数时倒着绘制)

		bitInfo.bmiHeader.biPlanes = 1;

		bitInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);

		bitInfo.bmiHeader.biCompression = BI_RGB;

		bitInfo.bmiHeader.biClrImportant = 0;

		bitInfo.bmiHeader.biClrUsed = 0;

		bitInfo.bmiHeader.biSizeImage = 0;

		bitInfo.bmiHeader.biXPelsPerMeter = 0;

		bitInfo.bmiHeader.biYPelsPerMeter = 0;



		CDC* pDC = GetDlgItem(IDC_STATIC2)->GetDC();  //获取图片控件DC

		CStatic* pwnd = (CStatic*)GetDlgItem(IDC_STATIC2);
		pwnd->GetWindowRect(rect);

		//绘图

		::StretchDIBits(

			pDC->GetSafeHdc(),

			0, 0, rect.Width(), rect.Height(),

			0, 0, frame.cols, frame.rows,

			frame.data,

			&bitInfo,

			DIB_RGB_COLORS,

			SRCCOPY

		);

		ReleaseDC(pDC);  //释放DC
方法3:窗口覆盖
  • OnInitialUpdate()中初始化
	cv::namedWindow("view", cv::WINDOW_AUTOSIZE);

	HWND hWnd = (HWND)cvGetWindowHandle("view");

	HWND hParent = ::GetParent(hWnd);

	::SetParent(hWnd, GetDlgItem(IDC_STATIC2)->m_hWnd);

	::ShowWindow(hParent, SW_HIDE);
  • 显示
	using namespace cv;

	cv::VideoCapture cam(0);

	cv::Mat frame;

	cam >> frame;

	CRect rect;	
	GetDlgItem(IDC_STATIC2)->GetClientRect(&rect);	
	Rect dst(rect.left, rect.top, rect.right, rect.bottom);	
	resize(frame, frame, cv::Size(rect.Width(), rect.Height()));
	imshow("view", frame);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值