改进视觉工具调用

This commit is contained in:
tfarcraw 2026-07-21 22:53:32 +08:00
parent b7eebd24e3
commit ccaf5415b4
7 changed files with 74 additions and 17 deletions

View File

@ -37,6 +37,18 @@
/// </summary>
public bool Done { get; set; }
/// <summary>
/// 流程是否正常结束(默认 false,正常结束时置 true)
/// </summary>
public bool IsSuccess { get; set; }
/// <summary>
/// 失败原因(非空表示异常结束)
/// </summary>
public string? ErrorState { get; set; }
/// <summary>
/// 单个步骤超时时间(秒),默认 5 秒
/// </summary>
public double TimeoutSeconds { get; set; } = 5;
/// <summary>
/// 默认构造函数,自动调用 Reset 初始化
/// </summary>
public FlowModel()
@ -51,6 +63,8 @@
public void Reset()
{
Done = false;
IsSuccess = false;
ErrorState = null;
Step = 0;
StepTime = DateTime.Now;
FlowStarTime = DateTime.Now;

View File

@ -23,7 +23,7 @@ namespace PLCSharp.VVMs.Vision.VisionFlowHandler.Algorithm
if (!item.DoubleParams.TryGetValue("CircleCenterX", out double cx) ||
!item.DoubleParams.TryGetValue("CircleCenterY", out double cy) ||
!item.DoubleParams.TryGetValue("CircleRadius", out double radius))
throw new Exception("圆形ROI未配置,请先画圆!");
{ item.Flow.ErrorState = "圆形ROI未配置,请先画圆!"; return false; }
// 读取卡尺参数
var threshold = item.IntParams.TryGetValue("Threshold", out int t) ? Math.Max(t, 1) : 30;
@ -180,7 +180,10 @@ namespace PLCSharp.VVMs.Vision.VisionFlowHandler.Algorithm
_ = func.RenderDrawAsync();
if (edgePoints.Count < 3)
throw new Exception($"仅找到 {edgePoints.Count} 个边缘点,至少需要3个!");
{
item.Flow.ErrorState = $"仅找到 {edgePoints.Count} 个边缘点,至少需要3个!";
return false;
}
// RANSAC 拟合圆
var circle = FitCircleRansac(edgePoints, maxIterations: 200, inlierThreshold: inlierTh);
@ -193,7 +196,10 @@ namespace PLCSharp.VVMs.Vision.VisionFlowHandler.Algorithm
});
if (ransacInliers < minInliers)
throw new Exception($"内点数不足 ({ransacInliers} < {minInliers}),请调整 RANSAC 阈值或检查边缘点!");
{
item.Flow.ErrorState = $"内点数不足 ({ransacInliers} < {minInliers}),请调整 RANSAC 阈值!";
return false;
};
func.DrawCommands.Add(DrawCommand.Circle(circle.CenterX, circle.CenterY, circle.Radius, magenta, 2));

View File

@ -14,7 +14,7 @@ namespace PLCSharp.VVMs.Vision.VisionFlowHandler.Algorithm
!item.DoubleParams.TryGetValue("LineStartY", out double sy) ||
!item.DoubleParams.TryGetValue("LineEndX", out double ex) ||
!item.DoubleParams.TryGetValue("LineEndY", out double ey))
throw new Exception("卡尺线未配置,请先画线!");
{ item.Flow.ErrorState = "卡尺线未配置,请先画线!"; return false; }
var threshold = item.IntParams.TryGetValue("Threshold", out int t) ? Math.Max(t, 1) : 30;
var numCalipers = item.IntParams.TryGetValue("NumCalipers", out int nc) ? Math.Max(nc, 1) : 50;
@ -31,7 +31,7 @@ namespace PLCSharp.VVMs.Vision.VisionFlowHandler.Algorithm
double lineDx = ex - sx, lineDy = ey - sy;
double lineLen = Math.Sqrt(lineDx * lineDx + lineDy * lineDy);
if (lineLen < 1) throw new Exception("直线太短!");
if (lineLen < 1) { item.Flow.ErrorState = "直线太短!"; return false; }
double ux = lineDx / lineLen, uy = lineDy / lineLen;
double pdx = -uy, pdy = ux;
double halfLen = caliperLength / 2.0;
@ -87,7 +87,10 @@ namespace PLCSharp.VVMs.Vision.VisionFlowHandler.Algorithm
}
if (edgePoints.Count < 2)
throw new Exception($"仅找到 {edgePoints.Count} 个边缘点,至少需要2个!");
{
item.Flow.ErrorState = $"仅找到 {edgePoints.Count} 个边缘点,至少需要2个!";
return false;
}
// RANSAC 内点
var inls = RansacInliers(edgePoints, inlierTh);

View File

@ -72,7 +72,10 @@ namespace PLCSharp.VVMs.Vision.VisionFlowHandler.Algorithm
// ===== 2. 加载模板 =====
var tmpl = func.LocalImageDatas.FirstOrDefault(m => m.Name == templateName);
if (tmpl?.Mat == null || tmpl.Mat.Empty())
throw new Exception("模板提取失败,请先画选区截取模板!");
{
item.Flow.ErrorState = "模板提取失败,请先画选区截取模板!";
return false;
}
templateRoi = tmpl.Mat.Clone();
// ===== 3. 灰度化 =====
@ -98,7 +101,10 @@ namespace PLCSharp.VVMs.Vision.VisionFlowHandler.Algorithm
out bestScore, out bestAngle, out bestX, out bestY, out etW, out etH, out bestResult, ref totalTemplates);
if (bestScore < scoreMin)
throw new Exception($"未找到可靠匹配(最佳分数 {bestScore:F3} < 阈值 {scoreMin})!");
{
item.Flow.ErrorState = $"未找到可靠匹配(最佳分数 {bestScore:F3} < 阈值 {scoreMin})";
return false;
}
// GetRotationMatrix2D 正角度 = OpenCV 顺时针 → 取反统一为视觉方向
bestAngle = -bestAngle;

View File

@ -51,7 +51,7 @@ namespace PLCSharp.VVMs.Vision.VisionFlowHandler.Algorithm
case "旋转矩形":
if (!item.DoubleParams.TryGetValue("TemplateWidth", out templateW) ||
!item.DoubleParams.TryGetValue("TemplateHeight", out templateH))
throw new Exception("旋转矩形模板参数缺失!");
{ item.Flow.ErrorState = "旋转矩形模板参数缺失!"; return false; }
break;
case "圆形":
if (!item.DoubleParams.TryGetValue("TemplateRadius", out double radius))

View File

@ -1,4 +1,4 @@
<UserControl x:Class="PLCSharp.VVMs.Vision.VisionFlowHandler.Algorithm.PointToPointDistance"
<UserControl x:Class="PLCSharp.VVMs.Vision.VisionFlowHandler.Algorithm.PointToPointDistance"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

View File

@ -1,9 +1,8 @@

using System.Text.Json;
using PLCSharp.Core.Tools;
using OpenCvSharp;
using PLCSharp.Core.Common;
using PLCSharp.Core.Prism;
using PLCSharp.Core.Tools;
using PLCSharp.Core.UserControls;
using PLCSharp.Models;
using PLCSharp.VVMs.Vision.VisionFlowHandler;
@ -14,6 +13,7 @@ using Prism.Mvvm;
using System.Collections.ObjectModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Text.Json;
using System.Windows.Media;
namespace PLCSharp.VVMs.Vision
@ -277,15 +277,43 @@ namespace PLCSharp.VVMs.Vision
}
flow.Done = true;
flow.IsSuccess = true;
}
break;
case 2:
if (RunItem(VisionFlows[index]))
{
flow.Step--;
index++;
var item = VisionFlows[index];
flow.TaskBool = System.Threading.Tasks.Task.Run(() => RunItem(item));
item.Flow.StepTime = DateTime.Now;
flow.Step++;
}
break;
case 3:
{
var item = VisionFlows[index];
var task = flow.TaskBool;
if (task == null) { flow.Step--; break; }
if (task.IsCompleted)
{
if (task.Result)
{
flow.Step = 1; // 回到 case 1 取下一个
index++;
}
else
{
item.Flow.ErrorState ??= "未知错误";
flow.IsSuccess = false;
flow.Done = true;
}
}
else if (item.Flow.CheckStepTime(flow.TimeoutSeconds))
{
item.Flow.ErrorState = $"执行超时(>{flow.TimeoutSeconds:F0}秒)";
flow.IsSuccess = false;
flow.Done = true;
}
}
break;
}
@ -531,7 +559,7 @@ namespace PLCSharp.VVMs.Vision
/// </summary>
public class VisionParams : BindableBase
{
private ObservableDictionary<string, double> _ResultDoubles = [];
/// <summary>
/// 通用数值结果(如 ORB 匹配输出的 X/Y/角度偏移)