在MFC上显示mat格式图片

在对话框中的picture control中

在OnInitDialog()中

namedWindow("view", WINDOW_AUTOSIZE);
   HWND hWnd = (HWND) cvGetWindowHandle("view" );
   HWND hParent = ::GetParent(hWnd);
   ::SetParent(hWnd, GetDlgItem(IDC_PICTURE)->m_hWnd);
   ::ShowWindow(hParent, SW_HIDE);//将控件句柄设置为窗口的父句柄

   //img为Mat格式的带显示图片
 Mat img1;
   CRect rect;
   GetDlgItem(IDC_PICTURE)->GetClientRect(&rect);
   if (!img.empty())
   {
    cv::resize(img,img1,cv::Size(rect.Width(),rect.Height()));//将图片调整为控件大小显示
    imshow("view",img1);
   }

在单文档的视类里显示Mat

在这里要用到ATL库里的CImage库

void DrawMat2view(Mat m_image)
{


	if(m_image.empty())
	{
		return;
	}
		                                                                                                                                                                                                                                                              
	CDC *pDC = GetDC();
	
	/*rect 获取当前客户区*/
	CRect rect;
	GetClientRect(rect);


	//如果图像的大小大于屏幕区域的大小,则把图像缩放到原来的三分之一
// 	if (mat.cols>(rect.right-rect.left)||mat.rows>(rect.bottom-rect.top))
// 	{
// 		resize(mat,mat,Size(0.3*mat.cols,0.3*mat.rows));
// 	}
	/*如果注释下面两行代码,则图片充满客户区*/
	//rect.left  = rect.left + (rect.right - rect.left-mat.size().width)/2;
	rect.right  = rect.left + m_image.size().width;
	
	//rect.top  = rect.top + (rect.bottom - rect.top-mat.size().height)/2;
	rect.bottom = rect.top  + m_image.size().height;
	
	ATL::CImage CI;




	int width    = m_image.cols;
	int height   = m_image.rows;
	int channels = m_image.channels();


	CI.Destroy();     /*clear*/
	CI.Create(width, 
		height,       /*positive: left-bottom-up   or negative: left-top-down*/
		8*channels ); /*numbers of bits per pixel*/


	//copy values 复制Mat数据
	uchar* ps;
	uchar* pimg = (uchar*)CI.GetBits(); //A pointer to the bitmap buffer


	//The pitch is the distance, in bytes. represent the beginning of 
	// one bitmap line and the beginning of the next bitmap line
	int step = CI.GetPitch();


	if (m_image.channels() == 1)
	{
		RGBQUAD* pColorTable = NULL;
		int nMaxColors =  CI.GetMaxColorTableEntries();
		pColorTable = new RGBQUAD[nMaxColors];
		CI.GetColorTable(0, nMaxColors, pColorTable);
		for (int i = 0; i < nMaxColors; i++)
		{
			pColorTable[i].rgbBlue = (BYTE)i;
			pColorTable[i].rgbGreen = (BYTE)i;
			pColorTable[i].rgbRed = (BYTE)i;
		}
		CI.SetColorTable(0, nMaxColors, pColorTable);
		delete[] pColorTable;
	}


	if (m_image.channels() == 1|| m_image.channels() == 3)
	{
		for (int i = 0; i < height; ++i)
		{
			ps = (m_image.ptr<uchar>(i));
			for ( int j = 0; j < width; ++j )
			{
				if ( channels == 1 ) //gray
				{
					*(pimg + i*step + j) = ps[j];
				}
				else if ( channels == 3 ) //color
				{
					for (int k = 0 ; k < 3; ++k )
					{
						*(pimg + i*step + j*3 + k ) = ps[j*3 + k];
					}			
				}
			}	
		}




		/*设置伸缩模式,否则会出现图像的失真*/






	}else
	{
		MessageBox(_T("只能处理灰度图或者三通道图片"));
		ReleaseDC(pDC);
		return;
	}


	SetStretchBltMode(pDC->m_hDC,STRETCH_DELETESCANS|COLORONCOLOR );
	CI.StretchBlt(pDC->m_hDC,rect,SRCCOPY);


	/*释放资源*/
	ReleaseDC(pDC);


}
在虚函数ondraw()里调用DrawMat2view(mat_m)函数,将mat_m设置为全局变量,通过相应ID_FILE_OPEN打图像;

ID_FILE_OPEN响应函数

LPCTSTR lpszFilter=_T("Image Files(*.*)|*.*|ImageFiles(*.bmp)|*.bmp|任何文件|*.*||");

	CFileDialog fdlg(TRUE,lpszFilter,NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,lpszFilter,NULL);
	CString filename  ;

	if(fdlg.DoModal()== IDOK)
	{
		filename = fdlg.GetPathName();
		string str = CString2StdString(filename);  //将读取的unicode类型的文件路径转换为std类型
    /*string CImageProcessingView::CString2StdString(const CString& cstr)
     {
	  CT2A str(cstr);
	  return string(str.m_psz);
     }*/
		m_cvImage=imread(str,2|4);
		if (!m_cvImage.data)
		{
			AfxMessageBox(_T("图像读取失败!"));
			return;
		}
		
		//IsFileOpen = TRUE;

	}
	Invalidate();//窗口重绘



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值