90 lines
3.0 KiB
C#
90 lines
3.0 KiB
C#
// ============================================================================
|
|
// Copyright © 2026 Hexagon Technology Center GmbH. All Rights Reserved.
|
|
// 文件名: DivisionProcessor.cs
|
|
// 描述: 除法运算算子,用于图像归一化处理
|
|
// 功能:
|
|
// - 对图像像素值进行除法运算
|
|
// - 支持缩放因子调整
|
|
// - 可选归一化到0-255范围
|
|
// - 常用于背景校正和图像归一化
|
|
// 算法: 像素级除法运算
|
|
// 作者: 李伟 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;
|
|
|
|
/// <summary>
|
|
/// 除法运算算子
|
|
/// </summary>
|
|
public class DivisionProcessor : ImageProcessorBase
|
|
{
|
|
private static readonly ILogger _logger = Log.ForContext<DivisionProcessor>();
|
|
|
|
public DivisionProcessor()
|
|
{
|
|
Name = LocalizationHelper.GetString("DivisionProcessor_Name");
|
|
Description = LocalizationHelper.GetString("DivisionProcessor_Description");
|
|
}
|
|
|
|
protected override void InitializeParameters()
|
|
{
|
|
Parameters.Add("Divisor", new ProcessorParameter(
|
|
"Divisor",
|
|
LocalizationHelper.GetString("DivisionProcessor_Divisor"),
|
|
typeof(double),
|
|
2.0,
|
|
0.01,
|
|
255.0,
|
|
LocalizationHelper.GetString("DivisionProcessor_Divisor_Desc")));
|
|
|
|
Parameters.Add("Scale", new ProcessorParameter(
|
|
"Scale",
|
|
LocalizationHelper.GetString("DivisionProcessor_Scale"),
|
|
typeof(double),
|
|
1.0,
|
|
0.1,
|
|
10.0,
|
|
LocalizationHelper.GetString("DivisionProcessor_Scale_Desc")));
|
|
|
|
Parameters.Add("Normalize", new ProcessorParameter(
|
|
"Normalize",
|
|
LocalizationHelper.GetString("DivisionProcessor_Normalize"),
|
|
typeof(bool),
|
|
true,
|
|
null,
|
|
null,
|
|
LocalizationHelper.GetString("DivisionProcessor_Normalize_Desc")));
|
|
_logger.Debug("InitializeParameters");
|
|
}
|
|
|
|
public override Image<Gray, byte> Process(Image<Gray, byte> inputImage)
|
|
{
|
|
double divisor = GetParameter<double>("Divisor");
|
|
double scale = GetParameter<double>("Scale");
|
|
bool normalize = GetParameter<bool>("Normalize");
|
|
|
|
var floatImage = inputImage.Convert<Gray, float>();
|
|
var result = floatImage / divisor * scale;
|
|
|
|
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));
|
|
}
|
|
}
|
|
_logger.Debug("Process:Divisor = {0}, Scale = {1}, Normalize = {2}", divisor, scale, normalize);
|
|
return result.Convert<Gray, byte>();
|
|
}
|
|
} |