目标:显示当前页面的所有颜色数量
结果:
一、控件设置
1、CogToolBlock
因为图片不只有一张,并且保险丝的数量和位置也不确定,所以需要编写脚本,通过CogPMAlignTool定位,然后将定位的XY坐标传输给CogCompositeColorMatchTool,最后通过脚本,遍历出所有的保险丝位置,然后判断颜色
外部只写一个控件,就是CogToolBlock
接下来进入CogToolBlock,开始配置其他控件
所有控件:
2、CogImageCovertTool
因为CogPMAlignTool只能处理灰度图,所以通过CogImageCovertTool转换为灰度图后,再将转换后的图传入CogPMAlignTool,进行位置判断。直接连线即可,不需要打开,自动转换
3、CogPMAlignTool
双击进入此控件
4、CogCompositeColorMatchTool
双击进入控件
二、代码设置
1、基础配置
Region.(Cognex.VisionPro.CogRectangleAffine).CenterX
Region.(Cognex.VisionPro.CogRectangleAffine).CenterY
Region.(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 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> /// //全局变量:一个用于管理 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 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) { //输出 //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 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 }