当前位置:网站首页>将Opencv绘制图片显示在MFC Picture Control控件上
将Opencv绘制图片显示在MFC Picture Control控件上
2022-07-04 15:25:00 【八至】
//将picture control控件type类型设置为位图Bitmp格式
//对图片进行格式转换使其能正确显示在控件上
void CMFCApplication1Dlg::OnBnClickedButton2()
{
CRect rect;
GetDlgItem(IDC_STATIC110)->GetClientRect(&rect); // 获取picture control控件大小(控件ID:IDC_STATIC110)
double mult1 = 0.0, mult2 = 0.0;
cv::Size mults_size(imgTmp.size());//imgTmp为opencv绘制图片
if (rect.Width() < imgTmp.size().width)//根据窗口大小转换图片显示比例,保证图片完整显示
{
mults_size.width = rect.Width();
mult1 = (double)rect.Width() / (double)imgTmp.size().width;
mults_size.height = imgTmp.size().height * mult1;
}
if (rect.Height() < imgTmp.size().height)
{
mult2 = (double)rect.Height() / (double)mults_size.height;
mults_size.width = mults_size.width * mult2;
mults_size.height = rect.Height();
}
cv::resize(imgTmp, imgTmp, mults_size);// 缩放
// 转换格式
switch (imgTmp.channels())
{
case 1:
cv::cvtColor(imgTmp, imgTmp, CV_GRAY2BGRA); // GRAY单通道
break;
case 3:
cv::cvtColor(imgTmp, imgTmp, CV_BGR2BGRA); // BGR三通道
break;
default:
break;
}
int pixelBytes = imgTmp.channels()*(imgTmp.depth() + 1); // 计算一个像素多少个字节
// 制作bitmapinfo(数据头)
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;// biWidth' * biHeight' * biBitCount' (上述公式中的biWidth’必须是4的整倍数(不是biWidth,而是biWidth’,表示大于或等于biWidth的,最接近4的整倍数。举个例子,如果biWidth=240,则biWidth’=240;如果biWidth=241,biWidth’=244)。 )
bitInfo.bmiHeader.biXPelsPerMeter = 0;//是现实世界中每米包含的像素数 设为3780即可
bitInfo.bmiHeader.biYPelsPerMeter = 0;
// Mat.data + bitmap数据头 -> MFC
CDC *pDC = GetDlgItem(IDC_STATIC110)->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);
}
边栏推荐
- 网页游戏引擎
- 跳跃表实例
- 智慧物流园区供应链管理系统解决方案:数智化供应链赋能物流运输行业供应链新模式
- World Environment Day | Chow Tai Fook serves wholeheartedly to promote carbon reduction and environmental protection
- 从数数开始
- S2b2b solution for lighting industry: efficiently enable the industrial supply chain and improve the economic benefits of enterprises
- Visual studio 2019 (localdb) mssqllocaldb SQL Server 2014 database version is 852 and cannot be opened. This server supports 782
- 力扣今日题-1200. 最小绝对差
- Congratulations to Mr. Zhang Pengfei, chief data scientist of artefact, for winning the campaign Asia tech MVP 2022
- How to implement a delay queue?
猜你喜欢

PingCode 性能测试之负载测试实践
Redis 的内存淘汰策略和过期删除策略的区别

照明行业S2B2B解决方案:高效赋能产业供应链,提升企业经济效益

Embedded software architecture design - function call

NoSQL之readis配置与优化(终章)

C# 更加优质的操作MongoDB数据库

Detailed process of DC-2 range construction and penetration practice (DC range Series)

Learn more about the basic situation of 2022pmp examination

祝贺Artefact首席数据科学家张鹏飞先生荣获 Campaign Asia Tech MVP 2022

How can programmers improve the speed of code writing?
随机推荐
C # realizes FFT forward and inverse transformation and frequency domain filtering
Median and order statistics
中银证券网上开户安全吗?
高度剩余法
leetcode刷题目录总结
~89 deformation translation
Smart Logistics Park supply chain management system solution: digital intelligent supply chain enables a new supply chain model for the logistics transportation industry
Rebalance operation in spark and its difference from repartition operation
C# 实现 FFT 正反变换 和 频域滤波
[Acwing] 58周赛 4490. 染色
c# 实现定义一套中间SQL可以跨库执行的SQL语句
Object.keys()的用法
Years of training, towards Kata 3.0! Enter the safe container experience out of the box | dragon lizard Technology
中位数与次序统计量
对人胜率84%,DeepMind AI首次在西洋陆军棋中达到人类专家水平
7 RSA密码体制
FIREBIRD使用经验总结
Market trend report, technical innovation and market forecast of China's hair repair therapeutic apparatus
Yanwen logistics plans to be listed on Shenzhen Stock Exchange: it is mainly engaged in international express business, and its gross profit margin is far lower than the industry level
The winning rate against people is 84%, and deepmind AI has reached the level of human experts in army chess for the first time