在MFC中显示cv::Mat

BOOL DrawMat(CDC *pDC, cv::Mat &img, CRect &rect)
{
	if(img.empty() || img.depth() != CV_8U || img.channels() != 3)
		return FALSE;

	BITMAPINFO bmInfo;
	memset(&bmInfo, 0, sizeof(bmInfo));

	bmInfo.bmiHeader.biSize   = sizeof(BITMAPINFOHEADER);
	bmInfo.bmiHeader.biWidth  = img.cols;
	bmInfo.bmiHeader.biHeight = -img.rows;
	bmInfo.bmiHeader.biPlanes = 1;
	bmInfo.bmiHeader.biBitCount = 24;
	bmInfo.bmiHeader.biCompression = BI_RGB;

	int width = cv::min(rect.Width(), img.cols);
	int height = cv::min(rect.Height(), img.rows);

	StretchDIBits(      
        pDC->GetSafeHdc(),
        0, 0, width, height, //rect.Width(), rect.Height(),
        0, 0, width, height, //img.cols, img.rows,
        img.data,
        &bmInfo,
        DIB_RGB_COLORS,
        SRCCOPY);

	return TRUE;
}

你可能感兴趣的:(OpenCV)