当前位置: 首页 > article >正文

Visonpro 检测是否有缺齿

一、效果展示

二、上面是原展开工具CogPolarUnwrapTool;

第二种方法: 用Blob 和 CogCopyRegionTool

 

三、 用预处理工具  加减常数,让图片变得更亮点

四、圆展开工具

 

五、模板匹配

六、代码分解

1.创建集合和文子显示工具

CogGraphicCollection dt = new CogGraphicCollection();
  CogGraphicLabel Label;

2.加载工具,并实例化一个List集合

 dt.Clear();
    CogPolarUnwrapTool polar = mToolBlock.Tools["CogPolarUnwrapTool1"]as CogPolarUnwrapTool;
    CogPMAlignTool pma = mToolBlock.Tools["CogPMAlignTool1"]as CogPMAlignTool;
    List<double> listX = new List<double>();

3.定义图标中的坐标吗,添加集合,排序

 double total = 0;
    double x, y;
    for(int i = 0;i < pma.Results.Count;i++)
    {
      listX.Add(pma.Results[i].GetPose().TranslationX);
      total += pma.Results[i].GetPose().TranslationY;
    }
    y = total / pma.Results.Count;
    listX.Sort();

4.进行传参,找出之前的x和y坐标系,并用参数接收是否存在缺失

int a = 0;
    for(int i = 0;i < listX.Count - 1;i++)
    {
     
      if((listX[i + 1] - listX[i]) > 50)
      {
        x = (listX[i + 1] + listX[i]) / 2;
        y = total / pma.Results.Count;
        double lastx,lasty;
        polar.RunParams.GetInputPointFromOutputPoint(polar.InputImage, polar.Region, x, y, out lastx, out lasty);
        dt.Add(Create(lastx, lasty));
        a++;
      }
    }

5.进行判断是否缺失

 Label = new CogGraphicLabel();
    if(a > 0){
      Label.SetXYText(100, 100, "缺失"+a);
      Label.Color = CogColorConstants.Red;
      Label.Font = new Font("楷体", 30);
      dt.Add(Label);
    }else{
      Label.SetXYText(100, 100, "OK");
      Label.Color = CogColorConstants.Green;
      Label.Font = new Font("楷体", 30);
       dt.Add(Label);
    }

6.将上面传入圆展开的x,y坐标,进行创建圆

private CogCircle Create(double x, double y)
  {
    CogCircle co = new CogCircle();
    co.CenterX = x;
    co.CenterY = y;
    co.Radius = 20;
    co.LineWidthInScreenPixels = 6;
    co.Color = CogColorConstants.Red;
    dt.Add(co);
    return co;
    
  }

7.输出

for(int i = 0;i < dt.Count;i++)
    {
      mToolBlock.AddGraphicToRunRecord(dt[i], lastRecord, "CogIPOneImageTool1.InputImage", "");
    }

8.代码All

#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.PMAlign;
#endregion

public class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase
{
  #region Private Member Variables
  private Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock;
  #endregion
  CogGraphicCollection dt = new CogGraphicCollection();
  CogGraphicLabel Label;
  /// <summary>
  /// Called when the parent tool is run.
  /// Add code here to customize or replace the normal run behavior.
  /// </summary>
  /// <param name="message">Sets the Message in the tool's RunStatus.</param>
  /// <param name="result">Sets the Result in the tool's RunStatus</param>
  /// <returns>True if the tool should run normally,
  ///          False if GroupRun customizes run behavior</returns>
  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
    dt.Clear();
    CogPolarUnwrapTool polar = mToolBlock.Tools["CogPolarUnwrapTool1"]as CogPolarUnwrapTool;
    CogPMAlignTool pma = mToolBlock.Tools["CogPMAlignTool1"]as CogPMAlignTool;
    List<double> listX = new List<double>();

    // Run each tool using the RunTool function
    foreach(ICogTool tool in mToolBlock.Tools)
      mToolBlock.RunTool(tool, ref message, ref result);
    double total = 0;
    double x, y;
    for(int i = 0;i < pma.Results.Count;i++)
    {
      listX.Add(pma.Results[i].GetPose().TranslationX);
      total += pma.Results[i].GetPose().TranslationY;
    }
    y = total / pma.Results.Count;
    listX.Sort();
    int a = 0;
    for(int i = 0;i < listX.Count - 1;i++)
    {
     
      if((listX[i + 1] - listX[i]) > 50)
      {
        x = (listX[i + 1] + listX[i]) / 2;
        y = total / pma.Results.Count;
        double lastx,lasty;
        polar.RunParams.GetInputPointFromOutputPoint(polar.InputImage, polar.Region, x, y, out lastx, out lasty);
        dt.Add(Create(lastx, lasty));
        a++;
      }
    }
    Label = new CogGraphicLabel();
    if(a > 0){
      Label.SetXYText(100, 100, "缺失"+a);
      Label.Color = CogColorConstants.Red;
      Label.Font = new Font("楷体", 30);
      dt.Add(Label);
    }else{
      Label.SetXYText(100, 100, "OK");
      Label.Color = CogColorConstants.Green;
      Label.Font = new Font("楷体", 30);
       dt.Add(Label);
    }
    return false;
  }
  private CogCircle Create(double x, double y)
  {
    CogCircle co = new CogCircle();
    co.CenterX = x;
    co.CenterY = y;
    co.Radius = 20;
    co.LineWidthInScreenPixels = 6;
    co.Color = CogColorConstants.Red;
    dt.Add(co);
    return co;
    
  }

  #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 < dt.Count;i++)
    {
      mToolBlock.AddGraphicToRunRecord(dt[i], lastRecord, "CogIPOneImageTool1.InputImage", "");
    }
  }
  #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

}

 

七、polar.RunParams.GetInputPointFromOutputPoint

该方法用于输出图像中的坐标 (x, y) 转换为输入图像中的坐标 (lastx, lasty)

  • polar.InputImage 是输入图像对象
  • polar.Region 可能是指定的处理区域
  • x, y 是输出图像中的坐标
  • out lastx, out lasty 是输出参数,表示转换后的输入图像中的坐标。

​​​​​​​polar.RunParams.GetInputPointFromOutputPoint(

  1. polar.InputImage, // 输入图像

  2. polar.Region, // 处理区域

  3. x, // 输出图像中的X坐标

  4. y, // 输出图像中的Y坐标

  5. out lastx, // 输出:对应的输入图像中的X坐标

  6. out lasty // 输出:对应的输入图像中的Y坐标 );

​​​​​​​注意事项:

  1. 方法的作用:该方法的主要作用是将输出图像中的坐标映射回输入图像中的坐标,通常用于图像变换后的逆向坐标查找。
  2. 参数检查:确保 polar.InputImage 和 polar.Region 已正确初始化,并且 x, y 坐标在有效的范围内。
  3. 返回值lastx 和 lasty 是通过 out 参数返回的,因此调用后可以直接使用这两个变量获取转换后的坐标。

八、第二种方法

1.定义值

2. 工具展示

3. Blob设置

4.CogCopyReigonTool设置

添加终端链接

 

 

 5.CogBlob覆盖后设置

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;
#endregion

public class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase
{
  #region Private Member Variables
  private Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock;
  #endregion
  CogGraphicLabel label = new CogGraphicLabel();

  /// <summary>
  /// Called when the parent tool is run.
  /// Add code here to customize or replace the normal run behavior.
  /// </summary>
  /// <param name="message">Sets the Message in the tool's RunStatus.</param>
  /// <param name="result">Sets the Result in the tool's RunStatus</param>
  /// <returns>True if the tool should run normally,
  ///          False if GroupRun customizes run behavior</returns>
  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
    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);
    
    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;
  }

  #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)
  {
    mToolBlock.AddGraphicToRunRecord(label, 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

}


http://www.kler.cn/a/549664.html

相关文章:

  • 【第1章:深度学习概览——1.4 深度学习的核心组件与概念解析之激活函数的作用与类型】
  • pytorch cnn 实现猫狗分类
  • 【C++】详解 set multiset map multiset 的使用
  • 网络安全讲座
  • Redis 的常见应用场景
  • 【第9章:计算机视觉实战—9.1 目标检测与识别:YOLO、Faster R-CNN等模型的实现与应用】
  • 案例-06.部门管理-根据ID查询
  • ESP32通过MQTT连接阿里云平台实现消息发布与订阅
  • 9. Docker 当中的复杂安装(MySQL主从复制的安装,Redis 的3主3从的安装配置)的详细说明[附加步骤图]
  • Linux(ubuntu)下载ollama速度慢解决办法
  • 今日写题06work(链表)
  • Golang GORM系列:GORM数据库迁移
  • Apache 服务启动失败的故障诊断
  • Leetcode 221-最大正方形
  • 使用css实现镂空效果
  • 小初高各学科教材,PDF电子版下载
  • 数据库-通用数据接口标准
  • mysql系列8—Innodb的undolog
  • MVC模式和MVVM模式
  • 【漫话机器学习系列】092.模型的一致性(Consistency of a Model)