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

visionpro脚本

visionproToolBlock的脚本的优先级优于工具连线的优先级,一般是照着脚本的执行顺序进行执行对应的工具,最常用的是C#的高级脚本,C#的脚本如下分为5部分。

第一部分:主要是一些库的引用,对于有些类型不知道库的时候,可以通过查询帮助文档输入对应的名称,查询到具体信息,其中就包含他所在的库;

第二部分:主要是一些全局变量的声明,包括标签工具、一些数据类型的声明等等,这里还声明了一个ToolBlock工具,用于该Block的工具的定义、声明、获取Record等用途;

第三部分:主要对基类中的函数进行重写;

第四部分:主要对当前的Record进行操作;

第五部分:主要对整个ToolBlock中的工具运行完成之后的最终结果进行操作,主要包括了标签操作、循环运行时中间结果的保留并附着在最终结果上等。

本片文章主要是进行两个ToolBlock的实现,包含计算两个点之间的距离,寻找某个特征点的坐标。

ToolBlock1

ToolBlock1主要是进行三角形的一个顶点到正方形顶点的距离测量,检测效果如下:

对于该任务,首先需要对三角形进行特征匹配,特征匹配完成之后,根据特征匹配建立一个新的坐标系,该坐标系+之前所建立的坐标系可以完成该图旋转,放大和缩小后的对应特征检测,再根据在这两个坐标系下的定位,找到对应矩形的顶点,完成距离的检测,对应的工作流程如图所示:

该过程基本没有连线操作,主要是通过脚本实现对应的功能,脚本代码如下:

#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.CalibFix;
using Cognex.VisionPro.PMAlign;
using Cognex.VisionPro.Caliper;
using Cognex.VisionPro.Dimensioning;
#endregionpublic class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase
{#region Private Member Variablesprivate Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock;CogGraphicLabel mylabel = new CogGraphicLabel();#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)try{CogPMAlignTool cpmatool = mToolBlock.Tools["CogPMAlignTool1"] as CogPMAlignTool;cpmatool.InputImage = (CogImage8Grey) mToolBlock.Inputs["OutputImage"].Value;cpmatool.Run();if(cpmatool.Results.Count != 1){mToolBlock.Outputs["strerr"].Value = "block1 cpmatool fail";return false;}CogFixtureTool cfixtool2 = mToolBlock.Tools["CogFixtureTool2"] as CogFixtureTool;cfixtool2.RunParams.UnfixturedFromFixturedTransform = cpmatool.Results[0].GetPose();cfixtool2.InputImage = cpmatool.InputImage;cfixtool2.Run();if(cfixtool2.InputImage == null){mToolBlock.Outputs["strerr"].Value = "block1 cfixtool2 fail";return false;}//找点1CogFindLineTool cfindltool1 = mToolBlock.Tools["CogFindLineTool1"] as CogFindLineTool;cfindltool1.InputImage = (CogImage8Grey) cpmatool.InputImage;cfindltool1.Run();if(cfindltool1.RunStatus.Result != CogToolResultConstants.Accept){mToolBlock.Outputs["strerr"].Value = "block1 cfindltool1 fail";return false;}CogFindLineTool cfindltool2 = mToolBlock.Tools["CogFindLineTool2"] as CogFindLineTool;cfindltool2.InputImage = (CogImage8Grey) cpmatool.InputImage;cfindltool2.Run();if(cfindltool2.RunStatus.Result != CogToolResultConstants.Accept){mToolBlock.Outputs["strerr"].Value = "block1 cfindltool2 fail";return false;}CogIntersectLineLineTool cinterlinetool1 = mToolBlock.Tools["CogIntersectLineLineTool1"] as CogIntersectLineLineTool;cinterlinetool1.InputImage = (CogImage8Grey) cpmatool.InputImage;cinterlinetool1.LineA = cfindltool1.Results.GetLine();cinterlinetool1.LineB = cfindltool2.Results.GetLine();cinterlinetool1.Run();if(cinterlinetool1.RunStatus.Result != CogToolResultConstants.Accept){mToolBlock.Outputs["strerr"].Value = "block1 cinterlinetool1 fail";return false;}//寻找点2CogFindLineTool cfindltool3 = mToolBlock.Tools["CogFindLineTool3"] as CogFindLineTool;cfindltool3.InputImage = (CogImage8Grey) cpmatool.InputImage;cfindltool3.Run();if(cfindltool3.RunStatus.Result != CogToolResultConstants.Accept){mToolBlock.Outputs["strerr"].Value = "block1 cfindltool3 fail";return false;}CogFindLineTool cfindltool4 = mToolBlock.Tools["CogFindLineTool4"] as CogFindLineTool;cfindltool4.InputImage = (CogImage8Grey) cpmatool.InputImage;cfindltool4.Run();if(cfindltool4.RunStatus.Result != CogToolResultConstants.Accept){mToolBlock.Outputs["strerr"].Value = "block1 cfindltool4 fail";return false;}CogIntersectLineLineTool cinterlinetool2 = mToolBlock.Tools["CogIntersectLineLineTool2"] as CogIntersectLineLineTool;cinterlinetool2.InputImage = (CogImage8Grey) cpmatool.InputImage;cinterlinetool2.LineA = cfindltool3.Results.GetLine();cinterlinetool2.LineB = cfindltool4.Results.GetLine();cinterlinetool2.Run();if(cinterlinetool2.RunStatus.Result != CogToolResultConstants.Accept){mToolBlock.Outputs["strerr"].Value = "block1 cinterlinetool2 fail";return false;}//计算点与点之间的距离CogDistancePointPointTool cdistpptool = mToolBlock.Tools["CogDistancePointPointTool1"] as CogDistancePointPointTool;cdistpptool.InputImage = (CogImage8Grey) cpmatool.InputImage;cdistpptool.StartX = cinterlinetool1.X;cdistpptool.StartY = cinterlinetool1.Y;cdistpptool.EndX = cinterlinetool2.X;cdistpptool.EndY = cinterlinetool2.Y;cdistpptool.Run();if(cdistpptool.RunStatus.Result != CogToolResultConstants.Accept){mToolBlock.Outputs["strerr"].Value = "block1 cdistpptool fail";return false;}mToolBlock.Outputs["strerr"].Value = "success";mToolBlock.Outputs["distance"].Value = cdistpptool.Distance;mylabel.SetXYText(50, 20, "宽度" + cdistpptool.Distance.ToString("F2"));mylabel.Color = CogColorConstants.Cyan;mylabel.Font = new Font("楷体", 15);}catch(Exception e){mToolBlock.Outputs["strerr"].Value = "fail";}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(mylabel,lastRecord,"CogPMAlignTool1.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 REMOVEbase.Initialize(host);// Store a local copy of the script hostthis.mToolBlock = ((Cognex.VisionPro.ToolBlock.CogToolBlock)(host));}#endregion}

 对应工具的声明和他所有的成员均可以通过在外面的工具,添加终端中寻找,他所有的成员方法均在此地方能找到。

 ToolBlock2

ToolBlock2实现的主要功能是对三个字符‘R’的左下角进行定位,包含了特征匹配,找线,找交点等操作。结果如下图所示:

对于该任务首先需要特征匹配,匹配完成之后,找线,找交点,对于工具连线操作而言,是无法进行循环操作的,所以此时就必须使用脚本进行操作,才能将三个点均找出。工具和代码如下所示:

脚本代码如下所示:

#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.CalibFix;
using Cognex.VisionPro.PMAlign;
using Cognex.VisionPro.Caliper;
using Cognex.VisionPro.Dimensioning;
using System.Collections.Generic;
#endregionpublic class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase
{#region Private Member Variablesprivate Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock;CogGraphicLabel mylabel = new CogGraphicLabel();private List<ICogRecord> records = new List<ICogRecord>();#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();// #endifmToolBlock.Outputs["strerr"].Value = "";string str = "";try{CogPMAlignTool cpmatool = mToolBlock.Tools["CogPMAlignTool1"] as CogPMAlignTool;cpmatool.InputImage = (ICogImage) mToolBlock.Inputs["InputImage"].Value;cpmatool.Run();if(cpmatool.Results.Count != 3){mToolBlock.Outputs["strerr"].Value = "block2 cpmatool fail";return false;}records.Clear();for(int i = 0;i < cpmatool.Results.Count;i++){CogFixtureTool cfixtool2 = mToolBlock.Tools["CogFixtureTool2"] as CogFixtureTool;cfixtool2.RunParams.UnfixturedFromFixturedTransform = cpmatool.Results[i].GetPose();cfixtool2.InputImage = cpmatool.InputImage;cfixtool2.Run();if(cfixtool2.InputImage == null){mToolBlock.Outputs["strerr"].Value = "block2 cfixtool2 fail";return false;}CogFindLineTool cfindltool1 = mToolBlock.Tools["CogFindLineTool1"] as CogFindLineTool;cfindltool1.InputImage = (CogImage8Grey) cpmatool.InputImage;cfindltool1.Run();if(cfindltool1.RunStatus.Result != CogToolResultConstants.Accept){mToolBlock.Outputs["strerr"].Value = "block2 cfindltool1 fail";return false;}CogFindLineTool cfindltool2 = mToolBlock.Tools["CogFindLineTool2"] as CogFindLineTool;cfindltool2.InputImage = (CogImage8Grey) cpmatool.InputImage;cfindltool2.Run();if(cfindltool2.RunStatus.Result != CogToolResultConstants.Accept){mToolBlock.Outputs["strerr"].Value = "block2 cfindltool2 fail";return false;}CogIntersectLineLineTool cinterlinetool1 = mToolBlock.Tools["CogIntersectLineLineTool1"] as CogIntersectLineLineTool;cinterlinetool1.InputImage = cpmatool.InputImage;cinterlinetool1.LineA = cfindltool1.Results.GetLine();cinterlinetool1.LineB = cfindltool2.Results.GetLine();cinterlinetool1.Run();if(cinterlinetool1.Intersects == false){mToolBlock.Outputs["strerr"].Value = "block2 cinterlinetool1 fail";return false;}PointF p1 = (new PointF(float.Parse(cinterlinetool1.X.ToString("F2")), float.Parse(cinterlinetool1.Y.ToString("F2"))));str += p1.ToString();mToolBlock.Outputs["strerr"].Value = "success";records.Add(cinterlinetool1.CreateLastRunRecord());}mToolBlock.Outputs["respoint"].Value = str;mylabel.SetXYText(50, 5, "坐标" + str);mylabel.Color = CogColorConstants.Black;mylabel.Font = new Font("楷体", 10);}catch(Exception e){str = "Exception: {e.Message}";mToolBlock.Outputs["strerr"].Value = str;}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){     lastRecord.SubRecords[0].SubRecords.Clear();for(int i = 0;i < 3;i++){lastRecord.SubRecords[0].SubRecords.Add(records[i]);}mToolBlock.AddGraphicToRunRecord(mylabel, lastRecord, "CogPMAlignTool1.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 REMOVEbase.Initialize(host);// Store a local copy of the script hostthis.mToolBlock = ((Cognex.VisionPro.ToolBlock.CogToolBlock)(host));}#endregion}

 循环主要是针对特征匹配到的R的数量进行循环次数操作的,这里需要注意,因为visionpro在进行操作的时候,会将之前找到的交点的痕迹覆盖掉,因此需要将中间过程的Record记录下来,因此在一开始需要声明 private List<ICogRecord> records = new List<ICogRecord>()对中间过程的Record记录,添加到列表的操作:records.Add(cinterlinetool1.CreateLastRunRecord()),然后需要在最后lastrunrecord上记录:

public override void ModifyLastRunRecord(Cognex.VisionPro.ICogRecord lastRecord){     lastRecord.SubRecords[0].SubRecords.Clear();for(int i = 0;i < 3;i++){lastRecord.SubRecords[0].SubRecords.Add(records[i]);}mToolBlock.AddGraphicToRunRecord(mylabel, lastRecord, "CogPMAlignTool1.InputImage", " ");}

注意这里的SubRecoRecordrds[i]的编号表示需要在第几个工具结果上添加记录下来的Record,还应该注意标签放置操作应该在这个操作之后,在这个操作之前会被clear掉。

OuterBlock

有时候可能一个工程文件中有多个Block模块,此时需要在外层套一个Block,将所有子Block包含,并且可能不同的Block对应不同的照片进行执行,为了便于在最外层查看lastrunrecord,一般将所有子Block的第一个CogFixtureTool挪动到OuterBlock中,再将其OutputImage传入到各个子Block中,如下所示:

 为了更好的控制某个Block的执行,一般需要在外面添加参数并且通过脚本进行控制实现:

#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.CalibFix;
#endregionpublic class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase
{#region Private Member Variablesprivate Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock;#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);CogFixtureTool cfixtool = mToolBlock.Tools["CogFixtureTool1"] as CogFixtureTool;cfixtool.InputImage = (CogImage8Grey) mToolBlock.Inputs["OutputImage"].Value;cfixtool.Run();if(cfixtool.InputImage == null){mToolBlock.Outputs["outtoolblock"].Value = "outtoolblock cfixtool fail ";return false;}CogToolBlock block1 = mToolBlock.Tools["CogToolBlock1"] as CogToolBlock;CogToolBlock block2 = mToolBlock.Tools["CogToolBlock2"] as CogToolBlock;if(cfixtool.OutputImage.Width == 640){block1.Run();}else{block2.Run();}mToolBlock.Outputs["outtoolblock"].Value = block1.Outputs["strerr"].Value.ToString() + block2.Outputs["strerr"].Value.ToString();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 REMOVEbase.Initialize(host);// Store a local copy of the script hostthis.mToolBlock = ((Cognex.VisionPro.ToolBlock.CogToolBlock)(host));}#endregion}

 最后就能实现在距离计算图片输入的时候,执行Block1,找字符R的时候,执行Block2,同时旁边的图片均能显示。

 

 


http://www.mrgr.cn/news/31027.html

相关文章:

  • 物联网技术及其在智慧城市中的应用
  • 13、DHCP和FTP协议
  • Maven 中央仓库地址 mvnrepository.com
  • LeetCode 216-组合总数Ⅲ
  • 酒店叮咚门铃的类型有哪些
  • kafka面试题解答(四)
  • MySQL 数据库备份与恢复指南
  • 使用反向代理软件frp在局域网内共享上网
  • GAMES202 作业1
  • Acwing Hash表
  • docker 升级步骤
  • Nginx配置虚拟主机
  • 【计算机网络】传输层协议UDP
  • 【Elasticsearch】-图片向量化存储
  • 个人小结(2.0)
  • vue2中字符串动态拼接字段给到接口
  • Android 增加宏开关控制android.bp
  • 推荐五种msvcr71.dll丢失的解决方法,msvcr71.dll为什么会丢失?
  • 使用canal.deployer-1.1.7和canal.adapter-1.1.7实现mysql数据同步
  • 数据库的char 和 varchar 有什么不同
  • java(2)方法的使用
  • ubuntu下使用qt编译QOCI(libqsqloci.so)驱动详解及测试
  • 三维手势 handpose 3D RGB 手势3D建模 三维建模-手势舞 >> DataBall
  • VSCode开发ros程序无法智能提示的解决方法(一)
  • 苹果M4 MacBook Air被曝2025Q1发布 屏幕面板10月出货
  • 实例讲解电动汽车钥匙Start上下电控制策略及Simulink建模方法