机器视觉13-PolarUnwrap和PatInspect案例

文章详细描述了如何在CognexVisionPro中使用高级脚本实现功能,如添加圆形展开工具、OCR字符提取、图形区域设置和缺陷检测,展示了两个具体案例的应用场景。
摘要由CSDN通过智能技术生成

案例1:

1.添加圆形展开工具

2.调整内外径圆环 (注意:转换方向 以3闭合圆形的地方开始  沿4箭头的方向分割圆环)

59f9dec184e84097b87500e5b8c67267.png

1.添加ocr工具提取字符

2.提取字符操作

3.选框范围设置要包括提取的字符(注意:不要超过图形边缘)

77f29d1e4c2e4e21a015c3b98fdc615b.png

区段设置(如果有需要)

2137c19d622749768d349f8be3ec343f.png

引用 ovr工具的命名空间

#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.OCRMax;
#endregion

public class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase
{
  #region Private Member Variables
  private Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock;
  #endregion
 
  //声明label字段
  CogGraphicLabel label;
  public override bool GroupRun(ref string message, ref CogToolResultConstants result)
  {

 
   CogOCRMaxTool     ocr = mToolBlock.Tools["CogOCRMaxTool1"] as CogOCRMaxTool;
    label = new CogGraphicLabel();
    
    // Run each tool using the RunTool function
    foreach(ICogTool tool in mToolBlock.Tools)
      mToolBlock.RunTool(tool, ref message, ref result);
   
    //ocr 提取字符串
    string text = ocr.LineResult.ResultString;
    //设置label的位置和内容
    label.SetXYText(200, 800, "编号:" + text);、
    //label的颜色
    label.Color = CogColorConstants.Green;
    //label的字体样式
    Font font = new Font("楷体", 20);
    label.Font = font;
    
    return false;
  }

  public override void ModifyLastRunRecord(Cognex.VisionPro.ICogRecord lastRecord)
  {
    mToolBlock.AddGraphicToRunRecord(label, lastRecord, "CogPolarUnwrapTool1.InputImage", "script");
  }

案例2:

456804d148e642f69cfdd9c9b6ae64c0.png

cc20188a9f0249fea75a336577a28d9f.png

1.使用fixture空间坐标

2和3 不使用

e6b191a2ab154accbfac126e7b0f19ab.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.OCRMax;
#endregion

public class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase
{
  #region Private Member Variables
  private Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock;
  #endregion
  CogGraphicLabel label =new CogGraphicLabel();
  CogGraphicLabel label1 =new CogGraphicLabel();
 
  /// <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
    
   CogOCRMaxTool   ocr = mToolBlock.Tools["CogOCRMaxTool1"] as CogOCRMaxTool;
   CogOCRMaxTool   ocr2 = mToolBlock.Tools["CogOCRMaxTool2"] as CogOCRMaxTool;
    
    
    // Run each tool using the RunTool function
    foreach(ICogTool tool in mToolBlock.Tools)
      mToolBlock.RunTool(tool, ref message, ref result);
    
    
   //左边字符提取内容
    string text = ocr.LineResult.ResultString;

//左边label设置
    label.SetXYText(120, 200, "左边:" + text);
    label.Color = CogColorConstants.Green;
    Font font = new Font("楷体", 20);
    label.Font = font;

//右边字符提取内容
    string text1 = ocr2.LineResult.ResultString;

//右边label设置
    label1.SetXYText(120, 230, "右边:" + text1);
    label1.Color = CogColorConstants.Red;
    Font font1 = new Font("楷体", 30);
    label1.Font = font1;
    return false;
  }

  public override void ModifyLastRunRecord(Cognex.VisionPro.ICogRecord lastRecord)
  { 

    左边label添加
    mToolBlock.AddGraphicToRunRecord(label, lastRecord, "CogPMAlignTool1.InputImage", "script");

右边label添加
    mToolBlock.AddGraphicToRunRecord(label1, lastRecord, "CogPMAlignTool1.InputImage", "script");
  } 

案例2:筛子

1.把彩图转换为灰度图

e8570611efd54bb8bb7f44b7b6e658cd.png

1.blob工具 设置相关属性

823e5571675948189c53e45e0e77a4bc.png

1.blob筛选结果

bd174a6371a54d458610d3fd4ba6ac32.png

 结果图:

fd9ea3ea4cdd427781a88676c241cdef.png

案例4:污染 缺失眼睛

1aade0d875b24e818965ba714fe0763d.png

5b267721682a488d90e14abf0730bc3f.png

5f40410c8e7e41a6bf14fda95c47f907.png

f8f0b4ee3b4343cc9449233bc137a9fa.png

5d3bd5e96e474b61b62c71b4fc02d864.png

d1c814107da54b0ba8ce1cc3151f53fc.png

77ef031cbc3c468aac102885a5a40ee8.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.PMAlign;
using Cognex.VisionPro.PatInspect;
using Cognex.VisionPro.Blob;
#endregion

public class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase
{
  #region Private Member Variables
  private Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock;
 
  //声明blob
   private CogBlobTool mBlobRight;
   private CogBlobTool mBlobLeft;
   private CogBlobTool mBlobWuRan;
  //声明文本
   private CogGraphicLabel mLabel =new CogGraphicLabel();
  #endregion


  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
     mBlobLeft = mToolBlock.Tools["CogBlobTool左眼"]as CogBlobTool;
     mBlobRight = mToolBlock.Tools["CogBlobTool右眼"]as CogBlobTool;
     mBlobWuRan = mToolBlock.Tools["CogBlobTool污染"]as CogBlobTool;
    // Run each tool using the RunTool function
    foreach(ICogTool tool in mToolBlock.Tools)
      mToolBlock.RunTool(tool, ref message, ref result);
      
    //判断缺陷是否存在
    if(mBlobLeft.Results.GetBlobs().Count == 0&&mBlobRight.Results.GetBlobs().Count == 0&&mBlobWuRan.Results.GetBlobs().Count == 0)
    {
      //无缺陷
      mLabel.Color = CogColorConstants.Green;
      mLabel.SetXYText(200, 200, "Result:OK");
    
    }
    else{
      
      //有缺陷
      //分别对缺陷结果进行筛选
      string a = mBlobLeft.Results.GetBlobs().Count == 0 ? "" : "右眼缺失";
      string b = mBlobRight.Results.GetBlobs().Count == 0 ? "" : "左眼缺失";
      string c = mBlobWuRan.Results.GetBlobs().Count == 0 ? "" : "有污染";
    

      mLabel.Color = CogColorConstants.Red;
      mLabel.SetXYText(200, 200, "缺陷结果:"+a+"\n"+b+"\n"+c+"\n");
    
    }
    return false;
  }

public override void ModifyLastRunRecord(Cognex.VisionPro.ICogRecord lastRecord)
  {
    
    mToolBlock.AddGraphicToRunRecord(mLabel, lastRecord, "CogPMAlignTool1.InputImage", "script");  
  }

案例4:

2e3a1b76cec84429aef2467f72d11c27.png

f70fb36cba2f473ca1cfeb356a21acef.png

f786672947804d67a6bfa36c57c73a55.png

9d66645b574448caa62ea4afb344719d.png

eae6190477644efdb1d5776decd52490.png

d53a424ac48049ec901f45d5cca127f6.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值