// ============================================================================ // 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; /// /// Kirsch边缘检测算子 /// public class KirschEdgeProcessor : ImageProcessorBase { private static readonly ILogger _logger = Log.ForContext(); // 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"))); _logger.Debug("InitializeParameters"); } public override Image Process(Image inputImage) { int threshold = GetParameter("Threshold"); double scale = GetParameter("Scale"); int width = inputImage.Width; int height = inputImage.Height; byte[,,] inputData = inputImage.Data; Image result = new Image(width, height); byte[,,] outputData = result.Data; // 对每个像素应用8个Kirsch模板,取最大响应 for (int y = 1; y < height - 1; y++) { for (int x = 1; x < width - 1; x++) { int maxResponse = 0; // 对8个方向分别计算 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 = inputData[y + ky - 1, x + kx - 1, 0]; sum += pixelValue * KirschKernels[k][ky, kx]; } } // 取绝对值 sum = Math.Abs(sum); if (sum > maxResponse) { maxResponse = sum; } } // 应用阈值和缩放 if (maxResponse > threshold) { int value = (int)(maxResponse * scale); outputData[y, x, 0] = (byte)Math.Min(255, Math.Max(0, value)); } else { outputData[y, x, 0] = 0; } } } _logger.Debug("Process: Threshold = {Threshold}, Scale = {Scale}", threshold, scale); return result; } }