using System;
using System.Collections.Generic;
using System.Threading;
using HalconDotNet;
using TeamAAS.FlowEditor.Execution;
using TeamAAS.FlowEditor.Models;
using TeamAAS.FlowEditor.Plugins;
using Plugins.Halcon.Models;
using Plugins.Halcon;
namespace Plugins.Halcon
{
// ─────────────────────────────────────────────────────────────────────
// HALCON 检测识别分类算子(新增)。
// ─────────────────────────────────────────────────────────────────────
/// H边缘提取:Sobel 梯度 + 阈值,输出边缘区域(可配合形状筛选取感兴趣边缘)。
[Serializable]
[Plugin("H边缘提取", PluginCategory.Halocn模块, typeof(HalconEdgeModel), "VectorLine",
NodeShape = NodeCategory.Normal, IsSubFlowNode = true, VisionCategory = VisionPlugin.检测识别,
Description = "Sobel 边缘提取:梯度幅值 + 灰度阈值 → 边缘区域;高级编辑可加载图像调试")]
public class HalconEdgePlugin : BasePlugin
{
public override List DeclareOutputs() => new List
{
new OutputField("边缘区域", typeof(object)),
new OutputField("结果", typeof(bool)),
};
public override NodeRunStatus PluginRun(CancellationToken token, out Dictionary results)
{
var res = new Dictionary();
results = res;
NodeRunStatus Fail(string m) { res["Error"] = m; res["结果"] = false; Log(1, $"边缘提取失败: {m}", TeamAAS.LogLevel.Error); return NodeRunStatus.Failed; }
bool owned = false;
HObject image = null;
HObject edgeAmp = null;
try
{
var src = GetResolve(Model.ImageSource);
if (src == null) return Fail("未获取到输入图像(请绑定「输入图像」,例如 &{输入.图像})");
owned = !(src is HObject);
image = HalconRuntime.ToHObject(src);
Log(3, $"边缘提取:Sobel[{Model.FilterType}] 孔径={Model.ApertureSize},阈值[{Model.MinGray},{Model.MaxGray}]");
HOperatorSet.SobelAmp(image, out edgeAmp, Model.FilterType, Model.ApertureSize);
var edges = HalconRuntime.Threshold(edgeAmp, Model.MinGray, Model.MaxGray);
res["边缘区域"] = edges;
res["结果"] = true;
HalconEditorPreview.Show(Model, src, edges, "green"); // 底图 + 边缘叠加
Log(2, $"边缘提取完成:区域对象数={HalconRuntime.CountObj(edges)}");
return NodeRunStatus.Success;
}
catch (Exception ex) { return Fail(ex.Message); }
finally
{
if (edgeAmp != null) { try { edgeAmp.Dispose(); } catch { } }
if (owned) HalconRuntime.Dispose(image);
}
}
}
}
namespace Plugins.Halcon.Models
{
using PropertyGridLib.Attributes;
using PropertyGridLib.Controls;
using System.ComponentModel;
using TeamAAS.FlowEditor.Plugins;
using TeamAAS.FlowEngine.FormulaData;
/// 边缘提取模型。
[Serializable]
public class HalconEdgeModel : HalconOperatorModelBase
{
[Category("I.输入")]
[DisplayName("1.输入图像")]
[Description("上游图像(GenImage/HObject)。子流程内通常绑定 &{输入.图像}")]
[FormulaEditor(typeof(PluginDataProvider))]
public FormulaBound