Files
XplorePlane/XP.ImageProcessing.Processors/边缘检测/KirschEdgeProcessor.cs
T
wei.lw.li d5d964d115 feat: 滤波/边缘检测算子标记高级参数
滤波类:GaussianBlur(Sigma)、Bilateral(SigmaSpace)、
BandPass(FilterType,Order)、ShockFilter(Dt)
边缘检测:Sobel(Direction,KernelSize,Scale)、
HorizontalEdge(Method,Sensitivity)、Kirsch(Scale)
2026-08-12 09:25:08 +08:00

121 lines
4.3 KiB
C#

// ============================================================================
// Copyright © 2026 Hexagon Technology Center GmbH. All Rights Reserved.
// 文件名: KirschEdgeProcessor.cs
// 描述: Kirsch边缘检测算子,用于检测图像边缘
// 功能:
// - Kirsch算子边缘检测
// - 8个方向的边缘检测
// - 输出最大响应方向的边缘
// - 对噪声敏感度低
// 算法: Kirsch算子(8方向模板)
// 作者: 李伟 wei.lw.li@hexagon.com
// ============================================================================
using Emgu.CV;
using Emgu.CV.Structure;
using XP.ImageProcessing.Core;
using Serilog;
namespace XP.ImageProcessing.Processors;
/// <summary>
/// Kirsch边缘检测算子(支持 8 位和 16 位灰度图像)
/// </summary>
public class KirschEdgeProcessor<TDepth> : ImageProcessorBase<TDepth>
where TDepth : struct, IComparable
{
private static readonly ILogger _logger = Log.ForContext<KirschEdgeProcessor<TDepth>>();
// Kirsch算子的8个方向模板
private static readonly int[][,] KirschKernels = new int[8][,]
{
// N
new int[,] { { 5, 5, 5 }, { -3, 0, -3 }, { -3, -3, -3 } },
// NW
new int[,] { { 5, 5, -3 }, { 5, 0, -3 }, { -3, -3, -3 } },
// W
new int[,] { { 5, -3, -3 }, { 5, 0, -3 }, { 5, -3, -3 } },
// SW
new int[,] { { -3, -3, -3 }, { 5, 0, -3 }, { 5, 5, -3 } },
// S
new int[,] { { -3, -3, -3 }, { -3, 0, -3 }, { 5, 5, 5 } },
// SE
new int[,] { { -3, -3, -3 }, { -3, 0, 5 }, { -3, 5, 5 } },
// E
new int[,] { { -3, -3, 5 }, { -3, 0, 5 }, { -3, -3, 5 } },
// NE
new int[,] { { -3, 5, 5 }, { -3, 0, 5 }, { -3, -3, -3 } }
};
public KirschEdgeProcessor()
{
Name = LocalizationHelper.GetString("KirschEdgeProcessor_Name");
Description = LocalizationHelper.GetString("KirschEdgeProcessor_Description");
}
protected override void InitializeParameters()
{
Parameters.Add("Threshold", new ProcessorParameter(
"Threshold",
LocalizationHelper.GetString("KirschEdgeProcessor_Threshold"),
typeof(int),
100,
0,
1000,
LocalizationHelper.GetString("KirschEdgeProcessor_Threshold_Desc")));
Parameters.Add("Scale", new ProcessorParameter(
"Scale",
LocalizationHelper.GetString("KirschEdgeProcessor_Scale"),
typeof(double),
1.0,
0.1,
5.0,
LocalizationHelper.GetString("KirschEdgeProcessor_Scale_Desc")) { IsAdvanced = true });
_logger.Debug("InitializeParameters");
}
public override Image<Gray, TDepth> Process(Image<Gray, TDepth> inputImage)
{
int threshold = GetParameter<int>("Threshold");
double scale = GetParameter<double>("Scale");
// 16 位图时阈值也按比例放大
if (typeof(TDepth) == typeof(ushort))
threshold = PixelDepthHelper.ScaleThreshold<TDepth>(threshold);
int width = inputImage.Width;
int height = inputImage.Height;
var result = new Image<Gray, TDepth>(width, height);
for (int y = 1; y < height - 1; y++)
{
for (int x = 1; x < width - 1; x++)
{
int maxResponse = 0;
for (int k = 0; k < 8; k++)
{
int sum = 0;
for (int ky = 0; ky < 3; ky++)
for (int kx = 0; kx < 3; kx++)
{
int pixelValue = PixelDepthHelper.ReadPixel(inputImage, y + ky - 1, x + kx - 1);
sum += pixelValue * KirschKernels[k][ky, kx];
}
sum = Math.Abs(sum);
if (sum > maxResponse) maxResponse = sum;
}
if (maxResponse > threshold)
{
int value = (int)(maxResponse * scale);
PixelDepthHelper.WritePixel(result, y, x, value);
}
}
}
_logger.Debug("Process: Threshold = {Threshold}, Scale = {Scale}", threshold, scale);
return result;
}
}