127 lines
4.7 KiB
C#
127 lines
4.7 KiB
C#
// ============================================================================
|
|
// Copyright © 2016-2025 Hexagon Technology Center GmbH. All Rights Reserved.
|
|
// 文件� SubPixelZoomProcessor.cs
|
|
// æ��è¿°: 亚åƒ�ç´ æ”¾å¤§ç®—å�,通过高质é‡�æ�’值实现图åƒ�的亚åƒ�ç´ çº§æ”¾å¤§
|
|
// 功能:
|
|
// - 支�任��率放大(��数�率�1.5x�.3x�
|
|
// - 多��值方法(最近邻��线性��三次�Lanczos�
|
|
// - �选�化补�(抵消�值模糊)
|
|
// - �选指定输出尺�
|
|
// 算法: 基于 OpenCV Resize 的高质��值放�
|
|
// 作� �伟 wei.lw.li@hexagon.com
|
|
// ============================================================================
|
|
|
|
using Emgu.CV;
|
|
using Emgu.CV.CvEnum;
|
|
using Emgu.CV.Structure;
|
|
using Serilog;
|
|
using System.Drawing;
|
|
using XP.ImageProcessing.Core;
|
|
|
|
namespace XP.ImageProcessing.Processors;
|
|
|
|
/// <summary>
|
|
/// 亚åƒ�ç´ æ”¾å¤§ç®—å?
|
|
/// </summary>
|
|
public class SubPixelZoomProcessor : ImageProcessorBase
|
|
{
|
|
private static readonly ILogger _logger = Log.ForContext<SubPixelZoomProcessor>();
|
|
|
|
public SubPixelZoomProcessor()
|
|
{
|
|
Name = LocalizationHelper.GetString("SubPixelZoomProcessor_Name");
|
|
Description = LocalizationHelper.GetString("SubPixelZoomProcessor_Description");
|
|
}
|
|
|
|
protected override void InitializeParameters()
|
|
{
|
|
Parameters.Add("ScaleFactor", new ProcessorParameter(
|
|
"ScaleFactor",
|
|
LocalizationHelper.GetString("SubPixelZoomProcessor_ScaleFactor"),
|
|
typeof(double),
|
|
2.0,
|
|
1.0,
|
|
16.0,
|
|
LocalizationHelper.GetString("SubPixelZoomProcessor_ScaleFactor_Desc")));
|
|
|
|
Parameters.Add("Interpolation", new ProcessorParameter(
|
|
"Interpolation",
|
|
LocalizationHelper.GetString("SubPixelZoomProcessor_Interpolation"),
|
|
typeof(string),
|
|
"Lanczos",
|
|
null,
|
|
null,
|
|
LocalizationHelper.GetString("SubPixelZoomProcessor_Interpolation_Desc"),
|
|
new string[] { "Nearest", "Bilinear", "Bicubic", "Lanczos" }));
|
|
|
|
Parameters.Add("SharpenAfter", new ProcessorParameter(
|
|
"SharpenAfter",
|
|
LocalizationHelper.GetString("SubPixelZoomProcessor_SharpenAfter"),
|
|
typeof(bool),
|
|
false,
|
|
null,
|
|
null,
|
|
LocalizationHelper.GetString("SubPixelZoomProcessor_SharpenAfter_Desc")));
|
|
|
|
Parameters.Add("SharpenStrength", new ProcessorParameter(
|
|
"SharpenStrength",
|
|
LocalizationHelper.GetString("SubPixelZoomProcessor_SharpenStrength"),
|
|
typeof(double),
|
|
0.5,
|
|
0.1,
|
|
3.0,
|
|
LocalizationHelper.GetString("SubPixelZoomProcessor_SharpenStrength_Desc")));
|
|
|
|
_logger.Debug("InitializeParameters");
|
|
}
|
|
|
|
public override Image<Gray, byte> Process(Image<Gray, byte> inputImage)
|
|
{
|
|
double scaleFactor = GetParameter<double>("ScaleFactor");
|
|
string interpolation = GetParameter<string>("Interpolation");
|
|
bool sharpenAfter = GetParameter<bool>("SharpenAfter");
|
|
double sharpenStrength = GetParameter<double>("SharpenStrength");
|
|
|
|
Inter interMethod = interpolation switch
|
|
{
|
|
"Nearest" => Inter.Nearest,
|
|
"Bilinear" => Inter.Linear,
|
|
"Bicubic" => Inter.Cubic,
|
|
_ => Inter.Lanczos4
|
|
};
|
|
|
|
int newWidth = (int)Math.Round(inputImage.Width * scaleFactor);
|
|
int newHeight = (int)Math.Round(inputImage.Height * scaleFactor);
|
|
|
|
// 确�最�尺寸为 1
|
|
newWidth = Math.Max(1, newWidth);
|
|
newHeight = Math.Max(1, newHeight);
|
|
|
|
var result = new Image<Gray, byte>(newWidth, newHeight);
|
|
CvInvoke.Resize(inputImage, result, new Size(newWidth, newHeight), 0, 0, interMethod);
|
|
|
|
// �化补�
|
|
if (sharpenAfter)
|
|
{
|
|
// Unsharp Masking: result = result + strength * (result - blur)
|
|
int ksize = Math.Max(3, (int)(scaleFactor * 2) | 1); // 奇数æ ?
|
|
using var blurred = result.SmoothGaussian(ksize);
|
|
|
|
for (int y = 0; y < newHeight; y++)
|
|
{
|
|
for (int x = 0; x < newWidth; x++)
|
|
{
|
|
float val = result.Data[y, x, 0];
|
|
float blur = blurred.Data[y, x, 0];
|
|
float sharpened = val + (float)(sharpenStrength * (val - blur));
|
|
result.Data[y, x, 0] = (byte)Math.Clamp((int)sharpened, 0, 255);
|
|
}
|
|
}
|
|
}
|
|
|
|
_logger.Debug("Process: Scale={Scale}, Interp={Interp}, Size={W}x{H}, Sharpen={Sharpen}",
|
|
scaleFactor, interpolation, newWidth, newHeight, sharpenAfter);
|
|
|
|
return result;
|
|
}
|
|
} |