CogFitLineTool 工具调用多个卡尺得分

效果

解析

上面的图像是利用找线的卡尺数量 根据卡尺数量来进行判定 收集有效得分的卡尺

由于在图像处理中 有些图像它不能完整的找到一个直线 故用两个找边 通过得出两个找边的有效分数 通过脚本传输给CogFitLineTool 里面

工具

脚本解析

1.创建一个 class

cs 复制代码
  public class Point
  {
    public double x;
    public double y;
  }

2.创建收集点和工具的List

cs 复制代码
 private List<ICogTool>LFindtool = new List<ICogTool>();
  private List<Point>Lpoint = new List<Point>();

3.Clear 集合 声明工具

cs 复制代码
LFindtool.Clear();
    Lpoint.Clear();
    CogFitLineTool fitline = mToolBlock.Tools["CogFitLineTool1"]as CogFitLineTool;

4.收集CogFindLineTool 工具

cs 复制代码
foreach(ICogTool tool in mToolBlock.Tools)
    {
      if(tool is CogFindLineTool)
      {
        LFindtool.Add(tool);
      }
    }

5.遍历收集的工具&赋值

cs 复制代码
 foreach(ICogTool items in LFindtool)
    {
       mToolBlock.RunTool(items, ref message, ref result);
      //如果result==我们接受的结果
      if(result == CogToolResultConstants.Accept)
      {
        //将所有找线工具声明成一个变量
        CogFindLineTool findline = items as CogFindLineTool;
        //遍历找线的得分结果
        foreach(CogFindLineResult s in findline.Results)
        {
          if(s.Used)
          {
            //声明赋值
            Point pointt = new Point();
            pointt.x = s.X;
            pointt.y = s.Y;
            Lpoint.Add(pointt);
          }
        }
      }
    }

6.清除CogFitLineTool 点数

cs 复制代码
while(fitline.RunParams.NumPoints > 0)
    {
      fitline.RunParams.DeletePoint(0);
    }

7.赋值卡尺得分给 CogFitLineTool & Running

cs 复制代码
foreach(Point sth in Lpoint)
    {
      fitline.RunParams.AddPoint(sth.x, sth.y);
    }
    
    fitline.Run();

8.All 脚本

cs 复制代码
#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 System.Collections.Generic;
#endregion

public class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase
{
  #region Private Member Variables
  private Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock;
  #endregion
  public class Point
  {
    public double x;
    public double y;
  }
  private List<ICogTool>LFindtool = new List<ICogTool>();
  private List<Point>Lpoint = new List<Point>();

  /// <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
    LFindtool.Clear();
    Lpoint.Clear();
    CogFitLineTool fitline = mToolBlock.Tools["CogFitLineTool1"]as CogFitLineTool;


    // Run each tool using the RunTool function
    foreach(ICogTool tool in mToolBlock.Tools)
    {
      if(tool is CogFindLineTool)
      {
        LFindtool.Add(tool);
      }
    }
    foreach(ICogTool items in LFindtool)
    {
       mToolBlock.RunTool(items, ref message, ref result);
      if(result == CogToolResultConstants.Accept)
      {
        CogFindLineTool findline = items as CogFindLineTool;
        foreach(CogFindLineResult s in findline.Results)
        {
          if(s.Used)
          {
            Point pointt = new Point();
            pointt.x = s.X;
            pointt.y = s.Y;
            Lpoint.Add(pointt);
          }
        }
      }
    }
    while(fitline.RunParams.NumPoints > 0)
    {
      fitline.RunParams.DeletePoint(0);
    }
    foreach(Point sth in Lpoint)
    {
      fitline.RunParams.AddPoint(sth.x, sth.y);
    }
    
    fitline.Run();
     

    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

}
相关推荐
在所不辞兄5 分钟前
【人工智能驱动的电催化研究-01】人工智能是什么?AI、机器学习、深度学习和神经网络之间有什么区别?
人工智能·深度学习·神经网络·机器学习·有限元仿真·电化学·电催化
罗西的思考12 小时前
【轻量化复刻大模型 / KIMI K3】KDA(Kimi Delta Attention):并行
人工智能·算法·机器学习
方方洛15 小时前
ray教程-23-附录C-术语表
人工智能·分布式·机器学习
程序猿编码17 小时前
解决 LLM 算力浪费,一套统一 KV 管理层实现缓存复用
人工智能·神经网络·机器学习·缓存·llm·大模型推理
Blockbuater_drug18 小时前
《经典机器学习实战》第 1 课:导论——是什么、简史、负责任 AI 与标准流程
机器学习·scikit-learn·监督学习·数据清洗·过拟合·训练集测试集·公平性
武天旭20 小时前
AI安全--AI大模型的关键技术组成
人工智能·深度学习·安全·机器学习
程序猿编码21 小时前
RK3588 NPU LLM 推理引擎:自研底层算子,性能碾压原厂 SDK
机器学习·llm·rk3588·大模型推理
Godspeed Zhao1 天前
从零开始学AI21——决策树
算法·决策树·机器学习
Dovis(誓平步青云)1 天前
启元 T1 Pro 视觉伺服实践:基于 HSV 阈值与针孔测距的启元机器人寻物实现
开发语言·人工智能·机器学习·二次开发
高洁011 天前
端侧大模型的应用场景与工程实践
人工智能·深度学习·机器学习·transformer·tornado