主页 > 手机  > 

Visonpro检测是否有缺齿

Visonpro检测是否有缺齿
一、效果展示

二、上面是原展开工具CogPolarUnwrapTool; 第二种方法: 用Blob 和 CogCopyRegionTool

 

三、 用预处理工具  加减常数,让图片变得更亮点

四、圆展开工具   五、模板匹配

六、代码分解 1.创建集合和文子显示工具 CogGraphicCollection dt = new CogGraphicCollection(); CogGraphicLabel Label; 2.加载工具,并实例化一个List集合 dt.Clear(); CogPolarUnwrapTool polar = mToolBlock.Tools["CogPolarUnwrapTool1"]as CogPolarUnwrapTool; CogPMAlignTool pma = mToolBlock.Tools["CogPMAlignTool1"]as CogPMAlignTool; List<double> listX = new List<double>(); 3.定义图标中的坐标吗,添加集合,排序 double total = 0; double x, y; for(int i = 0;i < pma.Results.Count;i++) { listX.Add(pma.Results[i].GetPose().TranslationX); total += pma.Results[i].GetPose().TranslationY; } y = total / pma.Results.Count; listX.Sort(); 4.进行传参,找出之前的x和y坐标系,并用参数接收是否存在缺失 int a = 0; for(int i = 0;i < listX.Count - 1;i++) { if((listX[i + 1] - listX[i]) > 50) { x = (listX[i + 1] + listX[i]) / 2; y = total / pma.Results.Count; double lastx,lasty; polar.RunParams.GetInputPointFromOutputPoint(polar.InputImage, polar.Region, x, y, out lastx, out lasty); dt.Add(Create(lastx, lasty)); a++; } } 5.进行判断是否缺失 Label = new CogGraphicLabel(); if(a > 0){ Label.SetXYText(100, 100, "缺失"+a); Label.Color = CogColorConstants.Red; Label.Font = new Font("楷体", 30); dt.Add(Label); }else{ Label.SetXYText(100, 100, "OK"); Label.Color = CogColorConstants.Green; Label.Font = new Font("楷体", 30); dt.Add(Label); } 6.将上面传入圆展开的x,y坐标,进行创建圆 private CogCircle Create(double x, double y) { CogCircle co = new CogCircle(); co.CenterX = x; co.CenterY = y; co.Radius = 20; co.LineWidthInScreenPixels = 6; co.Color = CogColorConstants.Red; dt.Add(co); return co; } 7.输出 for(int i = 0;i < dt.Count;i++) { mToolBlock.AddGraphicToRunRecord(dt[i], lastRecord, "CogIPOneImageTool1.InputImage", ""); } 8.代码All #region namespace imports using System; using System.Collections; using System.Collections.Generic; 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; #endregion public class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase { #region Private Member Variables private Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock; #endregion CogGraphicCollection dt = new CogGraphicCollection(); CogGraphicLabel Label; /// <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 DEBUG // if (System.Diagnostics.Debugger.IsAttached) System.Diagnostics.Debugger.Break(); // #endif dt.Clear(); CogPolarUnwrapTool polar = mToolBlock.Tools["CogPolarUnwrapTool1"]as CogPolarUnwrapTool; CogPMAlignTool pma = mToolBlock.Tools["CogPMAlignTool1"]as CogPMAlignTool; List<double> listX = new List<double>(); // Run each tool using the RunTool function foreach(ICogTool tool in mToolBlock.Tools) mToolBlock.RunTool(tool, ref message, ref result); double total = 0; double x, y; for(int i = 0;i < pma.Results.Count;i++) { listX.Add(pma.Results[i].GetPose().TranslationX); total += pma.Results[i].GetPose().TranslationY; } y = total / pma.Results.Count; listX.Sort(); int a = 0; for(int i = 0;i < listX.Count - 1;i++) { if((listX[i + 1] - listX[i]) > 50) { x = (listX[i + 1] + listX[i]) / 2; y = total / pma.Results.Count; double lastx,lasty; polar.RunParams.GetInputPointFromOutputPoint(polar.InputImage, polar.Region, x, y, out lastx, out lasty); dt.Add(Create(lastx, lasty)); a++; } } Label = new CogGraphicLabel(); if(a > 0){ Label.SetXYText(100, 100, "缺失"+a); Label.Color = CogColorConstants.Red; Label.Font = new Font("楷体", 30); dt.Add(Label); }else{ Label.SetXYText(100, 100, "OK"); Label.Color = CogColorConstants.Green; Label.Font = new Font("楷体", 30); dt.Add(Label); } return false; } private CogCircle Create(double x, double y) { CogCircle co = new CogCircle(); co.CenterX = x; co.CenterY = y; co.Radius = 20; co.LineWidthInScreenPixels = 6; co.Color = CogColorConstants.Red; dt.Add(co); return co; } #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) { for(int i = 0;i < dt.Count;i++) { mToolBlock.AddGraphicToRunRecord(dt[i], lastRecord, "CogIPOneImageTool1.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 }   七、polar.RunParams.GetInputPointFromOutputPoint 该方法用于输出图像中的坐标 (x, y) 转换为输入图像中的坐标 (lastx, lasty) polar.InputImage 是输入图像对象polar.Region 可能是指定的处理区域x, y 是输出图像中的坐标out lastx, out lasty 是输出参数,表示转换后的输入图像中的坐标。 ​​​​​​​polar.RunParams.GetInputPointFromOutputPoint( polar.InputImage, // 输入图像 polar.Region, // 处理区域 x, // 输出图像中的X坐标 y, // 输出图像中的Y坐标 out lastx, // 输出:对应的输入图像中的X坐标 out lasty // 输出:对应的输入图像中的Y坐标 ); ​​​​​​​注意事项: 方法的作用:该方法的主要作用是将输出图像中的坐标映射回输入图像中的坐标,通常用于图像变换后的逆向坐标查找。参数检查:确保 polar.InputImage 和 polar.Region 已正确初始化,并且 x, y 坐标在有效的范围内。返回值:lastx 和 lasty 是通过 out 参数返回的,因此调用后可以直接使用这两个变量获取转换后的坐标。 八、第二种方法 1.定义值

2. 工具展示

3. Blob设置

4.CogCopyReigonTool设置 添加终端链接  

 

 5.CogBlob覆盖后设置

6.代码 #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.Blob; #endregion public class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase { #region Private Member Variables private Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock; #endregion CogGraphicLabel label = new CogGraphicLabel(); /// <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 DEBUG // if (System.Diagnostics.Debugger.IsAttached) System.Diagnostics.Debugger.Break(); // #endif int count = (int) mToolBlock.Inputs["Count"].Value; CogBlobTool blob = mToolBlock.Tools["CogBlobTool2"]as CogBlobTool; // Run each tool using the RunTool function foreach(ICogTool tool in mToolBlock.Tools) mToolBlock.RunTool(tool, ref message, ref result); int currentCount = blob.Results.GetBlobs().Count; int diff = count - currentCount; string res = ""; label.Color = CogColorConstants.Green; label.Font = new Font("宋体", 20); if (diff > 0) { res = "缺失:" + diff; label.Color = CogColorConstants.Red; } else { res = "良品"; } label.SetXYText(100, 250, res); 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) { mToolBlock.AddGraphicToRunRecord(label, lastRecord, "CogIPOneImageTool1.InputImage", "Script"); } #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 }

标签:

Visonpro检测是否有缺齿由讯客互联手机栏目发布,感谢您对讯客互联的认可,以及对我们原创作品以及文章的青睐,非常欢迎各位朋友分享到个人网站或者朋友圈,但转载请说明文章出处“Visonpro检测是否有缺齿