HalconDetectionNodes.cs 4.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4. using HalconDotNet;
  5. using TeamAAS.FlowEditor.Models;
  6. using TeamAAS.FlowEditor.Plugins;
  7. using Plugins.Halcon.Models;
  8. using Plugins.Halcon;
  9. namespace Plugins.Halcon
  10. {
  11. // ─────────────────────────────────────────────────────────────────────
  12. // HALCON 检测识别分类算子(从主程序集迁入分类 DLL)。
  13. // 显示名/类型全名保持不变 —— 工具箱、流程文件、公式绑定全部兼容。
  14. // ─────────────────────────────────────────────────────────────────────
  15. /// <summary>H形状筛选:按特征与阈值区间从区域集中保留符合条件的区域。</summary>
  16. [Serializable]
  17. [Plugin("H形状筛选", PluginCategory.Halocn模块, typeof(HalconSelectShapeModel), "FilterVariant",
  18. NodeShape = NodeCategory.Normal, IsSubFlowNode = true, VisionCategory = VisionPlugin.检测识别,
  19. Description = "形状筛选:按特征(area/roundness/compactness…)与[min,max]保留区域,输出区域集与数量")]
  20. public class HalconSelectShapePlugin : BasePlugin<HalconSelectShapeModel>
  21. {
  22. public override List<OutputField> DeclareOutputs() => new List<OutputField>
  23. {
  24. new OutputField("区域集", typeof(object)),
  25. new OutputField("数量", typeof(int)),
  26. new OutputField("结果", typeof(bool)),
  27. };
  28. public override NodeRunStatus PluginRun(CancellationToken token, out Dictionary<string, object> results)
  29. {
  30. var res = new Dictionary<string, object>();
  31. results = res;
  32. NodeRunStatus Fail(string m) { res["Error"] = m; res["结果"] = false; Log(1, $"形状筛选失败: {m}", TeamAAS.LogLevel.Error); return NodeRunStatus.Failed; }
  33. try
  34. {
  35. var src = GetResolve(Model.RegionSource);
  36. if (!(src is HObject region)) return Fail("未获取到输入区域集(请绑定上游「区域集」输出)");
  37. if (string.IsNullOrWhiteSpace(Model.Feature)) return Fail("未指定筛选特征");
  38. var selected = HalconRuntime.SelectShape(region, Model.Feature, "and", Model.Min, Model.Max);
  39. int n = HalconRuntime.CountObj(selected);
  40. res["区域集"] = selected;
  41. res["数量"] = n;
  42. res["结果"] = true;
  43. HalconEditorPreview.Show(Model, null, selected, "cyan"); // 只更新叠加区域,保留底图
  44. Log(2, $"形状筛选[{Model.Feature} ∈ {Model.Min}~{Model.Max}]:命中 {n} 个区域");
  45. return NodeRunStatus.Success;
  46. }
  47. catch (Exception ex) { return Fail(ex.Message); }
  48. }
  49. }
  50. }
  51. namespace Plugins.Halcon.Models
  52. {
  53. using PropertyGridLib.Attributes;
  54. using PropertyGridLib.Controls;
  55. using System.ComponentModel;
  56. using TeamAAS.FlowEditor.Plugins;
  57. using TeamAAS.FlowEngine.FormulaData;
  58. /// <summary>形状筛选:按特征与阈值区间从区域集中保留符合条件的区域。</summary>
  59. [Serializable]
  60. public class HalconSelectShapeModel : HalconOperatorModelBase
  61. {
  62. [Category("I.输入")]
  63. [DisplayName("1.输入区域")]
  64. [Description("上游区域集(一般来自连通域的输出「区域集」)")]
  65. [FormulaEditor(typeof(PluginDataProvider))]
  66. public FormulaBound<object> RegionSource { get; set; } = new FormulaBound<object>();
  67. [Category("II.参数")]
  68. [DisplayName("1.筛选特征")]
  69. [Description("HALCON 特征名:area(面积)/roundness(圆度)/compactness(紧凑度)/rect2_features 等")]
  70. public string Feature { get; set; } = "area";
  71. [Category("II.参数")]
  72. [DisplayName("2.最小值")]
  73. [Description("特征下限(含)")]
  74. public double Min { get; set; } = 100;
  75. [Category("II.参数")]
  76. [DisplayName("3.最大值")]
  77. [Description("特征上限(含)")]
  78. public double Max { get; set; } = 999999;
  79. }
  80. }