当前位置:网站首页>将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);
}
边栏推荐
- How to contribute to the source code of ongdb core project
- 7 RSA密码体制
- System. Currenttimemillis() and system Nanotime (), which is faster? Don't use it wrong!
- Can you really use MySQL explain?
- Market trend report, technical innovation and market forecast of tetrabromophthalate (pht4 diol) in China
- 基于wifi控制的51单片机温度报警器
- 新的职业已经出现,怎么能够停滞不前 ,人社部公布建筑新职业
- How to implement a delay queue?
- Understand Alibaba cloud's secret weapon "dragon architecture" in the article "science popularization talent"
- How can programmers improve the speed of code writing?
猜你喜欢
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
2022年国内云管平台厂商哪家好?为什么?
Principle and general steps of SQL injection
leetcode:421. 数组中两个数的最大异或值
~89 deformation translation
VMware Tools和open-vm-tools的安装与使用:解决虚拟机不全屏和无法传输文件的问题
《吐血整理》保姆级系列教程-玩转Fiddler抓包教程(2)-初识Fiddler让你理性认识一下
电子元器件B2B商城系统开发:赋能企业构建进销存标准化流程实例
Kunming Third Ring Road Closure project will pass through these places. Is there one near your home?
Analysis of abnormal frequency of minor GC in container environment
随机推荐
C implementation defines a set of intermediate SQL statements that can be executed across libraries
Spark 中的 Rebalance 操作以及与Repartition操作的区别
智慧物流园区供应链管理系统解决方案:数智化供应链赋能物流运输行业供应链新模式
China tall oil fatty acid market trend report, technical dynamic innovation and market forecast
中位数与次序统计量
APOC custom functions and procedures
网页游戏引擎
最大子数组与矩阵乘法
World Environment Day | Chow Tai Fook serves wholeheartedly to promote carbon reduction and environmental protection
China's roof ladder market trend report, technological innovation and market forecast
Redis 的内存淘汰策略和过期删除策略的区别
Median and order statistics
Market trend report, technical innovation and market forecast of tetrabromophthalate (pht4 diol) in China
Is it safe for Anxin securities to open an account online? Is the account opening fee charged
C # realizes FFT forward and inverse transformation and frequency domain filtering
[Acwing] 58周赛 4489. 最长子序列
嵌入式软件架构设计-函数调用
太方便了,钉钉上就可完成代码发布审批啦!
基于wifi控制的51单片机温度报警器
Inside and outside: flow chart drawing elementary: six common mistakes