修复注释乱码
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user