当前位置:网站首页>将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);
}
边栏推荐
- "Cannot initialize Photoshop because the temporary storage disk is full" graphic solution
- Position encoding practice in transformer
- Learn more about the basic situation of 2022pmp examination
- MVC模式和三层架构
- Object.keys()的用法
- Sql实现Split
- Task state rollback and data blocking tasks based on check point mechanism
- DC-2靶场搭建及渗透实战详细过程(DC靶场系列)
- Go language loop statement (under Lesson 10)
- MD5加密的两种方式
猜你喜欢
Congratulations to Mr. Zhang Pengfei, chief data scientist of artefact, for winning the campaign Asia tech MVP 2022
~88 running people practice
智慧物流園區供應鏈管理系統解决方案:數智化供應鏈賦能物流運輸行業供應鏈新模式
Solution of dealer collaboration system in building materials industry: empowering enterprises to build core competitiveness
PingCode 性能测试之负载测试实践
照明行业S2B2B解决方案:高效赋能产业供应链,提升企业经济效益
~89 deformation translation
Learn more about the basic situation of 2022pmp examination
VMware Tools和open-vm-tools的安装与使用:解决虚拟机不全屏和无法传输文件的问题
leetcode:421. 数组中两个数的最大异或值
随机推荐
Research Report on market supply and demand and strategy of China's well completion equipment industry
C# 更加优质的操作MongoDB数据库
聊聊异步编程的 7 种实现方式
金额计算用 BigDecimal 就万无一失了?看看这五个坑吧~~
"Cannot initialize Photoshop because the temporary storage disk is full" graphic solution
Congratulations to Mr. Zhang Pengfei, chief data scientist of artefact, for winning the campaign Asia tech MVP 2022
如何实现一个延时队列 ?
表单传递时,如何隐式将值传过去
安信证券属于什么档次 开户安全吗
新的职业已经出现,怎么能够停滞不前 ,人社部公布建筑新职业
Is it safe to open an account online
The Ministry of human resources and Social Security announced the new construction occupation
Hair growth shampoo industry Research Report - market status analysis and development prospect forecast
PyTorch深度学习快速入门教程
《吐血整理》保姆级系列教程-玩转Fiddler抓包教程(2)-初识Fiddler让你理性认识一下
China's roof ladder market trend report, technological innovation and market forecast
对人胜率84%,DeepMind AI首次在西洋陆军棋中达到人类专家水平
Understand asp Net core - Authentication Based on jwtbearer
【模板】【luogu P4630】Duathlon 铁人两项(圆方树)
Redis 的内存淘汰策略和过期删除策略的区别