/*! display Mat on Picture Control in MFC
*! Mat img: the image that to display
*! UINT nID: the ID of control
*! support BGR(Mat default),BGRA,GARY
*! other type need use cvtColor to convert
*/
void CxxxDlg::DrawMat(cv::Mat srcImg, UINT nID)
{
cv::Mat imgTmp;
CRect rect;
GetDlgItem(nID)->GetClientRect(&rect);
cv::resize(srcImg, imgTmp, cv::Size(rect.Width(), rect.Height()));
// convert to CV_8UC4
switch (imgTmp.channels())
{
case 1:
cv::cvtColor(imgTmp, imgTmp, CV_GRAY2BGRA); // GRAY --> BGRA(CV_8UC4)
break;
case 3:
cv::cvtColor(imgTmp, imgTmp, CV_BGR2BGRA); // BGR --> BGRA(CV_8UC4)
break;
default:
break;
}
// why convert to CV_8UC4 ?
// because windows need 4-byte aligned in a row
// if there are 3 pixels in a row,
// CV_8UC3: 3 pixel = 9 bytes, 9 % 4 = 1(no alignment)
// CV_8UC4: 3 pixel = 12 bytes, 12 % 4 = 0(it's ok)
// calculate how many bytes a pixel
int pixelBytes = imgTmp.channels()*(imgTmp.depth() + 1);
// create a header by using BITMAPINFO structure
BITMAPINFO bitInfo;
bitInfo.bmiHeader.biBitCount = 8 * pixelBytes;
bitInfo.bmiHeader.biWidth = imgTmp.cols;
bitInfo.bmiHeader.biHeight = -imgTmp.rows;
bitInfo.bmiHeader.biPlanes = 1;
bitInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bitInfo.bmiHeader.biCompression = BI_RGB;
bitInfo.bmiHeader.biClrImportant = 0;
bitInfo.bmiHeader.biClrUsed = 0;
bitInfo.bmiHeader.biSizeImage = 0;
bitInfo.bmiHeader.biXPelsPerMeter = 0;
bitInfo.bmiHeader.biYPelsPerMeter = 0;
// display Mat
// Mat data + BITMAPINFO --> MFC DigItem
CDC *pDC = GetDlgItem(nID)->GetDC();
::StretchDIBits(
pDC->GetSafeHdc(),
0, 0, rect.Width(), rect.Height(),
0, 0, rect.Width(), rect.Height(),
imgTmp.data,
&bitInfo,
DIB_RGB_COLORS,
SRCCOPY
);
ReleaseDC(pDC);
}
/*
思路:
Windows下显示图片存在4字节对齐的问题,Mat默认的颜色空间为BGR,一个像素3个字节,如果一行有5个像素(15个字节),
那么直接在MFC上缩放显示就会出错(控件比图片大时显示好像没问题,但是控件比图片小时显示就会错开).
网上许多都是遍历图片计算一行的字节数然后手动在后面补0对齐的.
我的思路是直接把图片转成4通道.BGRA,一个像素就是4个字节,不管一行几个像素总是能保证对齐.
入参分别是要显示的Mat类型数据,和对应控件的ID
*/