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

发布于:2025-05-01 ⋅ 阅读:(19) ⋅ 点赞:(0)

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,相机以及机器人的二次开发)等系统化学习文章。


网站公告

今日签到

点亮在社区的每一天
去签到