VisionPro 判断圆是不是无限接近圆或存在缺陷

项目上可能需要判断圆是否是无限接近圆或者判断圆边缘是否存在缺陷等。
第一种方法:找圆工具和点到点的距离计算圆边缘上的点到圆心距离的最大值和最小值的差值。
在这里插入图片描述

#region namespace imports
using System;
using System.Collections;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Cognex.VisionPro;
using Cognex.VisionPro.ToolBlock;
using Cognex.VisionPro3D;
using Cognex.VisionPro.Caliper;
using Cognex.VisionPro.Dimensioning;
#endregion

public class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase
{
  #region Private Member Variables
  private Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock;
  private CogGraphicCollection gc;
  #endregion

  /// <summary>
  /// Called when the parent tool is run.
  /// Add code here to customize or replace the normal run behavior.
  /// </summary>
  /// <param name="message">Sets the Message in the tool's RunStatus.</param>
  /// <param name="result">Sets the Result in the tool's RunStatus</param>
  /// <returns>True if the tool should run normally,
  ///          False if GroupRun customizes run behavior</returns>
  public override bool GroupRun(ref string message, ref CogToolResultConstants result)
  {
    // To let the execution stop in this script when a debugger is attached, uncomment the following lines.
    #if DEBUG
    if (System.Diagnostics.Debugger.IsAttached) System.Diagnostics.Debugger.Break();
    #endif


    // Run each tool using the RunTool function
    foreach(ICogTool tool in mToolBlock.Tools)
      mToolBlock.RunTool(tool, ref message, ref result);
    
    if(gc == null)
      gc = new CogGraphicCollection();
    gc.Clear();
    
    double Min_Distance = 0.0;
    double Max_Distance = 0.0;
    double Minus_Value = 0;
    double Point_CenterD = 0;
    
    //string[] Price_Distance = {};
    
    CogGraphicLabel label6 = new CogGraphicLabel();
    
    CogFindCircleTool FindCircle = mToolBlock.Tools["CogFindCircleTool1"] as CogFindCircleTool;
    CogDistancePointPointTool PointP_Distance = mToolBlock.Tools["CogDistancePointPointTool1"]as CogDistancePointPointTool;
    
    FindCircle.Run();
    PointP_Distance.Run();
    
    double[] SaveDistance = new double[FindCircle.Results.NumPointsFound];
    
    //if(FindCircle.Results.Count > 0)
    {
      System.Windows.Forms.MessageBox.Show("FindCircle.Results.NumPointsFound" + FindCircle.Results.Count.ToString());
      try
      {
        for(int i = 0;i < FindCircle.Results.Count;i++)
        {
          System.Windows.Forms.MessageBox.Show(i.ToString());
          //System.Windows.Forms.MessageBox.Show("FindCircle.Results[i].Used :" + FindCircle.Results[i].Used);
          if(FindCircle.Results[i].Used)//没有找到的点过滤点,不是过滤的红色点
          {
            PointP_Distance.StartX = FindCircle.Results[i].X;
            PointP_Distance.StartY = FindCircle.Results[i].Y;
            PointP_Distance.EndX = FindCircle.Results.GetCircle().CenterX;
            PointP_Distance.EndY = FindCircle.Results.GetCircle().CenterY;
            Point_CenterD = PointP_Distance.Distance;  
            
            //将每个边缘点到圆心的距离存double数组中
            SaveDistance[i] = Point_CenterD;
          }          
        }
        System.Windows.Forms.MessageBox.Show("存储点到圆距离个数" + SaveDistance.Length);
        ArrayList List_SaveDistance = new ArrayList(SaveDistance);
        List_SaveDistance.Sort();
        Min_Distance = Convert.ToDouble(List_SaveDistance[0]);
        Max_Distance = Convert.ToDouble(List_SaveDistance[List_SaveDistance.Count - 1]);
          
        System.Windows.Forms.MessageBox.Show("最大值" + Max_Distance.ToString());
        System.Windows.Forms.MessageBox.Show("最小值" + Min_Distance.ToString());
        Minus_Value = Max_Distance - Min_Distance;
        System.Windows.Forms.MessageBox.Show("真圆度" + Minus_Value.ToString());
      } 
      catch(Exception e)
      {
        MessageBox.Show(e.ToString());
      }
      
      
      System.Windows.Forms.MessageBox.Show("点到圆距离" + Point_CenterD.ToString());
      label6.SetXYText(100, 400, "圆度:" + Minus_Value.ToString("f3"));
      label6.SelectedSpaceName = "#";
      label6.Font = new Font("宋体", 12);
      label6.Color = Cognex.VisionPro.CogColorConstants.Green;
        
      gc.Add(label6);
    }
    mToolBlock.Outputs["Actual_Value"].Value = Minus_Value;

    return false;
  }

  #region When the Current Run Record is Created
  /// <summary>
  /// Called when the current record may have changed and is being reconstructed
  /// </summary>
  /// <param name="currentRecord">
  /// The new currentRecord is available to be initialized or customized.</param>
  public override void ModifyCurrentRunRecord(Cognex.VisionPro.ICogRecord currentRecord)
  {
  }
  #endregion

  #region When the Last Run Record is Created
  /// <summary>
  /// Called when the last run record may have changed and is being reconstructed
  /// </summary>
  /// <param name="lastRecord">
  /// The new last run record is available to be initialized or customized.</param>
  public override void ModifyLastRunRecord(Cognex.VisionPro.ICogRecord lastRecord)
  {
  }
  #endregion

  #region When the Script is Initialized
  /// <summary>
  /// Perform any initialization required by your script here
  /// </summary>
  /// <param name="host">The host tool</param>
  public override void Initialize(Cognex.VisionPro.ToolGroup.CogToolGroup host)
  {
    // DO NOT REMOVE - Call the base class implementation first - DO NOT REMOVE
    base.Initialize(host);


    // Store a local copy of the script host
    this.mToolBlock = ((Cognex.VisionPro.ToolBlock.CogToolBlock) (host));
  }
  #endregion

}


第二种方法:通过灰度工具:CogHistogramTool1 Blob工具:CogBlobTool1
判断面积大小进行是否存储缺陷
在这里插入图片描述
在这里插入图片描述

#region 真圆度检测
    Histogram.InputImage = Fix.OutputImage;
    Histogram.Run();
    Minus_Blob.InputImage = Fix.OutputImage;
    Minus_Blob.Run();
    
    double Blob_Area = 0;
    Blob_Area = Convert.ToDouble(mToolBlock.Inputs["Input_MinusArea"].Value.ToString());
    
    if(Minus_Blob.Results.GetBlobs().Count > 0)
    {
      if(Minus_Blob.Results.GetBlobs()[0].Area > Blob_Area)
      {
        label6.SetXYText(100, 400, "圆度检测:" + Minus_Blob.Results.GetBlobs()[0].Area.ToString());
        label6.Color = CogColorConstants.Green;
        gc.Add(label6);
        gc.Add(Minus_Blob.Results.GetBlobs()[0].CreateResultGraphics(CogBlobResultGraphicConstants.All));
        mToolBlock.Outputs["MinusArea_Value"].Value = Minus_Blob.Results.GetBlobs()[0].Area.ToString();
      }
      else
      {
        mToolBlock.Outputs["Result"].Value = 1;
        label6.SetXYText(100, 400, "圆度检测NG");
        label6.Color = CogColorConstants.Red;
        mToolBlock.Outputs["MinusArea_Value"].Value = Minus_Blob.Results.GetBlobs()[0].Area.ToString();
        gc.Add(label6);
      }
    }
    #endregion
    

第三方法:通过圆上工具的属性获取点到圆拟合边的距离,获取最大值,大于最大值判定为NG;
在这里插入图片描述

#region 通过提取点到圆边缘距离进行判断
    double[] SaveDistance = new double[FindCircle.Results.Count];
    
    if (FindCircle.Results.Count > 0)
    {
      try
      {
        for (int i = 0; i < FindCircle.Results.Count; i++)
        {
          //System.Windows.Forms.MessageBox.Show(i.ToString());
          //System.Windows.Forms.MessageBox.Show("FindCircle.Results[i].Used :" + FindCircle.Results[i].Used);
          if (FindCircle.Results[i].Used)//没有找到的点过滤点,不是过滤的红色点
          {
            //将每个边缘点到圆心的距离存double数组中
            SaveDistance[i] = FindCircle.Results[i].DistanceToCircle;
          }
        }
        //System.Windows.Forms.MessageBox.Show("存储点到圆距离个数" + SaveDistance.Length);
        ArrayList List_SaveDistance = new ArrayList(SaveDistance);
        List_SaveDistance.Sort();

        //System.Windows.Forms.MessageBox.Show("List_SaveDistance:" + List_SaveDistance.ToString());
        
        int k = 0;
        //for(int k = 0;k < List_SaveDistance.Count;k++)
        if (k < List_SaveDistance.Count)
        {
          do
          {
            Min_Distance = Convert.ToDouble(List_SaveDistance[k]);
            k++;
          } while (Convert.ToDouble(List_SaveDistance[k].ToString()) == 0);
        }
        
        Min_Distance = Convert.ToDouble(List_SaveDistance[k]);
        Max_Distance = Convert.ToDouble(List_SaveDistance[List_SaveDistance.Count - 1]);
        
        System.Windows.Forms.MessageBox.Show("最小值" + Min_Distance.ToString());
        System.Windows.Forms.MessageBox.Show("k" + k);
       

        System.Windows.Forms.MessageBox.Show("最大值" + Max_Distance.ToString());
        
        ArrayList List_MaxStore = new ArrayList();
        
        for(int w = 0;w < List_SaveDistance.Count;w++)
        {
          if(Convert.ToDouble(List_SaveDistance[w].ToString()) > Convert.ToDouble(mToolBlock.Inputs["Input_Distance"].Value.ToString()))
          {
            List_MaxStore.Add(List_SaveDistance[w]);
          }
        }
      
        
        
        System.Windows.Forms.MessageBox.Show("List_MaxStore长度:" + List_MaxStore.Count);

        if (List_MaxStore.Count > 3)//表明大于点到圆上的距离最大值有三个数视为NG
        {
          label6.SetXYText(100, 400, "圆度检测:OK");
          label6.Color = CogColorConstants.Green;
          gc.Add(label6);
          mToolBlock.Outputs["Out_DistanceMax"].Value = Minus_Value.ToString();
        }
        else
        {
          label6.SetXYText(100, 400, "圆度检测:NG");
          label6.Color = CogColorConstants.Red;
          gc.Add(label6);
          mToolBlock.Outputs["Out_DistanceMax"].Value = Minus_Value.ToString();
        }

      }
      catch (Exception e)
      {
        MessageBox.Show(e.ToString());
      }

    }
    else
    {
      mToolBlock.Outputs["Result"].Value = 1;
      label6.SetXYText(100, 400, "找圆NG");
      label6.Color = CogColorConstants.Red;
      gc.Add(label6);
    }
    #endregion
  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值