VisionPro斑点工具CogBlobTool

目录

目标:检测工具器件圆圈处是否缺失,False:NG, TRUE:PASS

1.CogPMAlignTool目标定位

2.CogFixtureTool中心点坐标

3.CogBlobTool斑点工具

4.CogResultAnalysisTool

5.高级脚本判断

6.VS进行C#的界面要求展示


目标:检测工具器件圆圈处是否缺失,False:NG, TRUE:PASS

运用工具:1.CogPMAlignTool目标定位,2.CogFixtureTool中心点坐标,3.CogBlobTool斑点工具,4.CogResultAnalysisTool运算函数 5.文字显示/高级脚本编辑

1.CogPMAlignTool目标定位

抓取图像,训练角度调整可允许随意反转360度,训练

2.CogFixtureTool中心点坐标

获取坐标位置与GetPose进行对接,获取输入图像

3.CogBlobTool斑点工具

区域选择方形区域

运行查看结果显示(小孔测量)

其中ID2的面积不是我们所需要的地方所以需要进行过滤操作 

下一步复制操作 (大孔操作)

极性为白底黑点,也可以理解为亮底黑面,运行查看结果

过滤掉没用的信息,ID2,4,3可以看到是没用的信息

根据性能比填充最小面积为200像素时最优,这里的小孔填充和大孔填充与Opencv很像,小孔填充是利用面积筛选的方式留下需要的信息,大孔填充则是利用类似泛洪填充,或者是闭运算(先膨胀后腐蚀)。

 可以看到四个空位已经找到

接下来进行个数计算 

4.CogResultAnalysisTool

 紧接着需要添加终端,找到可以传输的double类型的数值,具体实现参考上一个VisionPro博客

接着是显示文字,因为需要判断,所以选择高级脚本编写

5.高级脚本判断

#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.PMAlign;
using Cognex.VisionPro.CalibFix;
using Cognex.VisionPro.Blob;
using Cognex.VisionPro.ResultsAnalysis;
#endregion

public class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase
{
  #region Private Member Variables
  private Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock;
  //1
  CogGraphicLabel myLabel = new CogGraphicLabel();
  #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);
    
    //2
    CogResultsAnalysisTool c = new CogResultsAnalysisTool();
    c = mToolBlock.Tools["CogResultsAnalysisTool1"] as CogResultsAnalysisTool;
    if(c.Result.EvaluatedExpressions["Sum"].Value.ToString() == 4.ToString())
    {
      myLabel.SetXYText(0, 0, "PASS" + c.Result.EvaluatedExpressions["Sum"].Value.ToString());
    }
    else
    {
    myLabel.SetXYText(0, 0, "PASS" + c.Result.EvaluatedExpressions["Sum"].Value.ToString());
    }
    myLabel.Color = CogColorConstants.Red;
    myLabel.Font = new Font("宋体", 15);

    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)
  {
    mToolBlock.AddGraphicToRunRecord(
      myLabel,
      lastRecord,
      "CogFixtureTool1.OutputImage",
      ""
      );
  }
  #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

}

代码中有一处判断:

if(c.Result.EvaluatedExpressions["Sum"].Value.ToString() == 4.ToString())

 这里进行了强转,string类型是不可以与int类型进行赋值比较

6.VS进行C#的界面要求展示

点击VisionPro Application模块

 

文件中添加刚刚保存好的 程序

 一路next

在界面中添加输入字段 

路径为Tools下CogToolBlock中output输出的内容的value值

选用VS执行C#

 点击持续运行,qt界面中显示的胃实际检测孔洞数量

 

 

  • 2
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

芝士是只猫

开源使得世界变得更美丽

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值