Opencv ImlImage 在MFC中显示

原理:ImlImage转成BitMAPINFO,然后调用StretchDIBits就可以了。

1.OnInitDialog()

m_img = cvLoadImage("test.jpg");

ASSERT(m_img != NULL);

memset(&m_bitmapinfoheader, 0, sizeof(m_bitmapinfoheader));
m_bitmapinfoheader.bmiHeader.biSize        = sizeof(BITMAPINFOHEADER);
m_bitmapinfoheader.bmiHeader.biWidth       = m_img->width;;
m_bitmapinfoheader.bmiHeader.biHeight      = -m_img->height; // top-down image(什么图像顶行在这里的低行,具体看windows程序设计)
m_bitmapinfoheader.bmiHeader.biPlanes      = 1;
m_bitmapinfoheader.bmiHeader.biBitCount    = m_img->nChannels * 8;
m_bitmapinfoheader.bmiHeader.biCompression = BI_RGB;
m_bitmapinfoheader.bmiHeader.biSizeImage   = 0;
img_height = m_bitmapinfoheader.bmiHeader.biHeight;
img_width = m_bitmapinfoheader.bmiHeader.biWidth;

lpBits = (BYTE*)m_img->imageData;

2.类成员定义

BITMAPINFO m_bitmapinfoheader;

BYTE *lpBits;
CRect m_rc_show;
int img_width, img_height;

3.对话框OnPaint函数

CPaintDC dc(this);
GetDlgItem(IDC_STATIC_IMG)->GetWindowRect(m_rc_show);
ScreenToClient(m_rc_show);
//ClientToScreen(&rc);
int bRet = StretchDIBits(
dc.GetSafeHdc(),
m_rc_show.left,
m_rc_show.top,
m_rc_show.Width(),
m_rc_show.Height(),
0,
0,
img_width,
abs(img_height),
lpBits,
(BITMAPINFO*)&m_bitmapinfoheader,
DIB_RGB_COLORS, SRCCOPY
);

F5,查看结果,好像图片较大会被拉伸。有了这个方法就可以处理opencv 采集usb 摄像头数据,并且处理了。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值