合并图像处理库,删除图像lib库

This commit is contained in:
李伟
2026-04-13 13:40:37 +08:00
parent 2a762396d5
commit c7ce4ea6a1
105 changed files with 16341 additions and 133 deletions
@@ -0,0 +1,87 @@
// ============================================================================
// 文件名: AngleMeasurementProcessor.cs
// 描述: 角度测量算子 — 共端点的两条直线夹角
// 功能:
// - 用户定义三个点:端点(顶点)、射线1终点、射线2终点
// - 计算两条射线之间的夹角(0°~180°)
// - 在图像上绘制两条射线、角度弧线和标注
// ============================================================================
using Emgu.CV;
using Emgu.CV.Structure;
using ImageProcessing.Core;
using Serilog;
using System.Drawing;
namespace ImageProcessing.Processors;
public class AngleMeasurementProcessor : ImageProcessorBase
{
private static readonly ILogger _logger = Log.ForContext<AngleMeasurementProcessor>();
public AngleMeasurementProcessor()
{
Name = LocalizationHelper.GetString("AngleMeasurementProcessor_Name");
Description = LocalizationHelper.GetString("AngleMeasurementProcessor_Description");
}
protected override void InitializeParameters()
{
// 三个点坐标(由交互控件注入,使用 double 避免取整误差)
Parameters.Add("VX", new ProcessorParameter("VX", "VX", typeof(double), 250.0, null, null, "") { IsVisible = false });
Parameters.Add("VY", new ProcessorParameter("VY", "VY", typeof(double), 250.0, null, null, "") { IsVisible = false });
Parameters.Add("AX", new ProcessorParameter("AX", "AX", typeof(double), 100.0, null, null, "") { IsVisible = false });
Parameters.Add("AY", new ProcessorParameter("AY", "AY", typeof(double), 250.0, null, null, "") { IsVisible = false });
Parameters.Add("BX", new ProcessorParameter("BX", "BX", typeof(double), 250.0, null, null, "") { IsVisible = false });
Parameters.Add("BY", new ProcessorParameter("BY", "BY", typeof(double), 100.0, null, null, "") { IsVisible = false });
}
public override Image<Gray, byte> Process(Image<Gray, byte> inputImage)
{
double vx = GetParameter<double>("VX"), vy = GetParameter<double>("VY");
double ax = GetParameter<double>("AX"), ay = GetParameter<double>("AY");
double bx = GetParameter<double>("BX"), by = GetParameter<double>("BY");
OutputData.Clear();
// 向量 VA 和 VB
double vax = ax - vx, vay = ay - vy;
double vbx = bx - vx, vby = by - vy;
double lenA = Math.Sqrt(vax * vax + vay * vay);
double lenB = Math.Sqrt(vbx * vbx + vby * vby);
double angleDeg = 0;
if (lenA > 0.001 && lenB > 0.001)
{
double dot = vax * vbx + vay * vby;
double cosAngle = Math.Clamp(dot / (lenA * lenB), -1.0, 1.0);
angleDeg = Math.Acos(cosAngle) * 180.0 / Math.PI;
}
// 计算角度弧的起始角和扫过角(用于绘制弧线)
double angleA = Math.Atan2(vay, vax) * 180.0 / Math.PI;
double angleB = Math.Atan2(vby, vbx) * 180.0 / Math.PI;
// 确保从 angleA 到 angleB 的扫过方向是较小的夹角
double sweep = angleB - angleA;
if (sweep > 180) sweep -= 360;
if (sweep < -180) sweep += 360;
string angleText = $"{angleDeg:F2} deg";
OutputData["AngleMeasurementResult"] = true;
OutputData["Vertex"] = new Point((int)Math.Round(vx), (int)Math.Round(vy));
OutputData["PointA"] = new Point((int)Math.Round(ax), (int)Math.Round(ay));
OutputData["PointB"] = new Point((int)Math.Round(bx), (int)Math.Round(by));
OutputData["AngleDeg"] = angleDeg;
OutputData["ArcStartAngle"] = angleA;
OutputData["ArcSweepAngle"] = sweep;
OutputData["AngleText"] = angleText;
_logger.Information("AngleMeasurement: Angle={Angle}, V=({VX},{VY}), A=({AX},{AY}), B=({BX},{BY})",
angleText, vx, vy, ax, ay, bx, by);
return inputImage.Clone();
}
}
@@ -0,0 +1,403 @@
// ============================================================================
// Copyright © 2026 Hexagon Technology Center GmbH. All Rights Reserved.
// 文件名: BgaVoidRateProcessor.cs
// 描述: BGA 空洞率检测算子(两步自动检测法)
//
// 处理流程:
// 第一步 — 焊球定位: 高斯模糊 → Otsu反向二值化 → 闭运算 → 轮廓检测 → 圆度过滤 → 椭圆拟合
// 第二步 — 气泡检测: 焊球轮廓掩码 → 双阈值分割 → 轮廓检测 → 面积过滤 → 气泡率计算
//
// 支持多边形ROI限定检测区域,支持IPC-7095标准PASS/FAIL判定
// 正片模式:焊球=暗区域,气泡=亮区域
//
// 作者: 李伟 wei.lw.li@hexagon.com
// ============================================================================
using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;
using Emgu.CV.Util;
using ImageProcessing.Core;
using Serilog;
using System.Drawing;
namespace ImageProcessing.Processors;
public class BgaVoidRateProcessor : ImageProcessorBase
{
private static readonly ILogger _logger = Log.ForContext<BgaVoidRateProcessor>();
public BgaVoidRateProcessor()
{
Name = LocalizationHelper.GetString("BgaVoidRateProcessor_Name");
Description = LocalizationHelper.GetString("BgaVoidRateProcessor_Description");
}
protected override void InitializeParameters()
{
// ── ROI限定区域 ──
Parameters.Add("RoiMode", new ProcessorParameter(
"RoiMode",
LocalizationHelper.GetString("BgaVoidRateProcessor_RoiMode"),
typeof(string), "None", null, null,
LocalizationHelper.GetString("BgaVoidRateProcessor_RoiMode_Desc"),
new string[] { "None", "Polygon" }));
// 多边形ROI点数和坐标(由UI注入,不可见,最多支持32个点)
Parameters.Add("PolyCount", new ProcessorParameter("PolyCount", "PolyCount", typeof(int), 0, null, null, "") { IsVisible = false });
for (int i = 0; i < 32; i++)
{
Parameters.Add($"PolyX{i}", new ProcessorParameter($"PolyX{i}", $"PolyX{i}", typeof(int), 0, null, null, "") { IsVisible = false });
Parameters.Add($"PolyY{i}", new ProcessorParameter($"PolyY{i}", $"PolyY{i}", typeof(int), 0, null, null, "") { IsVisible = false });
}
// ── 第一步:BGA定位参数 ──
Parameters.Add("BgaMinArea", new ProcessorParameter(
"BgaMinArea",
LocalizationHelper.GetString("BgaVoidRateProcessor_BgaMinArea"),
typeof(int), 500, 10, 1000000,
LocalizationHelper.GetString("BgaVoidRateProcessor_BgaMinArea_Desc")));
Parameters.Add("BgaMaxArea", new ProcessorParameter(
"BgaMaxArea",
LocalizationHelper.GetString("BgaVoidRateProcessor_BgaMaxArea"),
typeof(int), 500000, 100, 10000000,
LocalizationHelper.GetString("BgaVoidRateProcessor_BgaMaxArea_Desc")));
Parameters.Add("BgaBlurSize", new ProcessorParameter(
"BgaBlurSize",
LocalizationHelper.GetString("BgaVoidRateProcessor_BgaBlurSize"),
typeof(int), 5, 1, 31,
LocalizationHelper.GetString("BgaVoidRateProcessor_BgaBlurSize_Desc")));
Parameters.Add("BgaCircularity", new ProcessorParameter(
"BgaCircularity",
LocalizationHelper.GetString("BgaVoidRateProcessor_BgaCircularity"),
typeof(double), 0.5, 0.0, 1.0,
LocalizationHelper.GetString("BgaVoidRateProcessor_BgaCircularity_Desc")));
// ── 第二步:气泡检测参数 ──
Parameters.Add("MinThreshold", new ProcessorParameter(
"MinThreshold",
LocalizationHelper.GetString("BgaVoidRateProcessor_MinThreshold"),
typeof(int), 128, 0, 255,
LocalizationHelper.GetString("BgaVoidRateProcessor_MinThreshold_Desc")));
Parameters.Add("MaxThreshold", new ProcessorParameter(
"MaxThreshold",
LocalizationHelper.GetString("BgaVoidRateProcessor_MaxThreshold"),
typeof(int), 255, 0, 255,
LocalizationHelper.GetString("BgaVoidRateProcessor_MaxThreshold_Desc")));
Parameters.Add("MinVoidArea", new ProcessorParameter(
"MinVoidArea",
LocalizationHelper.GetString("BgaVoidRateProcessor_MinVoidArea"),
typeof(int), 10, 1, 10000,
LocalizationHelper.GetString("BgaVoidRateProcessor_MinVoidArea_Desc")));
Parameters.Add("VoidLimit", new ProcessorParameter(
"VoidLimit",
LocalizationHelper.GetString("BgaVoidRateProcessor_VoidLimit"),
typeof(double), 25.0, 0.0, 100.0,
LocalizationHelper.GetString("BgaVoidRateProcessor_VoidLimit_Desc")));
Parameters.Add("Thickness", new ProcessorParameter(
"Thickness",
LocalizationHelper.GetString("BgaVoidRateProcessor_Thickness"),
typeof(int), 2, 1, 10,
LocalizationHelper.GetString("BgaVoidRateProcessor_Thickness_Desc")));
}
public override Image<Gray, byte> Process(Image<Gray, byte> inputImage)
{
string roiMode = GetParameter<string>("RoiMode");
int bgaMinArea = GetParameter<int>("BgaMinArea");
int bgaMaxArea = GetParameter<int>("BgaMaxArea");
int bgaBlurSize = GetParameter<int>("BgaBlurSize");
double bgaCircularity = GetParameter<double>("BgaCircularity");
int minThresh = GetParameter<int>("MinThreshold");
int maxThresh = GetParameter<int>("MaxThreshold");
int minVoidArea = GetParameter<int>("MinVoidArea");
double voidLimit = GetParameter<double>("VoidLimit");
int thickness = GetParameter<int>("Thickness");
// 确保模糊核为奇数
if (bgaBlurSize % 2 == 0) bgaBlurSize++;
OutputData.Clear();
int w = inputImage.Width, h = inputImage.Height;
// 构建ROI掩码(限定检测区域)
Image<Gray, byte>? roiMask = null;
if (roiMode == "Polygon")
{
int polyCount = GetParameter<int>("PolyCount");
if (polyCount >= 3)
{
var pts = new Point[polyCount];
for (int i = 0; i < polyCount; i++)
pts[i] = new Point(GetParameter<int>($"PolyX{i}"), GetParameter<int>($"PolyY{i}"));
roiMask = new Image<Gray, byte>(w, h);
using var vop = new VectorOfPoint(pts);
using var vvop = new VectorOfVectorOfPoint(vop);
CvInvoke.DrawContours(roiMask, vvop, 0, new MCvScalar(255), -1);
_logger.Debug("ROI Polygon: {Count} points", polyCount);
}
}
OutputData["RoiMode"] = roiMode;
OutputData["RoiMask"] = roiMask;
_logger.Debug("BgaVoidRate 两步法: BgaArea=[{Min},{Max}], Blur={Blur}, Circ={Circ}, Thresh=[{TMin},{TMax}]",
bgaMinArea, bgaMaxArea, bgaBlurSize, bgaCircularity, minThresh, maxThresh);
// ================================================================
// 第一步:自动检测BGA焊球位置
// ================================================================
var bgaResults = DetectBgaBalls(inputImage, bgaBlurSize, bgaMinArea, bgaMaxArea, bgaCircularity, roiMask);
_logger.Information("第一步完成: 检测到 {Count} 个BGA焊球", bgaResults.Count);
if (bgaResults.Count == 0)
{
OutputData["BgaVoidResult"] = true;
OutputData["BgaCount"] = 0;
OutputData["BgaBalls"] = bgaResults;
OutputData["VoidRate"] = 0.0;
OutputData["Classification"] = "N/A";
OutputData["ResultText"] = "No BGA detected";
OutputData["Thickness"] = thickness;
OutputData["VoidLimit"] = voidLimit;
OutputData["TotalBgaArea"] = 0;
OutputData["TotalVoidArea"] = 0;
OutputData["TotalVoidCount"] = 0;
roiMask?.Dispose();
return inputImage.Clone();
}
// ================================================================
// 第二步:在每个焊球区域内检测气泡
// ================================================================
int totalBgaArea = 0;
int totalVoidArea = 0;
int totalVoidCount = 0;
foreach (var bga in bgaResults)
{
DetectVoidsInBga(inputImage, bga, minThresh, maxThresh, minVoidArea);
totalBgaArea += bga.BgaArea;
totalVoidArea += bga.VoidPixels;
totalVoidCount += bga.Voids.Count;
}
double overallVoidRate = totalBgaArea > 0 ? (double)totalVoidArea / totalBgaArea * 100.0 : 0;
string classification = overallVoidRate <= voidLimit ? "PASS" : "FAIL";
// 检查每个焊球是否单独超标
foreach (var bga in bgaResults)
{
bga.Classification = bga.VoidRate <= voidLimit ? "PASS" : "FAIL";
}
_logger.Information("第二步完成: 总气泡率={VoidRate:F1}%, 气泡数={Count}, 判定={Class}",
overallVoidRate, totalVoidCount, classification);
// 输出数据
OutputData["BgaVoidResult"] = true;
OutputData["BgaCount"] = bgaResults.Count;
OutputData["BgaBalls"] = bgaResults;
OutputData["VoidRate"] = overallVoidRate;
OutputData["FillRate"] = 100.0 - overallVoidRate;
OutputData["TotalBgaArea"] = totalBgaArea;
OutputData["TotalVoidArea"] = totalVoidArea;
OutputData["TotalVoidCount"] = totalVoidCount;
OutputData["VoidLimit"] = voidLimit;
OutputData["Classification"] = classification;
OutputData["Thickness"] = thickness;
OutputData["ResultText"] = $"Void: {overallVoidRate:F1}% | {classification} | BGA×{bgaResults.Count}";
roiMask?.Dispose();
return inputImage.Clone();
}
/// <summary>
/// 第一步:自动检测BGA焊球位置
/// 使用Otsu二值化 + 轮廓检测 + 圆度过滤 + 椭圆拟合
/// </summary>
private List<BgaBallInfo> DetectBgaBalls(Image<Gray, byte> input, int blurSize, int minArea, int maxArea, double minCircularity, Image<Gray, byte>? roiMask)
{
var results = new List<BgaBallInfo>();
int w = input.Width, h = input.Height;
// 高斯模糊降噪
var blurred = new Image<Gray, byte>(w, h);
CvInvoke.GaussianBlur(input, blurred, new Size(blurSize, blurSize), 0);
// Otsu自动二值化(X-Ray正片:焊球=暗区域)
var binary = new Image<Gray, byte>(w, h);
CvInvoke.Threshold(blurred, binary, 0, 255, ThresholdType.Otsu | ThresholdType.BinaryInv);
// 如果有ROI掩码,只保留ROI区域内的二值化结果
if (roiMask != null)
{
CvInvoke.BitwiseAnd(binary, roiMask, binary);
}
// 形态学闭运算填充小孔洞
var kernel = CvInvoke.GetStructuringElement(ElementShape.Ellipse, new Size(5, 5), new Point(-1, -1));
CvInvoke.MorphologyEx(binary, binary, MorphOp.Close, kernel, new Point(-1, -1), 2, BorderType.Default, new MCvScalar(0));
// 查找轮廓
using var contours = new VectorOfVectorOfPoint();
using var hierarchy = new Mat();
CvInvoke.FindContours(binary, contours, hierarchy, RetrType.External, ChainApproxMethod.ChainApproxSimple);
int bgaIndex = 0;
for (int i = 0; i < contours.Size; i++)
{
double area = CvInvoke.ContourArea(contours[i]);
if (area < minArea || area > maxArea) continue;
// 圆度过滤: circularity = 4π × area / perimeter²
double perimeter = CvInvoke.ArcLength(contours[i], true);
if (perimeter < 1) continue;
double circularity = 4.0 * Math.PI * area / (perimeter * perimeter);
if (circularity < minCircularity) continue;
// 需要至少5个点才能拟合椭圆
if (contours[i].Size < 5) continue;
var ellipse = CvInvoke.FitEllipse(contours[i]);
var moments = CvInvoke.Moments(contours[i]);
if (moments.M00 < 1) continue;
bgaIndex++;
results.Add(new BgaBallInfo
{
Index = bgaIndex,
CenterX = moments.M10 / moments.M00,
CenterY = moments.M01 / moments.M00,
FittedEllipse = ellipse,
ContourPoints = contours[i].ToArray(),
BgaArea = (int)area,
Circularity = circularity
});
}
// 按面积从大到小排序
results.Sort((a, b) => b.BgaArea.CompareTo(a.BgaArea));
for (int i = 0; i < results.Count; i++) results[i].Index = i + 1;
blurred.Dispose();
binary.Dispose();
kernel.Dispose();
return results;
}
/// <summary>
/// 第二步:在单个BGA焊球区域内检测气泡
/// 使用焊球轮廓作为掩码,双阈值分割气泡区域
/// </summary>
private void DetectVoidsInBga(Image<Gray, byte> input, BgaBallInfo bga, int minThresh, int maxThresh, int minVoidArea)
{
int w = input.Width, h = input.Height;
// 创建该焊球的掩码
var mask = new Image<Gray, byte>(w, h);
using (var vop = new VectorOfPoint(bga.ContourPoints))
using (var vvop = new VectorOfVectorOfPoint(vop))
{
CvInvoke.DrawContours(mask, vvop, 0, new MCvScalar(255), -1);
}
int bgaPixels = CvInvoke.CountNonZero(mask);
bga.BgaArea = bgaPixels;
// 双阈值分割(正片模式:气泡=亮,灰度在[minThresh, maxThresh]范围内判为气泡)
var voidImg = new Image<Gray, byte>(w, h);
byte[,,] srcData = input.Data;
byte[,,] dstData = voidImg.Data;
byte[,,] maskData = mask.Data;
for (int y = 0; y < h; y++)
{
for (int x = 0; x < w; x++)
{
if (maskData[y, x, 0] > 0)
{
byte val = srcData[y, x, 0];
dstData[y, x, 0] = (val >= minThresh && val <= maxThresh) ? (byte)255 : (byte)0;
}
}
}
int voidPixels = CvInvoke.CountNonZero(voidImg);
bga.VoidPixels = voidPixels;
bga.VoidRate = bgaPixels > 0 ? (double)voidPixels / bgaPixels * 100.0 : 0;
// 检测每个气泡的轮廓
using var contours = new VectorOfVectorOfPoint();
using var hierarchy = new Mat();
CvInvoke.FindContours(voidImg, contours, hierarchy, RetrType.External, ChainApproxMethod.ChainApproxSimple);
for (int i = 0; i < contours.Size; i++)
{
double area = CvInvoke.ContourArea(contours[i]);
if (area < minVoidArea) continue;
var moments = CvInvoke.Moments(contours[i]);
if (moments.M00 < 1) continue;
bga.Voids.Add(new VoidInfo
{
Index = bga.Voids.Count + 1,
CenterX = moments.M10 / moments.M00,
CenterY = moments.M01 / moments.M00,
Area = area,
AreaPercent = bgaPixels > 0 ? area / bgaPixels * 100.0 : 0,
BoundingBox = CvInvoke.BoundingRectangle(contours[i]),
ContourPoints = contours[i].ToArray()
});
}
// 按面积从大到小排序
bga.Voids.Sort((a, b) => b.Area.CompareTo(a.Area));
for (int i = 0; i < bga.Voids.Count; i++) bga.Voids[i].Index = i + 1;
mask.Dispose();
voidImg.Dispose();
}
}
/// <summary>
/// 单个BGA焊球信息
/// </summary>
public class BgaBallInfo
{
public int Index { get; set; }
public double CenterX { get; set; }
public double CenterY { get; set; }
public RotatedRect FittedEllipse { get; set; }
public Point[] ContourPoints { get; set; } = Array.Empty<Point>();
public int BgaArea { get; set; }
public double Circularity { get; set; }
public int VoidPixels { get; set; }
public double VoidRate { get; set; }
public string Classification { get; set; } = "N/A";
public List<VoidInfo> Voids { get; set; } = new();
}
/// <summary>
/// 单个气泡信息
/// </summary>
public class VoidInfo
{
public int Index { get; set; }
public double CenterX { get; set; }
public double CenterY { get; set; }
public double Area { get; set; }
public double AreaPercent { get; set; }
public Rectangle BoundingBox { get; set; }
public Point[] ContourPoints { get; set; } = Array.Empty<Point>();
}
@@ -0,0 +1,254 @@
// ============================================================================
// Copyright © 2026 Hexagon Technology Center GmbH. All Rights Reserved.
// 文件名: ContourProcessor.cs
// 描述: 轮廓查找算子,用于检测和分析图像中的轮廓
// 功能:
// - 检测图像中的外部轮廓
// - 根据面积范围过滤轮廓
// - 计算轮廓的几何特征(面积、周长、中心、外接矩形等)
// - 输出轮廓信息供后续处理使用
// 算法: 基于OpenCV的轮廓检测算法
// 作者: 李伟 wei.lw.li@hexagon.com
// ============================================================================
using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;
using Emgu.CV.Util;
using ImageProcessing.Core;
using Serilog;
using System.Drawing;
namespace ImageProcessing.Processors;
/// <summary>
/// 轮廓查找算子
/// </summary>
public class ContourProcessor : ImageProcessorBase
{
private static readonly ILogger _logger = Log.ForContext<ContourProcessor>();
public ContourProcessor()
{
Name = LocalizationHelper.GetString("ContourProcessor_Name");
Description = LocalizationHelper.GetString("ContourProcessor_Description");
}
protected override void InitializeParameters()
{
Parameters.Add("TargetColor", new ProcessorParameter(
"TargetColor",
LocalizationHelper.GetString("ContourProcessor_TargetColor"),
typeof(string),
"White",
null,
null,
LocalizationHelper.GetString("ContourProcessor_TargetColor_Desc"),
new string[] { "White", "Black" }));
Parameters.Add("UseThreshold", new ProcessorParameter(
"UseThreshold",
LocalizationHelper.GetString("ContourProcessor_UseThreshold"),
typeof(bool),
false,
null,
null,
LocalizationHelper.GetString("ContourProcessor_UseThreshold_Desc")));
Parameters.Add("ThresholdValue", new ProcessorParameter(
"ThresholdValue",
LocalizationHelper.GetString("ContourProcessor_ThresholdValue"),
typeof(int),
120,
0,
255,
LocalizationHelper.GetString("ContourProcessor_ThresholdValue_Desc")));
Parameters.Add("UseOtsu", new ProcessorParameter(
"UseOtsu",
LocalizationHelper.GetString("ContourProcessor_UseOtsu"),
typeof(bool),
false,
null,
null,
LocalizationHelper.GetString("ContourProcessor_UseOtsu_Desc")));
Parameters.Add("MinArea", new ProcessorParameter(
"MinArea",
LocalizationHelper.GetString("ContourProcessor_MinArea"),
typeof(double),
10.0,
0.0,
10000.0,
LocalizationHelper.GetString("ContourProcessor_MinArea_Desc")));
Parameters.Add("MaxArea", new ProcessorParameter(
"MaxArea",
LocalizationHelper.GetString("ContourProcessor_MaxArea"),
typeof(double),
100000.0,
0.0,
1000000.0,
LocalizationHelper.GetString("ContourProcessor_MaxArea_Desc")));
Parameters.Add("Thickness", new ProcessorParameter(
"Thickness",
LocalizationHelper.GetString("ContourProcessor_Thickness"),
typeof(int),
2,
1,
10,
LocalizationHelper.GetString("ContourProcessor_Thickness_Desc")));
_logger.Debug("InitializeParameters");
}
public override Image<Gray, byte> Process(Image<Gray, byte> inputImage)
{
string targetColor = GetParameter<string>("TargetColor");
bool useThreshold = GetParameter<bool>("UseThreshold");
int thresholdValue = GetParameter<int>("ThresholdValue");
bool useOtsu = GetParameter<bool>("UseOtsu");
double minArea = GetParameter<double>("MinArea");
double maxArea = GetParameter<double>("MaxArea");
int thickness = GetParameter<int>("Thickness");
_logger.Debug("Process started: TargetColor = '{TargetColor}', UseThreshold = {UseThreshold}, ThresholdValue = {ThresholdValue}, UseOtsu = {UseOtsu}",
targetColor, useThreshold, thresholdValue, useOtsu);
OutputData.Clear();
// 创建输入图像的副本用于处理
Image<Gray, byte> processImage = inputImage.Clone();
// 步骤1:如果启用阈值分割,先进行二值化
if (useThreshold)
{
_logger.Debug("Applying threshold processing");
Image<Gray, byte> thresholdImage = new Image<Gray, byte>(processImage.Size);
if (useOtsu)
{
// 使用Otsu自动阈值
CvInvoke.Threshold(processImage, thresholdImage, 0, 255, ThresholdType.Otsu);
_logger.Debug("Applied Otsu threshold");
}
else
{
// 使用固定阈值
CvInvoke.Threshold(processImage, thresholdImage, thresholdValue, 255, ThresholdType.Binary);
_logger.Debug("Applied binary threshold with value {ThresholdValue}", thresholdValue);
}
// 保存阈值处理后的图像用于调试
try
{
string debugPath = Path.Combine("logs", $"contour_threshold_{DateTime.Now:yyyyMMdd_HHmmss}.png");
Directory.CreateDirectory("logs");
thresholdImage.Save(debugPath);
_logger.Information("Saved threshold image to: {DebugPath}", debugPath);
}
catch (Exception ex)
{
_logger.Warning(ex, "Failed to save threshold image for debugging");
}
processImage.Dispose();
processImage = thresholdImage;
}
// 步骤2:如果目标是黑色区域,需要反转图像
bool isBlackTarget = targetColor != null &&
(targetColor.Equals("Black", StringComparison.OrdinalIgnoreCase) ||
targetColor.Equals("黑色", StringComparison.OrdinalIgnoreCase));
if (isBlackTarget)
{
_logger.Debug("Inverting image for black region detection");
CvInvoke.BitwiseNot(processImage, processImage);
// 保存翻转后的图像用于调试
try
{
string debugPath = Path.Combine("logs", $"contour_inverted_{DateTime.Now:yyyyMMdd_HHmmss}.png");
Directory.CreateDirectory("logs");
processImage.Save(debugPath);
_logger.Information("Saved inverted image to: {DebugPath}", debugPath);
}
catch (Exception ex)
{
_logger.Warning(ex, "Failed to save inverted image for debugging");
}
}
// 步骤3:查找轮廓
using (VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint())
{
Mat hierarchy = new Mat();
CvInvoke.FindContours(processImage, contours, hierarchy, RetrType.External, ChainApproxMethod.ChainApproxSimple);
_logger.Debug("Found {TotalContours} total contours before filtering", contours.Size);
List<ContourInfo> contourInfos = new();
for (int i = 0; i < contours.Size; i++)
{
double area = CvInvoke.ContourArea(contours[i]);
if (area >= minArea && area <= maxArea)
{
var moments = CvInvoke.Moments(contours[i]);
var boundingRect = CvInvoke.BoundingRectangle(contours[i]);
double perimeter = CvInvoke.ArcLength(contours[i], true);
var circle = CvInvoke.MinEnclosingCircle(contours[i]);
contourInfos.Add(new ContourInfo
{
Index = i,
Area = area,
Perimeter = perimeter,
CenterX = moments.M10 / moments.M00,
CenterY = moments.M01 / moments.M00,
BoundingBox = boundingRect,
Points = contours[i].ToArray(),
CircleCenter = circle.Center,
CircleRadius = circle.Radius
});
_logger.Debug("Contour {Index}: Area = {Area}, Center = ({CenterX:F2}, {CenterY:F2})",
i, area, moments.M10 / moments.M00, moments.M01 / moments.M00);
}
else
{
_logger.Debug("Contour {Index} filtered out: Area = {Area} (not in range {MinArea} - {MaxArea})",
i, area, minArea, maxArea);
}
}
OutputData["ContourCount"] = contourInfos.Count;
OutputData["Contours"] = contourInfos;
OutputData["Thickness"] = thickness;
hierarchy.Dispose();
processImage.Dispose();
_logger.Information("Process completed: TargetColor = '{TargetColor}', Found {ContourCount} contours (filtered from {TotalContours})",
targetColor, contourInfos.Count, contours.Size);
return inputImage.Clone();
}
}
}
/// <summary>
/// 轮廓信息
/// </summary>
public class ContourInfo
{
public int Index { get; set; }
public double Area { get; set; }
public double Perimeter { get; set; }
public double CenterX { get; set; }
public double CenterY { get; set; }
public Rectangle BoundingBox { get; set; }
public Point[] Points { get; set; } = Array.Empty<Point>();
public PointF CircleCenter { get; set; }
public float CircleRadius { get; set; }
}
@@ -0,0 +1,303 @@
// ============================================================================
// Copyright © 2026 Hexagon Technology Center GmbH. All Rights Reserved.
// 文件名: EllipseDetectionProcessor.cs
// 描述: 椭圆检测算子,基于轮廓分析和椭圆拟合检测图像中的椭圆
// 功能:
// - 阈值分割 + 轮廓提取
// - 椭圆拟合(FitEllipse
// - 面积/轴长/离心率/拟合误差多维过滤
// - 支持双阈值分割和 Otsu 自动阈值
// 算法: 阈值分割 + OpenCV FitEllipse
// 作者: 李伟 wei.lw.li@hexagon.com
// ============================================================================
using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;
using Emgu.CV.Util;
using ImageProcessing.Core;
using Serilog;
using System.Drawing;
namespace ImageProcessing.Processors;
/// <summary>
/// 椭圆检测结果
/// </summary>
public class EllipseInfo
{
/// <summary>序号</summary>
public int Index { get; set; }
/// <summary>中心点X</summary>
public float CenterX { get; set; }
/// <summary>中心点Y</summary>
public float CenterY { get; set; }
/// <summary>长轴长度</summary>
public float MajorAxis { get; set; }
/// <summary>短轴长度</summary>
public float MinorAxis { get; set; }
/// <summary>旋转角度(度)</summary>
public float Angle { get; set; }
/// <summary>面积</summary>
public double Area { get; set; }
/// <summary>周长</summary>
public double Perimeter { get; set; }
/// <summary>离心率 (0=圆, 接近1=扁椭圆)</summary>
public double Eccentricity { get; set; }
/// <summary>拟合误差(像素)</summary>
public double FitError { get; set; }
/// <summary>轮廓点集</summary>
public Point[] ContourPoints { get; set; } = Array.Empty<Point>();
/// <summary>外接矩形</summary>
public Rectangle BoundingBox { get; set; }
}
/// <summary>
/// 椭圆检测器
/// </summary>
public class EllipseDetector
{
private static readonly ILogger _logger = Log.ForContext<EllipseDetector>();
public int MinThreshold { get; set; } = 64;
public int MaxThreshold { get; set; } = 192;
public bool UseOtsu { get; set; } = false;
public int MinContourPoints { get; set; } = 30;
public double MinArea { get; set; } = 100;
public double MaxArea { get; set; } = 1000000;
public float MinMajorAxis { get; set; } = 10;
public double MaxEccentricity { get; set; } = 0.95;
public double MaxFitError { get; set; } = 5.0;
public int Thickness { get; set; } = 2;
/// <summary>执行椭圆检测</summary>
public List<EllipseInfo> Detect(Image<Gray, byte> inputImage, Image<Gray, byte>? roiMask = null)
{
_logger.Debug("Ellipse detection started: UseOtsu={UseOtsu}, MinThreshold={Min}, MaxThreshold={Max}",
UseOtsu, MinThreshold, MaxThreshold);
var results = new List<EllipseInfo>();
using var binary = new Image<Gray, byte>(inputImage.Size);
if (UseOtsu)
{
CvInvoke.Threshold(inputImage, binary, MinThreshold, 255, ThresholdType.Otsu);
_logger.Debug("Using Otsu auto threshold");
}
else
{
// 双阈值分割:介于MinThreshold和MaxThreshold之间的为前景(255),其他为背景(0)
byte[,,] inputData = inputImage.Data;
byte[,,] outputData = binary.Data;
int height = inputImage.Height;
int width = inputImage.Width;
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
byte pixelValue = inputData[y, x, 0];
outputData[y, x, 0] = (pixelValue >= MinThreshold && pixelValue <= MaxThreshold)
? (byte)255
: (byte)0;
}
}
_logger.Debug("Dual threshold segmentation: MinThreshold={Min}, MaxThreshold={Max}", MinThreshold, MaxThreshold);
}
// 应用ROI掩码
if (roiMask != null)
{
CvInvoke.BitwiseAnd(binary, roiMask, binary);
}
using var contours = new VectorOfVectorOfPoint();
using var hierarchy = new Mat();
CvInvoke.FindContours(binary, contours, hierarchy, RetrType.List, ChainApproxMethod.ChainApproxNone);
_logger.Debug("Found {Count} contours", contours.Size);
int index = 0;
for (int i = 0; i < contours.Size; i++)
{
var contour = contours[i];
if (contour.Size < Math.Max(5, MinContourPoints)) continue;
double area = CvInvoke.ContourArea(contour);
if (area < MinArea || area > MaxArea) continue;
RotatedRect ellipseRect = CvInvoke.FitEllipse(contour);
float majorAxis = Math.Max(ellipseRect.Size.Width, ellipseRect.Size.Height);
float minorAxis = Math.Min(ellipseRect.Size.Width, ellipseRect.Size.Height);
if (majorAxis < MinMajorAxis) continue;
double eccentricity = 0;
if (majorAxis > 0)
{
double ratio = minorAxis / majorAxis;
eccentricity = Math.Sqrt(1.0 - ratio * ratio);
}
if (eccentricity > MaxEccentricity) continue;
double fitError = ComputeFitError(contour.ToArray(), ellipseRect);
if (fitError > MaxFitError) continue;
results.Add(new EllipseInfo
{
Index = index++,
CenterX = ellipseRect.Center.X,
CenterY = ellipseRect.Center.Y,
MajorAxis = majorAxis,
MinorAxis = minorAxis,
Angle = ellipseRect.Angle,
Area = area,
Perimeter = CvInvoke.ArcLength(contour, true),
Eccentricity = eccentricity,
FitError = fitError,
ContourPoints = contour.ToArray(),
BoundingBox = CvInvoke.BoundingRectangle(contour)
});
}
_logger.Information("Ellipse detection completed: detected {Count} ellipses", results.Count);
return results;
}
private static double ComputeFitError(Point[] contourPoints, RotatedRect ellipse)
{
double cx = ellipse.Center.X, cy = ellipse.Center.Y;
double a = Math.Max(ellipse.Size.Width, ellipse.Size.Height) / 2.0;
double b = Math.Min(ellipse.Size.Width, ellipse.Size.Height) / 2.0;
double angleRad = ellipse.Angle * Math.PI / 180.0;
double cosA = Math.Cos(angleRad), sinA = Math.Sin(angleRad);
if (a < 1e-6) return double.MaxValue;
double totalError = 0;
foreach (var pt in contourPoints)
{
double dx = pt.X - cx, dy = pt.Y - cy;
double localX = dx * cosA + dy * sinA;
double localY = -dx * sinA + dy * cosA;
double ellipseVal = (localX * localX) / (a * a) + (localY * localY) / (b * b);
totalError += Math.Abs(Math.Sqrt(ellipseVal) - 1.0) * Math.Sqrt(a * b);
}
return totalError / contourPoints.Length;
}
}
/// <summary>
/// 椭圆检测算子
/// </summary>
public class EllipseDetectionProcessor : ImageProcessorBase
{
private static readonly ILogger _logger = Log.ForContext<EllipseDetectionProcessor>();
public EllipseDetectionProcessor()
{
Name = LocalizationHelper.GetString("EllipseDetectionProcessor_Name");
Description = LocalizationHelper.GetString("EllipseDetectionProcessor_Description");
}
protected override void InitializeParameters()
{
// ── 多边形ROI(由UI注入,最多32个点) ──
Parameters.Add("PolyCount", new ProcessorParameter("PolyCount", "PolyCount", typeof(int), 0, null, null, "") { IsVisible = false });
for (int i = 0; i < 32; i++)
{
Parameters.Add($"PolyX{i}", new ProcessorParameter($"PolyX{i}", $"PolyX{i}", typeof(int), 0, null, null, "") { IsVisible = false });
Parameters.Add($"PolyY{i}", new ProcessorParameter($"PolyY{i}", $"PolyY{i}", typeof(int), 0, null, null, "") { IsVisible = false });
}
Parameters.Add("MinThreshold", new ProcessorParameter(
"MinThreshold", LocalizationHelper.GetString("EllipseDetectionProcessor_MinThreshold"),
typeof(int), 64, 0, 255,
LocalizationHelper.GetString("EllipseDetectionProcessor_MinThreshold_Desc")));
Parameters.Add("MaxThreshold", new ProcessorParameter(
"MaxThreshold", LocalizationHelper.GetString("EllipseDetectionProcessor_MaxThreshold"),
typeof(int), 192, 0, 255,
LocalizationHelper.GetString("EllipseDetectionProcessor_MaxThreshold_Desc")));
Parameters.Add("UseOtsu", new ProcessorParameter(
"UseOtsu", LocalizationHelper.GetString("EllipseDetectionProcessor_UseOtsu"),
typeof(bool), false, null, null,
LocalizationHelper.GetString("EllipseDetectionProcessor_UseOtsu_Desc")));
Parameters.Add("MinContourPoints", new ProcessorParameter(
"MinContourPoints", LocalizationHelper.GetString("EllipseDetectionProcessor_MinContourPoints"),
typeof(int), 30, 5, 1000,
LocalizationHelper.GetString("EllipseDetectionProcessor_MinContourPoints_Desc")));
Parameters.Add("MinArea", new ProcessorParameter(
"MinArea", LocalizationHelper.GetString("EllipseDetectionProcessor_MinArea"),
typeof(double), 100.0, 0.0, 1000000.0,
LocalizationHelper.GetString("EllipseDetectionProcessor_MinArea_Desc")));
Parameters.Add("MaxArea", new ProcessorParameter(
"MaxArea", LocalizationHelper.GetString("EllipseDetectionProcessor_MaxArea"),
typeof(double), 1000000.0, 0.0, 10000000.0,
LocalizationHelper.GetString("EllipseDetectionProcessor_MaxArea_Desc")));
Parameters.Add("MaxEccentricity", new ProcessorParameter(
"MaxEccentricity", LocalizationHelper.GetString("EllipseDetectionProcessor_MaxEccentricity"),
typeof(double), 0.95, 0.0, 1.0,
LocalizationHelper.GetString("EllipseDetectionProcessor_MaxEccentricity_Desc")));
Parameters.Add("MaxFitError", new ProcessorParameter(
"MaxFitError", LocalizationHelper.GetString("EllipseDetectionProcessor_MaxFitError"),
typeof(double), 5.0, 0.0, 50.0,
LocalizationHelper.GetString("EllipseDetectionProcessor_MaxFitError_Desc")));
Parameters.Add("Thickness", new ProcessorParameter(
"Thickness", LocalizationHelper.GetString("EllipseDetectionProcessor_Thickness"),
typeof(int), 2, 1, 10,
LocalizationHelper.GetString("EllipseDetectionProcessor_Thickness_Desc")));
_logger.Debug("InitializeParameters");
}
public override Image<Gray, byte> Process(Image<Gray, byte> inputImage)
{
int thickness = GetParameter<int>("Thickness");
_logger.Debug("Ellipse detection started");
OutputData.Clear();
// 构建多边形ROI掩码
int polyCount = GetParameter<int>("PolyCount");
Image<Gray, byte>? roiMask = null;
if (polyCount >= 3)
{
var pts = new Point[polyCount];
for (int i = 0; i < polyCount; i++)
pts[i] = new Point(GetParameter<int>($"PolyX{i}"), GetParameter<int>($"PolyY{i}"));
roiMask = new Image<Gray, byte>(inputImage.Width, inputImage.Height);
using var vop = new VectorOfPoint(pts);
using var vvop = new VectorOfVectorOfPoint(vop);
CvInvoke.DrawContours(roiMask, vvop, 0, new MCvScalar(255), -1);
}
var detector = new EllipseDetector
{
MinThreshold = GetParameter<int>("MinThreshold"),
MaxThreshold = GetParameter<int>("MaxThreshold"),
UseOtsu = GetParameter<bool>("UseOtsu"),
MinContourPoints = GetParameter<int>("MinContourPoints"),
MinArea = GetParameter<double>("MinArea"),
MaxArea = GetParameter<double>("MaxArea"),
MaxEccentricity = GetParameter<double>("MaxEccentricity"),
MaxFitError = GetParameter<double>("MaxFitError"),
Thickness = thickness
};
var ellipses = detector.Detect(inputImage, roiMask);
OutputData["Ellipses"] = ellipses;
OutputData["EllipseCount"] = ellipses.Count;
OutputData["Thickness"] = thickness;
roiMask?.Dispose();
_logger.Information("Ellipse detection completed: detected {Count} ellipses", ellipses.Count);
return inputImage.Clone();
}
}
@@ -0,0 +1,133 @@
// ============================================================================
// Copyright © 2026 Hexagon Technology Center GmbH. All Rights Reserved.
// 文件名: FillRateProcessor.cs
// 描述: 通孔填锡率测量算子(倾斜投影几何法),基于四椭圆ROI
// 功能:
// - 样品倾斜约45°放置,利用投影位移关系计算填锡率
// - 四个椭圆定义:
// E1 = 通孔底部轮廓
// E2 = 通孔顶部轮廓
// E3 = 填锡起点(与E1重合,代表0%填锡)
// E4 = 填锡终点(锡实际填充到的高度)
// - 填锡率 = |E4中心 - E3中心| / |E2中心 - E1中心| × 100%
// - 纯几何方法,不依赖灰度分析
// - IPC-610 THT 分级判定(Class 1/2/3
// 算法: 倾斜投影位移比例
// 作者: 李伟 wei.lw.li@hexagon.com
// ============================================================================
using Emgu.CV;
using Emgu.CV.Structure;
using ImageProcessing.Core;
using Serilog;
using System.Drawing;
namespace ImageProcessing.Processors;
/// <summary>
/// 通孔填锡率测量算子(倾斜投影几何法)
/// </summary>
public class FillRateProcessor : ImageProcessorBase
{
private static readonly ILogger _logger = Log.ForContext<FillRateProcessor>();
public FillRateProcessor()
{
Name = LocalizationHelper.GetString("FillRateProcessor_Name");
Description = LocalizationHelper.GetString("FillRateProcessor_Description");
}
protected override void InitializeParameters()
{
// 四个椭圆(由交互控件注入,UI不可见)
AddEllipseParams("E1", 200, 250, 60, 50, 0); // 底部
AddEllipseParams("E2", 220, 180, 60, 50, 0); // 顶部
AddEllipseParams("E3", 200, 250, 60, 50, 0); // 填锡起点(=E1
AddEllipseParams("E4", 210, 220, 55, 45, 0); // 填锡终点
Parameters.Add("THTLimit", new ProcessorParameter(
"THTLimit",
LocalizationHelper.GetString("FillRateProcessor_THTLimit"),
typeof(double), 75.0, 0.0, 100.0,
LocalizationHelper.GetString("FillRateProcessor_THTLimit_Desc")));
Parameters.Add("Thickness", new ProcessorParameter(
"Thickness",
LocalizationHelper.GetString("FillRateProcessor_Thickness"),
typeof(int), 2, 1, 10,
LocalizationHelper.GetString("FillRateProcessor_Thickness_Desc")));
}
private void AddEllipseParams(string prefix, int cx, int cy, double a, double b, double angle)
{
Parameters.Add($"{prefix}_CX", new ProcessorParameter($"{prefix}_CX", $"{prefix}_CX", typeof(int), cx, null, null, "") { IsVisible = false });
Parameters.Add($"{prefix}_CY", new ProcessorParameter($"{prefix}_CY", $"{prefix}_CY", typeof(int), cy, null, null, "") { IsVisible = false });
Parameters.Add($"{prefix}_A", new ProcessorParameter($"{prefix}_A", $"{prefix}_A", typeof(double), a, null, null, "") { IsVisible = false });
Parameters.Add($"{prefix}_B", new ProcessorParameter($"{prefix}_B", $"{prefix}_B", typeof(double), b, null, null, "") { IsVisible = false });
Parameters.Add($"{prefix}_Angle", new ProcessorParameter($"{prefix}_Angle", $"{prefix}_Angle", typeof(double), angle, null, null, "") { IsVisible = false });
}
public override Image<Gray, byte> Process(Image<Gray, byte> inputImage)
{
double thtLimit = GetParameter<double>("THTLimit");
int thickness = GetParameter<int>("Thickness");
// 获取四个椭圆中心
int e1cx = GetParameter<int>("E1_CX"), e1cy = GetParameter<int>("E1_CY");
int e2cx = GetParameter<int>("E2_CX"), e2cy = GetParameter<int>("E2_CY");
int e3cx = GetParameter<int>("E3_CX"), e3cy = GetParameter<int>("E3_CY");
int e4cx = GetParameter<int>("E4_CX"), e4cy = GetParameter<int>("E4_CY");
// 获取椭圆轴参数(用于绘制)
double e1a = GetParameter<double>("E1_A"), e1b = GetParameter<double>("E1_B"), e1ang = GetParameter<double>("E1_Angle");
double e2a = GetParameter<double>("E2_A"), e2b = GetParameter<double>("E2_B"), e2ang = GetParameter<double>("E2_Angle");
double e3a = GetParameter<double>("E3_A"), e3b = GetParameter<double>("E3_B"), e3ang = GetParameter<double>("E3_Angle");
double e4a = GetParameter<double>("E4_A"), e4b = GetParameter<double>("E4_B"), e4ang = GetParameter<double>("E4_Angle");
_logger.Debug("FillRate: E1=({E1X},{E1Y}), E2=({E2X},{E2Y}), E3=({E3X},{E3Y}), E4=({E4X},{E4Y})",
e1cx, e1cy, e2cx, e2cy, e3cx, e3cy, e4cx, e4cy);
OutputData.Clear();
// 计算通孔全高度的投影位移(E1底部 → E2顶部)
double fullDx = e2cx - e1cx;
double fullDy = e2cy - e1cy;
double fullDistance = Math.Sqrt(fullDx * fullDx + fullDy * fullDy);
// 计算填锡高度的投影位移(E3起点 → E4终点)
double fillDx = e4cx - e3cx;
double fillDy = e4cy - e3cy;
double fillDistance = Math.Sqrt(fillDx * fillDx + fillDy * fillDy);
// 填锡率 = 填锡位移 / 全高度位移
double fillRate = fullDistance > 0 ? (fillDistance / fullDistance) * 100.0 : 0;
fillRate = Math.Clamp(fillRate, 0, 100);
// 判定
string classification = fillRate >= thtLimit ? "PASS" : "FAIL";
// 存储结果
OutputData["FillRateResult"] = true;
OutputData["FillRate"] = fillRate;
OutputData["VoidRate"] = 100.0 - fillRate;
OutputData["FullDistance"] = fullDistance;
OutputData["FillDistance"] = fillDistance;
OutputData["THTLimit"] = thtLimit;
OutputData["Classification"] = classification;
OutputData["Thickness"] = thickness;
// 椭圆几何(用于绘制)
OutputData["E1"] = (new Point(e1cx, e1cy), new Size((int)e1a, (int)e1b), e1ang);
OutputData["E2"] = (new Point(e2cx, e2cy), new Size((int)e2a, (int)e2b), e2ang);
OutputData["E3"] = (new Point(e3cx, e3cy), new Size((int)e3a, (int)e3b), e3ang);
OutputData["E4"] = (new Point(e4cx, e4cy), new Size((int)e4a, (int)e4b), e4ang);
string resultText = $"{fillRate:F1}% | {classification}";
OutputData["ResultText"] = resultText;
_logger.Information("FillRate (geometric): {Rate}%, {Class}, FullDist={FD:F1}, FillDist={FiD:F1}",
fillRate, classification, fullDistance, fillDistance);
return inputImage.Clone();
}
}
@@ -0,0 +1,150 @@
// ============================================================================
// Copyright © 2026 Hexagon Technology Center GmbH. All Rights Reserved.
// 文件名: LineMeasurementProcessor.cs
// 描述: 直线测量算子,用于测量图像中两点之间的距离
// 功能:
// - 用户指定两个点坐标(像素坐标)
// - 计算两点之间的欧氏距离(像素单位)
// - 支持像素尺寸标定,输出实际物理距离
// - 在图像上绘制测量线和标注
// - 输出测量结果供后续处理使用
// 算法: 欧氏距离计算
// 作者: 李伟 wei.lw.li@hexagon.com
// ============================================================================
using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;
using ImageProcessing.Core;
using Serilog;
using System.Drawing;
namespace 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();
}
}
@@ -0,0 +1,116 @@
// ============================================================================
// Copyright © 2026 Hexagon Technology Center GmbH. All Rights Reserved.
// 文件名: PointToLineProcessor.cs
// 描述: 点到直线距离测量算子
// 功能:
// - 用户定义一条直线(两个端点)和一个测量点
// - 计算测量点到直线的垂直距离
// - 支持像素尺寸标定输出物理距离
// - 在图像上绘制直线、测量点、垂足和距离标注
// 算法: 点到直线距离公式
// 作者: 李伟 wei.lw.li@hexagon.com
// ============================================================================
using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;
using ImageProcessing.Core;
using Serilog;
using System.Drawing;
namespace ImageProcessing.Processors;
public class PointToLineProcessor : ImageProcessorBase
{
private static readonly ILogger _logger = Log.ForContext<PointToLineProcessor>();
public PointToLineProcessor()
{
Name = LocalizationHelper.GetString("PointToLineProcessor_Name");
Description = LocalizationHelper.GetString("PointToLineProcessor_Description");
}
protected override void InitializeParameters()
{
// 直线两端点 + 测量点(由交互控件注入)
Parameters.Add("L1X", new ProcessorParameter("L1X", "L1X", typeof(int), 100, null, null, "") { IsVisible = false });
Parameters.Add("L1Y", new ProcessorParameter("L1Y", "L1Y", typeof(int), 200, null, null, "") { IsVisible = false });
Parameters.Add("L2X", new ProcessorParameter("L2X", "L2X", typeof(int), 400, null, null, "") { IsVisible = false });
Parameters.Add("L2Y", new ProcessorParameter("L2Y", "L2Y", typeof(int), 200, null, null, "") { IsVisible = false });
Parameters.Add("PX", new ProcessorParameter("PX", "PX", typeof(int), 250, null, null, "") { IsVisible = false });
Parameters.Add("PY", new ProcessorParameter("PY", "PY", typeof(int), 100, null, null, "") { IsVisible = false });
Parameters.Add("PixelSize", new ProcessorParameter(
"PixelSize",
LocalizationHelper.GetString("PointToLineProcessor_PixelSize"),
typeof(double), 1.0, null, null,
LocalizationHelper.GetString("PointToLineProcessor_PixelSize_Desc")));
Parameters.Add("Unit", new ProcessorParameter(
"Unit",
LocalizationHelper.GetString("PointToLineProcessor_Unit"),
typeof(string), "px", null, null,
LocalizationHelper.GetString("PointToLineProcessor_Unit_Desc"),
new string[] { "px", "mm", "μm", "cm" }));
Parameters.Add("Thickness", new ProcessorParameter(
"Thickness",
LocalizationHelper.GetString("PointToLineProcessor_Thickness"),
typeof(int), 2, 1, 10,
LocalizationHelper.GetString("PointToLineProcessor_Thickness_Desc")));
}
public override Image<Gray, byte> Process(Image<Gray, byte> inputImage)
{
int l1x = GetParameter<int>("L1X"), l1y = GetParameter<int>("L1Y");
int l2x = GetParameter<int>("L2X"), l2y = GetParameter<int>("L2Y");
int px = GetParameter<int>("PX"), py = GetParameter<int>("PY");
double pixelSize = GetParameter<double>("PixelSize");
string unit = GetParameter<string>("Unit");
int thickness = GetParameter<int>("Thickness");
OutputData.Clear();
// 点到直线距离公式: |AB × AP| / |AB|
double abx = l2x - l1x, aby = l2y - l1y;
double abLen = Math.Sqrt(abx * abx + aby * aby);
double pixelDistance = 0;
int footX = px, footY = py;
if (abLen > 0.001)
{
// 叉积求距离
double cross = Math.Abs(abx * (l1y - py) - aby * (l1x - px));
pixelDistance = cross / abLen;
// 垂足: 投影参数 t = AP·AB / |AB|²
double apx = px - l1x, apy = py - l1y;
double t = (apx * abx + apy * aby) / (abLen * abLen);
footX = (int)(l1x + t * abx);
footY = (int)(l1y + t * aby);
OutputData["ProjectionT"] = t;
}
double actualDistance = pixelDistance * pixelSize;
string distanceText = unit == "px"
? $"{pixelDistance:F2} px"
: $"{actualDistance:F4} {unit} ({pixelDistance:F2} px)";
OutputData["PointToLineResult"] = true;
OutputData["Line1"] = new Point(l1x, l1y);
OutputData["Line2"] = new Point(l2x, l2y);
OutputData["MeasurePoint"] = new Point(px, py);
OutputData["FootPoint"] = new Point(footX, footY);
OutputData["PixelDistance"] = pixelDistance;
OutputData["ActualDistance"] = actualDistance;
OutputData["Unit"] = unit;
OutputData["Thickness"] = thickness;
OutputData["DistanceText"] = distanceText;
_logger.Information("PointToLine: Distance={Dist}, Foot=({FX},{FY})", distanceText, footX, footY);
return inputImage.Clone();
}
}
@@ -0,0 +1,230 @@
// ============================================================================
// 文件名: VoidMeasurementProcessor.cs
// 描述: 空隙测量算子
//
// 处理流程:
// 1. 构建多边形ROI掩码,计算ROI面积
// 2. 在ROI内进行双阈值分割提取气泡区域
// 3. 形态学膨胀合并相邻气泡
// 4. 轮廓检测,计算每个气泡面积
// 5. 计算空隙率 = 总气泡面积 / ROI面积
// ============================================================================
using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;
using Emgu.CV.Util;
using ImageProcessing.Core;
using Serilog;
using System.Drawing;
namespace ImageProcessing.Processors;
public class VoidMeasurementProcessor : ImageProcessorBase
{
private static readonly ILogger _logger = Log.ForContext<VoidMeasurementProcessor>();
public VoidMeasurementProcessor()
{
Name = LocalizationHelper.GetString("VoidMeasurementProcessor_Name");
Description = LocalizationHelper.GetString("VoidMeasurementProcessor_Description");
}
protected override void InitializeParameters()
{
// ── 多边形ROI(由UI注入,最多32个点) ──
Parameters.Add("PolyCount", new ProcessorParameter("PolyCount", "PolyCount", typeof(int), 0, null, null, "") { IsVisible = false });
for (int i = 0; i < 32; i++)
{
Parameters.Add($"PolyX{i}", new ProcessorParameter($"PolyX{i}", $"PolyX{i}", typeof(int), 0, null, null, "") { IsVisible = false });
Parameters.Add($"PolyY{i}", new ProcessorParameter($"PolyY{i}", $"PolyY{i}", typeof(int), 0, null, null, "") { IsVisible = false });
}
// ── 气泡检测参数 ──
Parameters.Add("MinThreshold", new ProcessorParameter(
"MinThreshold",
LocalizationHelper.GetString("VoidMeasurementProcessor_MinThreshold"),
typeof(int), 128, 0, 255,
LocalizationHelper.GetString("VoidMeasurementProcessor_MinThreshold_Desc")));
Parameters.Add("MaxThreshold", new ProcessorParameter(
"MaxThreshold",
LocalizationHelper.GetString("VoidMeasurementProcessor_MaxThreshold"),
typeof(int), 255, 0, 255,
LocalizationHelper.GetString("VoidMeasurementProcessor_MaxThreshold_Desc")));
Parameters.Add("MinVoidArea", new ProcessorParameter(
"MinVoidArea",
LocalizationHelper.GetString("VoidMeasurementProcessor_MinVoidArea"),
typeof(int), 10, 1, 100000,
LocalizationHelper.GetString("VoidMeasurementProcessor_MinVoidArea_Desc")));
Parameters.Add("MergeRadius", new ProcessorParameter(
"MergeRadius",
LocalizationHelper.GetString("VoidMeasurementProcessor_MergeRadius"),
typeof(int), 3, 0, 30,
LocalizationHelper.GetString("VoidMeasurementProcessor_MergeRadius_Desc")));
Parameters.Add("BlurSize", new ProcessorParameter(
"BlurSize",
LocalizationHelper.GetString("VoidMeasurementProcessor_BlurSize"),
typeof(int), 3, 1, 31,
LocalizationHelper.GetString("VoidMeasurementProcessor_BlurSize_Desc")));
Parameters.Add("VoidLimit", new ProcessorParameter(
"VoidLimit",
LocalizationHelper.GetString("VoidMeasurementProcessor_VoidLimit"),
typeof(double), 25.0, 0.0, 100.0,
LocalizationHelper.GetString("VoidMeasurementProcessor_VoidLimit_Desc")));
}
public override Image<Gray, byte> Process(Image<Gray, byte> inputImage)
{
int minThresh = GetParameter<int>("MinThreshold");
int maxThresh = GetParameter<int>("MaxThreshold");
int minVoidArea = GetParameter<int>("MinVoidArea");
int mergeRadius = GetParameter<int>("MergeRadius");
int blurSize = GetParameter<int>("BlurSize");
double voidLimit = GetParameter<double>("VoidLimit");
if (blurSize % 2 == 0) blurSize++;
OutputData.Clear();
int w = inputImage.Width, h = inputImage.Height;
// ── 构建多边形ROI掩码 ──
int polyCount = GetParameter<int>("PolyCount");
Image<Gray, byte>? roiMask = null;
Point[]? roiPoints = null;
if (polyCount >= 3)
{
roiPoints = new Point[polyCount];
for (int i = 0; i < polyCount; i++)
roiPoints[i] = new Point(GetParameter<int>($"PolyX{i}"), GetParameter<int>($"PolyY{i}"));
roiMask = new Image<Gray, byte>(w, h);
using var vop = new VectorOfPoint(roiPoints);
using var vvop = new VectorOfVectorOfPoint(vop);
CvInvoke.DrawContours(roiMask, vvop, 0, new MCvScalar(255), -1);
}
else
{
// 无ROI时使用全图
roiMask = new Image<Gray, byte>(w, h);
roiMask.SetValue(new Gray(255));
}
int roiArea = CvInvoke.CountNonZero(roiMask);
_logger.Debug("VoidMeasurement: ROI area={Area}, Thresh=[{Min},{Max}], MergeR={MR}",
roiArea, minThresh, maxThresh, mergeRadius);
// ── 高斯模糊降噪 ──
var blurred = new Image<Gray, byte>(w, h);
CvInvoke.GaussianBlur(inputImage, blurred, new Size(blurSize, blurSize), 0);
// ── 双阈值分割提取气泡(亮区域) ──
var voidImg = new Image<Gray, byte>(w, h);
byte[,,] srcData = blurred.Data;
byte[,,] dstData = voidImg.Data;
byte[,,] maskData = roiMask.Data;
for (int y = 0; y < h; y++)
{
for (int x = 0; x < w; x++)
{
if (maskData[y, x, 0] > 0)
{
byte val = srcData[y, x, 0];
dstData[y, x, 0] = (val >= minThresh && val <= maxThresh) ? (byte)255 : (byte)0;
}
}
}
// ── 形态学膨胀合并相邻气泡 ──
if (mergeRadius > 0)
{
int kernelSize = mergeRadius * 2 + 1;
using var kernel = CvInvoke.GetStructuringElement(ElementShape.Ellipse,
new Size(kernelSize, kernelSize), new Point(-1, -1));
CvInvoke.Dilate(voidImg, voidImg, kernel, new Point(-1, -1), 1, BorderType.Default, new MCvScalar(0));
// 与ROI掩码取交集,防止膨胀超出ROI
CvInvoke.BitwiseAnd(voidImg, roiMask, voidImg);
}
// ── 轮廓检测 ──
using var contours = new VectorOfVectorOfPoint();
using var hierarchy = new Mat();
CvInvoke.FindContours(voidImg, contours, hierarchy, RetrType.External, ChainApproxMethod.ChainApproxSimple);
var voids = new List<VoidRegionInfo>();
int totalVoidArea = 0;
for (int i = 0; i < contours.Size; i++)
{
double area = CvInvoke.ContourArea(contours[i]);
if (area < minVoidArea) continue;
var moments = CvInvoke.Moments(contours[i]);
if (moments.M00 < 1) continue;
int intArea = (int)Math.Round(area);
totalVoidArea += intArea;
voids.Add(new VoidRegionInfo
{
Index = voids.Count + 1,
CenterX = moments.M10 / moments.M00,
CenterY = moments.M01 / moments.M00,
Area = intArea,
AreaPercent = roiArea > 0 ? area / roiArea * 100.0 : 0,
BoundingBox = CvInvoke.BoundingRectangle(contours[i]),
ContourPoints = contours[i].ToArray()
});
}
// 按面积从大到小排序
voids.Sort((a, b) => b.Area.CompareTo(a.Area));
for (int i = 0; i < voids.Count; i++) voids[i].Index = i + 1;
double voidRate = roiArea > 0 ? (double)totalVoidArea / roiArea * 100.0 : 0;
string classification = voidRate <= voidLimit ? "PASS" : "FAIL";
int maxVoidArea = voids.Count > 0 ? voids[0].Area : 0;
_logger.Information("VoidMeasurement: VoidRate={Rate:F1}%, Voids={Count}, MaxArea={Max}, {Class}",
voidRate, voids.Count, maxVoidArea, classification);
// ── 输出数据 ──
OutputData["VoidMeasurementResult"] = true;
OutputData["RoiArea"] = roiArea;
OutputData["RoiPoints"] = roiPoints;
OutputData["TotalVoidArea"] = totalVoidArea;
OutputData["VoidRate"] = voidRate;
OutputData["VoidLimit"] = voidLimit;
OutputData["VoidCount"] = voids.Count;
OutputData["MaxVoidArea"] = maxVoidArea;
OutputData["Classification"] = classification;
OutputData["Voids"] = voids;
OutputData["ResultText"] = $"Void: {voidRate:F1}% | {classification} | {voids.Count} voids | ROI: {roiArea}px";
blurred.Dispose();
voidImg.Dispose();
roiMask.Dispose();
return inputImage.Clone();
}
}
/// <summary>
/// 单个空隙区域信息
/// </summary>
public class VoidRegionInfo
{
public int Index { get; set; }
public double CenterX { get; set; }
public double CenterY { get; set; }
public int Area { get; set; }
public double AreaPercent { get; set; }
public Rectangle BoundingBox { get; set; }
public Point[] ContourPoints { get; set; } = Array.Empty<Point>();
}