行业资讯
📅 2026/9/6 3:59:39
VisionPro-保险丝颜色识别-高级脚本练习
目标显示当前页面的所有颜色数量结果一、控件设置1、CogToolBlock因为图片不只有一张并且保险丝的数量和位置也不确定所以需要编写脚本通过CogPMAlignTool定位然后将定位的XY坐标传输给CogCompositeColorMatchTool最后通过脚本遍历出所有的保险丝位置然后判断颜色外部只写一个控件就是CogToolBlock接下来进入CogToolBlock开始配置其他控件所有控件2、CogImageCovertTool因为CogPMAlignTool只能处理灰度图所以通过CogImageCovertTool转换为灰度图后再将转换后的图传入CogPMAlignTool进行位置判断。直接连线即可不需要打开自动转换3、CogPMAlignTool双击进入此控件4、CogCompositeColorMatchTool双击进入控件二、代码设置1、基础配置Region.(Cognex.VisionPro.CogRectangleAffine).CenterXRegion.(Cognex.VisionPro.CogRectangleAffine).CenterYRegion.(Cognex.VisionPro.CogRectangleAffine).Rotation上面这三个仅作演示不需要连接或添加终端使用默认的CogCompositeColorMatchTool1工具即可2、代码#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.PMAlign; using Cognex.VisionPro.CompositeColorMatch; #endregion public class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase { #region Private Member Variables private 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 namemessageSets the Message in the tools RunStatus./param /// param nameresultSets the Result in the tools RunStatus/param /// returnsTrue if the tool should run normally, /// False if GroupRun customizes run behavior/returns /// //全局变量:一个用于管理 VisionPro 图形对象ICogGraphic的集合可以同时存储标签、圆、矩形、多边形等各种图形 CogGraphicLabel label new CogGraphicLabel(); 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); //定义五个颜色变量 int red 0; int yellow 0; int green 0; int blue 0; int orange 0; //获取PMAlign对象 CogPMAlignTool pma mToolBlock.Tools[CogPMAlignTool1] as CogPMAlignTool; //获取CompositeColorMatch对象 CogCompositeColorMatchTool colorMatch mToolBlock.Tools[CogCompositeColorMatchTool1] as CogCompositeColorMatchTool; //获取CompositeColorMatch的Region对象 CogRectangleAffine region colorMatch.Region as CogRectangleAffine; //根据pma找到的数量进入循环 foreach(CogPMAlignResult item in pma.Results) { //移动CompositeColorMatch的中心点 region.CenterX item.GetPose().TranslationX; region.CenterY item.GetPose().TranslationY; region.Rotation item.GetPose().Rotation; //旋转角度 //完成移动后直接运行 colorMatch.Run(); //获取运行后的最高匹配度的颜色名字 string colorName colorMatch.Result.ResultOfBestMatch.Color.Name; //根据名字进行操作 if (colorName red) { red; } else if(colorName yellow) { yellow; } else if(colorName blue) { blue; } else if(colorName orange) { orange; } else if(colorName green) { green; } } //创建最终要输出的字符串 String colorString String.Format(蓝色:{0},绿色{1},橙色:{2},红色:{3},黄色:{4}, blue, green, orange, red, yellow); //指定label的输出位置和内容 label.SetXYText(240, 30, colorString); //修改颜色 label.Color CogColorConstants.DarkRed; //修改字体和大小 label.Font new Font(楷体, 20); 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 namecurrentRecord /// 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 namelastRecord /// The new last run record is available to be initialized or customized./param public override void ModifyLastRunRecord(Cognex.VisionPro.ICogRecord lastRecord) { //输出 //label输出内容 //CogImageConvertTool1.InputImage输出到那张图中 //日志 mToolBlock.AddGraphicToRunRecord(label, lastRecord, CogImageConvertTool1.InputImage, ); } #endregion #region When the Script is Initialized /// summary /// Perform any initialization required by your script here /// /summary /// param namehostThe 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 }3、对象对照图三、结果