博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
彻底解决显示Opencv中Mat图像到Mfc窗口问题
阅读量:4303 次
发布时间:2019-05-27

本文共 1199 字,大约阅读时间需要 3 分钟。

第一步,转换格式前预先获得待显示控件的大小,若相等则不做处理,若不等则首先改变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);
    }
    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;
    }
 
    return 0;
}

第三步,可以直接显示CImage格式图像
    CImage imgDst;
        Mat2CImage(&cvImgTmp, imgDst);
        imgDst.Draw(GetDlgItem(IDC_STATIC_SRC)->GetDC()->GetSafeHdc(), rect);

原文:https://blog.csdn.net/u014682691/article/details/51296219 
 

你可能感兴趣的文章
子网掩码
查看>>
第一天上班没精神
查看>>
启动eclipse报错:Failed to load the JNI shared library
查看>>
eclipse安装插件的两种方式在线和离线
查看>>
linux下源的相关笔记(suse)
查看>>
linux系统分区文件系统划分札记
查看>>
Linux(SUSE 12)安装Tomcat
查看>>
Linux(SUSE 12)安装jboss4并实现远程访问
查看>>
Neutron在给虚拟机分配网络时,底层是如何实现的?
查看>>
netfilter/iptables全攻略
查看>>
Overlay之VXLAN架构
查看>>
Eclipse : An error occurred while filtering resources(Maven错误提示)
查看>>
在eclipse上用tomcat部署项目404解决方案
查看>>
web.xml 配置中classpath: 与classpath*:的区别
查看>>
suse如何修改ssh端口为2222?
查看>>
详细理解“>/dev/null 2>&1”
查看>>
suse如何创建定时任务?
查看>>
suse搭建ftp服务器方法
查看>>
centos虚拟机设置共享文件夹并通过我的电脑访问[增加smbd端口修改]
查看>>
文件拷贝(IFileOperation::CopyItem)
查看>>