机器视觉20 齿轮缺角 引脚缺失 液位高度 图像去噪

1.案例  齿轮缺角

1.预留图形预处理工具

1.边缘提取工具

利用copyRegin工具 实现 齿轮环 分割效果 为blob分析 齿轮缺失做准备

1和2 把 blob中的灰度图添加到 copyRegin的俩个图像输入参数中

3.把blob匹配结果中心坐标给copyRegin 匹配区域坐标

4.设置填充数值  128   目的  为了 使填充的区域灰度值和 目标图像背景灰度值 一致

5.填充区域设置

6.区域外 调整像素 调整为 不写入像素 

 1.勾选使用图像配对

1.运行

2.运行后最终效果    (此 图效果  可以清晰的匹配 每个齿轮)

1.使用blob工具

2.设置参数

3.匹配效果图根据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.ImageProcessing;
using Cognex.VisionPro.PMAlign;
using Cognex.VisionPro.Blob;
#endregion

public class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase
{
  #region Private Member Variables
  private Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock;
  #endregion
 
  //定义图形集合
  CogGraphicLabel label = new CogGraphicLabel();
 
  public override bool GroupRun(ref string message, ref CogToolResultConstants result)
  {

      
    
    int count = (int)mToolBlock.Inputs["Count"].Value;
    CogBlobTool blob = mToolBlock.Tools["CogBlobTool2"] as CogBlobTool;


    // Run each tool using the RunTool function
    foreach(ICogTool tool in mToolBlock.Tools)
      mToolBlock.RunTool(tool, ref message, ref result);
    
    //获取blob结果个数
    int currentCount = blob.Results.GetBlobs().Count;
    //获取齿轮差值
    int diff = count - currentCount;
    
       
    string res = "";
    label.Color = CogColorConstants.Green;
    label.Font = new Font("宋体", 20);
    //根据差值判断是否有缺失
    if (diff > 0)
    {
      res = "缺失:" + diff;
      label.Color = CogColorConstants.Red;
    }
    else
    {
      res = "良品";
      
    }
    label.SetXYText(100, 250, res);
    return false;
  }

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

案例:引脚缺失

1使用cogIPoneImage去掉背景图中的白色噪点

2.添加灰阶形态NxM  内核高度与宽度 设置  3 x3

1.使用blob  匹配出图像中的引脚1.设置过滤参数1.使用卡尺工具  测量每个引脚距离  目的:检测是否有缺失的引脚

#region namespace imports
using System;
using System.Collections;
using System.Collections.Generic;
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.Blob;
using Cognex.VisionPro.Caliper;
#endregion

public class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase
{
  #region Private Member Variables
  private Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock;
  #endregion
  //声明图形集合
  private CogGraphicCollection col = new CogGraphicCollection();
  //声明BlobTool成员变量
  CogBlobTool blob;
  // 声明卡尺工具成员变量
  CogCaliperTool cal1;
  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

 
    //映射blob
    blob = mToolBlock.Tools["CogBlobTool1"] as CogBlobTool;
    //映射卡尺工具
    cal1 = mToolBlock.Tools["CogCaliperTool1"] as CogCaliperTool;

    //判断是否有图形缺陷  true 为无缺陷  false反之
    bool b = true;


    //清空图形集合
    col.Clear();
    // Run each tool using the RunTool function
    foreach(ICogTool tool in mToolBlock.Tools)
      mToolBlock.RunTool(tool, ref message, ref result);
    
    
    //判断引脚是否偏移

  //根据blob个数结果
    for (int i = 0; i < blob.Results.GetBlobs().Count; i++)
    {
      //获取角度
      double angle = Math.Abs((blob.Results.GetBlobs()[i].Angle) * 180 / Math.PI);
      
      //通过角度大小判断是否NG
      if (Math.Abs(angle- 90) > 5)
      {
        //初始化边线图形
        CogPolygon polygon = new CogPolygon();
        polygon = blob.Results.GetBlobs()[i].GetBoundary();
        polygon.Color = CogColorConstants.Red;
        
        col.Add(polygon);
        b = false;
      }
        
      
      //获取blob检测的高度
      double h = blob.Results.GetBlobs()[i].GetMeasure(CogBlobMeasureConstants.BoundingBoxExtremaAngleHeight);
      //通过Y坐标判断  斑点在上方还是下方
      //上方
      if (blob.Results.GetBlobs()[i].CenterOfMassY < 220)
      {
        //判断高度 大于110或者小于90  上方特征有缺陷
        if (h > 110 || h < 90)
        {
          //
          CogPolygon polygon = new CogPolygon();
          polygon = blob.Results.GetBlobs()[i].GetBoundary();
          polygon.Color = CogColorConstants.Red;
          col.Add(polygon);
          b = false;
        }
      }
        //下方
      else if(blob.Results.GetBlobs()[i].CenterOfMassY > 220)
      {
        //判断高度 大于65或者小于50  下方特征有缺陷
        if (h > 65 || h < 50)
        {
          
          //初始化边界显示线
          CogPolygon polygon = new CogPolygon();
          polygon = blob.Results.GetBlobs()[i].GetBoundary();
          polygon.Color = CogColorConstants.Red;
          //添加到图形集合工具
          col.Add(polygon);
          b = false;
        }
      }
    }
    
    
 
    //卡尺判断间距  判断引脚是否有缺失
     
    //初始化泛型集合
    List<double> listX = new List<double>();
    
    //循环 卡尺工具结果个数
    for (int j = 0; j < cal1.Results.Count; j++)
    {
      
      //添加每个结果X坐标点到泛型集合中
      listX.Add(cal1.Results[j].Edge0.PositionX);
    }
    listX.Sort();//升序    目的:把做升序排列  方便后续 进行差值计算
      
    
    
    for (int i = 0; i < listX.Count - 1; i++)
    {
      
      //相邻坐标点求 差值
      double width = listX[i + 1] - listX[i];
      //差值大于80  证明引脚有缺失
      if (width > 80)
      {
        
        //初始化图形限定框
        CogRectangleAffine rec = new CogRectangleAffine();
        //限定款位置
        rec.CenterX = (listX[i + 1] + listX[i]) / 2;
        rec.CenterY = cal1.Results[i].Edge0.PositionY;
        rec.SideXLength = width;
        rec.SideYLength = 100;
        rec.Color = CogColorConstants.Red;
        //添加到图形集合工具
        col.Add(rec);
        b = false;
      }
    }
    
    //初始化label 显示NG 或者OK信息
    CogGraphicLabel label = new CogGraphicLabel();
    label.Font = new Font("微软雅黑", 20);
    if (b)
    {
      label.SetXYText(100, 80, "OK");
      label.Color = CogColorConstants.Green;
    }
    else
    {
      label.SetXYText(100, 80, "NG");
      label.Color = CogColorConstants.Red;
    }
    //添加到图形集合工具
    col.Add(label);
    return false;
  }

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

案例:液位高度检测

1.预处理工具 处理图像对比度

1.边缘提取工具 突出图像边缘效果 ​​​​​

1.模板匹配

2.中心原点位置 调整到正常液位坐标  用于后续对比1.卡尺工具1.利用卡尺测量出瓶口端基准线位置

1.创建线工具 

2.根据卡尺工具线的坐标 创建线

1.点到线距离工具

2. x y是卡尺工具上端线的点信息

3.Line 创建线工具的结果

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

public class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase
{
  #region Private Member Variables
  private Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock;
  #endregion
  //创建图像集合
  CogGraphicCollection col = new CogGraphicCollection();
  CogPMAlignTool pma;
  //
  CogCaliperTool cal;
  //
  CogCreateLineTool line;
  //
  CogDistancePointLineTool dis;
  public override bool GroupRun(ref string message, ref CogToolResultConstants result)
  {
   
    //工具映射
    pma = mToolBlock.Tools["CogPMAlignTool1"] as CogPMAlignTool;
    cal = mToolBlock.Tools["CogCaliperTool1"] as CogCaliperTool;
    line = mToolBlock.Tools["CogCreateLineTool1"] as CogCreateLineTool;
    dis = mToolBlock.Tools["CogDistancePointLineTool1"] as CogDistancePointLineTool;
    
    
    //清空图形集合
    col.Clear();
    // Run each tool using the RunTool function
    foreach(ICogTool tool in mToolBlock.Tools)
      mToolBlock.RunTool(tool, ref message, ref result);
    
    
    //根据pma个数结果 循环
    for (int i = 0; i < pma.Results.Count; i++)
    {
      //液位上端卡尺工具的位置
      cal.Region.CenterX = pma.Results[i].GetPose().TranslationX;
      cal.Region.CenterY = pma.Results[i].GetPose().TranslationY;
      //运行卡尺工具
      cal.Run();
      
      //获得点到线距离工具的信息
      dis.X = cal.Results[0].Edge0.PositionX;
      dis.Y = cal.Results[0].Edge0.PositionY;
      dis.Line = line.GetOutputLine();
      //运行工具
      dis.Run();
      
      
      //创建液位上端线段
      double x0 = cal.Results[0].Edge0.PositionX+20;
      double y0 = cal.Results[0].Edge0.PositionY;
      double x1 = cal.Results[0].Edge0.PositionX-20;
      double y1 = cal.Results[0].Edge0.PositionY;
      
      CogLineSegment segment = new CogLineSegment();
      segment.SetStartEnd(x0, y0, x1, y1);
      segment.Color = CogColorConstants.Red;
      
      //创建文本图形
      CogGraphicLabel label = new CogGraphicLabel();
      string res = "";
      //判断液位误差范围
      if (dis.Distance > 130 || dis.Distance < 110)
      {
        res = "NG: " + dis.Distance.ToString("0.00");
        label.Color = CogColorConstants.Red;
      }
      else
      {
        res = "OK: " + dis.Distance.ToString("0.00");
        label.Color = CogColorConstants.Green;
      }
      label.SetXYText(dis.X, dis.Y, res);
      //添加label到集合
      col.Add(label);
      //添加线段到集合
      col.Add(segment);
      
      
    }
    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 < col.Count; i++)
    {
      mToolBlock.AddGraphicToRunRecord(col[i], lastRecord, "CogIPOneImageTool1.InputImage", "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

}

1.图像去噪  

中值Nxm: 内核高度和宽度  设置 7x7                                    消除干扰像素  

灰度形态调整:设置腐蚀    来缩小白色区域  扩大黑色区域    加粗字体效果

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值