VisionPro实战之胶囊缺陷识别

目录

1.案例要求

2.思路和操作

1.Block工具,格式转换

2.匹配所有胶囊,不包括空胶囊

3.使用颜色识别工具

4.编写脚本进行遍历

完整脚本代码:

3.最终实现效果

1.案例要求

测量出每一版胶囊是否合格,不合格的话缺陷是什么都标注出来,下面这张图片是合格胶囊的示例。

2.思路和操作

1.Block工具,格式转换

2.匹配所有胶囊,不包括空胶囊

这里适当调整参数以便更好地匹配到非空胶囊

3.使用颜色识别工具

这两种不合格的产品我们可以通过颜色识别跟正常的胶囊区分开

4.编写脚本进行遍历

CogGraphicCollection col = new CogGraphicCollection();//图形工具存放内容
CogGraphicLabel label = new CogGraphicLabel();//输出总的胶囊是否合格
CogGraphicLabel[] labels = new CogGraphicLabel[15];//输出单个胶囊是否合格

col.Clear();//清除内容,防止内容留存输出到别的图片

//加载组件对象

CogPMAlignTool p1 = mToolBlock.Tools["CogPMAlignTool1"] as CogPMAlignTool;
CogColorMatchTool cm = mToolBlock.Tools["CogColorMatchTool1"] as CogColorMatchTool;

int count = p1.Results.Count;//识别出胶囊的数量
int count_ng = 0,count_ok=0;//count_ng表示缺陷数量,count_ok表示合格数量

//遍历每一个保险器件

for(int i = 0;i < count;i++)
{
  CogCircle c1 = new CogCircle();
  c1.CenterX = p1.Results[i].GetPose().TranslationX;
  c1.CenterY = p1.Results[i].GetPose().TranslationY;
  c1.Radius = 10;
  cm.Region = c1;
  cm.Run();//判断是否合格
  
  if(cm.Result.ResultOfBestMatch.Color.Name == "NG")
  {
    count_ng++;
    labels[i] = new CogGraphicLabel();
    labels[i].SetXYText(c1.CenterX, c1.CenterY, "NG");
  }
  else 
  {
    count_ok++;
    labels[i] = new CogGraphicLabel();
    labels[i].SetXYText(c1.CenterX, c1.CenterY, "OK");
  }
  col.Add(labels[i]);
}
if(count_ok == 15)
{
  label.SetXYText(200, 100, "合格");
}
else
{
  label.Color = CogColorConstants.Red;
  label.SetXYText(200, 100, "缺失:" + (15 - count_ok - count_ng) + " NG:" + count_ng);
}
col.Add(label);

//最后输出显示到这个页面上

foreach(ICogGraphic item in col)
    {
      mToolBlock.AddGraphicToRunRecord(item, lastRecord, "CogImageConvertTool1.InputImage", "");
    }

完整脚本代码:
#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.ImageProcessing;
using Cognex.VisionPro.PMAlign;
using Cognex.VisionPro.ColorMatch;
#endregion

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

  CogGraphicCollection col = new CogGraphicCollection();
  CogGraphicLabel label = new CogGraphicLabel();
  CogGraphicLabel[] labels = new CogGraphicLabel[15];
  /// <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


    col.Clear();
    // Run each tool using the RunTool function
    foreach(ICogTool tool in mToolBlock.Tools)
      mToolBlock.RunTool(tool, ref message, ref result);

    CogPMAlignTool p1 = mToolBlock.Tools["CogPMAlignTool1"] as CogPMAlignTool;
    CogColorMatchTool cm = mToolBlock.Tools["CogColorMatchTool1"] as CogColorMatchTool;
    int count = p1.Results.Count;
    int count_ng = 0,count_ok=0;
    for(int i = 0;i < count;i++)
    {
      CogCircle c1 = new CogCircle();
      c1.CenterX = p1.Results[i].GetPose().TranslationX;
      c1.CenterY = p1.Results[i].GetPose().TranslationY;
      c1.Radius = 10;
      cm.Region = c1;
      cm.Run();
      
      if(cm.Result.ResultOfBestMatch.Color.Name == "NG")
      {
        count_ng++;
        labels[i] = new CogGraphicLabel();
        labels[i].SetXYText(c1.CenterX, c1.CenterY, "NG");
      }
      else 
      {
        count_ok++;
        labels[i] = new CogGraphicLabel();
        labels[i].SetXYText(c1.CenterX, c1.CenterY, "OK");
      }
      col.Add(labels[i]);
    }
    if(count_ok == 15)
    {
      label.SetXYText(200, 100, "合格");
    }
    else
    {
      label.Color = CogColorConstants.Red;
      label.SetXYText(200, 100, "缺失:" + (15 - count_ok - count_ng) + " NG:" + count_ng);
    }
    col.Add(label);
    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)
  {
    foreach(ICogGraphic item in col)
    {
      mToolBlock.AddGraphicToRunRecord(item, lastRecord, "CogImageConvertTool1.InputImage", "");
    }
  }
  #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

}

3.最终实现效果

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值