机器视觉21

案例1:齿轮个数检测 

6cc2e9695e430d2f0dfc59000784b93e.png

9aa39fff44498d986d510379f7cec078.png

26092275e8f9a6dc1b03deb1cb0dc878.png

752246f5094548d2446288601107a314.png

 f7bb767a26ddde43e8f77524c8a7474c.png

1f93f31c63b2e9ea04e320bcc7e0df26.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.ImageProcessing;
using Cognex.VisionPro.Blob;
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)
  {
 
 
    //注释掉系统自动执行工具foreach逻辑
//    foreach(ICogTool tool in mToolBlock.Tools)
//      mToolBlock.RunTool(tool, ref message, ref result);
    
    //映射CogIPOneImageTool
    CogIPOneImageTool ipOne = mToolBlock.Tools["CogIPOneImageTool1"] as CogIPOneImageTool;
    //映射CogSobelEdgeTool
    CogSobelEdgeTool sobel = mToolBlock.Tools["CogSobelEdgeTool1"] as CogSobelEdgeTool;
    //映射CogBlobTool
    CogBlobTool blob1 = mToolBlock.Tools["CogBlobTool1"] as CogBlobTool;
    //映射CogDistancePointPointTool
    CogDistancePointPointTool dis = mToolBlock.Tools["CogDistancePointPointTool1"] as CogDistancePointPointTool;
    //映射CogCopyRegionTool
    CogCopyRegionTool copyTool = mToolBlock.Tools["CogCopyRegionTool1"] as CogCopyRegionTool;
    //映射 CogBlobTool
    CogBlobTool blob2 = mToolBlock.Tools["CogBlobTool2"] as CogBlobTool;
   
    //清空图形集合
    col.Clear();
    //1.运行CogIPOneImageTool
    ipOne.Run();
    //2.运行sobel工具
    sobel.Run();
    //3.运行blob1工具 得到每一个齿轮的轮廓
    blob1.Run();
    //4.遍历blob结果  
    for (int i = 0; i < blob1.Results.GetBlobs().Count; i++)
    {
      
      //得到每一个blob的中心点坐标
      double startX = blob1.Results.GetBlobs()[i].CenterOfMassX;
      double startY = blob1.Results.GetBlobs()[i].CenterOfMassY;
      
      //声明一个多边形用来接收blob检测出的轮廓
      CogPolygon p = new CogPolygon();
      p = blob1.Results.GetBlobs()[i].GetBoundary();
      
      //声明集合存储Distance工具的结果数据
      ArrayList list = new ArrayList();
      list.Clear();
     
      //遍历polygon的所有顶点  测量圆心到顶点的距离 并添加到集合,
      //通过计算点到点的距离,求所有距离的平均值 来得出cogpyregion 复制区域的一个半径
      for (int j = 0; j < p.NumVertices; j++)
      {
        //获取顶点坐标
        double endX = p.GetVertexX(j);
        double endY = p.GetVertexY(j);
        //把齿轮圆心坐标
        dis.StartX = startX;
        dis.StartY = startY;
        //polygon的所有顶点坐标
        dis.EndX = endX;
        dis.EndY = endY;
        //运行距离测量工具
        dis.Run();
        //添加结果到集合
        list.Add(dis.Distance);
      }
      
   
      //求距离平均值,该值设置为copyregiontool的region半径
      double mean = 0,total = 0;
      for (int k = 0; k < list.Count; k++)
      {
        total = total + (double) list[k];
      }
      mean = total / list.Count;
      
      //创建圆形Region,并设置属性
      CogCircle c = new CogCircle();
      c.CenterX = startX;
      c.CenterY = startY;
      c.Radius = mean;
      
      //为CopyRegionTool的Region属性赋值
      copyTool.Region = c;
      //运行CopyRegion工具,得到一张掩膜后的新图像
      copyTool.Run();
      
      //图像传递给blob2工具,同时blob2的检测区域可以使用blob11检测的边界区域
      blob2.Region = p;
      //运行blob2工具  获取结果
      blob2.Run();
      
      //设置label
      CogGraphicLabel label = new CogGraphicLabel();
      label.Color = CogColorConstants.Green;
      label.SetXYText(startX,
        startY,
        "齿数:" + blob2.Results.GetBlobs().Count);
      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");
    }
  } 

 案例2 点码检测

1.模板匹配工具1  为了给 FixTure 提供定位坐标   

2.选择框的位置 是整幅图中 图案内容基本没有变化的区域  容易定位

3.中心原点坐标 并没有放在物体中心位置   后续定位坐标  要以此做为原点坐标

1b1c2766f8bb67eb99a66f42afdde594.png

87d8681205645986654efd09d3a9e5a9.png

4236d693074b1c859cdb2c9b3588ff0c.png

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

public class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase
{
  #region Private Member Variables
  private Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock;
  #endregion
  //声明类型CogGraphicLabel的不可变数组
  private CogGraphicLabel[] mLabel;
  //声明  CogPMAlignTool
  private CogPMAlignTool mpma2;

  public override bool GroupRun(ref string message, ref CogToolResultConstants result)
  {
    
    //初始化CogGraphicLabel的不可变数组
    mLabel = new CogGraphicLabel[16];
    // 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


    // Run each tool using the RunTool function
    foreach(ICogTool tool in mToolBlock.Tools)
      mToolBlock.RunTool(tool, ref message, ref result);
    //映射mpa
    mpma2 = mToolBlock.Tools["CogPMAlignTool2"]as CogPMAlignTool;
    
    //根据mpa2结果循环
    for (int i = 0; i < mpma2.Results.Count; i++)
    {
      //初始化label 放入数组中
      mLabel[i] = new CogGraphicLabel();
      mLabel[i].Color = CogColorConstants.Red;
      mLabel[i].Font = new Font("宋体", 16);
      //获取每个匹配结果的 坐标
      double x = mpma2.Results[i].GetPose().TranslationX;
      double y = mpma2.Results[i].GetPose().TranslationY;
      //根据坐标来匹配当前点码位置 创建对应label
      if (y<-600)
      {
        if(x>600)
        {
          mLabel[i].SetXYText(x, y, "1");
        }
        else if(x > 450 && x < 600)
        {
          mLabel[i].SetXYText(x, y, "2");
        }
        else if(x < 450 && x > 300)
        {
          mLabel[i].SetXYText(x, y, "3");
        }
        else
        {
          mLabel[i].SetXYText(x, y, "4");
        }
      }//if (y-y0>-350)
      else if (y<-450&&y>-600)
      {
        if(x > 600)
        {
          mLabel[i].SetXYText(x, y, "5");
        }
        else if(x > 450 && x < 600)
        {
          mLabel[i].SetXYText(x, y, "6");
        }
        else if(x < 450 && x > 300)
        {
          mLabel[i].SetXYText(x, y, "7");
        }
        else
        {
          mLabel[i].SetXYText(x, y, "8");
        }
      }//y-y0<-350&&y-y0>-500
      else if(y<-300&&y>-450)
      {
        if(x > 600)
        {
          mLabel[i].SetXYText(x, y, "9");
        }
        else if(x > 450 && x < 600)
        {
          mLabel[i].SetXYText(x, y, "10");
        }
        else if(x < 450 && x > 300)
        {
          mLabel[i].SetXYText(x, y, "11");
        }
        else
        {
          mLabel[i].SetXYText(x, y, "12");
        }
      }//y-y0<-500&&y-y0>-650
      else
      {
        if(x > 600)
        {
          mLabel[i].SetXYText(x, y, "13");
        }
        else if(x > 450 && x < 600)
        {
          mLabel[i].SetXYText(x, y, "14");
        }
        else if(x < 450 && x > 300)
        {
          mLabel[i].SetXYText(x, y, "15");
        }
        else
        {
          mLabel[i].SetXYText(x, y, "16");
        }
      }//else y-y0<-650
        
      
    }
    

    return false;
  }

 public override void ModifyLastRunRecord(Cognex.VisionPro.ICogRecord lastRecord)
  {
    for (int i = 0; i < mpma2.Results.Count; i++)
    {
      mToolBlock.AddGraphicToRunRecord(mLabel[i], lastRecord, "CogFixtureTool1.OutputImage", "script");
    }
  } 

 案例3:划痕检测  并描边

 3.划痕内部出现许多黑色斑点  后续使用cogIPoneImage调整d260827c7099ba949c56e25f7b3439d3.png

1.cogIPoneImage工具

2.添加灰度形态调整

3.设置关闭      使划痕中的黑色区域变小  让划痕内部连通

7cffe4257ea2f2f6a514aa983008f043.png

 1.Blob检测出划痕

6dd74f1870ba63b94cef8c853daa1c03.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.ImageProcessing;
using Cognex.VisionPro.Blob;
using Cognex.VisionPro.Dimensioning;
#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)
  {
   
   
    col = new CogGraphicCollection();
    // 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

    CogBlobTool mBlob = 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 < mBlob.Results.GetBlobs().Count; i++)
    {
      CogPolygon p = new CogPolygon();
      p = mBlob.Results.GetBlobs()[i].GetBoundary();
      p.Color = CogColorConstants.Red;
      col.Add(p);
    }

    return false;
  }

 public override void ModifyLastRunRecord(Cognex.VisionPro.ICogRecord lastRecord)
  {
    foreach (ICogGraphic  g in col)
    {
      mToolBlock.AddGraphicToRunRecord( g, lastRecord, "CogSobelEdgeTool1.InputImage", "script");
    }
    
  } 

 案例4:磁极崩边检测

1.灰度图转换工具

15ac9c35c6fa0dce20cf414e309263e4.png

1.模板匹配

847d97d61009d9e4794a92044291804b.png 1.FixTure定位工具4d5fd250d233b40823d1ee9bf776bb5d.png

 1.CogIPoneImage工具 图像预处理

2.添加中值Nxm 设定内核高度和宽度 9x9

89344d6b0ddc00987c7b67c251d286e2.png

1.添加灰阶形态Nxm 设置内核高度宽度 7X7

e7619a319252a294885a675dd9206041.png

1.CogAffineTransformTool工具 图像处理工具     作用:消除仿射矩形的旋转和倾斜的影响

2.选择FixTure空间坐标

036c8f77b1fc2748712c915ddb452b72.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值