// ============================================================================
// Copyright © 2026 Hexagon Technology Center GmbH. All Rights Reserved.
// 文件名: IntegralProcessor.cs
// 描述: 积分运算算子,计算积分图像
// 功能:
// - 计算积分图像(累加和)
// - 用于快速区域求和
// - 支持归一化输出
// 算法: 积分图像算法
// 作者: 李伟 wei.lw.li@hexagon.com
// ============================================================================
using Emgu.CV;
using Emgu.CV.Structure;
using XP.ImageProcessing.Core;
using Serilog;
using System.Drawing;
namespace XP.ImageProcessing.Processors;
///
/// 积分运算算子
///
public class IntegralProcessor : ImageProcessorBase
{
private static readonly ILogger _logger = Log.ForContext();
public IntegralProcessor()
{
Name = LocalizationHelper.GetString("IntegralProcessor_Name");
Description = LocalizationHelper.GetString("IntegralProcessor_Description");
}
protected override void InitializeParameters()
{
Parameters.Add("Normalize", new ProcessorParameter(
"Normalize",
LocalizationHelper.GetString("IntegralProcessor_Normalize"),
typeof(bool),
true,
null,
null,
LocalizationHelper.GetString("IntegralProcessor_Normalize_Desc")));
_logger.Debug("InitializeParameters");
}
public override Image Process(Image inputImage)
{
bool normalize = GetParameter("Normalize");
int width = inputImage.Width;
int height = inputImage.Height;
// 使用double类型避免溢出
var integralImage = new Image(width, height);
// 计算积分图像
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
double sum = inputImage.Data[y, x, 0];
if (x > 0)
sum += integralImage.Data[y, x - 1, 0];
if (y > 0)
sum += integralImage.Data[y - 1, x, 0];
if (x > 0 && y > 0)
sum -= integralImage.Data[y - 1, x - 1, 0];
integralImage.Data[y, x, 0] = sum;
}
}
var result = integralImage.Convert();
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));
}
}
integralImage.Dispose();
_logger.Debug("Process: Normalize = {Normalize}", normalize);
return result.Convert();
}
}