diff --git a/PLCSharpWpf/VVMs/Vision/VisionConfigViewModel.Flows.cs b/PLCSharpWpf/VVMs/Vision/VisionConfigViewModel.Flows.cs index 24bd4c1..4d6dbb9 100644 --- a/PLCSharpWpf/VVMs/Vision/VisionConfigViewModel.Flows.cs +++ b/PLCSharpWpf/VVMs/Vision/VisionConfigViewModel.Flows.cs @@ -105,6 +105,9 @@ public partial class VisionConfigViewModel case VisionFlowType.ROI剪切: ContentRegion = new ROICrop(); break; + case VisionFlowType.直方图均衡化: + ContentRegion = new HistogramEqualize(); + break; case VisionFlowType.ROI解码: ContentRegion = new BarcodeDecode(); break; @@ -402,6 +405,11 @@ public partial class VisionConfigViewModel break; case VisionFlowType.ROI剪切: break; + case VisionFlowType.直方图均衡化: + newFlow.IntParams["Method"] = 0; + newFlow.DoubleParams["ClipLimit"] = 2.0; + newFlow.IntParams["TileSize"] = 8; + break; case VisionFlowType.ROI解码: newFlow.IntParams["DecodeType"] = 0; newFlow.BoolParams["EnableMirror"] = false; diff --git a/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/XFeatMatchHandler.cs b/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/XFeatMatchHandler.cs index 5e0f205..651af48 100644 --- a/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/XFeatMatchHandler.cs +++ b/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/XFeatMatchHandler.cs @@ -1,4 +1,4 @@ -using OpenCvSharp; +using OpenCvSharp; using Microsoft.ML.OnnxRuntime; using Microsoft.ML.OnnxRuntime.Tensors; @@ -232,9 +232,13 @@ namespace PLCSharp.VVMs.Vision.VisionFlowHandler.Algorithm using Mat resized = new(); Cv2.Resize(img, resized, new Size(newW, newH), 0, 0, InterpolationFlags.Linear); + // XFeat 模型要求 3 通道输入 + if (resized.Channels() == 1) + Cv2.CvtColor(resized, resized, ColorConversionCodes.GRAY2BGR); + // 预计算梯度方向(用于特征点方向) - using Mat grayRs = new(); - Cv2.CvtColor(resized, grayRs, ColorConversionCodes.BGR2GRAY); + MatExtension.BGR2GRAY(resized, out Mat grayRs); + using Mat dx = new(), dy = new(); Cv2.Sobel(grayRs, dx, MatType.CV_32F, 1, 0, 3, 1, 0, BorderTypes.Replicate); Cv2.Sobel(grayRs, dy, MatType.CV_32F, 0, 1, 3, 1, 0, BorderTypes.Replicate); diff --git a/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Processing/HistogramEqualize.xaml b/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Processing/HistogramEqualize.xaml new file mode 100644 index 0000000..3bc76cc --- /dev/null +++ b/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Processing/HistogramEqualize.xaml @@ -0,0 +1,46 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Processing/HistogramEqualize.xaml.cs b/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Processing/HistogramEqualize.xaml.cs new file mode 100644 index 0000000..7ed7aba --- /dev/null +++ b/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Processing/HistogramEqualize.xaml.cs @@ -0,0 +1,12 @@ +using System.Windows.Controls; + +namespace PLCSharp.VVMs.Vision.VisionFlowHandler.Processing +{ + public partial class HistogramEqualize : UserControl + { + public HistogramEqualize() + { + InitializeComponent(); + } + } +} diff --git a/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Processing/HistogramEqualizeHandler.cs b/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Processing/HistogramEqualizeHandler.cs new file mode 100644 index 0000000..10b2c39 --- /dev/null +++ b/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Processing/HistogramEqualizeHandler.cs @@ -0,0 +1,46 @@ +using OpenCvSharp; + +namespace PLCSharp.VVMs.Vision.VisionFlowHandler.Processing +{ + /// + /// 直方图均衡化 — 增强图像对比度。支持全局 EqualizeHist 和自适应 CLAHE。 + /// + public class HistogramEqualizeHandler : IVisionFlowHandler + { + public VisionFlowType Type => VisionFlowType.直方图均衡化; + + public bool Execute(VisionFunction func, VisionFlow item) + { + var src = func.Src; + if (src == null || src.Empty()) { item.Flow.ErrorState = "请先获取图片!"; return false; } + + // 确保参数存在,避免 XAML 绑定报红框 + if (!item.IntParams.ContainsKey("Method")) item.IntParams["Method"] = 0; + if (!item.DoubleParams.ContainsKey("ClipLimit")) item.DoubleParams["ClipLimit"] = 2.0; + if (!item.IntParams.ContainsKey("TileSize")) item.IntParams["TileSize"] = 8; + + int method = item.IntParams.TryGetValue("Method", out int m) ? m : 0; // 0=EqualizeHist, 1=CLAHE + double clipLimit = item.DoubleParams.TryGetValue("ClipLimit", out double cl) ? cl : 2.0; + int tileSize = item.IntParams.TryGetValue("TileSize", out int ts) ? ts : 8; + + // 灰度化 + if (src.Channels() == 3) + Cv2.CvtColor(src, src, ColorConversionCodes.BGR2GRAY); + + if (method == 0) + { + // 全局直方图均衡化 + Cv2.EqualizeHist(src, src); + } + else + { + // 自适应直方图均衡化 (CLAHE) + using var clahe = Cv2.CreateCLAHE(clipLimit, new Size(tileSize, tileSize)); + clahe.Apply(src, src); + } + + item.Flow.Done = true; + return true; + } + } +} diff --git a/PLCSharpWpf/VVMs/Vision/VisionFunction.cs b/PLCSharpWpf/VVMs/Vision/VisionFunction.cs index 4d04a29..27b9001 100644 --- a/PLCSharpWpf/VVMs/Vision/VisionFunction.cs +++ b/PLCSharpWpf/VVMs/Vision/VisionFunction.cs @@ -382,6 +382,7 @@ namespace PLCSharp.VVMs.Vision [VisionFlowType.图像翻转] = new ImageFlipHandler(), [VisionFlowType.图像旋转] = new ImageRotateHandler(), [VisionFlowType.ROI剪切] = new ROICropHandler(), + [VisionFlowType.直方图均衡化] = new HistogramEqualizeHandler(), }; [NotMapped] diff --git a/PLCSharpWpf/VVMs/Vision/VisionType.cs b/PLCSharpWpf/VVMs/Vision/VisionType.cs index b325824..53e73b5 100644 --- a/PLCSharpWpf/VVMs/Vision/VisionType.cs +++ b/PLCSharpWpf/VVMs/Vision/VisionType.cs @@ -53,6 +53,9 @@ [FlowCategory("图像处理")] ROI剪切 = 21, + [FlowCategory("图像处理")] + 直方图均衡化 = 22, + [FlowCategory("图像处理")] 坐标转换 = 18,