项目上可能需要判断圆是否是无限接近圆或者判断圆边缘是否存在缺陷等。
第一种方法:找圆工具和点到点的距离计算圆边缘上的点到圆心距离的最大值和最小值的差值。
#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.Caliper;
using Cognex.VisionPro.Dimensioning;
#endregionpublic class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase
{#region Private Member Variablesprivate Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock;private CogGraphicCollection gc;#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>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 DEBUGif (System.Diagnostics.Debugger.IsAttached) System.Diagnostics.Debugger.Break();#endif// Run each tool using the RunTool functionforeach(ICogTool tool in mToolBlock.Tools)mToolBlock.RunTool(tool, ref message, ref result);if(gc == null)gc = new CogGraphicCollection();gc.Clear();double Min_Distance = 0.0;double Max_Distance = 0.0;double Minus_Value = 0;double Point_CenterD = 0;//string[] Price_Distance = {};CogGraphicLabel label6 = new CogGraphicLabel();CogFindCircleTool FindCircle = mToolBlock.Tools["CogFindCircleTool1"] as CogFindCircleTool;CogDistancePointPointTool PointP_Distance = mToolBlock.Tools["CogDistancePointPointTool1"]as CogDistancePointPointTool;FindCircle.Run();PointP_Distance.Run();double[] SaveDistance = new double[FindCircle.Results.NumPointsFound];//if(FindCircle.Results.Count > 0){System.Windows.Forms.MessageBox.Show("FindCircle.Results.NumPointsFound" + FindCircle.Results.Count.ToString());try{for(int i = 0;i < FindCircle.Results.Count;i++){System.Windows.Forms.MessageBox.Show(i.ToString());//System.Windows.Forms.MessageBox.Show("FindCircle.Results[i].Used :" + FindCircle.Results[i].Used);if(FindCircle.Results[i].Used)//没有找到的点过滤点,不是过滤的红色点{PointP_Distance.StartX = FindCircle.Results[i].X;PointP_Distance.StartY = FindCircle.Results[i].Y;PointP_Distance.EndX = FindCircle.Results.GetCircle().CenterX;PointP_Distance.EndY = FindCircle.Results.GetCircle().CenterY;Point_CenterD = PointP_Distance.Distance; //将每个边缘点到圆心的距离存double数组中SaveDistance[i] = Point_CenterD;} }System.Windows.Forms.MessageBox.Show("存储点到圆距离个数" + SaveDistance.Length);ArrayList List_SaveDistance = new ArrayList(SaveDistance);List_SaveDistance.Sort();Min_Distance = Convert.ToDouble(List_SaveDistance[0]);Max_Distance = Convert.ToDouble(List_SaveDistance[List_SaveDistance.Count - 1]);System.Windows.Forms.MessageBox.Show("最大值" + Max_Distance.ToString());System.Windows.Forms.MessageBox.Show("最小值" + Min_Distance.ToString());Minus_Value = Max_Distance - Min_Distance;System.Windows.Forms.MessageBox.Show("真圆度" + Minus_Value.ToString());} catch(Exception e){MessageBox.Show(e.ToString());}System.Windows.Forms.MessageBox.Show("点到圆距离" + Point_CenterD.ToString());label6.SetXYText(100, 400, "圆度:" + Minus_Value.ToString("f3"));label6.SelectedSpaceName = "#";label6.Font = new Font("宋体", 12);label6.Color = Cognex.VisionPro.CogColorConstants.Green;gc.Add(label6);}mToolBlock.Outputs["Actual_Value"].Value = Minus_Value;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){}#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 REMOVEbase.Initialize(host);// Store a local copy of the script hostthis.mToolBlock = ((Cognex.VisionPro.ToolBlock.CogToolBlock) (host));}#endregion}
第二种方法:通过灰度工具:CogHistogramTool1 Blob工具:CogBlobTool1
判断面积大小进行是否存储缺陷
#region 真圆度检测Histogram.InputImage = Fix.OutputImage;Histogram.Run();Minus_Blob.InputImage = Fix.OutputImage;Minus_Blob.Run();double Blob_Area = 0;Blob_Area = Convert.ToDouble(mToolBlock.Inputs["Input_MinusArea"].Value.ToString());if(Minus_Blob.Results.GetBlobs().Count > 0){if(Minus_Blob.Results.GetBlobs()[0].Area > Blob_Area){label6.SetXYText(100, 400, "圆度检测:" + Minus_Blob.Results.GetBlobs()[0].Area.ToString());label6.Color = CogColorConstants.Green;gc.Add(label6);gc.Add(Minus_Blob.Results.GetBlobs()[0].CreateResultGraphics(CogBlobResultGraphicConstants.All));mToolBlock.Outputs["MinusArea_Value"].Value = Minus_Blob.Results.GetBlobs()[0].Area.ToString();}else{mToolBlock.Outputs["Result"].Value = 1;label6.SetXYText(100, 400, "圆度检测NG");label6.Color = CogColorConstants.Red;mToolBlock.Outputs["MinusArea_Value"].Value = Minus_Blob.Results.GetBlobs()[0].Area.ToString();gc.Add(label6);}}#endregion
第三方法:通过圆上工具的属性获取点到圆拟合边的距离,获取最大值,大于最大值判定为NG;
#region 通过提取点到圆边缘距离进行判断double[] SaveDistance = new double[FindCircle.Results.Count];if (FindCircle.Results.Count > 0){try{for (int i = 0; i < FindCircle.Results.Count; i++){//System.Windows.Forms.MessageBox.Show(i.ToString());//System.Windows.Forms.MessageBox.Show("FindCircle.Results[i].Used :" + FindCircle.Results[i].Used);if (FindCircle.Results[i].Used)//没有找到的点过滤点,不是过滤的红色点{//将每个边缘点到圆心的距离存double数组中SaveDistance[i] = FindCircle.Results[i].DistanceToCircle;}}//System.Windows.Forms.MessageBox.Show("存储点到圆距离个数" + SaveDistance.Length);ArrayList List_SaveDistance = new ArrayList(SaveDistance);List_SaveDistance.Sort();//System.Windows.Forms.MessageBox.Show("List_SaveDistance:" + List_SaveDistance.ToString());int k = 0;//for(int k = 0;k < List_SaveDistance.Count;k++)if (k < List_SaveDistance.Count){do{Min_Distance = Convert.ToDouble(List_SaveDistance[k]);k++;} while (Convert.ToDouble(List_SaveDistance[k].ToString()) == 0);}Min_Distance = Convert.ToDouble(List_SaveDistance[k]);Max_Distance = Convert.ToDouble(List_SaveDistance[List_SaveDistance.Count - 1]);System.Windows.Forms.MessageBox.Show("最小值" + Min_Distance.ToString());System.Windows.Forms.MessageBox.Show("k" + k);System.Windows.Forms.MessageBox.Show("最大值" + Max_Distance.ToString());ArrayList List_MaxStore = new ArrayList();for(int w = 0;w < List_SaveDistance.Count;w++){if(Convert.ToDouble(List_SaveDistance[w].ToString()) > Convert.ToDouble(mToolBlock.Inputs["Input_Distance"].Value.ToString())){List_MaxStore.Add(List_SaveDistance[w]);}}System.Windows.Forms.MessageBox.Show("List_MaxStore长度:" + List_MaxStore.Count);if (List_MaxStore.Count > 3)//表明大于点到圆上的距离最大值有三个数视为NG{label6.SetXYText(100, 400, "圆度检测:OK");label6.Color = CogColorConstants.Green;gc.Add(label6);mToolBlock.Outputs["Out_DistanceMax"].Value = Minus_Value.ToString();}else{label6.SetXYText(100, 400, "圆度检测:NG");label6.Color = CogColorConstants.Red;gc.Add(label6);mToolBlock.Outputs["Out_DistanceMax"].Value = Minus_Value.ToString();}}catch (Exception e){MessageBox.Show(e.ToString());}}else{mToolBlock.Outputs["Result"].Value = 1;label6.SetXYText(100, 400, "找圆NG");label6.Color = CogColorConstants.Red;gc.Add(label6);}#endregion