修复注释乱码

This commit is contained in:
李伟
2026-04-14 17:11:31 +08:00
parent b8bcefc84b
commit cd03e30bb8
58 changed files with 761 additions and 767 deletions
@@ -1,27 +1,27 @@
// ============================================================================
// Copyright © 2026 Hexagon Technology Center GmbH. All Rights Reserved.
// ? HistogramOverlayProcessor.cs
// 文件名: HistogramOverlayProcessor.cs
// 描述: 直方图叠加算子,计算灰度直方图并以蓝色柱状图绘制到结果图像左上角
// 功能:
// - 计算输入图像的灰度直方图
// - 孵㦛蝏睃銝箄𤩺梁𠶖撌虫閫?
// - 颲枏枂湔䲮霈∟”潭㺭?
// 蝞埈: 啣漲湔䲮霈?+ 敶抵𠧧
// 雿𡏭? 𦒘 wei.lw.li@hexagon.com
// - 将直方图绘制为蓝色半透明柱状图叠加到图像左上角
// - 输出直方图统计表格数据
// 算法: 灰度直方图统计 + 彩色图像叠加
// 作者: 李伟 wei.lw.li@hexagon.com
// ============================================================================
using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;
using XP.ImageProcessing.Core;
using Serilog;
using System.Drawing;
using System.Text;
using XP.ImageProcessing.Core;
namespace XP.ImageProcessing.Processors;
/// <summary>
/// 湔䲮摮琜霈∠啣漲湔䲮曉僎隞亥嗅㦛蝏睃𨅯㦛誩椰銝𡃏霈∟”?
/// 直方图叠加算子,计算灰度直方图并以蓝色柱状图绘制到结果图像左上角,同时输出统计表格
/// </summary>
public class HistogramOverlayProcessor : ImageProcessorBase
{
@@ -29,11 +29,10 @@ public class HistogramOverlayProcessor : ImageProcessorBase
// 固定参数
private const int ChartWidth = 256; // 柱状图绘图区宽度
private const int ChartHeight = 200; // 柱状图绘图区高度
private const int AxisMarginLeft = 50; // Y頧湔蝑暸坔捐摨?
private const int AxisMarginBottom = 25; // X頧湔蝑暸摨?
private const int Padding = 8; // 峕艶憸嘥器頝?
private const int AxisMarginLeft = 50; // Y轴标签预留宽度
private const int AxisMarginBottom = 25; // X轴标签预留高度
private const int Padding = 8; // 背景额外内边距
private const int PaddingRight = 25; // 右侧额外内边距(容纳X轴末尾刻度文字)
private const int Margin = 10; // 距图像左上角边距
private const float BgAlpha = 0.6f;
@@ -48,7 +47,7 @@ public class HistogramOverlayProcessor : ImageProcessorBase
protected override void InitializeParameters()
{
// 惩虾靚?
// 无可调参数
}
public override Image<Gray, byte> Process(Image<Gray, byte> inputImage)
@@ -57,7 +56,7 @@ public class HistogramOverlayProcessor : ImageProcessorBase
int w = inputImage.Width;
var srcData = inputImage.Data;
// === 1. 霈∠啣漲湔䲮?===
// === 1. 计算灰度直方图 ===
var hist = new int[256];
for (int y = 0; y < h; y++)
for (int x = 0; x < w; x++)
@@ -96,15 +95,15 @@ public class HistogramOverlayProcessor : ImageProcessorBase
// === 3. 输出表格数据 ===
var sb = new StringBuilder();
sb.AppendLine("=== 啣漲湔䲮霈?===");
sb.AppendLine("=== 灰度直方图统计 ===");
sb.AppendLine($"图像尺寸: {w} x {h}");
sb.AppendLine($"总像素数: {totalPixels}");
sb.AppendLine($"撠讐摨? {minVal}");
sb.AppendLine($"憭抒摨? {maxVal}");
sb.AppendLine($"最小灰度: {minVal}");
sb.AppendLine($"最大灰度: {maxVal}");
sb.AppendLine($"平均灰度: {mean:F2}");
sb.AppendLine($"中位灰度: {medianVal}");
sb.AppendLine($"隡埈㺭啣漲: {modeVal} (箇緵 {modeCount} 甈?");
sb.AppendLine($"撌? {stdDev:F2}");
sb.AppendLine($"众数灰度: {modeVal} (出现 {modeCount} 次)");
sb.AppendLine($"标准差: {stdDev:F2}");
sb.AppendLine();
sb.AppendLine("灰度值\t像素数\t占比(%)");
for (int i = 0; i < 256; i++)
@@ -120,8 +119,8 @@ public class HistogramOverlayProcessor : ImageProcessorBase
var colorImage = inputImage.Convert<Bgr, byte>();
var colorData = colorImage.Data;
// 嚗朞臬躹?Padding + Y頧湔蝑?+ 蝏睃㦛?+ Padding偌撟喉
// Padding + 蝏睃㦛?+ X頧湔蝑?+ Padding
// 布局:背景区域包含 Padding + Y轴标签 + 绘图区 + Padding(水平)
// Padding + 绘图区 + X轴标签 + Padding(垂直)
int totalW = Padding + AxisMarginLeft + ChartWidth + PaddingRight;
int totalH = Padding + ChartHeight + AxisMarginBottom + Padding;
int bgW = Math.Min(totalW, w - Margin);
@@ -133,7 +132,7 @@ public class HistogramOverlayProcessor : ImageProcessorBase
int plotH = Math.Min(ChartHeight, bgH - Padding - AxisMarginBottom - Padding);
if (plotW <= 0 || plotH <= 0) goto SkipOverlay;
// 蝏睃㦛箏椰銝𡃏典㦛譍葉?
// 绘图区左上角在图像中的坐标
int plotX0 = Margin + Padding + AxisMarginLeft;
int plotY0 = Margin + Padding;
@@ -164,7 +163,7 @@ public class HistogramOverlayProcessor : ImageProcessorBase
}
});
// 蝏睃肽𠧧梁𠶖?
// 绘制蓝色柱状图
Parallel.For(0, plotH, dy =>
{
int imgY = plotY0 + dy;
@@ -188,7 +187,7 @@ public class HistogramOverlayProcessor : ImageProcessorBase
}
});
// === 5. 蝏睃頧渡瑪摨行瘜?===
// === 5. 绘制坐标轴线和刻度标注 ===
var white = new MCvScalar(255, 255, 255);
var gray = new MCvScalar(180, 180, 180);
@@ -204,7 +203,7 @@ public class HistogramOverlayProcessor : ImageProcessorBase
new Point(plotX0 + plotW, plotY0 + plotH),
white, 1);
// X頧游摨? 0, 64, 128, 192, 255
// X轴刻度: 0, 64, 128, 192, 255
int[] xTicks = { 0, 64, 128, 192, 255 };
foreach (int tick in xTicks)
{
@@ -220,7 +219,7 @@ public class HistogramOverlayProcessor : ImageProcessorBase
FontFace.HersheySimplex, FontScale, white, FontThickness);
}
// Y頧游摨? 0%, 25%, 50%, 75%, 100%
// Y轴刻度: 0%, 25%, 50%, 75%, 100%
for (int i = 0; i <= 4; i++)
{
int val = maxCount * i / 4;
@@ -248,7 +247,7 @@ public class HistogramOverlayProcessor : ImageProcessorBase
}
}
SkipOverlay:
SkipOverlay:
OutputData["PseudoColorImage"] = colorImage;
_logger.Debug("Process completed: histogram overlay, mean={Mean:F2}, stdDev={Std:F2}", mean, stdDev);
@@ -256,7 +255,7 @@ public class HistogramOverlayProcessor : ImageProcessorBase
}
/// <summary>
/// 蝝㰘恣唬蛹蝝批摮㛖泵銝莎憒?12345 ?"12.3K"嚗?
/// 格式化像素计数为紧凑字符串(如 12345 "12.3K"
/// </summary>
private static string FormatCount(int count)
{
@@ -264,4 +263,4 @@ public class HistogramOverlayProcessor : ImageProcessorBase
if (count >= 1_000) return $"{count / 1_000.0:F1}K";
return count.ToString();
}
}
}