ARTICLE DETAIL

资讯详情

深耕郑州网站建设与运营推广的一线实战洞察。

颜色工具(Color)和复杂脚本基础

颜色工具(Color)和复杂脚本基础 实例1将这个图片里面的保险丝颜色分别出来并标注。最终样式用到了四个工具:CogToolBlock:可以包括其他工具并在里面写代码CogImageConvertTool:将颜色变为灰白CogPMAlignTool:图形选取工具CogCompositeColorMatchTool:分别颜色其中CogCompositeColorMatchTool 输入中默认没有Region,需要自己手动调出1.CogCompositeColorMatchTool中的区域→区域形状一般和CogPMAlignTool中一样2.添加终端里面的Region中的CenterX/Y和Rotation(其中Rotation是旋转的值)3.代码其中大部分为CogToolBlock脚本自带的需要写入会在旁边写中文标注#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 //添加一个label方便显示文本 CogGraphicLabel label new CogGraphicLabel(); public override bool GroupRun(ref string message, ref CogToolResultConstants result) { #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; //调用CogPMAlignTool1方便使用 CogPMAlignTool pma mToolBlock.Tools[CogPMAlignTool1] as CogPMAlignTool; //调用CogCompositeColorMatchTool方便使用 CogCompositeColorMatchTool colorMatch mToolBlock.Tools[CogCompositeColorMatchTool1] as CogCompositeColorMatchTool; //创建CogRectangleAffine并将colorMatch.Region赋值给他 CogRectangleAffine region colorMatch.Region as CogRectangleAffine; //循环pma.Results里面的值并赋给item CogPMAlignResul类型 foreach(CogPMAlignResult item in pma.Results) { //将坐标赋值给region region.CenterX item.GetPose().TranslationX; region.CenterY item.GetPose().TranslationY; region.Rotation item.GetPose().Rotation; //重新运行一下colorMatch colorMatch.Run(); //拿出来colorMatch string colorName colorMatch.Result.ResultOfBestMatch.Color.Name; //判断颜色这边使用的是外面CogCompositeColorMatchTool1设置的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); //确定labelXY坐标和文本 label.SetXYText(240, 30, colorString); //label颜色 label.Color CogColorConstants.DarkRed; //确认字体 label.Font new Font(楷体, 20); return false; } #region When the Current Run Record is Created 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是绘制的对象看需求可以更改lastRecord指定记录类型一般都是用这个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 }
返回列表