提交代码
This commit is contained in:
@@ -41,7 +41,8 @@ namespace HardwareControl.Core.SampleAnalysis
|
||||
public SampleAnalysisResult Analyze(
|
||||
IReadOnlyList<CameraFrameSnapshot> baselineFrames,
|
||||
CameraFrameSnapshot target,
|
||||
int samplingPointCount)
|
||||
int samplingPointCount,
|
||||
double? samplingRadiusPixels = null)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(target);
|
||||
|
||||
@@ -72,6 +73,7 @@ namespace HardwareControl.Core.SampleAnalysis
|
||||
modelPath,
|
||||
targetImage,
|
||||
pointCount,
|
||||
samplingRadiusPixels,
|
||||
out var confidence,
|
||||
out var failureReason);
|
||||
|
||||
@@ -134,6 +136,7 @@ namespace HardwareControl.Core.SampleAnalysis
|
||||
fallbackMask,
|
||||
targetImage,
|
||||
pointCount,
|
||||
samplingRadiusPixels,
|
||||
out var fallbackFailure);
|
||||
|
||||
if (fallbackResult is not null)
|
||||
@@ -196,6 +199,7 @@ namespace HardwareControl.Core.SampleAnalysis
|
||||
string modelPath,
|
||||
Mat targetImage,
|
||||
int pointCount,
|
||||
double? samplingRadiusPixels,
|
||||
out float confidence,
|
||||
out string failureReason)
|
||||
{
|
||||
@@ -267,7 +271,7 @@ namespace HardwareControl.Core.SampleAnalysis
|
||||
targetImage.Size());
|
||||
|
||||
return BuildResultFromMask(
|
||||
mask, targetImage, pointCount, out failureReason);
|
||||
mask, targetImage, pointCount, samplingRadiusPixels, out failureReason);
|
||||
}
|
||||
|
||||
/// <summary>用掩膜提取轮廓、绘制采样点并编码结果图。</summary>
|
||||
@@ -275,6 +279,7 @@ namespace HardwareControl.Core.SampleAnalysis
|
||||
Mat mask,
|
||||
Mat targetImage,
|
||||
int pointCount,
|
||||
double? samplingRadiusPixels,
|
||||
out string failureReason)
|
||||
{
|
||||
failureReason = string.Empty;
|
||||
@@ -312,7 +317,10 @@ namespace HardwareControl.Core.SampleAnalysis
|
||||
var points = BuildSamplingPoints(
|
||||
contour,
|
||||
pointCount,
|
||||
CalculateSamplingMargin(contour, outputImage.Width, outputImage.Height));
|
||||
CalculateSamplingMargin(contour, outputImage.Width, outputImage.Height),
|
||||
samplingRadiusPixels,
|
||||
outputImage.Width / 2.0,
|
||||
outputImage.Height / 2.0);
|
||||
DrawSamplingPoints(outputImage, points, lineThickness);
|
||||
|
||||
Cv2.ImEncode(
|
||||
@@ -1237,10 +1245,16 @@ namespace HardwareControl.Core.SampleAnalysis
|
||||
|
||||
/// <summary>
|
||||
/// 在轮廓内部按网格生成候选点,要求候选点到轮廓的距离不小于安全边距;
|
||||
/// 配置采样半径时,候选点还须位于以画面中心为原点的半径范围内。
|
||||
/// 点数不足时逐步缩小间距,再均匀抽取指定数量的采样点。
|
||||
/// </summary>
|
||||
private static List<Point> BuildSamplingPoints(
|
||||
Point[] contour, int count, double margin)
|
||||
Point[] contour,
|
||||
int count,
|
||||
double margin,
|
||||
double? radiusPixels,
|
||||
double centerX,
|
||||
double centerY)
|
||||
{
|
||||
var area = Cv2.ContourArea(contour);
|
||||
if (area <= 0 || count <= 0)
|
||||
@@ -1266,6 +1280,11 @@ namespace HardwareControl.Core.SampleAnalysis
|
||||
for (var x = rect.Left + spacing / 2; x <= rect.Right; x += spacing)
|
||||
{
|
||||
var point = new Point((int)Math.Round(x), (int)Math.Round(y));
|
||||
if (!IsInsideRadius(point, centerX, centerY, radiusPixels))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var distance = Cv2.PointPolygonTest(
|
||||
contour, new Point2f(point.X, point.Y), true);
|
||||
if (distance >= margin)
|
||||
@@ -1286,7 +1305,8 @@ namespace HardwareControl.Core.SampleAnalysis
|
||||
if (candidates.Count == 0)
|
||||
{
|
||||
// 极窄轮廓可能无法满足安全边距,退回到离边缘最远的内部点
|
||||
var fallback = FindBestInteriorPoint(contour);
|
||||
var fallback = FindBestInteriorPoint(
|
||||
contour, radiusPixels, centerX, centerY);
|
||||
return fallback.HasValue
|
||||
? new List<Point> { fallback.Value }
|
||||
: new List<Point>();
|
||||
@@ -1313,8 +1333,27 @@ namespace HardwareControl.Core.SampleAnalysis
|
||||
return selected.Distinct().ToList();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 判断点是否位于以画面中心为圆心、radiusPixels 为半径的圆内;
|
||||
/// 未配置有效半径(null 或非正数)时不做限制。
|
||||
/// </summary>
|
||||
private static bool IsInsideRadius(
|
||||
Point point, double centerX, double centerY, double? radiusPixels)
|
||||
{
|
||||
if (radiusPixels is not > 0)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
var dx = point.X - centerX;
|
||||
var dy = point.Y - centerY;
|
||||
var radius = radiusPixels.Value;
|
||||
return dx * dx + dy * dy <= radius * radius;
|
||||
}
|
||||
|
||||
/// <summary>找到轮廓内部距边缘最远的点,作为极端情况下的兜底。</summary>
|
||||
private static Point? FindBestInteriorPoint(Point[] contour)
|
||||
private static Point? FindBestInteriorPoint(
|
||||
Point[] contour, double? radiusPixels, double centerX, double centerY)
|
||||
{
|
||||
var rect = Cv2.BoundingRect(contour);
|
||||
var step = Math.Max(2, Math.Min(rect.Width, rect.Height) / 50);
|
||||
@@ -1326,6 +1365,11 @@ namespace HardwareControl.Core.SampleAnalysis
|
||||
{
|
||||
for (var x = rect.Left; x <= rect.Right; x += step)
|
||||
{
|
||||
if (!IsInsideRadius(new Point(x, y), centerX, centerY, radiusPixels))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var distance = Cv2.PointPolygonTest(
|
||||
contour, new Point2f(x, y), true);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user