C#联合halcon开发实现窗口图像放大缩小移动功能

C# halcon窗口实现图像放大缩小移动功能

  • 1、使用halcon的控件HWindowControl,并为控件添加四个鼠标事件,HMouseDown、HMouseMove、HMouseUp、HMouseWheel。
    • 1.1 创建一个CS文件,命名为 ImageOperation ,并定义两个HTuple变量,变量名为RowDown,ColDown。
    • 1.2 实现MouseDown事件的封装:
    • 1.3 实现MouseMove事件的封装
    • 1.4 实现MouseUp事件的封装
    • 1.5 实现MouseWheel事件的封装
    • 1.6 HWindowControl控件鼠标事件的实现:
  • 2、使用halcon的HSmartWindowControl控件,该控件自带移动图片功能,只需要实现鼠标放大缩小功能即可。

1、使用halcon的控件HWindowControl,并为控件添加四个鼠标事件,HMouseDown、HMouseMove、HMouseUp、HMouseWheel。

对事件进行封装,方便后续多次调用。

1.1 创建一个CS文件,命名为 ImageOperation ,并定义两个HTuple变量,变量名为RowDown,ColDown。

/// 
/// 鼠标嗯下时的横纵坐标
/// 
HTuple RowDown, ColDown;

1.2 实现MouseDown事件的封装:

/// 
/// 图像放大缩小
/// 
/// 窗口句柄
/// 操作类型
/// 图像
public void MouseWheel(object sender, HMouseEventArgs e, HObject image)
{
   if (image.CountObj() > 0 || image != null)//必须有图的情况下使用
   {
       try
       {
           //窗口
           HWindowControl hWindowControl = sender as HWindowControl;
           HObject CurrImage = null;
           HOperatorSet.GenEmptyObj(out CurrImage);
           CurrImage = image.Clone();
           //放大倍数,当前鼠标选择的图像点坐标Row, Col,按下鼠标的左键还是右键:0-没按下,1-左键,2-中键,4-右键
           HTuple Zoom, Row, Col, Button;
           HTuple RowLeftUpper, ColumnLeftUpper, RowRightLower, ColumnRightLower, Ht, Wt, ImagePartRowLeftUp, ImagePartColLeftUp, ImagePartRowRightLow, ImagePartColRightLow;
           //鼠标向上滚动表示放大
           if (e.Delta > 0)
           {
               Zoom = 1.5;
           }
           //向下滚动缩小
           else
           {
               Zoom = 0.5;
           }
           //返回输出窗口中鼠标指针和鼠标按钮所按下的像素精确图像坐标,输出当前鼠标指针点的图像坐标以及按下的是鼠标左键还是右键,0是鼠标左键
           HOperatorSet.GetMposition(hWindowControl.HalconWindow, out Row, out Col, out Button);
           //Get part返回窗口中显示的图像部分的左上角和右下角

           //得到当前的窗口坐标,Row0:图像部分左上角的行索引,Column0:图像部分左上角的列索引,Row00:图像部分右下角的行索引,Column00:图像部分右下角的列索引
           HOperatorSet.GetPart(hWindowControl.HalconWindow, out RowLeftUpper, out ColumnLeftUpper, out RowRightLower, out ColumnRightLower);
           //显示的部分图像的高
           Ht = RowRightLower - RowLeftUpper;
           //显示的部分图像的宽
           Wt = ColumnRightLower - ColumnLeftUpper;
           //普通版halcon能处理的图像最大尺寸是32K*32K。如果无限缩小原图像,导致显示的图像超出限制,则会造成程序崩溃
           if (Ht * Wt < 32000 * 32000 || Zoom == 1.5)
           {
               //显示的放大或者缩小部分图像的左上角和右下角坐标
               ImagePartRowLeftUp = (RowLeftUpper + ((1 - (1.0 / Zoom)) * (Row - RowLeftUpper)));
               ImagePartColLeftUp = (ColumnLeftUpper + ((1 - (1.0 / Zoom)) * (Col - ColumnLeftUpper)));
               ImagePartRowRightLow = ImagePartRowLeftUp + (Ht / Zoom);
               ImagePartColRightLow = ImagePartColLeftUp + (Wt / Zoom);
               //设置部分显示图像
               HOperatorSet.SetPart(hWindowControl.HalconWindow, ImagePartRowLeftUp, ImagePartColLeftUp, ImagePartRowRightLow, ImagePartColRightLow);
               HOperatorSet.ClearWindow(hWindowControl.HalconWindow);
               if (CurrImage != null)
               {
                   HOperatorSet.DispObj(CurrImage, hWindowControl.HalconWindow);
               }
           }
       }
       catch (Exception)
       {
       }
   }
}

1.3 实现MouseMove事件的封装

 /// 
 /// 鼠标摁下
 /// 
 /// 窗口句柄
 /// 操作类型
 /// 原始图像
 /// 图像的宽
 /// 图像的高
 /// 是否摁下了鼠标左键
 public void MouseDown(object sender, HMouseEventArgs e, HObject image, HTuple hv_imageWidth, HTuple hv_imageHeight, ref bool isMouseDown)
 {
     if (image.CountObj() > 0 || image != null)//必须有图的情况下使用
     {
         try
         {
             HWindowControl hWindowControl = sender as HWindowControl;
             HTuple Row, Column, Button;
             //返回鼠标当前按下点的图像坐标
             HOperatorSet.GetMposition(hWindowControl.HalconWindow, out Row, out Column, out Button);
             RowDown = Row;    //鼠标按下时的行坐标
             ColDown = Column; //鼠标按下时的列坐标
             if (Button.I == 1)//左键嗯下值为1
                 isMouseDown = true;
             if (Button.I == 4)//鼠标右键恢复原图
             {
                 HOperatorSet.SetPart(hWindowControl.HalconWindow, 0, 0, hv_imageHeight, hv_imageWidth);
                 HOperatorSet.DispObj(image, hWindowControl.HalconWindow);
             }
         }
         catch (Exception)
         {
         }
     }
 }

1.4 实现MouseUp事件的封装

/// 
/// 鼠标移动
/// 
/// 窗口句柄
/// 操作类型
/// 原始图像
/// 是否摁下的鼠标左键
/// 返回鼠标当前位置的灰度值
public void MouseMove(object sender, HMouseEventArgs e, HObject image, bool isMouseDown, out string GrayMsg)
{
    GrayMsg = "";
    if (image.CountObj() > 0 || image != null)//必须有图的情况下使用
    {
        try
        {
            HWindowControl hWindowControl = sender as HWindowControl;
            HObject CurrImage = null;
            HOperatorSet.GenEmptyObj(out CurrImage);
            CurrImage = image.Clone();
            HTuple Row = new HTuple(), Column = new HTuple(), Button = new HTuple(), pointGray = new HTuple();
            HTuple hv_Width = new HTuple();
            HTuple hv_Height = new HTuple();
            if (CurrImage != null)
            {
                HOperatorSet.GetImageSize(CurrImage, out hv_Width, out hv_Height);
            }
            //获取当前鼠标的坐标值
            HOperatorSet.GetMposition(hWindowControl.HalconWindow, out Row, out Column, out Button);
            获取当前鼠标的坐标值
            if (hv_Height != null && (Row > 0 && Row < hv_Height) && (Column > 0 && Column < hv_Width))//设置3个条件项,防止程序崩溃。
                HOperatorSet.GetGrayval(CurrImage, Row, Column, out pointGray);//获取当前点的灰度值
            else
                pointGray = "_";
            GrayMsg = string.Format("Row:{0}  Col:{1}  Gray:{2}", Row, Column, pointGray);

            if (isMouseDown)
            {
                HTuple row1, col1, row2, col2;
                double RowMove = Row - RowDown;   //鼠标弹起时的行坐标减去按下时的行坐标,得到行坐标的移动值
                double ColMove = Column - ColDown;//鼠标弹起时的列坐标减去按下时的列坐标,得到列坐标的移动值
                //得到当前的窗口坐标
                HOperatorSet.GetPart(hWindowControl.HalconWindow, out row1, out col1, out row2, out col2);
                //移动后的左上角和右下角坐标,这里可能有些不好理解。以左上角原点为参考点
                HOperatorSet.SetPart(hWindowControl.HalconWindow, row1 - RowMove, col1 - ColMove, row2 - RowMove, col2 - ColMove);
                HOperatorSet.ClearWindow(hWindowControl.HalconWindow);
                if (CurrImage != null)
                    HOperatorSet.DispObj(CurrImage, hWindowControl.HalconWindow);
            }
        }
        catch (Exception)
        {
        }
    }
}

1.5 实现MouseWheel事件的封装

/// 
/// 鼠标松开
/// 
/// 窗口句柄
/// 操作类型
/// 原始图像
/// 返回是否松开了鼠标左键
public void MouseUp(object sender, HMouseEventArgs e, HObject image, ref bool isMouseDown)
{
    if (image.CountObj() > 0 || image != null)//必须有图的情况下使用
    {
        try
        {
            if (isMouseDown)
            {
                isMouseDown = false;
                HWindowControl hWindowControl = sender as HWindowControl;
                HObject CurrImage = null;
                HOperatorSet.GenEmptyObj(out CurrImage);
                CurrImage = image.Clone();
                HTuple row1, col1, row2, col2, Row, Column, Button;
                获取当前鼠标的坐标值
                HOperatorSet.GetMposition(hWindowControl.HalconWindow, out Row, out Column, out Button);
                double RowMove = Row - RowDown;   //鼠标弹起时的行坐标减去按下时的行坐标,得到行坐标的移动值
                double ColMove = Column - ColDown;//鼠标弹起时的列坐标减去按下时的列坐标,得到列坐标的移动值
                //得到当前的窗口坐标
                HOperatorSet.GetPart(hWindowControl.HalconWindow, out row1, out col1, out row2, out col2);
                //移动后的左上角和右下角坐标,这里可能有些不好理解。以左上角原点为参考点
                HOperatorSet.SetPart(hWindowControl.HalconWindow, row1 - RowMove, col1 - ColMove, row2 - RowMove, col2 - ColMove);
                HOperatorSet.ClearWindow(hWindowControl.HalconWindow);
                if (CurrImage != null)
                    HOperatorSet.DispObj(CurrImage, hWindowControl.HalconWindow);
            }
        }
        catch (Exception)
        {
        }
    }
}

1.6 HWindowControl控件鼠标事件的实现:

ImageOperation imageOperation = new ImageOperation();

/// 
/// 实现鼠标放大缩小图片
/// 
/// 
/// 
private void hWindowControl1_HMouseWheel(object sender, HMouseEventArgs e)
{
    imageOperation.MouseWheel(sender, e, CurrImage);
}

/// 
/// 鼠标是否摁下
/// 
bool isMouseDown = false;

/// 
/// 鼠标摁下准备拖动图片
/// 
/// 
/// 
private void hWindowControl1_HMouseDown(object sender, HMouseEventArgs e)
{

    if (!isDrawRegion)
        imageOperation.MouseDown(sender, e, CurrImage, hv_imageWidth, hv_imageHeight, ref isMouseDown);
}

/// 
/// 鼠标移动,开始拖动图片
/// 
/// 
/// 
private void hWindowControl1_HMouseMove(object sender, HMouseEventArgs e)
{
    if (!isDrawRegion)
    {
        string gary = "";
        imageOperation.MouseMove(sender, e, CurrImage, isMouseDown, out gary);
        label1.Text = gary;//显示灰度值
    }
}

/// 
/// 鼠标松开,完成图片拖动
/// 
/// 
/// 
private void hWindowControl1_HMouseUp(object sender, HMouseEventArgs e)
{
    if (!isDrawRegion)
        imageOperation.MouseUp(sender, e, CurrImage, ref isMouseDown);
}

2、使用halcon的HSmartWindowControl控件,该控件自带移动图片功能,只需要实现鼠标放大缩小功能即可。

窗体实现MouseWheel事件即可。

private void Form1_Load(object sender, EventArgs e)
{
    this.MouseWheel += new System.Windows.Forms.MouseEventHandler(CustomMouseWheel);
}

/// 
/// 放大缩小图像
/// 
/// 
/// 
private void CustomMouseWheel(object sender, MouseEventArgs e)
{
    System.Drawing.Point pt = this.Location;
    int leftBorder = hSmartWindowControl.Location.X;
    int rightBorder = hSmartWindowControl.Location.X + hSmartWindowControl.Size.Width;
    int topBorder = hSmartWindowControl.Location.Y;
    int bottomBorder = hSmartWindowControl.Location.Y + hSmartWindowControl.Size.Height;
    //判断鼠标指针是否在控件内部
    if (e.X > leftBorder && e.X < rightBorder && e.Y > topBorder && e.Y < bottomBorder)
    {
        MouseEventArgs newe = new MouseEventArgs(e.Button, e.Clicks, e.X - pt.X, e.Y - pt.Y, e.Delta);
        hSmartWindowControl.HSmartWindowControl_MouseWheel(sender, newe);
    }
}

你可能感兴趣的:(计算机视觉,人工智能,c#)