当前位置:   article > 正文

VisionPro学习案例3-结合C#脚本循环检测_visionpro做检测

visionpro做检测

目录

前言

一、检测要求

二、步骤

1.在CogToolBlock工具里添加检测工具

2.找圆

 3.Bolb分析

 4.结果处理

 5.卡尺找齿顶边

 6.测量距离

7.编辑C#脚本,循环检测并显示

三、总结


前言

visionpro中可以结合C#脚本进行检测,这可以更简洁全面的去实现复杂的检测功能,当然结果C#软件二次开发更加全面,不过有时外面还是直接在工具块里面添加脚本更方便些。


一、检测要求

     检测齿数及齿端到中心的距离。

 

二、步骤

1.在CogToolBlock工具里添加检测工具

  1. 1.CogFindCircleTool是一个找圆工具,主要是找到齿轮外径及圆心。
  2. 2.CogBlobTool是一般二值化分析工具,主要是找到每个齿的位置,角度。
  3. 3.CogResultsAnalysisTool,结果处理工具,主要是给卡尺工具算出角度。
  4. 4.CogCaliperTool,卡尺工具,用于找齿顶边。
  5. 5.CogDistancePointPointTool,测量点到点工具,用于测量距离。

2.找圆

        在找圆时,卡尺可以大一点,然后卡尺计分添加一个PositionNeg的算法,找最外面位置,否则圆会小一点。

 

 3.Bolb分析

        在找圆工具中可以得到圆心,半径,把他们输出出来给到bolb工具的区域对应参数。

然后再修改工具里面的径向缩放,角度范围参数。

 然后,通过面积管控排除一些毛刺,去除影响。

 

 4.结果处理

        结果处理blob得到的对应区域的角度。使卡尺角度摆正。

 5.卡尺找齿顶边

        通过blob给卡尺位置角度。

 

 

 6.测量距离

测量圆心到齿顶距离

7.编辑C#脚本,循环检测并显示

        脚本采用复杂脚本,简单脚本功能限制。

  1. #region namespace imports
  2. using System;
  3. using System.Collections;
  4. using System.Drawing;
  5. using System.IO;
  6. using System.Windows.Forms;
  7. using Cognex.VisionPro;
  8. using Cognex.VisionPro.ToolBlock;
  9. using Cognex.VisionPro3D;
  10. using Cognex.VisionPro.Caliper;
  11. using Cognex.VisionPro.Blob;
  12. using Cognex.VisionPro.ResultsAnalysis;
  13. using Cognex.VisionPro.Dimensioning;
  14. #endregion
  15. public class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase
  16. {
  17. #region Private Member Variables
  18. //声明
  19. private Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock;
  20. private Cognex.VisionPro.Blob.CogBlobTool mCogBlob;
  21. private Cognex.VisionPro.Caliper.CogCaliperTool mCogCaliper;
  22. private Cognex.VisionPro.Caliper.CogFindCircleTool mCogFindCircle;
  23. private CogGraphicCollection labels;
  24. private CogGraphicLabel myLabel;
  25. #endregion
  26. /// <summary>
  27. /// Called when the parent tool is run.
  28. /// Add code here to customize or replace the normal run behavior.
  29. /// </summary>
  30. /// <param name="message">Sets the Message in the tool's RunStatus.</param>
  31. /// <param name="result">Sets the Result in the tool's RunStatus</param>
  32. /// <returns>True if the tool should run normally,
  33. /// False if GroupRun customizes run behavior</returns>
  34. public override bool GroupRun(ref string message, ref CogToolResultConstants result)
  35. {
  36. // To let the execution stop in this script when a debugger is attached, uncomment the following lines.
  37. // #if DEBUG
  38. // if (System.Diagnostics.Debugger.IsAttached) System.Diagnostics.Debugger.Break();
  39. // #endif
  40. // Run each tool using the RunTool function
  41. foreach(ICogTool tool in mToolBlock.Tools)
  42. mToolBlock.RunTool(tool, ref message, ref result);
  43. //初始化
  44. mCogBlob = mToolBlock.Tools["CogBlobTool1"] as CogBlobTool;
  45. mCogCaliper = mToolBlock.Tools["CogCaliperTool1"] as CogCaliperTool;
  46. mCogFindCircle = mToolBlock.Tools["CogFindCircleTool1"] as CogFindCircleTool;
  47. mCogFindCircle.Run();
  48. mCogBlob.Run();
  49. int x = 0;
  50. int y = 0;
  51. //循环
  52. for (int i = 0; i < mCogBlob.Results.GetBlobs().Count; i++)
  53. {
  54. //卡尺
  55. mCogCaliper.Region.CenterX = mCogBlob.Results.GetBlobs()[i].CenterOfMassX;
  56. mCogCaliper.Region.CenterY = mCogBlob.Results.GetBlobs()[i].CenterOfMassY;
  57. mCogCaliper.Region.Rotation = mCogBlob.Results.GetBlobs()[i].Angle+1.2;
  58. mCogCaliper.Run();
  59. //测量
  60. CogDistancePointPointTool mCogDistancePointPoint = new CogDistancePointPointTool();
  61. mCogDistancePointPoint.InputImage = mToolBlock.Inputs[0].Value as CogImage8Grey;
  62. mCogDistancePointPoint.StartX = mCogCaliper.Results[0].Edge0.PositionX;
  63. mCogDistancePointPoint.StartY = mCogCaliper.Results[0].Edge0.PositionY;
  64. mCogDistancePointPoint.EndX = mCogFindCircle.Results.GetCircle().CenterX;
  65. mCogDistancePointPoint.EndY = mCogFindCircle.Results.GetCircle().CenterY;
  66. mCogDistancePointPoint.Run();
  67. //显示保存
  68. myLabel = new CogGraphicLabel();
  69. x = (i % 5) * 200+100;
  70. y = (i / 5) * 50 + 150;
  71. myLabel.SetXYText(x, y, "距离:" + mCogDistancePointPoint.Distance.ToString("f3"));
  72. myLabel.SelectedSpaceName = "@";
  73. myLabel.Font = new Font("微软黑体", 10);
  74. myLabel.Color = Cognex.VisionPro.CogColorConstants.Blue;
  75. labels.Add(myLabel);
  76. }
  77. return true;
  78. }
  79. #region When the Current Run Record is Created
  80. /// <summary>
  81. /// Called when the current record may have changed and is being reconstructed
  82. /// </summary>
  83. /// <param name="currentRecord">
  84. /// The new currentRecord is available to be initialized or customized.</param>
  85. public override void ModifyCurrentRunRecord(Cognex.VisionPro.ICogRecord currentRecord)
  86. {
  87. }
  88. #endregion
  89. #region When the Last Run Record is Created
  90. /// <summary>
  91. /// Called when the last run record may have changed and is being reconstructed
  92. /// </summary>
  93. /// <param name="lastRecord">
  94. /// The new last run record is available to be initialized or customized.</param>
  95. public override void ModifyLastRunRecord(Cognex.VisionPro.ICogRecord lastRecord)
  96. {
  97. //显示
  98. foreach (ICogGraphic graphic in labels)
  99. {
  100. mToolBlock.AddGraphicToRunRecord(graphic, lastRecord, "CogFindCircleTool1.InputImage", "script");
  101. }
  102. labels.Clear();
  103. CogGraphicLabel label = new CogGraphicLabel();
  104. label.SetXYText(160, 50, "齿数:" + mCogBlob.Results.GetBlobs().Count.ToString());
  105. label.SelectedSpaceName = "@";
  106. label.Font = new Font("微软黑体", 10);
  107. label.Color = Cognex.VisionPro.CogColorConstants.Blue;
  108. mToolBlock.AddGraphicToRunRecord(label, lastRecord, "CogFindCircleTool1.InputImage", "script");
  109. }
  110. #endregion
  111. #region When the Script is Initialized
  112. /// <summary>
  113. /// Perform any initialization required by your script here
  114. /// </summary>
  115. /// <param name="host">The host tool</param>
  116. public override void Initialize(Cognex.VisionPro.ToolGroup.CogToolGroup host)
  117. {
  118. // DO NOT REMOVE - Call the base class implementation first - DO NOT REMOVE
  119. base.Initialize(host);
  120. //实例化
  121. this.mCogBlob = new CogBlobTool();
  122. this.mCogCaliper = new CogCaliperTool();
  123. this.mCogFindCircle = new CogFindCircleTool();
  124. this.labels = new CogGraphicCollection();
  125. // Store a local copy of the script host
  126. this.mToolBlock = ((Cognex.VisionPro.ToolBlock.CogToolBlock)(host));
  127. }
  128. #endregion
  129. }

三、总结

对于要编辑很多工具的测量,采取脚本循环可以节省大量的时间,主要思路是找出每个节点,以及结果处理。

运行结果:

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/代码探险家/article/detail/925189
推荐阅读
相关标签
  

闽ICP备14008679号