规范类名及命名空间名称
This commit is contained in:
@@ -0,0 +1,181 @@
|
||||
// ============================================================================
|
||||
// Copyright © 2026 Hexagon Technology Center GmbH. All Rights Reserved.
|
||||
// 文件�? ImageProcessorBase.cs
|
||||
// 描述: 8位图像处理算子基类,定义图像处理算子的通用接口和行�?
|
||||
// 功能:
|
||||
// - 定义算子的基本属性(名称、描述)
|
||||
// - 参数管理(设置、获取、验证)
|
||||
// - ROI(感兴趣区域)处理支�?
|
||||
// - 输出数据管理(用于传递额外信息如轮廓等)
|
||||
// - 为所�?位图像处理算子提供统一的基础框架
|
||||
// 设计模式: 模板方法模式
|
||||
// 作�? 李伟 wei.lw.li@hexagon.com
|
||||
// ============================================================================
|
||||
|
||||
using Emgu.CV;
|
||||
using Emgu.CV.Structure;
|
||||
using Emgu.CV.Util;
|
||||
|
||||
namespace XP.ImageProcessing.Core;
|
||||
|
||||
/// <summary>
|
||||
/// 图像处理算子基类
|
||||
/// </summary>
|
||||
public abstract class ImageProcessorBase
|
||||
{
|
||||
/// <summary>算子名称</summary>
|
||||
public string Name { get; protected set; } = string.Empty;
|
||||
|
||||
/// <summary>算子描述</summary>
|
||||
public string Description { get; protected set; } = string.Empty;
|
||||
|
||||
/// <summary>参数字典</summary>
|
||||
protected Dictionary<string, ProcessorParameter> Parameters { get; set; }
|
||||
|
||||
/// <summary>输出数据(用于传递额外信息如轮廓等)</summary>
|
||||
public Dictionary<string, object> OutputData { get; protected set; }
|
||||
|
||||
/// <summary>ROI区域</summary>
|
||||
public System.Drawing.Rectangle? ROI { get; set; }
|
||||
|
||||
/// <summary>多边形ROI点集</summary>
|
||||
public System.Drawing.Point[]? PolygonROIPoints { get; set; }
|
||||
|
||||
protected ImageProcessorBase()
|
||||
{
|
||||
Parameters = new Dictionary<string, ProcessorParameter>();
|
||||
OutputData = new Dictionary<string, object>();
|
||||
InitializeParameters();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 初始化算子参数(子类实现�?
|
||||
/// </summary>
|
||||
protected abstract void InitializeParameters();
|
||||
|
||||
/// <summary>
|
||||
/// 执行图像处理(子类实现)
|
||||
/// </summary>
|
||||
public abstract Image<Gray, byte> Process(Image<Gray, byte> inputImage);
|
||||
|
||||
/// <summary>
|
||||
/// 执行图像处理(带矩形ROI支持�?
|
||||
/// </summary>
|
||||
public Image<Gray, byte> ProcessWithROI(Image<Gray, byte> inputImage)
|
||||
{
|
||||
if (ROI.HasValue && ROI.Value != System.Drawing.Rectangle.Empty)
|
||||
{
|
||||
inputImage.ROI = ROI.Value;
|
||||
var roiImage = inputImage.Copy();
|
||||
inputImage.ROI = System.Drawing.Rectangle.Empty;
|
||||
|
||||
var processedROI = Process(roiImage);
|
||||
|
||||
// �?ROI 偏移量保存到输出数据中,供轮廓绘制等使用
|
||||
OutputData["ROIOffset"] = new System.Drawing.Point(ROI.Value.X, ROI.Value.Y);
|
||||
|
||||
var result = inputImage.Clone();
|
||||
result.ROI = ROI.Value;
|
||||
processedROI.CopyTo(result);
|
||||
result.ROI = System.Drawing.Rectangle.Empty;
|
||||
|
||||
roiImage.Dispose();
|
||||
processedROI.Dispose();
|
||||
return result;
|
||||
}
|
||||
return Process(inputImage);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 执行图像处理(带多边形ROI掩码支持�?
|
||||
/// </summary>
|
||||
public Image<Gray, byte> ProcessWithPolygonROI(Image<Gray, byte> inputImage)
|
||||
{
|
||||
if (PolygonROIPoints == null || PolygonROIPoints.Length < 3)
|
||||
{
|
||||
return Process(inputImage);
|
||||
}
|
||||
|
||||
// 创建掩码
|
||||
var mask = new Image<Gray, byte>(inputImage.Width, inputImage.Height);
|
||||
mask.SetValue(new Gray(0));
|
||||
|
||||
// 绘制多边形掩码(白色表示ROI区域�?
|
||||
using (var vop = new VectorOfPoint(PolygonROIPoints))
|
||||
{
|
||||
using (var vvop = new VectorOfVectorOfPoint(vop))
|
||||
{
|
||||
CvInvoke.DrawContours(mask, vvop, 0, new MCvScalar(255), -1);
|
||||
}
|
||||
}
|
||||
|
||||
// 处理整个图像
|
||||
var processedImage = Process(inputImage);
|
||||
|
||||
// 创建结果图像
|
||||
var result = inputImage.Clone();
|
||||
|
||||
// 使用掩码:ROI内使用处理后的像素,ROI外保持原始像�?
|
||||
for (int y = 0; y < inputImage.Height; y++)
|
||||
{
|
||||
for (int x = 0; x < inputImage.Width; x++)
|
||||
{
|
||||
if (mask.Data[y, x, 0] > 0) // 在ROI�?
|
||||
{
|
||||
result.Data[y, x, 0] = processedImage.Data[y, x, 0];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 保存ROI信息
|
||||
OutputData["ROIMask"] = mask;
|
||||
OutputData["PolygonPoints"] = PolygonROIPoints;
|
||||
OutputData["ROIOffset"] = System.Drawing.Point.Empty;
|
||||
|
||||
processedImage.Dispose();
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取所有参数列�?
|
||||
/// </summary>
|
||||
public List<ProcessorParameter> GetParameters()
|
||||
{
|
||||
return new List<ProcessorParameter>(Parameters.Values);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置参数�?
|
||||
/// </summary>
|
||||
public void SetParameter(string name, object value)
|
||||
{
|
||||
if (Parameters.ContainsKey(name))
|
||||
{
|
||||
Parameters[name].Value = value;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new ArgumentException($"参数 {name} 不存在");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取参数值
|
||||
/// </summary>
|
||||
public T GetParameter<T>(string name)
|
||||
{
|
||||
if (Parameters.ContainsKey(name))
|
||||
{
|
||||
return (T)Convert.ChangeType(Parameters[name].Value, typeof(T))!;
|
||||
}
|
||||
throw new ArgumentException($"参数 {name} 不存在");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取单个参数
|
||||
/// </summary>
|
||||
public ProcessorParameter? GetParameterInfo(string name)
|
||||
{
|
||||
return Parameters.ContainsKey(name) ? Parameters[name] : null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
// ============================================================================
|
||||
// Copyright 穢 2026 Hexagon Technology Center GmbH. All Rights Reserved.
|
||||
// ��辣�? ProcessorParameter.cs
|
||||
// �讛膩: �曉�憭��蝞堒���㺭摰帋�蝐鳴��其��讛膩蝞堒���虾�滨蔭��㺭
|
||||
// �蠘�:
|
||||
// - 摰帋���㺭��抅�砍��改��滨妍��掩�卝���霈文�潘�
|
||||
// - �舀���㺭��凒蝥行�嚗��撠誩�潦���憭批�潘�
|
||||
// - �舀��帋蜀蝐餃���㺭嚗���厰�厰★嚗?
|
||||
// - �𣂷���㺭�讛膩靽⊥��其�UI�曄內
|
||||
// - 蝏煺�����啁恣��㦤�?
|
||||
// 雿𡏭�? �𦒘� wei.lw.li@hexagon.com
|
||||
// ============================================================================
|
||||
|
||||
namespace XP.ImageProcessing.Core;
|
||||
|
||||
/// <summary>
|
||||
/// �曉�憭��蝞堒���㺭摰帋�
|
||||
/// </summary>
|
||||
public class ProcessorParameter
|
||||
{
|
||||
/// <summary>��㺭�滨妍嚗�誨��葉雿輻鍂嚗?/summary>
|
||||
public string Name { get; set; }
|
||||
|
||||
/// <summary>�曄內�滨妍嚗㇎I銝剜遬蝷綽�</summary>
|
||||
public string DisplayName { get; set; }
|
||||
|
||||
/// <summary>��㺭蝐餃�</summary>
|
||||
public Type ValueType { get; set; }
|
||||
|
||||
/// <summary>敶枏��?/summary>
|
||||
public object Value { get; set; }
|
||||
|
||||
/// <summary>��撠誩�潘��舫�㚁�</summary>
|
||||
public object? MinValue { get; set; }
|
||||
|
||||
/// <summary>��憭批�潘��舫�㚁�</summary>
|
||||
public object? MaxValue { get; set; }
|
||||
|
||||
/// <summary>��㺭�讛膩</summary>
|
||||
public string Description { get; set; }
|
||||
|
||||
/// <summary>�舫�匧�澆�銵剁��其�銝𧢲�獢��</summary>
|
||||
public string[]? Options { get; set; }
|
||||
|
||||
/// <summary>��㺭�臬炏�航�</summary>
|
||||
public bool IsVisible { get; set; } = true;
|
||||
|
||||
public ProcessorParameter(string name, string displayName, Type valueType, object defaultValue,
|
||||
object? minValue = null, object? maxValue = null, string description = "", string[]? options = null)
|
||||
{
|
||||
Name = name;
|
||||
DisplayName = displayName;
|
||||
ValueType = valueType;
|
||||
Value = defaultValue;
|
||||
MinValue = minValue;
|
||||
MaxValue = maxValue;
|
||||
Description = description;
|
||||
Options = options;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0-windows</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<RootNamespace>XP.ImageProcessing.Core</RootNamespace>
|
||||
<AssemblyName>XP.ImageProcessing.Core</AssemblyName>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Emgu.CV" Version="4.10.0.5680" />
|
||||
<PackageReference Include="Emgu.CV.runtime.windows" Version="4.10.0.5680" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
Reference in New Issue
Block a user