线序识别;通过线的颜色来判断线序颜色  并标注颜色

 1. 二值图转换

2.模板匹配

3.定位工具

4.使用色彩匹配工具  对每个位置的线 都进行匹配   一共需要7个工具

c9b83467ceba49be8ba60a77b6427f88.png

1.提取7中色彩

2.对于每个线分别进行区域检测

c8257cdff04a485f86e13f498a28b821.png

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

public class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase
{
  #region Private Member Variables
  private Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock;
 
  //设置7个匹配工具字段
  private CogColorMatchTool pnt01;
  private CogColorMatchTool pnt02;
  private CogColorMatchTool pnt03;
  private CogColorMatchTool pnt04;
  private CogColorMatchTool pnt05;
  private CogColorMatchTool pnt06;
  private CogColorMatchTool pnt07;
  //设置7个文字工具
  private CogGraphicLabel pnt01Label;
  private CogGraphicLabel pnt02Label;
  private CogGraphicLabel pnt03Label;
  private CogGraphicLabel pnt04Label;
  private CogGraphicLabel pnt05Label;
  private CogGraphicLabel pnt06Label;
  private CogGraphicLabel pnt07Label;
  public override bool GroupRun(ref string message, ref CogToolResultConstants result)
  {
   //初始化 7个工具
    pnt01Label = new CogGraphicLabel();
    pnt02Label = new CogGraphicLabel();
    pnt03Label = new CogGraphicLabel();
    pnt04Label = new CogGraphicLabel();
    pnt05Label = new CogGraphicLabel();
    pnt06Label = new CogGraphicLabel();
    pnt07Label = new CogGraphicLabel();
    // 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);
    //映射对应的颜色匹配工具
    pnt01 = mToolBlock.Tools["CogColorMatchTool1"]as CogColorMatchTool;
    pnt02 = mToolBlock.Tools["CogColorMatchTool2"]as CogColorMatchTool;
    pnt03 = mToolBlock.Tools["CogColorMatchTool3"]as CogColorMatchTool;
    pnt04 = mToolBlock.Tools["CogColorMatchTool4"]as CogColorMatchTool;
    pnt05 = mToolBlock.Tools["CogColorMatchTool5"]as CogColorMatchTool;
    pnt06 = mToolBlock.Tools["CogColorMatchTool6"]as CogColorMatchTool;
    pnt07 = mToolBlock.Tools["CogColorMatchTool7"]as CogColorMatchTool;
    //文字工具设置颜色
    pnt01Label.Color = CogColorConstants.Green;
    pnt02Label.Color = CogColorConstants.Green;
    pnt03Label.Color = CogColorConstants.Green;
    pnt04Label.Color = CogColorConstants.Green;
    pnt05Label.Color = CogColorConstants.Green;
    pnt06Label.Color = CogColorConstants.Green;
    pnt07Label.Color = CogColorConstants.Green;
    //文字工具设置位置  和对应结果
    pnt01Label.SetXYText(100, 400, pnt01.Result.ResultOfBestMatch.Color.Name);
    pnt02Label.SetXYText(300, 400, pnt02.Result.ResultOfBestMatch.Color.Name);
    pnt03Label.SetXYText(500, 400, pnt03.Result.ResultOfBestMatch.Color.Name);
    pnt04Label.SetXYText(700, 400, pnt04.Result.ResultOfBestMatch.Color.Name);
    pnt05Label.SetXYText(900, 400, pnt05.Result.ResultOfBestMatch.Color.Name);
    pnt06Label.SetXYText(1100, 400, pnt06.Result.ResultOfBestMatch.Color.Name);
    pnt07Label.SetXYText(1300, 400, pnt07.Result.ResultOfBestMatch.Color.Name);
    

    return false;
  }

 
  public override void ModifyCurrentRunRecord(Cognex.VisionPro.ICogRecord currentRecord)
  {
  }
 

 
  public override void ModifyLastRunRecord(Cognex.VisionPro.ICogRecord lastRecord)
  {
    
    //文字工具添加到窗口中
    mToolBlock.AddGraphicToRunRecord(pnt01Label, lastRecord, "CogImageConvertTool1.InputImage", "script");
    mToolBlock.AddGraphicToRunRecord(pnt02Label, lastRecord, "CogImageConvertTool1.InputImage", "script");
    mToolBlock.AddGraphicToRunRecord(pnt03Label, lastRecord, "CogImageConvertTool1.InputImage", "script");
    mToolBlock.AddGraphicToRunRecord(pnt04Label, lastRecord, "CogImageConvertTool1.InputImage", "script");
    mToolBlock.AddGraphicToRunRecord(pnt05Label, lastRecord, "CogImageConvertTool1.InputImage", "script");
    mToolBlock.AddGraphicToRunRecord(pnt06Label, lastRecord, "CogImageConvertTool1.InputImage", "script");
    mToolBlock.AddGraphicToRunRecord(pnt07Label, lastRecord, "CogImageConvertTool1.InputImage", "script");
  }
 


  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));
  }
 

}

 案例:注胶有无识别:判断图中产品周边槽及中心孔内注胶有无。蓝色为有胶,黑色为无胶 并标注颜色

1.添加提取颜色工具

2.对有胶的图片进行颜色提取

3.依据获得的像素值 (如果是0 就是无胶  )

bf069c378f6a432e82147b3cde88c16c.png

密封条胶塞颜色识别:识别图中出现的密封条胶塞的颜色,并标注颜色

1.使用颜色提取工具对 每张图中的 不同颜色的密封胶条 提取色彩

2.使用blob工具 提取结果  如果能提取出 结果  说明 有对应颜色密封胶条

2137738bc6f44d7398839c8b02281401.png

1.哪个blob中有结果 证明 颜色存在

717712f09257434e8aa80c409057fe1c.png

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

public class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase
{
  #region Private Member Variables
  private Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock;
  //设置4个blob工具字段
  private CogBlobTool orange;
  private CogBlobTool yellow;
  private CogBlobTool red;
  private CogBlobTool blue;
  //设置4个文本工具字段
  private CogGraphicLabel orangeLabel;
  private CogGraphicLabel yellowLabel;
  private CogGraphicLabel redLabel;
  private CogGraphicLabel blueLabel;
  #endregion

 
  public override bool GroupRun(ref string message, ref CogToolResultConstants result)
  {
    //初始化4个文本工具
    orangeLabel = new CogGraphicLabel();
    yellowLabel = new CogGraphicLabel();
    redLabel = new CogGraphicLabel();
    blueLabel = new CogGraphicLabel();
    
    // 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);
     
    //根据blob检测结果的个数判断  颜色是否符合
    orange = mToolBlock.Tools["CogBlobToolOrange"]as CogBlobTool;
   
    if(orange.Results.GetBlobs().Count >= 1)
    {
      orangeLabel.Color = CogColorConstants.White;
      orangeLabel.SetXYText(200, 200, "橙色");
    }
    yellow = mToolBlock.Tools["CogBlobToolYellow"]as CogBlobTool;
    if(yellow.Results.GetBlobs().Count >= 1)
    {
      yellowLabel.Color = CogColorConstants.White;
      yellowLabel.SetXYText(200,200,"黄色");
    }
    red = mToolBlock.Tools["CogBlobToolRed"]as CogBlobTool;
    if(red.Results.GetBlobs().Count >= 1)
    {
      redLabel.Color = CogColorConstants.White;
      redLabel.SetXYText(200, 200, "红色");
    }
    blue = mToolBlock.Tools["CogBlobToolBlue"]as CogBlobTool;
    if(blue.Results.GetBlobs().Count >= 1)
    {
      blueLabel.Color = CogColorConstants.White;
      blueLabel.SetXYText(200, 200, "蓝色");
    }

    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)
  {
  }
 
  public override void ModifyLastRunRecord(Cognex.VisionPro.ICogRecord lastRecord)
  {
   // 根据blob检测结果的个数判断 添加对应文本工具
    if(orange.Results.GetBlobs().Count >= 1)
    {
      mToolBlock.AddGraphicToRunRecord(orangeLabel, lastRecord, "CogColorExtractorToolOrange.InputImage", "script");
    }
    if(yellow.Results.GetBlobs().Count >= 1)
    {
      mToolBlock.AddGraphicToRunRecord(yellowLabel, lastRecord, "CogColorExtractorToolOrange.InputImage", "script");
    }
    if(red.Results.GetBlobs().Count >= 1)
    {
      mToolBlock.AddGraphicToRunRecord(redLabel, lastRecord, "CogColorExtractorToolOrange.InputImage", "script");
    }
    if(blue.Results.GetBlobs().Count >= 1)
    {
      mToolBlock.AddGraphicToRunRecord(blueLabel, lastRecord, "CogColorExtractorToolOrange.InputImage", "script");
    }
 
  }
 
  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

}

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐