MFC显示Mat数据格式(Opencv)图片,保证4字节对齐,图片缩放不错行

/*! 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
*/

 

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值