机器视觉 齿轮检测

 案例:齿轮内径检测

1.使用模板匹配

2.设置匹配区域

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

81a46164049a40cf86e694350acf1298.png

1.添加找圆工具

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

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

0e3064385a054d7298d7c82375b625a3.png

 

 

RunParams.ExpectedCircularArc.CenterX

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

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

8a92fd4b31524dfd995fa774229d7eb1.png

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

文本    CogGraphicLabel label

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

 

 

 

 

 

案例:齿轮圆形到齿轮角距离检测

 

866cba3d2c454006a4e13a6cf35c9a98.png

 

09ae8cb771ee4f2caf32ea96a094474a.png

2144a8f94ac545a2ab76f99c1e929574.png 46e3a0239c4243be9b6feddf11275f21.png

 

2.  模板匹配中心原点坐标赋值给   Region.centerX  Region.centerY   卡尺工具的仿射矩形中心点坐标   

4a633a5e13534bfbac30a880681ed06a.png

 

 

Region.centerX  Region.centerY   卡尺工具的仿射矩形中心点坐标 

5269d56a2a7d49d0868a80adb4b8b97b.png

f06b9701fc5c4bfcac815a65eb5a49d9.png

1.

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

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

6234713a50274102a211771fd4d9e0e7.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);
      
      //创建线段
      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");
    }
  }

 

 

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

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

(04730)电路分析基础之正弦交流电路(一)

正弦交流电概述 我们在前面已讨论了直流电路的分析&#xff0c;在直流电路中电压或电流的大小和方向都是不随时间而变化的&#xff1b;但在交流电路中&#xff0c;电压或电流的大小和方向都在随时间而变化&#xff0c;其变化规律多种多样&#xff0c;应用得最普遍的是按正弦规…

全志H6-ARMLinux第1天:全志概述、刷机登陆、官方外设库、蜂鸣器、超声波测距

1. 全志H616课程概述&#xff08;456.01&#xff09; 1.1 为什么学 学习目标依然是Linux系统&#xff0c;平台是ARM架构 蜂巢快递柜&#xff0c;配送机器人&#xff0c;这些应用场景用 C51、STM32 单片机无法实现第三方介入库的局限性&#xff0c;比如刷脸支付和公交车收费设…

#HarmonyOS:装饰器UI描述---@Link

装饰器 装饰器&#xff08;Decorator&#xff09;是一种语法结构&#xff0c;用来在定义时修改类&#xff08;class&#xff09;的行为。 在语法上&#xff0c;装饰器有如下几个特征。 第一个是字符&#xff08;或者说前缀&#xff09;是&#xff0c;后面是一个表达式后面的…

vscode eide arm-gcc 编译环境搭建调试

安装cube&#xff0c;vscode 1.安装vscode插件 C/C Extension Pack Chinese (Simplified) (简体中文) Language Pack Cortex-Debug Embedded IDE 工具链设置 2.软件工程生成 调试 3.生成工程&#xff0c;导入工程 4. 配置工程 编译完毕

【LeetCode刷题-树】-- 99.恢复二叉树

99.恢复二叉树 方法&#xff1a; 对二叉搜索树进行中序遍历得到值序列不满足的位置找到对应被错误交换的节点记为x和y交换x和y两个节点 /*** Definition for a binary tree node.* public class TreeNode {* int val;* TreeNode left;* TreeNode right;* Tre…

一拎即走的轻薄云台投影,极米投影仪Z7X解锁观影新姿势

近年来&#xff0c;随着投影技术的不断提高以及大屏幕带来的加倍快乐&#xff0c;让投影仪成为了一种新的观影潮流。尤其是“去客厅化”的大背景下&#xff0c;年轻人几乎将目光都投向了投影仪&#xff0c;从而实现在家就能享受大屏观影的效果和体验。那么备受当下年轻消费者青…

仿照MyBatis手写一个持久层框架学习

首先数据准备&#xff0c;创建MySQL数据库mybatis&#xff0c;创建表并插入数据。 DROP TABLE IF EXISTS user_t; CREATE TABLE user_t ( id INT PRIMARY KEY, username VARCHAR ( 128 ) ); INSERT INTO user_t VALUES(1,Tom); INSERT INTO user_t VALUES(2,Jerry);JDBC API允…

【LeetCode热题100】【滑动窗口】找到字符串中所有字母异位词

给定两个字符串 s 和 p&#xff0c;找到 s 中所有 p 的 异位词 的子串&#xff0c;返回这些子串的起始索引。不考虑答案输出的顺序。 异位词 指由相同字母重排列形成的字符串&#xff08;包括相同的字符串&#xff09;。 示例 1: 输入: s "cbaebabacd", p "…

【C++】:红黑树

朋友们、伙计们&#xff0c;我们又见面了&#xff0c;本期来给大家解读一下有关多态的知识点&#xff0c;如果看完之后对你有一定的启发&#xff0c;那么请留下你的三连&#xff0c;祝大家心想事成&#xff01; C 语 言 专 栏&#xff1a;C语言&#xff1a;从入门到精通 数据结…

【SpringBoot教程】SpringBoot 创建定时任务(配合数据库动态执行)

作者简介&#xff1a;大家好&#xff0c;我是撸代码的羊驼&#xff0c;前阿里巴巴架构师&#xff0c;现某互联网公司CTO 联系v&#xff1a;sulny_ann&#xff08;17362204968&#xff09;&#xff0c;加我进群&#xff0c;大家一起学习&#xff0c;一起进步&#xff0c;一起对抗…

unity学习笔记19

一、角色动画的使用练习 从资源商店导入的动画资源&#xff08;Character Pack: Free Sample&#xff09;中将资源中的角色创建在场景里&#xff0c;现在场景里存在的角色并没有任何动画。 在资源中找到Animations文件夹&#xff0c;在这个文件有很多模型文件&#xff08;.FBX…

基于Java SSM框架实现个性化影片推荐系统项目【项目源码+论文说明】

基于java的SSM框架实现个性化影片推荐系统演示 摘要 随着科学技术的飞速发展&#xff0c;社会的方方面面、各行各业都在努力与现代的先进技术接轨&#xff0c;通过科技手段来提高自身的优势&#xff0c;个性化影片推荐系统当然也不能排除在外。个性化影片推荐系统是以实际运用…