MFC: The Mat type of picture displayed on the picture control

A, Mat image is displayed on the MFC Image Control

void CCameraLinkTestDlg::ShowMatImg(Mat& mat, int ID,CPoint point0,CPoint pointCont0)
{
	// TODO: 在此处添加实现代码.
	BITMAPINFO* pBmpInfo1 = NULL;
	if (mat.channels() == 1)//灰度图像显示
	{
		pBmpInfo1 = (BITMAPINFO*)new char[sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD)];////bmp图信息头	
		pBmpInfo1->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
		pBmpInfo1->bmiHeader.biWidth = mat.cols;//src_rec.Width();
		pBmpInfo1->bmiHeader.biHeight = -mat.rows; //-src_rec.Height();//为负值
		pBmpInfo1->bmiHeader.biPlanes = 1;
		pBmpInfo1->bmiHeader.biBitCount = 8;//八位
		pBmpInfo1->bmiHeader.biCompression = BI_RGB;
		pBmpInfo1->bmiHeader.biSizeImage = 0;
		pBmpInfo1->bmiHeader.biXPelsPerMeter = 0;
		pBmpInfo1->bmiHeader.biYPelsPerMeter = 0;
		pBmpInfo1->bmiHeader.biClrUsed = 0;
		pBmpInfo1->bmiHeader.biClrImportant = 0;

		for (int i = 0; i < 256; i++)//只有灰度图像需要颜色表
		{
			pBmpInfo1->bmiColors[i].rgbBlue = pBmpInfo1->bmiColors[i].rgbGreen = pBmpInfo1->bmiColors[i].rgbRed = (BYTE)i;
			pBmpInfo1->bmiColors[i].rgbReserved = 0;
		}
	}
	if (mat.channels() == 3)
	{
		pBmpInfo1 = (BITMAPINFO*) new char[sizeof(BITMAPINFOHEADER)];
		pBmpInfo1->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
		pBmpInfo1->bmiHeader.biWidth = mat.cols;//src_rec.Width();
		pBmpInfo1->bmiHeader.biHeight = -mat.rows; //-src_rec.Height();//为负值
		pBmpInfo1->bmiHeader.biPlanes = 1;
		pBmpInfo1->bmiHeader.biBitCount = 24;//24位
		pBmpInfo1->bmiHeader.biCompression = BI_RGB;
		pBmpInfo1->bmiHeader.biSizeImage = 0;
		pBmpInfo1->bmiHeader.biXPelsPerMeter = 0;
		pBmpInfo1->bmiHeader.biYPelsPerMeter = 0;
		pBmpInfo1->bmiHeader.biClrUsed = 0;
		pBmpInfo1->bmiHeader.biClrImportant = 0;
	}
	HDC h_dc = GetDlgItem(ID)->GetDC()->GetSafeHdc();
	CRect BoxSize;
	GetDlgItem(ID)->GetClientRect(&BoxSize);
	SetStretchBltMode(
		h_dc,           // handle to device context
		HALFTONE);
    StretchDIBits(h_dc, point0.x,point0.y, mat.cols, mat.rows, pointCont0.x, pointCont0.y, mat.cols, mat.rows, (void*)mat.data,
		(BITMAPINFO*)pBmpInfo1, DIB_RGB_COLORS, SRCCOPY);
	delete[]pBmpInfo1;
}

transfer:

//获取Picture Control控件的大小
CRect rect;
GetDlgItem(IDC_SHOWIMG)->GetClientRect(&rect);
leftTop.x = rect.left;
leftTop.y = rect.top;
widthOfPicCtrl = rect.Width();
heightOfPicCtrl = rect.Height();

//IDC_SHOWIMG:图片控件的ID
ShowMatImg(mat, IDC_SHOWIMG, CPoint(int((widthOfPicCtrl - mat.cols) / 2) +leftTop.x, int((heightOfPicCtrl - mat.rows)/2) + leftTop.y),leftTop);





Second, converting into CImage Mat Type Type

MatToCImage(Mat& mat, CImage& cImage)
{
	// TODO: 在此处添加实现代码.
	//图像宽
	int width = mat.cols;
	//图像高
	int height = mat.rows;
	//图像通道数
	int channels = mat.channels();

	cImage.Destroy();
	//创建一个CImage类型的图片对象
	cImage.Create(width, height, 8 * channels);
	
	uchar* ps;
	 //获取CImage的像素存贮区的指针
	uchar* pimg = (uchar*)cImage.GetBits();
	//每行的字节数,注意这个返回值有正有负
	int step = cImage.GetPitch();
	// 如果是1个通道的图像(灰度图像) DIB格式才需要对调色板设置  
	// CImage中内置了调色板,我们要对他进行赋值:
	if (1 == channels)
	{
		RGBQUAD* ColorTable;
		int MaxColors = 256;
		//这里可以通过CI.GetMaxColorTableEntries()得到大小(如果你是CI.Load读入图像的话)  
		ColorTable = new RGBQUAD[MaxColors];
		cImage.GetColorTable(0, MaxColors, ColorTable);//这里是取得指针  
		for (int i = 0; i < MaxColors; i++)
		{
			ColorTable[i].rgbBlue = (BYTE)i;
			//BYTE和uchar一回事,但MFC中都用它  
			ColorTable[i].rgbGreen = (BYTE)i;
			ColorTable[i].rgbRed = (BYTE)i;
		}
		cImage.SetColorTable(0, MaxColors, ColorTable);
		delete[]ColorTable;
	}

	for (int i = 0; i < height; i++)
	{
		ps = mat.ptr<uchar>(i);
		for (int j = 0; j < width; j++)
		{
			if (1 == channels)
			{
				*(pimg + i* step + j) = ps[j];
			}
			else if (3 == channels)
			{
				*(pimg + i*step + j* 3) = ps[j * 3];
				*(pimg + i*step + j* 3 + 1) = ps[j * 3 + 1];
				*(pimg + i*step + j* 3 + 2) = ps[j * 3 + 2];
			}
		}
	}
}

call function:

CWnd* pWnd = NULL;
//获得图片控件的句柄
pWnd = GetDlgItem(IDC_SHOWIMG);
//获得设备描述表DC
CDC* pDC = GetDlgItem(IDC_SHOWIMG)->GetDC();

//创建一个CImage类型的对象
CImage img;	
//将Mat类型转换成CImage类型				
MatToCImage(mat, img);
//显示在图片控件上
img.StretchBlt(pDC->m_hDC,0,0,mat.cols,mat.rows,0,0,mat.cols,mat.rows,SRCCOPY);

Keep learning:
taught you how to read with MFC and OpenCV, making mfc and display images (two ways)

C ++ MFC Control Picture control to display any image file (jpg, bmp, etc.) in proportion

Mat format image is displayed in an MFC program

MFC save the picture

He published 194 original articles · won praise 95 · views 80000 +

Guess you like

Origin blog.csdn.net/qq_41498261/article/details/104900656