1.创建,保存toolblock工具。
1.打开新的作业
2.将toolblock加入到toolgroup中。
3.双击CogToolBlock1,添加工具流,此工具用于找blob和圆,并添加output的结果
4.创建高级脚本,找图片中的所有圆,先找到blob,然后将blob的中心点作为找圆工具预期圆弧中心。
#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.Blob; using Cognex.VisionPro.Caliper; using System.Collections.Generic; using Cognex.VisionPro.ImageProcessing; #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> private List<CogCircle> circles = new List<CogCircle>();//保存找到的所有圆 double mean_radius = 0; //求平均圆半径 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 mean_radius = 0; //初始化平均半径 double total_radius = 0; //记录所有圆的总半径 CogCircle mycircle = new CogCircle(); //新建一个圆对象 // Run each tool using the RunTool function foreach(ICogTool tool in mToolBlock.Tools) //运行一遍工具 mToolBlock.RunTool(tool, ref message, ref result); CogBlobTool blob = mToolBlock.Tools["CogBlobTool1"] as CogBlobTool; //blob工具 CogFindCircleTool findCircle = mToolBlock.Tools["CogFindCircleTool1"] as CogFindCircleTool; //找圆工具 CogBlobResultCollection blobresult = blob.Results.GetBlobs(); //blobTool找到的所有blob if(blobresult.Count > 0) { foreach(CogBlobResult item in blobresult) //遍历每个blob,将中心传给找圆工具的期望圆弧中心 { findCircle.RunParams.ExpectedCircularArc.CenterX = item.CenterOfMassX; findCircle.RunParams.ExpectedCircularArc.CenterY = item.CenterOfMassY; mToolBlock.RunTool(findCircle, ref message, ref result); mycircle = findCircle.Results.GetCircle(); circles.Add(mycircle); total_radius += mycircle.Radius; } } mean_radius = total_radius / blobresult.Count; this.mToolBlock.Outputs["m_meanRadius"].Value = mean_radius; //作为输出参数 this.mToolBlock.Outputs["m_total"].Value = total_radius; //作为输出参数 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) { foreach(CogCircle circle in circles) //遍历每个圆,画在CogImageConvertTool.OutputImage上。 { circle.Color=CogColorConstants.Green; circle.LineWidthInScreenPixels = 3; mToolBlock.AddGraphicToRunRecord(circle, lastRecord, "CogImageConvertTool1.OutputImage", "script"); } CogGraphicLabel label = new CogGraphicLabel(); //写label string labelStr = string.Format("Radius Mean : {0:F2}", mean_radius); label.SetXYText(400, 50, labelStr); label.Color = CogColorConstants.Red; //label.LineWidthInScreenPixels = 10; // label.DragLineWidthInScreenPixels = 10; mToolBlock.AddGraphicToRunRecord(label, lastRecord, "CogImageConvertTool1.OutputImage", "script"); circles.Clear(); } #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 }
5.保存toolblock,文件名findCircle.vpp
6.如果要重新编辑toolblock,需要新建一个toolgroup并加入toolblock然后打开之前的toolblock。
c#中使用toolblock的结果
1 CogToolBlock cogToolBlock = new CogToolBlock(); 2 cogToolBlock = CogSerializer.LoadObjectFromFile("Resources/visionpro_vpp/findCircle.vpp") as CogToolBlock; 3 cogToolBlock.Inputs[0].Value = new CogImage8Grey(redMarking.ViewImage(flag).Bitmap); 4 cogToolBlock.Run(); 5 CogRecordsDisplay recordDisplay = new CogRecordsDisplay(); 6 ICogRecord record = cogToolBlock.CreateLastRunRecord().SubRecords["CogImageConvertTool1.OutputImage"]; 7 recordDisplay.Subject = record; 8 //double a=(double)cogToolBlock.Outputs[0].Value; 9 System.Drawing.Image img = recordDisplay.Display.CreateContentBitmap(Cognex.VisionPro.Display.CogDisplayContentBitmapConstants.Image); 10 //System.Drawing.Image img = recordDisplay.CreateContentBitmap(Cognex.VisionPro.Display.CogDisplayContentBitmapConstants.Display); 11 CogImage24PlanarColor display_iamge = new CogImage24PlanarColor(new Bitmap(img)); 12 cogDisplay_measure.Image = display_iamge; 13 cogDisplay_measure.Fit(true);