机器视觉16

 案例1:蓝色工件破损检测 并且标绘出缺陷轮廓

1.颜色提取工具   目的:获取输出一个灰度图像ff6027e82c1f41a3999ca1f83898d83b.png 用于blob检测

532e580223794c568bbae846d7fac77e.png

 1.提取多的特征颜色   目的: 是灰度图的特征更明显

eb7d745dc7ac4d5299bf0461f0c2d17d.png

 1.提取效果图

2dbe3cd4a94d4da9a7a114812210163f.png

 1.blob工具52d6cfe9843c49cc9fb1195703ba0f14.png

a239b1e40a43466d93323eb863c5e259.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.ImageProcessing;
using Cognex.VisionPro.PMAlign;
using Cognex.VisionPro.CalibFix;
using Cognex.VisionPro.Caliper;
using Cognex.VisionPro.Blob;
#endregion

public class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase
{
  #region Private Member Variables
  private Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock;
  #endregion
  CogGraphicCollection col;

  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

    col = new CogGraphicCollection();
    CogBlobTool blob = mToolBlock.Tools["CogBlobTool1"] as CogBlobTool;
    // Run each tool using the RunTool function
    foreach(ICogTool tool in mToolBlock.Tools)
      mToolBlock.RunTool(tool, ref message, ref result);
   
    for (int i = 0; i < blob.Results.GetBlobs().Count; i++)
    {
      
   // CogPolygon边界显示图形
      CogPolygon p = new CogPolygon();

  

   //  获取blob结果的Boundary边界
      p = blob.Results.GetBlobs()[i].GetBoundary();
      p.Color = CogColorConstants.Red;


      col.Add(p);
    }
    
    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");
    }
  }
  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:齿轮内径检测

1.使用模板匹配

2.设置匹配区域

3.使用掩膜不必要的干扰   (保留两个内径的圆形)

81a46164049a40cf86e694350acf1298.png

1.添加找圆工具

2.添加模板匹配中心坐标

3.给外圈圆添加找圆工具

0e3064385a054d7298d7c82375b625a3.png

RunParams.ExpectedCircularArc.CenterX

RunParams.ExpectedCircularArc.CenterY    找圆工具圆心坐标

因为我们模板匹配的是圆形  所引 匹配的圆形坐标 就是 找圆工具坐标

8a92fd4b31524dfd995fa774229d7eb1.png

1.显示结果添加了 圆形图案  CogCircle circle 

文本    CogGraphicLabel label

84cb15bf0c384335989fcec71dab6379.png

 

public class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase
{
  #region Private Member Variables
  private Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock;
  #endregion
 
  //声明图形集合
  private CogGraphicCollection col = new CogGraphicCollection();

  public override bool GroupRun(ref string message, ref CogToolResultConstants result)
  {
    
    //声明pma
    CogPMAlignTool pma = mToolBlock.Tools["CogPMAlignTool1"] as CogPMAlignTool;
    //声明找圆
    CogFindCircleTool fc = mToolBlock.Tools["CogFindCircleTool1"] as CogFindCircleTool;
    //声明一个圆图形变量  目的是在图形界面显示每一个找到的圆
    CogCircle circle;
    //声明一个label标签  目的是在图形界面显示每一个找到的圆半径
    CogGraphicLabel label;
    
    //清空
    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++)
    {
      //为找圆工具添加圆心
      fc.RunParams.ExpectedCircularArc.CenterX = pma.Results[i].GetPose().TranslationX;
      fc.RunParams.ExpectedCircularArc.CenterY = pma.Results[i].GetPose().TranslationY;
      //运行找圆
      fc.Run();
      
      //给变量圆赋值
      circle = new CogCircle();

   //获取找圆工具结果的圆形对象
      circle = fc.Results.GetCircle();
      circle.Color = CogColorConstants.Orange;
      //向集合中添加圆图形
      col.Add(circle);

//创建文字图像对象
      label = new CogGraphicLabel();
      label.SetXYText(circle.CenterX, circle.CenterY, "半径:" + circle.Radius.ToString("0.00"));
      label.Color = CogColorConstants.Red;
      //向集合中添加label
      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, "CogPMAlignTool1.InputImage", "Script");
    }
  }

 

案例3:齿轮圆形到齿轮角距离检测 求出最大值最小值 平均值

1.模板匹配齿轮  目的:匹配整个齿轮特征

866cba3d2c454006a4e13a6cf35c9a98.png

1.FixTure齿轮定位   后续提供定位参考

09ae8cb771ee4f2caf32ea96a094474a.png

1.模板匹配单个齿轮角

2.设置查找概数 等其他参数  目的:匹配所有齿轮角特征

2144a8f94ac545a2ab76f99c1e929574.png 1.模板匹配单个齿轮角

46e3a0239c4243be9b6feddf11275f21.png

1.卡尺工具 查找每个齿轮角的边线

2. 模板匹配每个卡尺坐标赋值给 卡尺工具的仿射矩形中心点坐标  Region.centerX  Region.centerY  目的:为了给卡尺工具 提供坐标点参考

4a633a5e13534bfbac30a880681ed06a.png

2.不要需要使用fixTure 原因:模板匹配2 提供坐标  

5269d56a2a7d49d0868a80adb4b8b97b.png

 1.找圆  目的:找到圆心坐标  用于后续点到点测量

f06b9701fc5c4bfcac815a65eb5a49d9.png

1.点到点的距离测量工具

2.卡尺工具 Edge0.PositionX 和Edge0.PositionY  的坐标赋值给 点到点工具的 起点坐标

3.找圆工具的圆心坐标赋值给 点到点工具的 终点坐标

6234713a50274102a211771fd4d9e0e7.png

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

public class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase
{
  #region Private Member Variables
  private Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock;
  #endregion
 
 
  //声明图像集合
  CogGraphicCollection col = new CogGraphicCollection();
 
  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
    //映射模板匹配对象
    CogPMAlignTool pma = mToolBlock.Tools["CogPMAlignTool2"] as CogPMAlignTool;
    //映射卡尺工具对象
    CogCaliperTool cal = mToolBlock.Tools["CogCaliperTool1"] as CogCaliperTool;
    //映射找圆工具对象
    CogFindCircleTool fc = mToolBlock.Tools["CogFindCircleTool1"] as CogFindCircleTool;
    //映射点到点距离工具对象
    CogDistancePointPointTool dis = mToolBlock.Tools["CogDistancePointPointTool1"] as CogDistancePointPointTool;
    //清空图像集合
    col.Clear();
   
   
    //声明集合 --存储距离数据
    List<double> distances = new List<double>();
    
    
    
    foreach(ICogTool tool in mToolBlock.Tools)
      mToolBlock.RunTool(tool, ref message, ref result);
    
    //以模板匹配结果个数遍历  
    for (int i = 0; i < pma.Results.Count; i++)
    {
      //获取pma结果的中心点坐标
      double x = pma.Results[i].GetPose().TranslationX;
      double y = pma.Results[i].GetPose().TranslationY;
      //角度 =弧度 *180/PI
      //弧度 =角度/180*PI
      //获取pma结果的弧度  转化成角度  
  

    double angle = pma.Results[i].GetPose().Rotation * 180 / Math.PI;
      //角度-90度  转换成弧度 供卡尺工具使用   目的:确定卡尺 投影和搜索方向

    double rad = (angle - 90) / 180 * Math.PI;
      
    
      //给卡尺工具赋值并运行卡尺工具
      cal.Region.CenterX = x;
      cal.Region.CenterY = y;

   //获取卡尺功的旋转度 (弧度)
      cal.Region.Rotation = rad;
      cal.Run();
      
      //给点到点距离工具赋值
      dis.StartX = fc.Results.GetCircle().CenterX;
      dis.StartY = fc.Results.GetCircle().CenterY;
      dis.EndX = cal.Results[0].Edge0.PositionX;
      dis.EndY = cal.Results[0].Edge0.PositionY;
      dis.Run();
      


      //保存距离到距离集合
      distances.Add(dis.Distance);
      
      //创建线段t图形 
      CogLineSegment line = new CogLineSegment();
      
      //设置线段的起点和终点
      line.SetStartEnd(fc.Results.GetCircle().CenterX, fc.Results.GetCircle().CenterY, cal.Results[0].Edge0.PositionX, cal.Results[0].Edge0.PositionY);
      //设置线段颜色
      line.Color = CogColorConstants.Red;
      //添加线段到图形集合中
      col.Add(line);
      
      //创建文本
      CogGraphicLabel label = new CogGraphicLabel();
     // 设置文本的位置  和文本内容
      label.SetXYText(cal.Results[0].Edge0.PositionX, cal.Results[0].Edge0.PositionY, dis.Distance.ToString("0.00"));
      //设置文本颜色
      label.Color = CogColorConstants.Orange;
      //添加文本到图形集合中
      col.Add(label);
     
    }
    
    //筛选最大值和最小值和平均值
    double max = 0;
    double min = distances[0];
    double mean;
    double total = 0;
    //遍历距离集合
    for (int i = 0; i < distances.Count; i++)
    {
      //取最大值
      if (distances[i] > max)
        max = distances[i];
      //取最小值
      if (distances[i] < min)
        min = distances[i];
      //累加所有数值 计算平均数
      total += distances[i];  
    }
    //取平均数
    mean = total / distances.Count;
    
      
    //创建文本 显示最大值 最小值 平均值
    CogGraphicLabel label2 = new CogGraphicLabel();
    label2.SetXYText(200, 150, "最大值:" + max.ToString("0.00"));
    label2.Color = CogColorConstants.Green;
    label2.Font = new Font("宋体", 20);
    CogGraphicLabel label3 = new CogGraphicLabel();
    label3.SetXYText(200, 180, "最小值:" + min.ToString("0.00"));
    label3.Color = CogColorConstants.Green;
    label3.Font = new Font("宋体", 20);
    CogGraphicLabel label4 = new CogGraphicLabel();
    label4.SetXYText(200, 210, "平均值:" + max.ToString("0.00"));
    label4.Color = CogColorConstants.Green;
    label4.Font = new Font("宋体", 20);
    //添加到图形集合
    col.Add(label2);
    col.Add(label3);
    col.Add(label4);
    //把结果给赋值给Block 输出属性
    mToolBlock.Outputs["Max"].Value = max;
    mToolBlock.Outputs["Min"].Value = min;
    mToolBlock.Outputs["Mean"].Value = mean;
    
    return false;
  }


 

  public override void ModifyLastRunRecord(Cognex.VisionPro.ICogRecord lastRecord)
  {
    
    //添加图形到lastRunRecord窗口
    for (int i = 0; i < col.Count; i++)
    {
      mToolBlock.AddGraphicToRunRecord(col[i], lastRecord, "CogPMAlignTool1.InputImage", "script");
    }
  }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值