当前位置:网站首页>将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);
}
边栏推荐
- 建筑建材行业经销商协同系统解决方案:赋能企业构建核心竞争力
- 祝贺Artefact首席数据科学家张鹏飞先生荣获 Campaign Asia Tech MVP 2022
- 对人胜率84%,DeepMind AI首次在西洋陆军棋中达到人类专家水平
- 2022年国内云管平台厂商哪家好?为什么?
- MVC模式和三层架构
- 聊聊异步编程的 7 种实现方式
- Solution of commercial supply chain coordination system in the mineral industry: build a digital intelligent supply chain platform to ensure the safe supply of mineral resources
- Capvision Rongying's prospectus in Hong Kong was "invalid": it was strictly questioned by the CSRC and required supplementary disclosure
- Why do you say that the maximum single table of MySQL database is 20million? Based on what?
- System. Currenttimemillis() and system Nanotime (), which is faster? Don't use it wrong!
猜你喜欢
Visual Studio 2019 (LocalDB)MSSQLLocalDB SQL Server 2014 数据库版本为852无法打开,此服务器支持782
Understand Alibaba cloud's secret weapon "dragon architecture" in the article "science popularization talent"
科普达人丨一文看懂阿里云的秘密武器“神龙架构”
聊聊异步编程的 7 种实现方式
overflow:auto与felx结合的用法
一图看懂ThreadLocal
Principle and general steps of SQL injection
嵌入式软件架构设计-函数调用
被PMP考试“折磨”出来的考试心得,值得你一览
Kunming Third Ring Road Closure project will pass through these places. Is there one near your home?
随机推荐
Smart Logistics Park supply chain management system solution: digital intelligent supply chain enables a new supply chain model for the logistics transportation industry
leetcode:421. 数组中两个数的最大异或值
Research Report on market supply and demand and strategy of China's four sided flat bag industry
Readis configuration and optimization of NoSQL (final chapter)
MD5加密的两种方式
Go micro tutorial - Chapter 2 go micro V3 using gin and etcd
leetcode刷题目录总结
The test experience "tortured" by the PMP test is worth your review
周大福践行「百周年承诺」,真诚服务推动绿色环保
Overflow: the combination of auto and Felx
力扣今日题-1200. 最小绝对差
Lv166 turned over
APOC custom functions and procedures
[Acwing] 58周赛 4489. 最长子序列
如何实现一个延时队列 ?
金额计算用 BigDecimal 就万无一失了?看看这五个坑吧~~
Go development: how to use go singleton mode to ensure the security of high concurrency of streaming media?
Start by counting
The winning rate against people is 84%, and deepmind AI has reached the level of human experts in army chess for the first time
高度剩余法