Files
XplorePlane/XP.ImageProcessing.Processors/数学运算/DifferenceProcessor.cs
T
2026-04-13 14:36:18 +08:00

128 lines
4.2 KiB
C#

// ============================================================================
// Copyright © 2026 Hexagon Technology Center GmbH. All Rights Reserved.
// 文件å? DifferenceProcessor.cs
// æè¿°: 差分è¿ç®—ç®—å­ï¼Œç”¨äºŽè¾¹ç¼˜æ£€æµ‹å’Œå˜åŒ–检æµ?
// 功能:
// - 对图åƒè¿›è¡Œå·®åˆ†è¿ç®?
// - æ”¯æŒæ°´å¹³ã€åž‚直和对角线差åˆ?
// - å¯ç”¨äºŽè¾¹ç¼˜æ£€æµ?
// - å¯é€‰å½’一化输å‡?
// 算法: åƒç´ çº§å·®åˆ†è¿ç®?
// 作è€? æŽä¼Ÿ wei.lw.li@hexagon.com
// ============================================================================
using Emgu.CV;
using Emgu.CV.Structure;
using Serilog;
using System.Drawing;
using XP.ImageProcessing.Core;
namespace XP.ImageProcessing.Processors;
/// <summary>
/// 差分è¿ç®—ç®—å­
/// </summary>
public class DifferenceProcessor : ImageProcessorBase
{
private static readonly ILogger _logger = Log.ForContext<DifferenceProcessor>();
public DifferenceProcessor()
{
Name = LocalizationHelper.GetString("DifferenceProcessor_Name");
Description = LocalizationHelper.GetString("DifferenceProcessor_Description");
}
protected override void InitializeParameters()
{
Parameters.Add("Direction", new ProcessorParameter(
"Direction",
LocalizationHelper.GetString("DifferenceProcessor_Direction"),
typeof(string),
"Horizontal",
null,
null,
LocalizationHelper.GetString("DifferenceProcessor_Direction_Desc"),
new string[] { "Horizontal", "Vertical", "Both" }));
Parameters.Add("Normalize", new ProcessorParameter(
"Normalize",
LocalizationHelper.GetString("DifferenceProcessor_Normalize"),
typeof(bool),
true,
null,
null,
LocalizationHelper.GetString("DifferenceProcessor_Normalize_Desc")));
_logger.Debug("InitializeParameters");
}
public override Image<Gray, byte> Process(Image<Gray, byte> inputImage)
{
string direction = GetParameter<string>("Direction");
bool normalize = GetParameter<bool>("Normalize");
int width = inputImage.Width;
int height = inputImage.Height;
var floatImage = inputImage.Convert<Gray, float>();
var result = new Image<Gray, float>(width, height);
if (direction == "Horizontal")
{
// 水平差分: I(x+1,y) - I(x,y)
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width - 1; x++)
{
result.Data[y, x, 0] = floatImage.Data[y, x + 1, 0] - floatImage.Data[y, x, 0];
}
result.Data[y, width - 1, 0] = 0;
}
}
else if (direction == "Vertical")
{
// 垂直差分: I(x,y+1) - I(x,y)
for (int y = 0; y < height - 1; y++)
{
for (int x = 0; x < width; x++)
{
result.Data[y, x, 0] = floatImage.Data[y + 1, x, 0] - floatImage.Data[y, x, 0];
}
}
for (int x = 0; x < width; x++)
{
result.Data[height - 1, x, 0] = 0;
}
}
else // Both
{
// 梯度幅� sqrt((dx)^2 + (dy)^2)
for (int y = 0; y < height - 1; y++)
{
for (int x = 0; x < width - 1; x++)
{
float dx = floatImage.Data[y, x + 1, 0] - floatImage.Data[y, x, 0];
float dy = floatImage.Data[y + 1, x, 0] - floatImage.Data[y, x, 0];
result.Data[y, x, 0] = (float)Math.Sqrt(dx * dx + dy * dy);
}
}
}
if (normalize)
{
double minVal = 0, maxVal = 0;
Point minLoc = new Point();
Point maxLoc = new Point();
CvInvoke.MinMaxLoc(result, ref minVal, ref maxVal, ref minLoc, ref maxLoc);
if (maxVal > minVal)
{
result = (result - minVal) * (255.0 / (maxVal - minVal));
}
}
floatImage.Dispose();
_logger.Debug("Process: Direction = {Direction}, Normalize = {Normalize}", direction, normalize);
return result.Convert<Gray, byte>();
}
}