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

VisionPro:轴承错位标识

找出轴承缺陷并标记:效果

1.打开导入图片

2.添加CogToolblock:方便后续写代码

3.对零件进行模板匹配

4.对图片进行预处理(重点)

5.添加找圆工具和展开工具

根据下图将线链接

6.添加模板匹配工具并运行

7.训练模板

好的全找出来

8.代码编写

红色框为添加的代码(下面有完整代码)

添加代码完成

#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;
using Cognex.VisionPro.CalibFix;
using Cognex.VisionPro.Caliper;
using Cognex.VisionPro.PixelMap;
#endregionpublic class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase
{#region Private Member Variablesprivate Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock;#endregionCogGraphicCollection col = new CogGraphicCollection();/// <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// 清空集合col.Clear();CogPMAlignTool pma2 = mToolBlock.Tools["CogPMAlignTool2"] as CogPMAlignTool;CogPolarUnwrapTool polar = mToolBlock.Tools["CogPolarUnwrapTool1"] as CogPolarUnwrapTool;//声明集合存储X坐标List<double> listX = new List<double> ();// Run each tool using the RunTool functionforeach(ICogTool tool in mToolBlock.Tools)mToolBlock.RunTool(tool, ref message, ref result);double y=0,yTotal = 0;for (int i = 0; i < pma2.Results.Count; i++){//给集合添加X坐标listX.Add(pma2.Results[i].GetPose().TranslationX);yTotal += pma2.Results[i].GetPose().TranslationY;}y = yTotal / pma2.Results.Count;//排序  升序listX.Sort();// 计算中间NG点信息for (int i = 0; i < listX.Count - 1; i++){if (listX[i + 1] - listX[i] > 60){double x = (listX[i + 1] + listX[i]) / 2;double xIn,yIn;//从输出图像得坐标获得输入图像得坐标polar.RunParams.GetInputPointFromOutputPoint(polar.InputImage, polar.Region, x, y, out xIn, out yIn);col.Add(CreateCircle(xIn, yIn));}}//判断最后一位坐标位置if (listX[listX.Count - 1] < 750 ){double x = listX[listX.Count - 1] + 40;double xIn,yIn;//从输出图像得坐标获得输入图像得坐标polar.RunParams.GetInputPointFromOutputPoint(polar.InputImage, polar.Region, x, y, out xIn, out yIn);col.Add(CreateCircle(xIn, yIn));}//判断最后一位坐标位置if (listX[0] > 40 ){double x = listX[1] - 40;double xIn,yIn;//从输出图像得坐标获得输入图像得坐标polar.RunParams.GetInputPointFromOutputPoint(polar.InputImage, polar.Region, x, y, out xIn, out yIn);col.Add(CreateCircle(xIn, yIn));}return false;}/// <summary>/// 创建圆形标签/// </summary>/// <param name="x">X坐标</param>/// <param name="y">Y坐标</param>/// <returns>返回一个圆</returns>private CogCircle CreateCircle(double x, double y){CogCircle c = new CogCircle();c.CenterX = x;c.CenterY = y;c.Radius = 5;c.LineWidthInScreenPixels = 5;c.Color = CogColorConstants.Red;return c;}#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){foreach (ICogGraphic g in col){mToolBlock.AddGraphicToRunRecord(g, lastRecord, "CogIPOneImageTool1.InputImage", "script");//mToolBlock.AddGraphicToRunRecord(g, lastRecord, "CogPolarUnwrapTool1.OutputImage", "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 REMOVEbase.Initialize(host);// Store a local copy of the script hostthis.mToolBlock = ((Cognex.VisionPro.ToolBlock.CogToolBlock)(host));}#endregion}

完成

http://www.xdnf.cn/news/7368.html

相关文章:

  • QT之绘图模块和双缓冲技术
  • MapStruct Date 转 LocalDate 偏差一天问题
  • 【C++】异常解析
  • AGI大模型(28):LangChain提示模板
  • MySQL中的Change Buffer是什么,它有什么作用?
  • 火山 RTC 引擎9 ----集成 appkey
  • 5月19日笔记
  • 4.7 时间模块
  • 网络传输(ping命令,wget命令,curl命令),端口
  • 接口排查不能靠猜:实战中如何用抓包工具精准定位问题(含 Charles 使用示例)
  • 人工智能、机器学习与深度学习:全面介绍与对比分析
  • 组态王通过开疆智能profinet转ModbusTCP网关连接西门子PLC配置案例
  • 队列和栈的区别
  • 【科研项目】大三保研人科研经历提升
  • Windows多功能工具箱软件推荐
  • gcc: attribute: packed
  • 区块链blog2_中心化与效率
  • 将 Element UI 表格拖动功能提取为公共方法
  • SpringBoot(二)--- SpringBoot基础(http协议、分层解耦)
  • HGDB中如何为表增加自增主键
  • 如何映射 MongoDB 的 _id 字段?
  • Java大厂面试实战:Spring Boot与微服务场景中的技术点解析
  • 4.6 sys模块
  • 线程池中任务队列满-如何把多余的任务存储到Redis中
  • python打卡第30天
  • 体育赛事直播App的架构设计与关键技术解析
  • JavaScript面试题之原型链详解
  • 多商户1.8.1版本前端问题优化集合指南
  • python:pymysql概念、基本操作和注入问题讲解
  • 嵌入式学习--江协51单片机day8