• 彻底解决显示Opencv中Mat图像到Mfc窗口问题


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

  • 相关阅读:
    java——base64 加密和解密
    BASE64Encoded() 方法报错说方法未定义
    java 实现HTTP连接(HTTPClient)
    如何关闭一些烦人的弹窗(总结)
    IDEA
    6、git和github
    5、预测和鉴定miRNA的靶基因
    4、在线blast比对结果解析(保守结构域)
    ASE分析
    3、Linux下配置Java环境
  • 原文地址:https://www.cnblogs.com/muyangshaonian/p/9650529.html
Copyright © 2020-2023  润新知