19,468
社区成员
发帖
与我相关
我的任务
分享
void MatToCImage(Mat &mat, CImage &img)
{
if (!mat.data)
return;
int nBPP = mat.channels() * 8;
img.Destroy();
img.Create(mat.cols, mat.rows, nBPP);
if (nBPP == 8)
{
static RGBQUAD pRGB[256];
for (int i = 0; i < 256; i++)
pRGB[i].rgbBlue = pRGB[i].rgbGreen = pRGB[i].rgbRed = i;
img.SetColorTable(0, 256, pRGB);
}
uchar* psrc = mat.data;
uchar* pdst = (uchar*)img.GetBits();
int imgPitch = img.GetPitch();
for (int y = 0; y < mat.rows; y++)
{
memcpy(pdst, psrc, mat.cols*mat.channels());//mat->step is incorrect for those images created by roi (sub-images!)
psrc += mat.step;
pdst += imgPitch;
}
}
//使用如下:
Mat mt = imread();
CImage img;
MatToCimage(mt,img);
HBITMAP hbmp = img.Detach();