100 lines
3.1 KiB
C#
100 lines
3.1 KiB
C#
// ============================================================================
|
|
// Copyright © 2026 Hexagon Technology Center GmbH. All Rights Reserved.
|
|
// 文件名: GammaProcessor.cs
|
|
// 描述: Gamma校正算子,用于调整图像亮度和对比度
|
|
// 功能:
|
|
// - Gamma非线性校正
|
|
// - 增益调整
|
|
// - 使用查找表(LUT)加速处理
|
|
// - 适用于图像显示和亮度调整
|
|
// 算法: Gamma校正公式 output = (input^(1/gamma)) * gain
|
|
// 作者: 李伟 wei.lw.li@hexagon.com
|
|
// ============================================================================
|
|
|
|
using Emgu.CV;
|
|
using Emgu.CV.Structure;
|
|
using XP.ImageProcessing.Core;
|
|
using Serilog;
|
|
|
|
namespace XP.ImageProcessing.Processors;
|
|
|
|
/// <summary>
|
|
/// Gamma校正算子
|
|
/// </summary>
|
|
public class GammaProcessor : ImageProcessorBase
|
|
{
|
|
private byte[] _lookupTable;
|
|
private static readonly ILogger _logger = Log.ForContext<GammaProcessor>();
|
|
|
|
public GammaProcessor()
|
|
{
|
|
Name = LocalizationHelper.GetString("GammaProcessor_Name");
|
|
Description = LocalizationHelper.GetString("GammaProcessor_Description");
|
|
_lookupTable = new byte[256];
|
|
}
|
|
|
|
protected override void InitializeParameters()
|
|
{
|
|
Parameters.Add("Gamma", new ProcessorParameter(
|
|
"Gamma",
|
|
LocalizationHelper.GetString("GammaProcessor_Gamma"),
|
|
typeof(double),
|
|
1.0,
|
|
0.1,
|
|
5.0,
|
|
LocalizationHelper.GetString("GammaProcessor_Gamma_Desc")));
|
|
|
|
Parameters.Add("Gain", new ProcessorParameter(
|
|
"Gain",
|
|
LocalizationHelper.GetString("GammaProcessor_Gain"),
|
|
typeof(double),
|
|
1.0,
|
|
0.1,
|
|
3.0,
|
|
LocalizationHelper.GetString("GammaProcessor_Gain_Desc")));
|
|
_logger.Debug("InitializeParameters");
|
|
}
|
|
|
|
public override Image<Gray, byte> Process(Image<Gray, byte> inputImage)
|
|
{
|
|
double gamma = GetParameter<double>("Gamma");
|
|
double gain = GetParameter<double>("Gain");
|
|
|
|
BuildLookupTable(gamma, gain);
|
|
|
|
var result = inputImage.Clone();
|
|
ApplyLookupTable(result);
|
|
_logger.Debug("Process:Gamma = {0}, Gain = {1}", gamma, gain);
|
|
return result;
|
|
}
|
|
|
|
private void BuildLookupTable(double gamma, double gain)
|
|
{
|
|
double invGamma = 1.0 / gamma;
|
|
|
|
for (int i = 0; i < 256; i++)
|
|
{
|
|
double normalized = i / 255.0;
|
|
double corrected = Math.Pow(normalized, invGamma) * gain;
|
|
int value = (int)(corrected * 255.0);
|
|
|
|
_lookupTable[i] = (byte)Math.Max(0, Math.Min(255, value));
|
|
}
|
|
_logger.Debug("Gamma and gain values recorded: gamma = {Gamma}, gain = {Gain}", gamma, gain);
|
|
}
|
|
|
|
private void ApplyLookupTable(Image<Gray, byte> image)
|
|
{
|
|
int width = image.Width;
|
|
int height = image.Height;
|
|
|
|
for (int y = 0; y < height; y++)
|
|
{
|
|
for (int x = 0; x < width; x++)
|
|
{
|
|
byte pixelValue = image.Data[y, x, 0];
|
|
image.Data[y, x, 0] = _lookupTable[pixelValue];
|
|
}
|
|
}
|
|
}
|
|
} |