C#/halcon图像滚轮缩放

 

  • 初始化窗口

1)图片控件为winform中的PictureBox控件时

需要调用halcon算子OpenWindow来初始化窗口,使winform中的图片窗口转换为适用于halcon的图片窗口。

2)图片控件为halcon中的HWindowControl控件时:

无需进行窗口转换,可直接按照如下方式调用。

WindowID = hWindowControl1.HalconWindow。

  • 添加图像缩放功能

打开Form窗体——查看图片控件属性——点击“事件”选项——找到鼠标滚轮滑动的事件,双击创建响应函数——将相应的代码放在刚刚添加的函数中,如下图所示:

  • 添加图像平移功能

按照上述步骤分别找到鼠标“按下”与“抬起”的事件,分别双击创建响应函数,然后将相应的代码放在刚刚添加的函数中。如下图所示:

  • 添加实时显示灰度值功能

按照上述步骤找到鼠标移动的事件,双击创建响应函数——在界面上添加一个“label”控件,然后将相应的代码放在刚刚添加的函数中。

  • 代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using HalconDotNet;

namespace CSharpAndHalcon12
{
    public partial class Form1 : Form
    {
        HTuple WindowID, ImageWidth, ImageHeight;
        private double RowDown;//鼠标按下时的行坐标
        private double ColDown;//鼠标按下时的列坐标
        HObject ho_image;      //图像变量

        public Form1()
        {
            InitializeComponent();
            CreateHalconWindow();
        }

        //创建Halcon窗口
        public void CreateHalconWindow()
        {
           // ///图片控件为winform中的PictureBox控件时/
            //HTuple FatherWindow = this.hWindowControl1.Handle;
            //HOperatorSet.SetWindowAttr("background_color", "green");
            //HOperatorSet.OpenWindow(0, 0, this.hWindowControl1.Width, this.hWindowControl1.Height, FatherWindow, "visible", "", out WindowID);

            /图片控件为halcon中的HWindowControl控件时/
            WindowID = hWindowControl1.HalconWindow;
        }
        //读图
        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            //openFileDialog.Filter = "JPEG文件|*.jpg*|BMP文件|*.bmp*|TIFF文件|*.tiff*";
            openFileDialog.Filter = "所有图像文件 | *.bmp; *.pcx; *.png; *.jpg; *.gif;*.tif; *.ico; *.dxf; *.cgm; *.cdr; *.wmf; *.eps; *.emf";
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                HTuple ImagePath = openFileDialog.FileName;
                HOperatorSet.ReadImage(out ho_image, ImagePath);
            }
            HOperatorSet.GetImageSize(ho_image, out ImageWidth, out ImageHeight);
            HOperatorSet.SetPart(WindowID, 0, 0, ImageHeight, ImageWidth);
            HOperatorSet.DispObj(ho_image, WindowID);
        }
        //缩放图像
        private void hWindowControl1_HMouseWheel(object sender, HMouseEventArgs e)
        {
            HTuple Zoom, Row, Col, Button;
            HTuple Row0, Column0, Row00, Column00, Ht, Wt, r1, c1, r2, c2;
            if (e.Delta > 0)
            {
                Zoom = 1.5;
            }
            else
            {
                Zoom = 0.5;
            }
            HOperatorSet.GetMposition(WindowID, out Row, out Col, out Button);
            HOperatorSet.GetPart(WindowID, out Row0, out Column0, out Row00, out Column00);
            Ht = Row00 - Row0;
            Wt = Column00 - Column0;
            if (Ht * Wt < 32000 * 32000 || Zoom == 1.5)//普通版halcon能处理的图像最大尺寸是32K*32K。如果无限缩小原图像,导致显示的图像超出限制,则会造成程序崩溃
            {
                r1 = (Row0 + ((1 - (1.0 / Zoom)) * (Row - Row0)));
                c1 = (Column0 + ((1 - (1.0 / Zoom)) * (Col - Column0)));
                r2 = r1 + (Ht / Zoom);
                c2 = c1 + (Wt / Zoom);
                HOperatorSet.SetPart(WindowID, r1, c1, r2, c2);
                HOperatorSet.ClearWindow(WindowID);
                HOperatorSet.DispObj(ho_image, WindowID);
            }
        }
        //鼠标按下,记录当前坐标值
        private void hWindowControl1_HMouseDown(object sender, HMouseEventArgs e)
        {
            HTuple Row, Column, Button;
            HOperatorSet.GetMposition(WindowID, out Row, out Column, out Button);
            RowDown = Row;    //鼠标按下时的行坐标
            ColDown = Column; //鼠标按下时的列坐标
        }
        //鼠标抬起,实现图像移动
        private void hWindowControl1_HMouseUp(object sender, HMouseEventArgs e)
        {
            HTuple row1, col1, row2, col2,Row, Column, Button;
            HOperatorSet.GetMposition(WindowID, out Row, out Column, out Button);
            double RowMove = Row - RowDown;   //鼠标弹起时的行坐标减去按下时的行坐标,得到行坐标的移动值
            double ColMove = Column - ColDown;//鼠标弹起时的列坐标减去按下时的列坐标,得到列坐标的移动值
            HOperatorSet.GetPart(WindowID, out row1, out col1, out row2, out col2);//得到当前的窗口坐标
            HOperatorSet.SetPart(WindowID, row1 - RowMove, col1 - ColMove, row2 - RowMove, col2 - ColMove);//这里可能有些不好理解。以左上角原点为参考点
            HOperatorSet.ClearWindow(WindowID);
            if (ImageHeight != null)
            {
                HOperatorSet.DispObj(ho_image, WindowID);
            }
            else
            {
                MessageBox.Show("请加载一张图片");
            }      
        }
        //鼠标移动,实时显示当前坐标与灰度值
        private void hWindowControl1_HMouseMove(object sender, HMouseEventArgs e)
        {
            HTuple Row, Column, Button, pointGray;
            HOperatorSet.GetMposition(WindowID, out Row, out Column, out Button);              //获取当前鼠标的坐标值
            if (ImageHeight != null && (Row > 0 && Row < ImageHeight) && (Column > 0 && Column < ImageWidth))//设置3个条件项,防止程序崩溃。
            {
                HOperatorSet.GetGrayval(ho_image, Row, Column, out pointGray);                 //获取当前点的灰度值
            }
            else
            {
                pointGray = "_";
            }
            String str = String.Format("Row:{0}  Column:{1}  Gray:{2}", Row, Column, pointGray); //格式化字符串
            label1.Text = str;                                                                   //在label控件上显示数值        
        }
        //全屏显示图像,使缩放后的图像回到原始大小
        private void button_FullWindow_Click(object sender, EventArgs e)
        {
            HOperatorSet.SetPart(WindowID, 0, 0, ImageHeight - 1, ImageWidth - 1);
            HOperatorSet.ClearWindow(WindowID);
            HOperatorSet.DispObj(ho_image, WindowID);
        }
    }
}
posted @ 2021-08-23 14:51  奔腾的岩浆  阅读(2504)  评论(0编辑  收藏  举报