int CMultipleCameraDlg::ShowMat(cv::Mat img, CWnd* hWndDisplay)
{
if (img.empty())
return 0;
//构造将要显示的Mat版本图片
RECT rect;
::GetClientRect(hWndDisplay->GetSafeHwnd(), &rect);
cv::Mat imgShow(abs(rect.top - rect.bottom), abs(rect.right - rect.left), CV_8UC1);
resize(img, imgShow, imgShow.size());
imwrite("2.jpg", imgShow);
//在控件上显示要用到的CImage类图片
ATL::CImage CI;
Mat2CImage(imgShow,CI);
//在控件显示图片
HDC dc;
dc = ::GetDC(hWndDisplay->GetSafeHwnd());
CI.Draw(dc, 0, 0);
::ReleaseDC(hWndDisplay->GetSafeHwnd(), dc);
CI.Destroy();
return 0;
}
void Mat2CImage(Mat & mat, ATL::CImage & cimage)
{
if (0 == mat.total())
{
return;
}
int nChannels = mat.channels();
if ((1 != nChannels) && (3 != nChannels))
{
return;
}
int nWidth = mat.cols;
int nHeight = mat.rows;
cimage.Destroy();
cimage.Create(nWidth, nHeight, 8 * nChannels);
uchar *pucRow;
uchar *pucImage = (uchar*)cimage.GetBits();
int nStep = cimage.GetPitch();
if (1 == nChannels)
{
RGBQUAD* rgbquadColorTable;
int nMaxColors = 256;
rgbquadColorTable = new RGBQUAD[nMaxColors];
cimage.GetColorTable(0, nMaxColors, rgbquadColorTable);
for (int nColor = 0; nColor < nMaxColors; nColor++)
{
rgbquadColorTable[nColor].rgbBlue = (uchar)nColor;
rgbquadColorTable[nColor].rgbGreen = (uchar)nColor;
rgbquadColorTable[nColor].rgbRed = (uchar)nColor;
}
cimage.SetColorTable(0, nMaxColors, rgbquadColorTable);
delete[]rgbquadColorTable;
}
for (int nRow = 0; nRow < nHeight; nRow++)
{
pucRow = (mat.ptr<uchar>(nRow));
for (int nCol = 0; nCol < nWidth; nCol++)
{
if (1 == nChannels)
{
*(pucImage + nRow * nStep + nCol) = pucRow[nCol];
}
else if (3 == nChannels)
{
for (int nCha = 0; nCha < 3; nCha++)
{
*(pucImage + nRow * nStep + nCol * 3 + nCha) = pucRow[nCol * 3 + nCha];
}
}
}
}
}