直方图均衡化
This commit is contained in:
parent
5087b946ed
commit
50f15ad271
@ -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;
|
||||
|
||||
@ -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);
|
||||
|
||||
@ -0,0 +1,46 @@
|
||||
<UserControl x:Class="PLCSharp.VVMs.Vision.VisionFlowHandler.Processing.HistogramEqualize"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:vision="clr-namespace:PLCSharp.VVMs.Vision"
|
||||
mc:Ignorable="d"
|
||||
d:DataContext="{d:DesignInstance Type=vision:VisionConfigViewModel}"
|
||||
d:DesignHeight="250" d:DesignWidth="300">
|
||||
<ScrollViewer VerticalScrollBarVisibility="Auto">
|
||||
<StackPanel>
|
||||
<GroupBox Header="均衡化方法">
|
||||
<Grid Margin="5">
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="Auto" />
|
||||
<ColumnDefinition Width="*" />
|
||||
</Grid.ColumnDefinitions>
|
||||
<TextBlock Text="方法" VerticalAlignment="Center" />
|
||||
<ComboBox Grid.Column="1" Margin="5,0,0,0"
|
||||
SelectedIndex="{Binding SelectVisionFlow.IntParams[Method], Mode=TwoWay}">
|
||||
<ComboBoxItem Content="全局 (EqualizeHist)" />
|
||||
<ComboBoxItem Content="自适应 (CLAHE)" />
|
||||
</ComboBox>
|
||||
</Grid>
|
||||
</GroupBox>
|
||||
|
||||
<GroupBox Header="CLAHE 参数">
|
||||
<Grid Margin="5">
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition />
|
||||
<RowDefinition />
|
||||
</Grid.RowDefinitions>
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="80" />
|
||||
<ColumnDefinition />
|
||||
</Grid.ColumnDefinitions>
|
||||
<TextBlock Text="裁剪限制" VerticalAlignment="Center" ToolTip="对比度限制,越大越强" />
|
||||
<TextBox Grid.Column="1" Text="{Binding SelectVisionFlow.DoubleParams[ClipLimit], Mode=TwoWay, TargetNullValue=2.0, UpdateSourceTrigger=LostFocus}" />
|
||||
|
||||
<TextBlock Grid.Row="1" Text="网格大小" VerticalAlignment="Center" ToolTip="CLAHE 分块大小 (px)" />
|
||||
<TextBox Grid.Row="1" Grid.Column="1" Text="{Binding SelectVisionFlow.IntParams[TileSize], Mode=TwoWay, TargetNullValue=8, UpdateSourceTrigger=LostFocus}" />
|
||||
</Grid>
|
||||
</GroupBox>
|
||||
</StackPanel>
|
||||
</ScrollViewer>
|
||||
</UserControl>
|
||||
@ -0,0 +1,12 @@
|
||||
using System.Windows.Controls;
|
||||
|
||||
namespace PLCSharp.VVMs.Vision.VisionFlowHandler.Processing
|
||||
{
|
||||
public partial class HistogramEqualize : UserControl
|
||||
{
|
||||
public HistogramEqualize()
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,46 @@
|
||||
using OpenCvSharp;
|
||||
|
||||
namespace PLCSharp.VVMs.Vision.VisionFlowHandler.Processing
|
||||
{
|
||||
/// <summary>
|
||||
/// 直方图均衡化 — 增强图像对比度。支持全局 EqualizeHist 和自适应 CLAHE。
|
||||
/// </summary>
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -382,6 +382,7 @@ namespace PLCSharp.VVMs.Vision
|
||||
[VisionFlowType.图像翻转] = new ImageFlipHandler(),
|
||||
[VisionFlowType.图像旋转] = new ImageRotateHandler(),
|
||||
[VisionFlowType.ROI剪切] = new ROICropHandler(),
|
||||
[VisionFlowType.直方图均衡化] = new HistogramEqualizeHandler(),
|
||||
|
||||
};
|
||||
[NotMapped]
|
||||
|
||||
@ -53,6 +53,9 @@
|
||||
[FlowCategory("图像处理")]
|
||||
ROI剪切 = 21,
|
||||
|
||||
[FlowCategory("图像处理")]
|
||||
直方图均衡化 = 22,
|
||||
|
||||
[FlowCategory("图像处理")]
|
||||
坐标转换 = 18,
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user