齿轮个数检测,点码检测

齿轮个数检测,点码检测_第1张图片

齿轮个数检测,点码检测_第2张图片

齿轮个数检测,点码检测_第3张图片

齿轮个数检测,点码检测_第4张图片

 齿轮个数检测,点码检测_第5张图片

齿轮个数检测,点码检测_第6张图片

#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.中心原点坐标 并没有放在物体中心位置   后续定位坐标  要以此做为原点坐标

齿轮个数检测,点码检测_第7张图片

齿轮个数检测,点码检测_第8张图片

齿轮个数检测,点码检测_第9张图片

齿轮个数检测,点码检测_第10张图片

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

你可能感兴趣的:(计算机视觉)