CogPMAligMultiTool 工具使用-发卡识别
需求识别下列发卡的形状工具使用1.脚本灰度值转换的引用2.添加转灰度值脚本CogImageConvertTool image new CogImageConvertTool(); image.InputImage mToolBlock.Inputs[InputImage].Value as ICogImage; image.RunParams.RunMode CogImageConvertRunModeConstants.Intensity; image.Run();3.将转换后图像赋值CogPMAlignMultiTool pma mToolBlock.Tools[CogPMAlignMultiTool1]as CogPMAlignMultiTool; pma.InputImage image.OutputImage; pma.Run();4.CogPMAlignMultiTool 工具实操5.创建label集合和类private CogGraphicCollection dt new CogGraphicCollection(); dt.Clear(); private CogGraphicLabel createlabel(string text, float size, double x, double y, CogColorConstants color) { CogGraphicLabel label new CogGraphicLabel(); label.Alignment CogGraphicLabelAlignmentConstants.TopLeft; label.BackgroundColor CogColorConstants.White; label.Color color; label.Font new Font(Arial, size, FontStyle.Bold, GraphicsUnit.Pixel); label.SetXYText(x, y, text); return label; }6.遍历CogPMAlignMultiTool 并赋值string type ; for(int i 0;i pma.Results.ResultItemCollection.Count;i) { switch(pma.Results.ResultItemCollection[i].Name) { case青蛙: type 青蛙; break; case笑脸: type 笑脸; break; case爱心: type 爱心; break; } dt.Add(createlabel(类型是: type, 25, 10, 10, CogColorConstants.Blue)); dt.Add(createlabel(DateTime.Now.ToString(yyyy-MM-dd HH:mm:ss), 25, 10, 90, CogColorConstants.Green)); }7.赋值展示图片foreach(ICogGraphic s in dt) { mToolBlock.AddGraphicToRunRecord(s, lastRecord, CogPMAlignMultiTool1.InputImage, script); }8.脚本All#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.PMAlign; using Cognex.VisionPro.ImageProcessing; #endregion public class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase { #region Private Member Variables private Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock; #endregion private CogGraphicCollection dt new CogGraphicCollection(); /// 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 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 dt.Clear(); CogImageConvertTool image new CogImageConvertTool(); image.InputImage mToolBlock.Inputs[InputImage].Value as ICogImage; image.RunParams.RunMode CogImageConvertRunModeConstants.Intensity; image.Run(); CogPMAlignMultiTool pma mToolBlock.Tools[CogPMAlignMultiTool1]as CogPMAlignMultiTool; pma.InputImage image.OutputImage; pma.Run(); // Run each tool using the RunTool function foreach(ICogTool tool in mToolBlock.Tools) mToolBlock.RunTool(tool, ref message, ref result); string type ; for(int i 0;i pma.Results.ResultItemCollection.Count;i) { switch(pma.Results.ResultItemCollection[i].Name) { case青蛙: type 青蛙; break; case笑脸: type 笑脸; break; case爱心: type 爱心; break; } dt.Add(createlabel(类型是: type, 25, 10, 10, CogColorConstants.Blue)); dt.Add(createlabel(DateTime.Now.ToString(yyyy-MM-dd HH:mm:ss), 25, 10, 90, CogColorConstants.Green)); } return false; } private CogGraphicLabel createlabel(string text, float size, double x, double y, CogColorConstants color) { CogGraphicLabel label new CogGraphicLabel(); label.Alignment CogGraphicLabelAlignmentConstants.TopLeft; label.BackgroundColor CogColorConstants.White; label.Color color; label.Font new Font(Arial, size, FontStyle.Bold, GraphicsUnit.Pixel); label.SetXYText(x, y, text); return label; } #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) { foreach(ICogGraphic s in dt) { mToolBlock.AddGraphicToRunRecord(s, lastRecord, CogPMAlignMultiTool1.InputImage, script); } } #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 }效果展示