机器视觉16

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

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

c67d44fa9ccc2c10efaff435248a9bb7.png

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

bcffe0415e10c0fa2fc35748ecc6bd2c.png

 1.提取效果图

9b070baffdad547aef83177e35dc30fd.png

 1.blob工具b463fafba1498f487ed7f90d9fba9a86.png

55853ea7df3d7bfbb3f4b7615ea4885d.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.设置匹配区域

2ebdd7f6bb3e44cc809986a6e1a5b353.png

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

cb701f4e6bda748ac48ff77960a85d1d.png

1.添加找圆工具

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

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

6ffdb20a02776a2900744f602426c473.png

RunParams.ExpectedCircularArc.CenterX

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

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

8f8e88e36dd3005d217dca6881f50b1c.png

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

文本    CogGraphicLabel label

2b3846afaadbea13580af8fa27d156a1.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:齿轮圆形到齿轮角距离检测 求出最大值最小值 平均值

d40392a5a5e3431289ef65b4e82a26df.png

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

e6b1d61c9420a4df9244608643a145f7.png

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

93719f9b661c05e0ef0b4f348fad2e3a.png

1.模板匹配单个齿轮角

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

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

4822498b0e2762d2cdd93a0a7924375d.png

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

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

8aeff627333c6544c903c7566f550ade.png

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

ae85b5a55d88052e6263de43852459fc.png

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

86237aedff0b1220a4f9f8cb12416eac.png

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

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

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

ad58f7692b41b68938b30dede099b11a.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");
    }
  }

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值