机器视觉12-缺陷检测例子

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

 

#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)
    {
      mLabel = new CogGraphicLabel();
      mLabel.Color = CogColorConstants.Green;
      mLabel.SetXYText(200, 200, "Result:OK");
    }
    else
    {
      mLabel = new CogGraphicLabel();
      mLabel.Color = CogColorConstants.Red;
      mLabel.SetXYText(200, 200, "Result:NG");
    }

    return false;
  }

//ModifyLastRunRecord 方法

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

1.图标缺陷检测:以其中一个产品为标准是识别出有问题的缺陷;

2.缺陷用红色画圈标注出来(只标注面积100以上的缺陷);3.在视图内显示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");
  }

1.3M: 正确检出图中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");
    }
   
   
  }

总结:

VP中工具 要通过run方法执行

VP中的图形类 要通过AddGraphicToRunRecord()来进行显示

 

  • 11
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值