规范类名及命名空间名称
This commit is contained in:
@@ -0,0 +1,149 @@
|
||||
// ============================================================================
|
||||
// Copyright © 2026 Hexagon Technology Center GmbH. All Rights Reserved.
|
||||
// 文件� LineMeasurementProcessor.cs
|
||||
// æ��è¿°: 直线测é‡�ç®—å�,用于测é‡�图åƒ�ä¸ä¸¤ç‚¹ä¹‹é—´çš„è·�ç¦?
|
||||
// 功能:
|
||||
// - 用户指定两个点å��æ ‡ï¼ˆåƒ�ç´ å��æ ‡ï¼?
|
||||
// - 计算两点之间的欧æ°�è·�离(åƒ�ç´ å�•ä½�ï¼?
|
||||
// - 支æŒ�åƒ�ç´ å°ºå¯¸æ ‡å®šï¼Œè¾“å‡ºå®žé™…ç‰©ç�†è·�ç¦?
|
||||
// - 在图åƒ�上绘制测é‡�çº¿å’Œæ ‡æ³¨
|
||||
// - 输出测é‡�结果供å�Žç»å¤„ç�†ä½¿ç”?
|
||||
// 算法: 欧��离计算
|
||||
// 作� �伟 wei.lw.li@hexagon.com
|
||||
// ============================================================================
|
||||
|
||||
using Emgu.CV;
|
||||
using Emgu.CV.Structure;
|
||||
using Serilog;
|
||||
using System.Drawing;
|
||||
using XP.ImageProcessing.Core;
|
||||
|
||||
namespace XP.ImageProcessing.Processors;
|
||||
|
||||
/// <summary>
|
||||
/// 直线测é‡�ç®—å� - 测é‡�两点之间的è·�ç¦?
|
||||
/// </summary>
|
||||
public class LineMeasurementProcessor : ImageProcessorBase
|
||||
{
|
||||
private static readonly ILogger _logger = Log.ForContext<LineMeasurementProcessor>();
|
||||
|
||||
public LineMeasurementProcessor()
|
||||
{
|
||||
Name = LocalizationHelper.GetString("LineMeasurementProcessor_Name");
|
||||
Description = LocalizationHelper.GetString("LineMeasurementProcessor_Description");
|
||||
}
|
||||
|
||||
protected override void InitializeParameters()
|
||||
{
|
||||
Parameters.Add("X1", new ProcessorParameter(
|
||||
"X1",
|
||||
LocalizationHelper.GetString("LineMeasurementProcessor_X1"),
|
||||
typeof(int), 100, null, null,
|
||||
LocalizationHelper.GetString("LineMeasurementProcessor_X1_Desc"))
|
||||
{ IsVisible = false });
|
||||
|
||||
Parameters.Add("Y1", new ProcessorParameter(
|
||||
"Y1",
|
||||
LocalizationHelper.GetString("LineMeasurementProcessor_Y1"),
|
||||
typeof(int), 100, null, null,
|
||||
LocalizationHelper.GetString("LineMeasurementProcessor_Y1_Desc"))
|
||||
{ IsVisible = false });
|
||||
|
||||
Parameters.Add("X2", new ProcessorParameter(
|
||||
"X2",
|
||||
LocalizationHelper.GetString("LineMeasurementProcessor_X2"),
|
||||
typeof(int), 400, null, null,
|
||||
LocalizationHelper.GetString("LineMeasurementProcessor_X2_Desc"))
|
||||
{ IsVisible = false });
|
||||
|
||||
Parameters.Add("Y2", new ProcessorParameter(
|
||||
"Y2",
|
||||
LocalizationHelper.GetString("LineMeasurementProcessor_Y2"),
|
||||
typeof(int), 400, null, null,
|
||||
LocalizationHelper.GetString("LineMeasurementProcessor_Y2_Desc"))
|
||||
{ IsVisible = false });
|
||||
|
||||
Parameters.Add("PixelSize", new ProcessorParameter(
|
||||
"PixelSize",
|
||||
LocalizationHelper.GetString("LineMeasurementProcessor_PixelSize"),
|
||||
typeof(double), 1.0, null, null,
|
||||
LocalizationHelper.GetString("LineMeasurementProcessor_PixelSize_Desc")));
|
||||
|
||||
Parameters.Add("Unit", new ProcessorParameter(
|
||||
"Unit",
|
||||
LocalizationHelper.GetString("LineMeasurementProcessor_Unit"),
|
||||
typeof(string), "px", null, null,
|
||||
LocalizationHelper.GetString("LineMeasurementProcessor_Unit_Desc"),
|
||||
new string[] { "px", "mm", "μm", "cm" }));
|
||||
|
||||
Parameters.Add("Thickness", new ProcessorParameter(
|
||||
"Thickness",
|
||||
LocalizationHelper.GetString("LineMeasurementProcessor_Thickness"),
|
||||
typeof(int), 2, 1, 10,
|
||||
LocalizationHelper.GetString("LineMeasurementProcessor_Thickness_Desc")));
|
||||
|
||||
Parameters.Add("ShowLabel", new ProcessorParameter(
|
||||
"ShowLabel",
|
||||
LocalizationHelper.GetString("LineMeasurementProcessor_ShowLabel"),
|
||||
typeof(bool), true, null, null,
|
||||
LocalizationHelper.GetString("LineMeasurementProcessor_ShowLabel_Desc")));
|
||||
}
|
||||
|
||||
public override Image<Gray, byte> Process(Image<Gray, byte> inputImage)
|
||||
{
|
||||
int x1 = GetParameter<int>("X1");
|
||||
int y1 = GetParameter<int>("Y1");
|
||||
int x2 = GetParameter<int>("X2");
|
||||
int y2 = GetParameter<int>("Y2");
|
||||
double pixelSize = GetParameter<double>("PixelSize");
|
||||
string unit = GetParameter<string>("Unit");
|
||||
int thickness = GetParameter<int>("Thickness");
|
||||
bool showLabel = GetParameter<bool>("ShowLabel");
|
||||
|
||||
_logger.Debug("LineMeasurement: ({X1},{Y1}) -> ({X2},{Y2}), PixelSize={PixelSize}, Unit={Unit}",
|
||||
x1, y1, x2, y2, pixelSize, unit);
|
||||
|
||||
OutputData.Clear();
|
||||
|
||||
// é™�制å��æ ‡åœ¨å›¾åƒ�范围内
|
||||
x1 = Math.Clamp(x1, 0, inputImage.Width - 1);
|
||||
y1 = Math.Clamp(y1, 0, inputImage.Height - 1);
|
||||
x2 = Math.Clamp(x2, 0, inputImage.Width - 1);
|
||||
y2 = Math.Clamp(y2, 0, inputImage.Height - 1);
|
||||
|
||||
// 计算åƒ�ç´ è·�离
|
||||
double dx = x2 - x1;
|
||||
double dy = y2 - y1;
|
||||
double pixelDistance = Math.Sqrt(dx * dx + dy * dy);
|
||||
|
||||
// 计算实际�离
|
||||
double actualDistance = pixelDistance * pixelSize;
|
||||
|
||||
// 计算角度(相对于水平方��
|
||||
double angleRad = Math.Atan2(dy, dx);
|
||||
double angleDeg = angleRad * 180.0 / Math.PI;
|
||||
|
||||
// å˜å‚¨æµ‹é‡�结果
|
||||
OutputData["MeasurementType"] = "Line";
|
||||
OutputData["Point1"] = new Point(x1, y1);
|
||||
OutputData["Point2"] = new Point(x2, y2);
|
||||
OutputData["PixelDistance"] = pixelDistance;
|
||||
OutputData["ActualDistance"] = actualDistance;
|
||||
OutputData["Unit"] = unit;
|
||||
OutputData["Angle"] = angleDeg;
|
||||
OutputData["Thickness"] = thickness;
|
||||
OutputData["ShowLabel"] = showLabel;
|
||||
|
||||
// 构建测�信�文本
|
||||
string distanceText = unit == "px"
|
||||
? $"{pixelDistance:F2} px"
|
||||
: $"{actualDistance:F4} {unit} ({pixelDistance:F2} px)";
|
||||
|
||||
OutputData["MeasurementText"] = distanceText;
|
||||
|
||||
_logger.Information("LineMeasurement completed: Distance={Distance}, Angle={Angle:F2}°",
|
||||
distanceText, angleDeg);
|
||||
|
||||
return inputImage.Clone();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user