VisionPro 判断圆是不是无限接近圆或存在缺陷

news2024/10/23 5:48:41

项目上可能需要判断圆是否是无限接近圆或者判断圆边缘是否存在缺陷等。
第一种方法:找圆工具和点到点的距离计算圆边缘上的点到圆心距离的最大值和最小值的差值。
在这里插入图片描述

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

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

  /// <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


    // Run each tool using the RunTool function
    foreach(ICogTool tool in mToolBlock.Tools)
      mToolBlock.RunTool(tool, ref message, ref result);
    
    if(gc == null)
      gc = new CogGraphicCollection();
    gc.Clear();
    
    double Min_Distance = 0.0;
    double Max_Distance = 0.0;
    double Minus_Value = 0;
    double Point_CenterD = 0;
    
    //string[] Price_Distance = {};
    
    CogGraphicLabel label6 = new CogGraphicLabel();
    
    CogFindCircleTool FindCircle = mToolBlock.Tools["CogFindCircleTool1"] as CogFindCircleTool;
    CogDistancePointPointTool PointP_Distance = mToolBlock.Tools["CogDistancePointPointTool1"]as CogDistancePointPointTool;
    
    FindCircle.Run();
    PointP_Distance.Run();
    
    double[] SaveDistance = new double[FindCircle.Results.NumPointsFound];
    
    //if(FindCircle.Results.Count > 0)
    {
      System.Windows.Forms.MessageBox.Show("FindCircle.Results.NumPointsFound" + FindCircle.Results.Count.ToString());
      try
      {
        for(int i = 0;i < FindCircle.Results.Count;i++)
        {
          System.Windows.Forms.MessageBox.Show(i.ToString());
          //System.Windows.Forms.MessageBox.Show("FindCircle.Results[i].Used :" + FindCircle.Results[i].Used);
          if(FindCircle.Results[i].Used)//没有找到的点过滤点,不是过滤的红色点
          {
            PointP_Distance.StartX = FindCircle.Results[i].X;
            PointP_Distance.StartY = FindCircle.Results[i].Y;
            PointP_Distance.EndX = FindCircle.Results.GetCircle().CenterX;
            PointP_Distance.EndY = FindCircle.Results.GetCircle().CenterY;
            Point_CenterD = PointP_Distance.Distance;  
            
            //将每个边缘点到圆心的距离存double数组中
            SaveDistance[i] = Point_CenterD;
          }          
        }
        System.Windows.Forms.MessageBox.Show("存储点到圆距离个数" + SaveDistance.Length);
        ArrayList List_SaveDistance = new ArrayList(SaveDistance);
        List_SaveDistance.Sort();
        Min_Distance = Convert.ToDouble(List_SaveDistance[0]);
        Max_Distance = Convert.ToDouble(List_SaveDistance[List_SaveDistance.Count - 1]);
          
        System.Windows.Forms.MessageBox.Show("最大值" + Max_Distance.ToString());
        System.Windows.Forms.MessageBox.Show("最小值" + Min_Distance.ToString());
        Minus_Value = Max_Distance - Min_Distance;
        System.Windows.Forms.MessageBox.Show("真圆度" + Minus_Value.ToString());
      } 
      catch(Exception e)
      {
        MessageBox.Show(e.ToString());
      }
      
      
      System.Windows.Forms.MessageBox.Show("点到圆距离" + Point_CenterD.ToString());
      label6.SetXYText(100, 400, "圆度:" + Minus_Value.ToString("f3"));
      label6.SelectedSpaceName = "#";
      label6.Font = new Font("宋体", 12);
      label6.Color = Cognex.VisionPro.CogColorConstants.Green;
        
      gc.Add(label6);
    }
    mToolBlock.Outputs["Actual_Value"].Value = Minus_Value;

    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)
  {
  }
  #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

}


第二种方法:通过灰度工具:CogHistogramTool1 Blob工具:CogBlobTool1
判断面积大小进行是否存储缺陷
在这里插入图片描述
在这里插入图片描述

#region 真圆度检测
    Histogram.InputImage = Fix.OutputImage;
    Histogram.Run();
    Minus_Blob.InputImage = Fix.OutputImage;
    Minus_Blob.Run();
    
    double Blob_Area = 0;
    Blob_Area = Convert.ToDouble(mToolBlock.Inputs["Input_MinusArea"].Value.ToString());
    
    if(Minus_Blob.Results.GetBlobs().Count > 0)
    {
      if(Minus_Blob.Results.GetBlobs()[0].Area > Blob_Area)
      {
        label6.SetXYText(100, 400, "圆度检测:" + Minus_Blob.Results.GetBlobs()[0].Area.ToString());
        label6.Color = CogColorConstants.Green;
        gc.Add(label6);
        gc.Add(Minus_Blob.Results.GetBlobs()[0].CreateResultGraphics(CogBlobResultGraphicConstants.All));
        mToolBlock.Outputs["MinusArea_Value"].Value = Minus_Blob.Results.GetBlobs()[0].Area.ToString();
      }
      else
      {
        mToolBlock.Outputs["Result"].Value = 1;
        label6.SetXYText(100, 400, "圆度检测NG");
        label6.Color = CogColorConstants.Red;
        mToolBlock.Outputs["MinusArea_Value"].Value = Minus_Blob.Results.GetBlobs()[0].Area.ToString();
        gc.Add(label6);
      }
    }
    #endregion
    

第三方法:通过圆上工具的属性获取点到圆拟合边的距离,获取最大值,大于最大值判定为NG;
在这里插入图片描述

#region 通过提取点到圆边缘距离进行判断
    double[] SaveDistance = new double[FindCircle.Results.Count];
    
    if (FindCircle.Results.Count > 0)
    {
      try
      {
        for (int i = 0; i < FindCircle.Results.Count; i++)
        {
          //System.Windows.Forms.MessageBox.Show(i.ToString());
          //System.Windows.Forms.MessageBox.Show("FindCircle.Results[i].Used :" + FindCircle.Results[i].Used);
          if (FindCircle.Results[i].Used)//没有找到的点过滤点,不是过滤的红色点
          {
            //将每个边缘点到圆心的距离存double数组中
            SaveDistance[i] = FindCircle.Results[i].DistanceToCircle;
          }
        }
        //System.Windows.Forms.MessageBox.Show("存储点到圆距离个数" + SaveDistance.Length);
        ArrayList List_SaveDistance = new ArrayList(SaveDistance);
        List_SaveDistance.Sort();

        //System.Windows.Forms.MessageBox.Show("List_SaveDistance:" + List_SaveDistance.ToString());
        
        int k = 0;
        //for(int k = 0;k < List_SaveDistance.Count;k++)
        if (k < List_SaveDistance.Count)
        {
          do
          {
            Min_Distance = Convert.ToDouble(List_SaveDistance[k]);
            k++;
          } while (Convert.ToDouble(List_SaveDistance[k].ToString()) == 0);
        }
        
        Min_Distance = Convert.ToDouble(List_SaveDistance[k]);
        Max_Distance = Convert.ToDouble(List_SaveDistance[List_SaveDistance.Count - 1]);
        
        System.Windows.Forms.MessageBox.Show("最小值" + Min_Distance.ToString());
        System.Windows.Forms.MessageBox.Show("k" + k);
       

        System.Windows.Forms.MessageBox.Show("最大值" + Max_Distance.ToString());
        
        ArrayList List_MaxStore = new ArrayList();
        
        for(int w = 0;w < List_SaveDistance.Count;w++)
        {
          if(Convert.ToDouble(List_SaveDistance[w].ToString()) > Convert.ToDouble(mToolBlock.Inputs["Input_Distance"].Value.ToString()))
          {
            List_MaxStore.Add(List_SaveDistance[w]);
          }
        }
      
        
        
        System.Windows.Forms.MessageBox.Show("List_MaxStore长度:" + List_MaxStore.Count);

        if (List_MaxStore.Count > 3)//表明大于点到圆上的距离最大值有三个数视为NG
        {
          label6.SetXYText(100, 400, "圆度检测:OK");
          label6.Color = CogColorConstants.Green;
          gc.Add(label6);
          mToolBlock.Outputs["Out_DistanceMax"].Value = Minus_Value.ToString();
        }
        else
        {
          label6.SetXYText(100, 400, "圆度检测:NG");
          label6.Color = CogColorConstants.Red;
          gc.Add(label6);
          mToolBlock.Outputs["Out_DistanceMax"].Value = Minus_Value.ToString();
        }

      }
      catch (Exception e)
      {
        MessageBox.Show(e.ToString());
      }

    }
    else
    {
      mToolBlock.Outputs["Result"].Value = 1;
      label6.SetXYText(100, 400, "找圆NG");
      label6.Color = CogColorConstants.Red;
      gc.Add(label6);
    }
    #endregion

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1498408.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【论文精读】大语言模型融合知识图谱的问答系统研究

&#x1f497;&#x1f497;&#x1f497;欢迎来到我的博客&#xff0c;你将找到有关如何使用技术解决问题的文章&#xff0c;也会找到某个技术的学习路线。无论你是何种职业&#xff0c;我都希望我的博客对你有所帮助。最后不要忘记订阅我的博客以获取最新文章&#xff0c;也欢…

安装sqlserver2022最新版只能使用.\SQLEXPRESS登录数据库怎么修改成.

.\SQLEXPRESS “服务器名称 localhost\SQLEXPRESS”中的 “SQLEXPRESS”就是数据库的实例名称/数据库名/服务器名&#xff0c; “localhost”即登录本计算机安装的数据库 安装sqlserver2022最新版只能使用.\SQLEXPRESS登录数据库怎么修改成. 2、查看SQL Server数据库的实例名…

Myqsort:基于冒泡排序算法的C语言实现

我们将详细介绍一个基于冒泡排序算法的自定义排序函数——Mysqrt。该函数通过使用用户提供的比较函数进行元素间的比较&#xff0c;并结合swap交换函数对任意类型的数据进行排序。下面是对代码的逐行解析。 逻辑导图 代码实现 // 头文件 #include<stdio.h>// 定义比较函…

RK3568平台 USB数据包的收发格式

一.USB硬件拓扑结构 compound device &#xff1a;多个设备组合起来&#xff0c;通过HUB跟Host相连composite device &#xff1a;一个物理设备有多个逻辑设备(multiple interfaces) 在软件开发过程中&#xff0c;我们可以忽略Hub的存在&#xff0c;硬件拓扑图简化如下&#x…

事务失效的八种情况!!!!

一、非publi修饰的方法。 /*** 私有方法上的注解&#xff0c;不生效&#xff08;因私有方法Spring扫描不到该方法&#xff0c;所以无法生成代理&#xff09;*/ Transactional private boolean test() {//test code }二、类内部访问。 类内部非直接访问带注解标记的方法 B&…

php:下拉列表查询(静态数据+数据库数据)

一、在php中嵌套 效果 1、从php中嵌套html语句 下拉列表的显示 echo <div class"text-nav-1 required "><div> . _(在职状态) . :</div> <select name"work_status">; // 定义选项数组 $options [all > _(全部),inwork &g…

新型设备巡检方案-手机云巡检

随着科技的不断发展&#xff0c;设备巡检工作也在逐步向智能化、高效化方向转变。传统的巡检方式往往需要人工逐个设备检查&#xff0c;耗时耗力&#xff0c;效率低下&#xff0c;同时还容易漏检和误检。而新型设备巡检应用—手机蓝牙云巡检的出现&#xff0c;则为设备巡检工作…

基于springboot+vue的高校教师电子名片系统

博主主页&#xff1a;猫头鹰源码 博主简介&#xff1a;Java领域优质创作者、CSDN博客专家、阿里云专家博主、公司架构师、全网粉丝5万、专注Java技术领域和毕业设计项目实战&#xff0c;欢迎高校老师\讲师\同行交流合作 ​主要内容&#xff1a;毕业设计(Javaweb项目|小程序|Pyt…

2024年腾讯云学生服务器活动详细说明、学生机购买流程

2024年腾讯云学生服务器优惠活动「云校园」&#xff0c;学生服务器优惠价格&#xff1a;轻量应用服务器2核2G学生价30元3个月、58元6个月、112元一年&#xff0c;轻量应用服务器4核8G配置191.1元3个月、352.8元6个月、646.8元一年&#xff0c;CVM云服务器2核4G配置842.4元一年&…

2024年最佳硬盘!为台式电脑、NAS等产品量身定做的顶级机械硬盘

机械硬盘&#xff08;HDD&#xff09;可能看起来像是古老的技术&#xff0c;但它们仍然在许多地方提供“足够好”的性能&#xff0c;并且它们很容易以同等的价格提供最多的存储空间。 尽管最好的SSD将为你的操作系统和引导驱动器提供最好的体验&#xff0c;并提供比HDD更好的应…

分布式商业生态战略是数字化时代企业低成本、高效率最新发展方案

企业存在的本质是解决市场难以解决的或即使可以解决也需要耗费大量人力、物力的问题&#xff0c;并以其特有的资源能力降低交易费用&#xff0c;实现盈利。从交易费用角度来说&#xff0c;交易费用决定了企业的存在&#xff0c;企业采取不同的组织方式主要是为了对资源采取不同…

分布式搜索引擎-elasticsearch基础

分布式搜索引擎-elasticsearch基础 1、什么是elasticsearch&#xff1f; elasticsearch是一款非常强大的开源搜索引擎&#xff0c;可以帮助我们从海量数据中快速找到需要的内容。 elasticsearch结合kibana、Logstash、Beats&#xff0c;也就是elastic stack&#xff08;ELK&a…

lanqiao:合根植物

题目描述&#xff1a; 代码实现&#xff1a;

AD20软件使用指南:拼板操作与Gerber文件生成详解

文章目录 一、前言二、拼板1.创建新的PCB&#xff0c;用于放置拼板文件2.放置拼板阵列3.设置阵列信息4.V割拼板&#xff0c;放置工艺边和定位孔和光点5.完成拼板 三、生成Gerber文件1.输出Gerber文件2.选择单位和格式3.选择输出的图层4.生成Gerber文件5.生成钻孔文件 四、上传嘉…

element-plus upload上传

vue3 element-plus 2.4.3 <script> const submitUpload () > {// 单文件proxy.$refs["uploadRef"].submit(); }const uploadFile (options) > {let formData new FormData();formData.append("file", options.file)return uploadFileWith…

好物周刊#43:设计素材下载

https://yuque.com/cunyu1943 村雨遥的好物周刊&#xff0c;记录每周看到的有价值的信息&#xff0c;主要针对计算机领域&#xff0c;每周五发布。 一、项目 1. frp 一个专注于内网穿透的高性能的反向代理应用&#xff0c;支持 TCP、UDP、HTTP、HTTPS 等多种协议&#xff0c…

Git小册-笔记迁移

Git简介 Git是目前世界上最先进的分布式版本控制系统&#xff08;没有之一&#xff09;。 所有的版本控制系统&#xff0c;其实只能跟踪文本文件的改动&#xff0c;比如TXT文件&#xff0c;网页&#xff0c;所有的程序代码等等&#xff0c;Git也不例外。版本控制系统可以告诉…

实用干货!产品经理的进阶秘籍:必备的10项核心技能

在现代商业环境中&#xff0c;产品经理的角色变得愈发关键和多样化。成功的产品经理不仅需要深厚的行业知识&#xff0c;还要具备一系列多面手的技能&#xff0c;以应对不断变化的市场需求和竞争压力。本文将深入探讨产品经理必备的10项核心技能&#xff0c;揭示职场进阶的秘籍…

数字孪生10个技术栈:原型设计,界面从无到有雏形的第一步。

一、什么是原型设计 可视化界面的原型设计是指在设计过程中创建一个低保真或高保真的可视化模型&#xff0c;以展示和演示最终产品的外观、布局和交互。它是设计师和开发团队在实际开发之前用来验证和确认设计方案的一个重要步骤。 可视化界面的原型设计主要包括以下几个方面&…

晶圆上特性表征

测试仪器&#xff1a; 半导体器件表征系统&#xff08;DC&CV&#xff09;&#xff1a;Keysight B1500A 半导体器件分析仪&#xff08;B1500A&#xff09;测量能力&#xff1a; 1.IV、CV、脉冲/动态IV范围为0.1 fA-1 A/0.5 uV-200 V 2.器件、材料、半导体、有源/无源元件的…