第一步,转换格式前预先获得待显示控件的大小,若相等则不做处理,若不等则首先改变Mat图像大小,再进行转换。
CRect rect;
GetDlgItem(IDC_STATIC_SRC)->GetClientRect(&rect);
cv::Size winSize(rect.right, rect.bottom);
// Resize the source to the size of the destination image if necessary
cv::Mat cvImgTmp(winSize, CV_8UC3);
if (imgSrc.size() != winSize)
{
cv::resize(imgSrc, cvImgTmp, winSize);
}
else
{
cvImgTmp = imgSrc;
}
第二步,将Mat图像转换成CImage格式
int Mat2CImage(Mat *mat, CImage &img) {
if (!mat || mat->empty())
return -1;
int nBPP = mat->channels() * 8;
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);
}