HalconMeasurementNodes.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading;
  5. using HalconDotNet;
  6. using TeamAAS.FlowEditor.Models;
  7. using TeamAAS.FlowEditor.Plugins;
  8. using Plugins.Halcon.Models;
  9. namespace Plugins.Halcon
  10. {
  11. // ─────────────────────────────────────────────────────────────────────
  12. // HALCON 几何测量分类算子(从主程序集迁入分类 DLL)。
  13. // 显示名/类型全名保持不变 —— 工具箱、流程文件、公式绑定全部兼容。
  14. // ─────────────────────────────────────────────────────────────────────
  15. /// <summary>H区域特征:面积、重心、区域数量。</summary>
  16. [Serializable]
  17. [Plugin("H区域特征", PluginCategory.Halocn模块, typeof(HalconAreaCenterModel), "Ruler",
  18. NodeShape = NodeCategory.Normal, IsSubFlowNode = true, VisionCategory = VisionPlugin.几何测量,
  19. Description = "区域特征:面积、重心、数量(区域集时给出总面积与首个区域重心)")]
  20. public class HalconAreaCenterPlugin : BasePlugin<HalconAreaCenterModel>
  21. {
  22. public override List<OutputField> DeclareOutputs() => new List<OutputField>
  23. {
  24. new OutputField("数量", typeof(int)),
  25. new OutputField("总面积", typeof(double)),
  26. new OutputField("重心行", typeof(double)),
  27. new OutputField("重心列", typeof(double)),
  28. new OutputField("结果", typeof(bool)),
  29. };
  30. public override NodeRunStatus PluginRun(CancellationToken token, out Dictionary<string, object> results)
  31. {
  32. var res = new Dictionary<string, object>();
  33. results = res;
  34. NodeRunStatus Fail(string m) { res["Error"] = m; res["结果"] = false; Log(1, $"区域特征失败: {m}", TeamAAS.LogLevel.Error); return NodeRunStatus.Failed; }
  35. try
  36. {
  37. var src = GetResolve(Model.RegionSource);
  38. if (!(src is HObject region)) return Fail("未获取到输入区域(请绑定上游「区域」输出)");
  39. HalconRuntime.AreaCenter(region, out double[] areas, out double[] rows, out double[] cols);
  40. int count = areas.Length;
  41. double total = areas.Length > 0 ? areas.Sum() : 0d;
  42. double row = rows.Length > 0 ? rows[0] : 0d;
  43. double col = cols.Length > 0 ? cols[0] : 0d;
  44. res["数量"] = count;
  45. res["总面积"] = total;
  46. res["重心行"] = row;
  47. res["重心列"] = col;
  48. res["结果"] = true;
  49. Log(2, $"区域特征:数量={count},总面积={total:F1},首区域重心=({row:F1},{col:F1})");
  50. return NodeRunStatus.Success;
  51. }
  52. catch (Exception ex) { return Fail(ex.Message); }
  53. }
  54. }
  55. /// <summary>H外接矩形:区域的最小外接正矩形边界与宽高。</summary>
  56. [Serializable]
  57. [Plugin("H外接矩形", PluginCategory.Halocn模块, typeof(HalconRectangleModel), "CropFree",
  58. NodeShape = NodeCategory.Normal, IsSubFlowNode = true, VisionCategory = VisionPlugin.几何测量,
  59. Description = "最小外接正矩形:行1/列1/行2/列2 与 宽/高(取第一个区域)")]
  60. public class HalconRectanglePlugin : BasePlugin<HalconRectangleModel>
  61. {
  62. public override List<OutputField> DeclareOutputs() => new List<OutputField>
  63. {
  64. new OutputField("行1", typeof(double)),
  65. new OutputField("列1", typeof(double)),
  66. new OutputField("行2", typeof(double)),
  67. new OutputField("列2", typeof(double)),
  68. new OutputField("宽", typeof(double)),
  69. new OutputField("高", typeof(double)),
  70. new OutputField("结果", typeof(bool)),
  71. };
  72. public override NodeRunStatus PluginRun(CancellationToken token, out Dictionary<string, object> results)
  73. {
  74. var res = new Dictionary<string, object>();
  75. results = res;
  76. NodeRunStatus Fail(string m) { res["Error"] = m; res["结果"] = false; Log(1, $"外接矩形失败: {m}", TeamAAS.LogLevel.Error); return NodeRunStatus.Failed; }
  77. try
  78. {
  79. var src = GetResolve(Model.RegionSource);
  80. if (!(src is HObject region)) return Fail("未获取到输入区域(请绑定上游「区域」输出)");
  81. HalconRuntime.SmallestRectangle1(region, out double r1, out double c1, out double r2, out double c2);
  82. res["行1"] = r1;
  83. res["列1"] = c1;
  84. res["行2"] = r2;
  85. res["列2"] = c2;
  86. res["宽"] = c2 - c1;
  87. res["高"] = r2 - r1;
  88. res["结果"] = true;
  89. Log(2, $"外接矩形:({r1:F1},{c1:F1})-({r2:F1},{c2:F1}),宽={c2 - c1:F1},高={r2 - r1:F1}");
  90. return NodeRunStatus.Success;
  91. }
  92. catch (Exception ex) { return Fail(ex.Message); }
  93. }
  94. }
  95. }
  96. namespace Plugins.Halcon.Models
  97. {
  98. using PropertyGridLib.Attributes;
  99. using PropertyGridLib.Controls;
  100. using System.ComponentModel;
  101. using TeamAAS.FlowEditor.Plugins;
  102. using TeamAAS.FlowEngine.FormulaData;
  103. /// <summary>区域特征:面积、重心、区域数量。</summary>
  104. [Serializable]
  105. public class HalconAreaCenterModel : HalconOperatorModelBase
  106. {
  107. [Category("I.输入")]
  108. [DisplayName("1.输入区域")]
  109. [Description("上游区域(单个或区域集;区域集时给出总面积与首个区域重心)")]
  110. [FormulaEditor(typeof(PluginDataProvider))]
  111. public FormulaBound<object> RegionSource { get; set; } = new FormulaBound<object>();
  112. }
  113. /// <summary>最小外接矩形:区域的外接正矩形边界与宽高。</summary>
  114. [Serializable]
  115. public class HalconRectangleModel : HalconOperatorModelBase
  116. {
  117. [Category("I.输入")]
  118. [DisplayName("1.输入区域")]
  119. [Description("上游区域(取第一个区域计算外接矩形)")]
  120. [FormulaEditor(typeof(PluginDataProvider))]
  121. public FormulaBound<object> RegionSource { get; set; } = new FormulaBound<object>();
  122. }
  123. }