[C++] 纯文本查看 复制代码 //MAT 转位图
BOOL CMFCAppMedical305Dlg::Mat2Bmp(const cv::Mat& pImage, CBitmap* bitmap)
{
if (!pImage.empty() && pImage.depth() == CV_8U)
{
uchar buffer[sizeof(BITMAPINFOHEADER) + 1024];
BITMAPINFO* bmi = (BITMAPINFO*)buffer;
int bmp_w = pImage.cols, bmp_h = pImage.rows;
int width = bmp_w;
int height = bmp_h;
int bpp = pImage.channels() * 8;
HBITMAP hBitmap;
char* dst_data = 0;
assert(bmi && width >= 0 && height >= 0 && (bpp == 8 || bpp == 24 || bpp == 32));
BITMAPINFOHEADER* bmih = &(bmi->bmiHeader);
memset(bmih, 0, sizeof(*bmih));
bmih->biSize = sizeof(BITMAPINFOHEADER);
bmih->biWidth = width;
bmih->biHeight = -height; // Negative height for top-down DIB
bmih->biPlanes = 1;
bmih->biBitCount = (unsigned short)bpp;
bmih->biCompression = BI_RGB;
if (bpp == 8)
{
RGBQUAD* palette = bmi->bmiColors;
int i;
for (i = 0; i < 256; i++)
{
palette.rgbBlue = palette.rgbGreen = palette.rgbRed = (BYTE)i;
palette.rgbReserved = 0;
}
}
if (bitmap->m_hObject != NULL)
{
bitmap->Detach();
}
hBitmap = CreateDIBitmap(CClientDC(NULL), bmih, CBM_INIT, pImage.data, bmi, DIB_RGB_COLORS);
bitmap->Attach(hBitmap);
return TRUE;
}
return FALSE;
}
|