plcsharp/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/MatrixGenHandler.cs

43 lines
1.5 KiB
C#

using OpenCvSharp;
namespace PLCSharp.VVMs.Vision.VisionFlowHandler.Algorithm
{
/// <summary>
/// 矩阵生成 — 根据输入位移和角度生成 2x3 仿射变换矩阵,写入局部变量表。
/// </summary>
public class MatrixGenHandler : IVisionFlowHandler
{
public VisionFlowType Type => VisionFlowType.矩阵生成;
public bool Execute(VisionFunction func, VisionFlow item)
{
double x = item.DoubleParams.TryGetValue("MoveX", out double mx) ? mx : 0;
double y = item.DoubleParams.TryGetValue("MoveY", out double my) ? my : 0;
double angle = item.DoubleParams.TryGetValue("Angle", out double a) ? a : 0;
string varName = item.StringParams.TryGetValue("MatrixVar", out var mn) && !string.IsNullOrEmpty(mn)
? mn : "生成矩阵";
var center = new Point2f(0, 0);
using Mat M = Cv2.GetRotationMatrix2D(center, angle, 1.0);
M.Set(0, 2, x);
M.Set(1, 2, y);
string json = MatExtension.SerializeAffineMat(M);
var v = func.Params.Variables.FirstOrDefault(v => v.Name == varName);
if (v == null)
{
v = new LocalVariableItem(varName, "String", json);
System.Windows.Application.Current.Dispatcher.Invoke(() => func.Params.Variables.Add(v));
}
else
{
v.RawValue = json;
}
item.Flow.Done = true;
return true;
}
}
}