Files
XplorePlane/XP.ImageProcessing.Processors/图像变换/RotateProcessor.cs
T
2026-04-13 14:36:18 +08:00

140 lines
4.9 KiB
C#

// ============================================================================
// Copyright © 2026 Hexagon Technology Center GmbH. All Rights Reserved.
// 文件å? RotateProcessor.cs
// æè¿°: å›¾åƒæ—‹è½¬ç®—å­
// 功能:
// - ä»»æ„角度旋转
// - 支æŒä¿æŒåŽŸå§‹å°ºå¯¸æˆ–è‡ªé€‚åº”æ‰©å±•ç”»å¸ƒ
// - å¯é€‰èƒŒæ™¯å¡«å……å€?
// - 支æŒåŒçº¿æ€§æ’å€?
// 算法: ä»¿å°„å˜æ¢æ—‹è½¬
// 作è€? æŽä¼Ÿ 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 RotateProcessor : ImageProcessorBase
{
private static readonly ILogger _logger = Log.ForContext<RotateProcessor>();
public RotateProcessor()
{
Name = LocalizationHelper.GetString("RotateProcessor_Name");
Description = LocalizationHelper.GetString("RotateProcessor_Description");
}
protected override void InitializeParameters()
{
Parameters.Add("Angle", new ProcessorParameter(
"Angle",
LocalizationHelper.GetString("RotateProcessor_Angle"),
typeof(double),
90.0,
-360.0,
360.0,
LocalizationHelper.GetString("RotateProcessor_Angle_Desc")));
Parameters.Add("ExpandCanvas", new ProcessorParameter(
"ExpandCanvas",
LocalizationHelper.GetString("RotateProcessor_ExpandCanvas"),
typeof(bool),
false,
null,
null,
LocalizationHelper.GetString("RotateProcessor_ExpandCanvas_Desc")));
Parameters.Add("BackgroundValue", new ProcessorParameter(
"BackgroundValue",
LocalizationHelper.GetString("RotateProcessor_BackgroundValue"),
typeof(int),
0,
0,
255,
LocalizationHelper.GetString("RotateProcessor_BackgroundValue_Desc")));
Parameters.Add("Interpolation", new ProcessorParameter(
"Interpolation",
LocalizationHelper.GetString("RotateProcessor_Interpolation"),
typeof(string),
"Bilinear",
null,
null,
LocalizationHelper.GetString("RotateProcessor_Interpolation_Desc"),
new string[] { "Nearest", "Bilinear", "Bicubic" }));
_logger.Debug("InitializeParameters");
}
public override Image<Gray, byte> Process(Image<Gray, byte> inputImage)
{
double angle = GetParameter<double>("Angle");
bool expandCanvas = GetParameter<bool>("ExpandCanvas");
int bgValue = GetParameter<int>("BackgroundValue");
string interpolation = GetParameter<string>("Interpolation");
Inter interMethod = interpolation switch
{
"Nearest" => Inter.Nearest,
"Bicubic" => Inter.Cubic,
_ => Inter.Linear
};
int srcW = inputImage.Width;
int srcH = inputImage.Height;
PointF center = new PointF(srcW / 2.0f, srcH / 2.0f);
// èŽ·å–æ—‹è½¬çŸ©é˜µ
using var rotMat = new Mat();
CvInvoke.GetRotationMatrix2D(center, angle, 1.0, rotMat);
int dstW, dstH;
if (expandCanvas)
{
// 计算旋转åŽèƒ½å®¹çº³æ•´å¹…图åƒçš„画布尺å¯?
double rad = Math.Abs(angle * Math.PI / 180.0);
double sinA = Math.Abs(Math.Sin(rad));
double cosA = Math.Abs(Math.Cos(rad));
dstW = (int)Math.Ceiling(srcW * cosA + srcH * sinA);
dstH = (int)Math.Ceiling(srcW * sinA + srcH * cosA);
// 调整旋转矩阵的平移分é‡ï¼Œä½¿å›¾åƒå±…ä¸?
double[] m = new double[6];
rotMat.CopyTo(m);
m[2] += (dstW - srcW) / 2.0;
m[5] += (dstH - srcH) / 2.0;
// 写回矩阵
using var adjusted = new Mat(2, 3, Emgu.CV.CvEnum.DepthType.Cv64F, 1);
System.Runtime.InteropServices.Marshal.Copy(m, 0, adjusted.DataPointer, 6);
var result = new Image<Gray, byte>(dstW, dstH, new Gray(bgValue));
CvInvoke.WarpAffine(inputImage, result, adjusted, new Size(dstW, dstH),
interMethod, Warp.Default, BorderType.Constant, new MCvScalar(bgValue));
_logger.Debug("Process: Angle={Angle}, ExpandCanvas=true, Size={W}x{H}", angle, dstW, dstH);
return result;
}
else
{
dstW = srcW;
dstH = srcH;
var result = new Image<Gray, byte>(dstW, dstH, new Gray(bgValue));
CvInvoke.WarpAffine(inputImage, result, rotMat, new Size(dstW, dstH),
interMethod, Warp.Default, BorderType.Constant, new MCvScalar(bgValue));
_logger.Debug("Process: Angle={Angle}, ExpandCanvas=false", angle);
return result;
}
}
}