将opencv中的Mat居中显示到MFC的picture控件上

将图像类型由Mat转换为CImage后,使图片适应居中显示

void CXXX::ShowImage(Mat mImage)
{
	CRect rect;//定义矩形类  
	CWnd *pWnd= GetDlgItem(IDC_STATIC_CAM_SHOW);			//获取控件句柄  
	pWnd->GetClientRect(&rect);								//获取句柄指向控件区域的大小  
	CDC *pDc = pWnd->GetDC();								//获取picture的DC  
	int nWindowW = rect.Width();							//获取窗口宽度 
	int nWindowH = rect.Height();							//获取窗口高度  
	int nImageW = mImage.cols;								//获取图片宽度  
	int nImageH = mImage.rows;								//获取图片高度
	/*使图片在控件中居中全部显示*/
	float ratioW = (float)nWindowW / nImageW;
	float ratioH = (float)nWindowH / nImageH;
	CImage ImageCam;
	MatToCImage(mImage, ImageCam);
	pDc->SetStretchBltMode(COLORONCOLOR);
	if (ratioW < ratioH)
		ImageCam.Draw(pDc->m_hDC, 0, (int)(nWindowH - nImageH * ratioW)/2, nWindowW, (int)(nImageH * ratioW), 0, 0, nImageW, nImageH);
	else
		ImageCam.Draw(pDc->m_hDC, (int)(nWindowW - nImageW * ratioH) / 2, 0, (int)(nImageW* ratioH), nWindowH, 0, 0, nImageW, nImageH);
	ReleaseDC(pDc);
}
	
void CXXX::MatToCImage(Mat& mat, CImage& cimage)
{
	if (0 == mat.total())
	{
		return;
	}
	int nChannels = mat.channels();
	if ((1 != nChannels) && (3 != nChannels))
	{
		return;
	}
	int nWidth = mat.cols;
	int nHeight = mat.rows;
	//重建cimage
	cimage.Destroy();
	cimage.Create(nWidth, nHeight, 8 * nChannels);
	//拷贝数据
	uchar* pucRow;									//指向数据区的行指针
	uchar* pucImage = (uchar*)cimage.GetBits();		//指向数据区的指针
	int nStep = cimage.GetPitch();					//每行的字节数,注意这个返回值有正有负
	if (1 == nChannels)								//对于单通道的图像需要初始化调色板
	{
		RGBQUAD* rgbquadColorTable;
		int nMaxColors = 256;
		rgbquadColorTable = new RGBQUAD[nMaxColors];
		cimage.GetColorTable(0, nMaxColors, rgbquadColorTable);
		for (int nColor = 0; nColor < nMaxColors; nColor++)
		{
			rgbquadColorTable[nColor].rgbBlue = (uchar)nColor;
			rgbquadColorTable[nColor].rgbGreen = (uchar)nColor;
			rgbquadColorTable[nColor].rgbRed = (uchar)nColor;
		}
		cimage.SetColorTable(0, nMaxColors, rgbquadColorTable);
		delete[]rgbquadColorTable;
	}
	for (int nRow = 0; nRow < nHeight; nRow++)
	{
		pucRow = (mat.ptr<uchar>(nRow));
		for (int nCol = 0; nCol < nWidth; nCol++)
		{
			if (1 == nChannels)
			{
				*(pucImage + nRow * nStep + nCol) = pucRow[nCol];
			}
			else if (3 == nChannels)
			{
				for (int nCha = 0; nCha < 3; nCha++)
				{
					*(pucImage + nRow * nStep + nCol * 3 + nCha) = pucRow[nCol * 3 + nCha];
				}
			}
		}
	}
}	
	

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要将OpenCVMat对象显示在WinForm上,可以使用Emgu CV库提供的ImageBox控件,ImageBox控件是一个PictureBox控件的扩展,可以方便地显示Emgu CV的图像。 以下是一个简单的示例代码,可以实现将OpenCVMat对象显示在WinForm上: 1. 首先,在WinForm的设计器添加一个ImageBox控件,命名为imageBox1。 2. 在代码添加以下引用: ``` using Emgu.CV; using Emgu.CV.UI; using Emgu.CV.Structure; ``` 3. 在代码创建一个OpenCVMat对象,然后将其转换为Emgu CV的Image<Bgr, Byte>对象,并将其显示在ImageBox控件上: ``` Mat mat = ... // 获取OpenCVMat对象 Image<Bgr, Byte> img = new Image<Bgr, Byte>(mat.ToImage<Bgr, Byte>().Bitmap); imageBox1.Image = img; ``` 这里,我们首先将OpenCVMat对象转换为Emgu CV的Image<Bgr, Byte>对象,然后将Image<Bgr, Byte>对象赋值给ImageBox控件的Image属性,即可显示在WinForm界面上。 需要注意的是,ImageBox控件默认使用的是BGR格式,因此需要将OpenCVMat对象转换为BGR格式。如果OpenCVMat对象使用的是灰度图像,可以使用以下代码进行转换: ``` Mat mat = ... // 获取OpenCVMat对象 Image<Gray, Byte> img = new Image<Gray, Byte>(mat.ToImage<Gray, Byte>().Bitmap); imageBox1.Image = img; ``` 同样地,如果需要在WinForm界面上频繁显示Mat对象,建议使用Emgu CV库,它可以直接将Mat对象显示在ImageBox控件上,也可以直接在Mat对象上进行图像处理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值