机器视觉8

案例1:测量出零件的真实宽度 并且标识出来

1.标定工具

2.模板匹配

3.定位工具

4.卡尺工具

卡尺工具参数设置

#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.CalibFix;
using Cognex.VisionPro.PMAlign;
using Cognex.VisionPro.Caliper;
//List<T> 命名空间
using System.Collections.Generic;
#endregion

public class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase
{
  #region Private Member Variables
  private Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock;
  #endregion
    //CogGraphicLabel 集合
    List<CogGraphicLabel> mlist = new  List<CogGraphicLabel>();


  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
   
    //映射卡尺工具
    CogCaliperTool cpt = mToolBlock.Tools["CogCaliperTool1"] as CogCaliperTool;
    //清空集合
    mlist.Clear();
 
      // Run each tool using the RunTool function
      foreach(ICogTool tool in mToolBlock.Tools)
        mToolBlock.RunTool(tool, ref message, ref result);
      
    //循环创建 CogGraphicLabel
    for(int i = 0; i < cpt.Results.Count;i++)
    {
    
      CogGraphicLabel mLabel = new CogGraphicLabel();
       
      //保留小数点两位数
      double widthNumber = Math.Round(cpt.Results[i].Width, 2);
      
      //设置卡尺坐标点 和卡尺宽度
      mLabel.SetXYText(cpt.Results[i].PositionX, cpt.Results[i].PositionY, widthNumber.ToString());
      //添加图形
      mlist.Add(mLabel);
      
    }
    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)
  {
    //添加图形
    for(int i = 0; i < mlist.Count ;i++){
    
      mToolBlock.AddGraphicToRunRecord(mlist[i], lastRecord, "CogCalibCheckerboardTool1.OutputImage", "script");   
    
    }
  }
  #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

}

 

案例2:充电器镭雕字符缺陷检测  并显示NG/OK结果

模板匹配  用于缺陷检测定位

缺陷检测 提供blob检测图像

blob检测

 

#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 mBlob;
  //声明文本
  private CogGraphicLabel mLabel;
  #endregion

//GroupRun 方法

  public override bool GroupRun(ref string message, ref CogToolResultConstants result)
  {
     
    //映射blob
    mBlob = mToolBlock.Tools["CogBlobTool1"]as CogBlobTool;
    //遍历block工具
    foreach(ICogTool tool in mToolBlock.Tools)
      mToolBlock.RunTool(tool, ref message, ref result);
    //判断blob结果个数  创建对应文本内容
    if(mBlob.Results.GetBlobs().Count == 0)
    {

    //创建label
      mLabel = new CogGraphicLabel();
      mLabel.Color = CogColorConstants.Green;
      mLabel.SetXYText(200, 200, "Result:OK");
    }
    else
    {

//创建label
      mLabel = new CogGraphicLabel();
      mLabel.Color = CogColorConstants.Red;
      mLabel.SetXYText(200, 200, "Result:NG");
    }

    return false;
  }

//ModifyLastRunRecord 方法

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

案例3:图标缺陷检测

1.缺陷用红色画圈标注出来(只标注面积100以上的缺陷);

2.在视图内显示OK/NG  

模板匹配  用于缺陷检测定位

缺陷检测工具

 

blob工具

 

创建圆形几何工具 用于标注缺陷

 

 

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

public class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase
{
  #region Private Member Variables
  private Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock;
 
  //声明blob
  private CogBlobTool mBlob;
  //声明文本
  private CogGraphicLabel mLabel;
  //声明创建圆
  private CogCreateCircleTool mCircle  ;
 
  #endregion
  public override bool GroupRun(ref string message, ref CogToolResultConstants result)
  {
    //映射blob
    mBlob = mToolBlock.Tools["CogBlobTool1"]as CogBlobTool;
    //映射圆
    mCircle = mToolBlock.Tools["CogCreateCircleTool1"]as CogCreateCircleTool;

    // Run each tool using the RunTool function
    foreach(ICogTool tool in mToolBlock.Tools)
      mToolBlock.RunTool(tool, ref message, ref result);
    //判断blob结果大于0 创建文本  并且 运行 圆工具
    if(mBlob.Results.GetBlobs().Count > 0)
    {
      
      mLabel = new CogGraphicLabel();
      mLabel.Color = CogColorConstants.Red;
      mLabel.SetXYText(200, 100, "NG");
    
        //运行圆工具   设置中心点位置 和半径
         mCircle.InputCircle.Radius = 25;
        mCircle.InputCircle.CenterX = mBlob.Results.GetBlobs()[0].CenterOfMassX;
        mCircle.InputCircle.CenterY = mBlob.Results.GetBlobs()[0].CenterOfMassY;

       //主动执行某个工具   run() 执行工具
        mCircle.Run();        
    }
    else
    {
      mLabel = new CogGraphicLabel();
      mLabel.Color = CogColorConstants.Green;
      mLabel.SetXYText(200, 100, "OK");
    }

    return false;
  }

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

案例4:正确检出图中3M字符出现的多墨、少墨现象  用圆标识出来   且显示NG/PK结果 

 

#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;

  #endregion
 
  //声明图形集合
  CogGraphicCollection col = new CogGraphicCollection();
  //声明一个圆图形
  CogCircle mCircle;
  //声明一个文本图形
  CogGraphicLabel mLabel;
  //声明blob工具
  CogBlobTool mBlob;

// GroupRun方法
  public override bool GroupRun(ref string message, ref CogToolResultConstants result)
  {
 
    //转换并赋值
    mBlob = mToolBlock.Tools["CogBlobTool1"]as CogBlobTool;
 
    //清空集合
     col.Clear();
   //运行vp后遍历 block中工具
    foreach(ICogTool tool in mToolBlock.Tools)
      mToolBlock.RunTool(tool, ref message, ref result);
    //判断blob结果  等于0 ok  大于 0 ng
    if(mBlob.Results.GetBlobs().Count == 0)
    {
      mLabel = new CogGraphicLabel();
      mLabel.Color = CogColorConstants.Green;
      mLabel.SetXYText(200, 200, "Result:OK");
      col.Add(mLabel);
    }
    else
    {
      mLabel = new CogGraphicLabel();
      mLabel.Color = CogColorConstants.Red;
      mLabel.SetXYText(200, 200, "Result:NG");
      col.Add(mLabel);
    }
    for (int i = 0; i < mBlob.Results.GetBlobs().Count; i++)
    {
      //初始化圆
      mCircle = new CogCircle();
      //设置颜色
      mCircle.Color = CogColorConstants.Red;
      //设置半径
      mCircle.Radius = 15;
      //设置圆心
      mCircle.CenterX = mBlob.Results.GetBlobs()[i].CenterOfMassX;
      mCircle.CenterY = mBlob.Results.GetBlobs()[i].CenterOfMassY;
      //设置圆线条 的像素宽度
      mCircle.LineWidthInScreenPixels = 2;
     

    //把画圆工具 放入到集合中
      col.Add(mCircle);
    }

    return false;
  }

// ModifyLastRunRecord 方法

public override void ModifyLastRunRecord(Cognex.VisionPro.ICogRecord lastRecord)
  {
    for (int i = 0; i < col.Count; i++)
    {
              mToolBlock.AddGraphicToRunRecord(col[i], lastRecord, "CogPMAlignTool1.InputImage", "script");
    }
   
   
  }

 

CogPolarUnwrapTool工具解析


作用:能够将环形区域转换为矩形区域  便于后续检测
转换原理:圆形输入区域转换矩形区域
1.圆形输入区域(Circular input Region)中的采样点的数量确定输出图像的大小(以像素为单位)。

2.PolarUnwrap工具自动计算矩形输出图像(Default Output lmage Size)的大小,以便最大限度地减少图像失真。

3.对于矩形输出区域(Default Output lmage Size),默认的X轴大小是内弧长度(InnerArcLength)和外弧长度(OuterArcLength)的平均值,

4.默认的Y轴大小是内半径(InnerRadius)和外半径(OuterRadius)之间的差值,如下图所示:

转换原理:椭圆输入区域()转换矩形区域(Default Output lmage Size:)

矩形区域:

1.默认的X轴大小是内弧和外弧长度的平均值,

2.默认的Y轴大小是最大和最小内半径和外半径之间的差值的平均值,

使用流程 

 圆形和椭圆形区域形状

 

 

 调整展开区域

 

采样模式对比:

最近相邻取样

使用其中心为最接近于采样点作为采样的像素的值:

双线性插值

双线性插值采样方法通过取四个像素,其中心是最接近于采样点的值的距离加权平均计算的采样值

双线性插值 其中a,b,c和 d是下图所示的四个距离,归一化使得像素中心之间的距离为1,p1,p2,p3和 p4是下面所示的四个像素的值数字。

 

总结:

最近相邻采样方比双线性差值采样方法执行得更快,但它不能产生最准确的变换图像。

案例:见例程

  • 18
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值