绘制Mat图像
GDI
void drawResult (cv::Mat outTrace, unsigned int id)
{
BYTE *g_pBits;
HDC g_hMemDC;
HBITMAP g_hBmp, g_hOldBmp;
CDC *pDC;
CStatic *pic;
int width, height;
CRect rect;
/******************初始化将Mat转为Iplimage************************************/
IplImage tmpIpl = (IplImage)(outTrace);
tmpIpl.imageData = (char*)outTrace.data;
IplImage* img = cvCreateImage (cvGetSize (&tmpIpl), IPL_DEPTH_8U, 3);
cvCvtColor (&tmpIpl, img, CV_GRAY2RGB);
/******************获取控件大小以及句柄************************************/
//TRACE("-----%d\n",((AfxGetApp()->m_pMainWnd)->m_hWnd));
pDC = (AfxGetApp()->m_pMainWnd)->GetDlgItem (id)->GetDC ();
pic = (CStatic*)(AfxGetApp()->m_pMainWnd)->GetDlgItem (id);
pic->GetClientRect (&rect);
width = rect.Width ();
height = rect.Height ();
g_hMemDC = ::CreateCompatibleDC (pDC->m_hDC);//创建兼容DC
BYTE bmibuf[sizeof(BITMAPINFO)+256 * sizeof(RGBQUAD)];
memset (bmibuf, 0, sizeof(bmibuf));
BITMAPINFO *pbmi = (BITMAPINFO*)bmibuf;
pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
pbmi->bmiHeader.biWidth = img->width;
pbmi->bmiHeader.biHeight = img->height;
pbmi->bmiHeader.biPlanes = 1;
pbmi->bmiHeader.biBitCount = 24;
pbmi->bmiHeader.biCompression = BI_RGB;
g_hBmp = ::CreateDIBSection (g_hMemDC, pbmi, DIB_RGB_COLORS, (void**)&g_pBits, 0, 0);//创建应用程序可以直接写入的、与设备无关的位图(DIB)
g_hOldBmp = (HBITMAP)::SelectObject (g_hMemDC, g_hBmp);//复原兼容DC数据
BitBlt (g_hMemDC, 0, 0, width, height, pDC->m_hDC, 0, 0, SRCCOPY);
//修改图像内容:g_pBits
int l_width = WIDTHBYTES (img->width* pbmi->bmiHeader.biBitCount);
for (int row = 0; row < img->height; row++)
memcpy (&g_pBits[row*l_width], &img->imageData[(img->height - row - 1)*l_width], l_width);
TransparentBlt (pDC->m_hDC, 0, 0, width, height, g_hMemDC, 0, 0, img->width, img->height, RGB (0, 0, 0));
SelectObject (g_hMemDC, g_hOldBmp);
//释放内存资源
cvReleaseImage(&img);
(AfxGetApp()->m_pMainWnd)->ReleaseDC (pDC);
::DeleteDC (g_hMemDC);
DeleteObject (pic);
DeleteObject (g_hBmp);
DeleteObject (g_hOldBmp);
}
D2D
ID2D1Bitmap *t_pDrawBitmap;
cv::Mat bmpImg;
cv::cvtColor(img, bmpImg, CV_BGR2RGBA);
//cv::imshow("TEST1",img);
//cv::imwrite("test.bmp",bmpImg);
//cv::waitKey(0);
D2D1_BITMAP_PROPERTIES t_dbp = { { DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_IGNORE }, m_hDpi, m_vDpi };
if (SUCCEEDED(m_pRenderTarget->CreateBitmap(D2D1::SizeU(bmpImg.cols, bmpImg.rows), (void*)bmpImg.data, (UINT)bmpImg.step, t_dbp, &t_pDrawBitmap)))
{
D2D1_SIZE_F t_bmpSize = t_pDrawBitmap->GetSize();
m_pRenderTarget->DrawBitmap(t_pDrawBitmap, D2D1::RectF(
rect.left,
rect.top,
rect.right,
rect.bottom));
//m_pRenderTarget->DrawBitmap(t_pDrawBitmap, D2D1::RectF(
// 0,
// 0,
// t_bmpSize.width,
// t_bmpSize.height));
}