MFC:把Mat图像显示到picture control控件 /清除picture control控件上的图片

MFC设置picture control的大小

CRect rect ;
GetDlgItem(IDC_piCtrl1)->GetWindowRect(&rect); //IDC_WAVE_DRAW为Picture Control的ID  
ScreenToClient(&rect);
GetDlgItem(IDC_piCtrl1)->MoveWindow(rect.left, rect.top, 674, 800, true); //固定Picture Control控件的大小  

把Mat图像显示到picture control控件

void  CAnnotationDlg::ShowMatImage(Mat& src,int ID)

{
	try

	{
		if (src.empty())//没有图像时推出

		{
			return;
		}
		BITMAPINFO *pBmpInfo1 = NULL;

		if (src.channels() == 1)//灰度图像显示
		{
			pBmpInfo1 = (BITMAPINFO *)new char[sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD)];bmp图信息头	

			pBmpInfo1->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);

			pBmpInfo1->bmiHeader.biWidth = src.cols;//src_rec.Width();

			pBmpInfo1->bmiHeader.biHeight = -src.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 (src.channels() == 3)

		{

			pBmpInfo1 = (BITMAPINFO*) new char[sizeof(BITMAPINFOHEADER)];

			pBmpInfo1->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);

			pBmpInfo1->bmiHeader.biWidth = src.cols;//src_rec.Width();

			pBmpInfo1->bmiHeader.biHeight = -src.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);



		int jj = StretchDIBits(h_dc, BoxSize.left, BoxSize.top, BoxSize.Width(), BoxSize.Height(), 0, 0, src.cols, src.rows, (void *)src.data,

			(BITMAPINFO*)pBmpInfo1, DIB_RGB_COLORS, SRCCOPY);

		delete[]pBmpInfo1;



		return;

	}

	catch (...)

	{

		return;

	}

}

调用举例

ShowMatImage(img, IDC_piCtrl1);

清除picture control控件上的图片

GetDlgItem(IDC_piCtrl1)->ShowWindow(FALSE);
这是点击按钮的响应函数 void CCrazyDlg::OnBnClickedOk() { CString FilePath; CFileDialog FileDlg(TRUE); if (IDOK == FileDlg.DoModal()) { //获取FileOpen对话框返回的路径名 FilePath = FileDlg.GetPathName(); //GetPathName返回的是CString类型,要经过转换为string类型才能使用imread打开图片 //std::string pathName(FilePath.GetBuffer()); std::string tempName = (LPCSTR)CStringA(FilePath); const char*pathName = tempName.c_str(); Mat bin; //读取图片 Mat im = imread(pathName); // 读图,并转化为灰度图 threshold(im, bin, 120, 255, CV_THRESH_BINARY | THRESH_OTSU); //显示图片 ShowMat(bin, GetDlgItem(IDC_SHOW)->GetSafeHwnd()); } } 这是ShowMat 函数 int CCrazyDlg::ShowMat(Mat img, HWND hWndDisplay) { if (img.channels()<3) { return -1; } //构造将要显示Mat版本图片 RECT rect; ::GetClientRect(hWndDisplay, &rect); Mat imgShow(abs(rect.top - rect.bottom), abs(rect.right - rect.left), CV_8UC3); resize(img, imgShow, imgShow.size()); //在控件显示要用到的CImage类图片 ATL::CImage CI; int w = imgShow.cols;//宽 int h = imgShow.rows;//高 int channels = imgShow.channels();//通道数 CI.Create(w, h, 8 * channels); //CI像素的复制 uchar *pS; uchar *pImg = (uchar *)CI.GetBits();//得到CImage数据区地址 int step = CI.GetPitch(); for (int i = 0; i<h; i++) { pS = imgShow.ptr<uchar>(i); for (int j = 0; j<w; j++) { for (int k = 0; k<3; k++) *(pImg + i*step + j * 3 + k) = pS[j * 3 + k]; //注意到这里的step不用乘以3 } } //在控件显示图片 HDC dc; dc = ::GetDC(hWndDisplay); CI.Draw(dc, 0, 0); ::ReleaseDC(hWndDisplay, dc); CI.Destroy(); return 0; } 我调试到threshold()那里就停止运行,错误信息是0x7734C42D 处(位于 Crazy.exe 中)有未经处理的异常: Microsoft C++ 异常: cv::Exception,位于内存位置 0x0039DFB4 处。求解释,我困扰了好久了
©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页