2:QT联合HALCON编程—图像显示放大缩小

1.声明事件

复制代码
#include <HalconCpp.h>
using namespace HalconCpp;


#include <QCloseEvent>//滚轮事件

2.在.h文件中声明和定义公共全局变量,以及图像缩放的函数

复制代码
            void wheelEvent(QWheelEvent *event);//定义函数
            HTuple wcRow0, wcRow1, wcCol0, wcCol1,m_objImage;
            bool GunLunFlag = false;
            HObject  ho_Image;

3.在.cpp文件中,构造函数中

复制代码
   GunLunFlag = true; //变量变为1

4.在.cpp的函数中开始定义

使用此函数时,需要替换窗口名和图像名。(如果在别处也需要使用这两个变量,所以才把这些变量声明到全局变量中去)

HTuple hv_WindowHandle; //公共窗口 大家都会去使用 全局作用域(在联合编程1节里面有提到)

复制代码
//滚轮事件函数
void MainWindow::wheelEvent(QWheelEvent *event)
{
    if(ui->gvDepthMap->geometry().contains(this->mapFromGlobal(QCursor::pos())))
    {
        //resizeImage();
        SetWindowExtents(hv_hv_WindowHandle, 0, 0,  ui->gvDepthMap->width(), ui->gvDepthMap->height());
        try
        {
            if (GunLunFlag == true)
        {
            QPoint pos = event->pos();
            HTuple Row0_1, Col0_1, Row1_1, Col1_1;
            HTuple ImagePtX, ImagePtY;
            QRect WndRect;
            double Scale = 0.1;
            HTuple Width, Height;
            GetImageSize(ho_Image, &Width, &Height);
            WndRect = QRect(QPoint(0, 0), QPoint(ui->gvDepthMap->width(), ui->gvDepthMap->height()));

            if (WndRect.contains(event->pos()))
            {
                if (event->delta() > 0)
                {
                    ImagePtX = wcCol0 + (pos.x() - WndRect.left()) / (WndRect.width() - 1.0) * (wcCol1 - wcCol0);
                    ImagePtY = wcRow0 + (pos.y() - WndRect.top()) / (WndRect.height() - 1.0) * (wcRow1 - wcRow0);
                    Row0_1 = ImagePtY - 1 / (1 - Scale) * (ImagePtY - wcRow0);
                    Row1_1 = ImagePtY - 1 / (1 - Scale) * (ImagePtY - wcRow1);
                    Col0_1 = ImagePtX - 1 / (1 - Scale) * (ImagePtX - wcCol0);
                    Col1_1 = ImagePtX - 1 / (1 - Scale) * (ImagePtX - wcCol1);
                    wcRow0 = Row0_1;
                    wcCol0 = Col0_1;
                    wcRow1 = Row1_1;
                    wcCol1 = Col1_1;
                    if (wcRow0 < 0 || wcCol0 < 0)
                    {
                        wcRow0 = 0;
                        wcCol0 = 0;
                        wcRow1 = Height;
                        wcCol1 = Width;
                    }
                    ClearWindow(hv_hv_WindowHandle);
                    SetPart(hv_hv_WindowHandle, wcRow0, wcCol0, wcRow1, wcCol1);
                    DispObj(ho_Image, hv_hv_WindowHandle);
                }
                else
                {
                    ImagePtX = wcCol0 + (pos.x() - WndRect.left()) / (WndRect.width() - 1.0) * (wcCol1 - wcCol0);
                    ImagePtY = wcRow0 + (pos.y() - WndRect.top()) / (WndRect.height() - 1.0) * (wcRow1 - wcRow0);
                    Row0_1 = ImagePtY - 1 / (1 + Scale) * (ImagePtY - wcRow0);
                    Row1_1 = ImagePtY - 1 / (1 + Scale) * (ImagePtY - wcRow1);
                    Col0_1 = ImagePtX - 1 / (1 + Scale) * (ImagePtX - wcCol0);
                    Col1_1 = ImagePtX - 1 / (1 + Scale) * (ImagePtX - wcCol1);
                    wcRow0 = Row0_1;
                    wcCol0 = Col0_1;
                    wcRow1 = Row1_1;
                    wcCol1 = Col1_1;
                }
                ClearWindow(hv_hv_WindowHandle);
                SetPart(hv_hv_WindowHandle, wcRow0, wcCol0, wcRow1, wcCol1);
                DispObj(ho_Image, hv_hv_WindowHandle);
            }
        }
        }
        catch(...)
        {
          
          // ui->(可以添加界面框显示内容)
           return;
        }
    }
}

已经看到这里了,不妨点个赞和关注吧!

刚开始写文章,如有不足请多多包含;之后会持续更新关于(halcon学习,VS联合编程,QT联合编程,C++,C#,Opencv图像处理库,三维点云库pcl,相机以及机器人的二次开发)等系统化学习文章。

相关推荐
编程零零七7 分钟前
Python巩固训练——第一天练习题
开发语言·python·python基础·python学习·python练习题
不見星空8 分钟前
leetcode 每日一题 1865. 找出和为指定值的下标对
算法·leetcode
我爱Jack19 分钟前
时间与空间复杂度详解:算法效率的度量衡
java·开发语言·算法
米饭「」20 分钟前
C++AVL树
java·开发语言·c++
心愿许得无限大1 小时前
Qt 常用界面组件
开发语言·c++·qt
2401_858286111 小时前
OS15.【Linux】gdb调试器的简单使用
linux·运维·服务器·开发语言·gdb
牛马baby1 小时前
MATLAB下载安装教程(附安装包)2025最新版(MATLAB R2024b)
开发语言·matlab
GiraKoo1 小时前
【GiraKoo】C++17的新特性
c++
Rockson1 小时前
C++如何查询实时贵金属行情
c++·api
shenyan~1 小时前
关于 c、c#、c++ 三者区别
开发语言·c++