当前位置:网站首页>将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);
}
边栏推荐
- 中信证券网上开户安全吗 开户收费吗
- [acwing] 58 weeks 4490 dyeing
- Height residual method
- MVC模式和三层架构
- Pytorch deep learning quick start tutorial
- leetcode刷题目录总结
- go-micro教程 — 第二章 go-micro v3 使用Gin、Etcd
- The Ministry of human resources and Social Security announced the new construction occupation
- Kunming Third Ring Road Closure project will pass through these places. Is there one near your home?
- How can programmers improve the speed of code writing?
猜你喜欢

Overflow: the combination of auto and Felx

Learn more about the basic situation of 2022pmp examination

第十八届IET交直流输电国际会议(ACDC2022)于线上成功举办

科普达人丨一文看懂阿里云的秘密武器“神龙架构”

Transformer中position encoding实践

Understand ThreadLocal in one picture

多年锤炼,迈向Kata 3.0 !走进开箱即用的安全容器体验之旅| 龙蜥技术

Solution du système de gestion de la chaîne d'approvisionnement du parc logistique intelligent

《吐血整理》保姆级系列教程-玩转Fiddler抓包教程(2)-初识Fiddler让你理性认识一下

一图看懂ThreadLocal
随机推荐
go-micro教程 — 第二章 go-micro v3 使用Gin、Etcd
Understand Alibaba cloud's secret weapon "dragon architecture" in the article "science popularization talent"
Inside and outside: flow chart drawing elementary: six common mistakes
嵌入式软件架构设计-函数调用
Congratulations to Mr. Zhang Pengfei, chief data scientist of artefact, for winning the campaign Asia tech MVP 2022
China tall oil fatty acid market trend report, technical dynamic innovation and market forecast
Is it safe to open an account online
聊聊异步编程的 7 种实现方式
Object. Usage of keys()
建筑建材行业经销商协同系统解决方案:赋能企业构建核心竞争力
Overflow: the combination of auto and Felx
ECCV 2022放榜了:1629篇论文中选,录用率不到20%
How to implement a delay queue?
How to "use" Perl modules in directories that are not in @inc- How do I 'use' a Perl module in a directory not in @INC?
智慧物流园区供应链管理系统解决方案:数智化供应链赋能物流运输行业供应链新模式
overflow:auto与felx结合的用法
中信证券网上开户安全吗 开户收费吗
Solution of dealer collaboration system in building materials industry: empowering enterprises to build core competitiveness
C implementation defines a set of intermediate SQL statements that can be executed across libraries
Market trend report, technical innovation and market forecast of taillight components in China