diff --git a/PLCSharpWpf/VVMs/Vision/VisionConfigViewModel.Flows.cs b/PLCSharpWpf/VVMs/Vision/VisionConfigViewModel.Flows.cs index 12578f2..1f870f8 100644 --- a/PLCSharpWpf/VVMs/Vision/VisionConfigViewModel.Flows.cs +++ b/PLCSharpWpf/VVMs/Vision/VisionConfigViewModel.Flows.cs @@ -368,6 +368,7 @@ public partial class VisionConfigViewModel newFlow.DoubleParams["RansacTh"] = 5; newFlow.IntParams["CaliperLength"] = 100; newFlow.IntParams["NumCalipers"] = 50; + newFlow.BoolParams["Caliper_OutOffset"] = true; break; case VisionFlowType.卡尺找圆: newFlow.IntParams["Threshold"] = 30; diff --git a/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/CaliperFindEdge.xaml b/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/CaliperFindEdge.xaml index 652afe8..aa3fa3a 100644 --- a/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/CaliperFindEdge.xaml +++ b/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/CaliperFindEdge.xaml @@ -52,13 +52,13 @@ - + - + - + @@ -74,13 +74,13 @@ - + - + - + @@ -121,11 +121,35 @@ - + + - + + + + + + + + + + + + + + + + + + diff --git a/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/CaliperFindEdgeHandler.cs b/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/CaliperFindEdgeHandler.cs index 0a5115d..ac3cd5a 100644 --- a/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/CaliperFindEdgeHandler.cs +++ b/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/CaliperFindEdgeHandler.cs @@ -176,13 +176,11 @@ namespace PLCSharp.VVMs.Vision.VisionFlowHandler.Algorithm item.DoubleParams.TryGetValue("TemplateLineEndX", out double tex) && item.DoubleParams.TryGetValue("TemplateLineEndY", out double tey)) { - // 计算模板中心偏移 double cenX = (sx + ex) / 2, cenY = (sy + ey) / 2; double tCenX = (tsx + tex) / 2, tCenY = (tsy + tey) / 2; double offsetX = Math.Round(cenX - tCenX, 3); double offsetY = Math.Round(cenY - tCenY, 3); - // 角度:统一用拟合线与首尾卡尺的交点 double tAngle = Math.Atan2(tey - tsy, tex - tsx); var lineInls = new Line2D(inls[0].X, inls[0].Y, inls[^1].X, inls[^1].Y); double fc = (0 + 0.5) / numCalipers; @@ -195,18 +193,66 @@ namespace PLCSharp.VVMs.Vision.VisionFlowHandler.Algorithm var ce = Intersect(lineInls, caliperLast); double cAngle = Math.Atan2(ce.Y - cs.Y, ce.X - cs.X); double offsetAngle = Math.Round((cAngle - tAngle) * 180 / Math.PI, 3); + func.Params.ResultDoubles["LineOffsetX"] = offsetX; func.Params.ResultDoubles["LineOffsetY"] = offsetY; func.Params.ResultDoubles["LineOffsetAngle"] = offsetAngle; - var offVarName = item.StringParams.TryGetValue("LineOffsetVar", out var ov) && !string.IsNullOrEmpty(ov) ? ov : "找线_Offset"; - var offVar = func.Params.Variables.FirstOrDefault(v => v.Name == offVarName); - if (offVar == null) + if (!item.BoolParams.TryGetValue("Caliper_OutOffset", out bool skipOff) || skipOff) { - offVar = new LocalVariableItem(offVarName, "Pos", new Pos()); - System.Windows.Application.Current.Dispatcher.Invoke(() => func.Params.Variables.Add(offVar)); + var offVarName = item.StringParams.TryGetValue("LineOffsetVar", out var ov) && !string.IsNullOrEmpty(ov) ? ov : "找线_Offset"; + var offVar = func.Params.Variables.FirstOrDefault(v => v.Name == offVarName); + if (offVar == null) + { + offVar = new LocalVariableItem(offVarName, "Pos", new Pos()); + System.Windows.Application.Current.Dispatcher.Invoke(() => func.Params.Variables.Add(offVar)); + } + offVar.RawValue = new Pos(offsetX, offsetY, 0, offsetAngle); + } + } + + // 坐标转换 + if (item.BoolParams.TryGetValue("Caliper_EnableTransform", out bool enableXf) && enableXf) + { + if (item.StringParams.TryGetValue("Caliper_TransformMat", out var xfName) && !string.IsNullOrEmpty(xfName)) + { + var sysVar = func.GlobalModel.VariablesModel.SystemVariables.FirstOrDefault(v => v.Name == xfName); + if (sysVar?.Value != null) + { + using var xfMat = MatExtension.DeserializeAffineMat(sysVar.Value.ToString()); + double xs = 1, ys = 1; + if (xfMat.Rows >= 2 && xfMat.Cols >= 2) + { + xs = Math.Sqrt(xfMat.At(0, 0) * xfMat.At(0, 0) + xfMat.At(1, 0) * xfMat.At(1, 0)); + ys = Math.Sqrt(xfMat.At(0, 1) * xfMat.At(0, 1) + xfMat.At(1, 1) * xfMat.At(1, 1)); + } + + Point2f XfPoint(double px, double py) + { + if (xfMat.Rows == 2) + return new Point2f((float)(px * xfMat.At(0, 0) + py * xfMat.At(0, 1) + xfMat.At(0, 2)), + (float)(px * xfMat.At(1, 0) + py * xfMat.At(1, 1) + xfMat.At(1, 2))); + double w = px * xfMat.At(2, 0) + py * xfMat.At(2, 1) + xfMat.At(2, 2); + if (Math.Abs(w) < 1e-10) return new Point2f(0, 0); + return new Point2f((float)((px * xfMat.At(0, 0) + py * xfMat.At(0, 1) + xfMat.At(0, 2)) / w), + (float)((px * xfMat.At(1, 0) + py * xfMat.At(1, 1) + xfMat.At(1, 2)) / w)); + } + + var lf = XfPoint(inls[0].X, inls[0].Y); + var lt = XfPoint(inls[^1].X, inls[^1].Y); + + var outVarName = item.StringParams.TryGetValue("Caliper_TransformOutVar", out var tns) && !string.IsNullOrEmpty(tns) ? tns : "找线_转换"; + var outVar = func.Params.Variables.FirstOrDefault(v => v.Name == outVarName); + if (outVar == null) + { + outVar = new LocalVariableItem(outVarName, "Line", new Line(new Pos(), new Pos())); + System.Windows.Application.Current.Dispatcher.Invoke(() => func.Params.Variables.Add(outVar)); + } + outVar.RawValue = new Line( + new Pos(Math.Round(lf.X, 3), Math.Round(lf.Y, 3), 0, 0), + new Pos(Math.Round(lt.X, 3), Math.Round(lt.Y, 3), 0, 0)); + } } - offVar.RawValue = new Pos(offsetX, offsetY, 0, offsetAngle); } _ = func.RenderDrawAsync(); item.Flow.Done = true; diff --git a/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/CaliperFindRect.xaml b/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/CaliperFindRect.xaml index e5238c9..9acda26 100644 --- a/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/CaliperFindRect.xaml +++ b/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/CaliperFindRect.xaml @@ -61,13 +61,13 @@ - + - + - + @@ -89,16 +89,16 @@ - + - + - + - + - + diff --git a/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/ColorArea.xaml b/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/ColorArea.xaml index 58d5e3a..bdc2ca7 100644 --- a/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/ColorArea.xaml +++ b/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/ColorArea.xaml @@ -57,19 +57,19 @@ - + - + - + - + - + - + @@ -86,10 +86,10 @@ - + - + diff --git a/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/GrayArea.xaml b/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/GrayArea.xaml index 7b786ae..f11cb09 100644 --- a/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/GrayArea.xaml +++ b/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/GrayArea.xaml @@ -54,10 +54,10 @@ - + - + @@ -74,10 +74,10 @@ - + - + diff --git a/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/GrayTemplateMatch.xaml b/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/GrayTemplateMatch.xaml index 6fe00a3..0da91cd 100644 --- a/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/GrayTemplateMatch.xaml +++ b/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/GrayTemplateMatch.xaml @@ -20,7 +20,7 @@ @@ -49,7 +49,7 @@ - + @@ -72,16 +72,16 @@ - + - + - + - + @@ -94,7 +94,7 @@ - + diff --git a/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/MatrixGen.xaml b/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/MatrixGen.xaml index 168707c..ee3c23e 100644 --- a/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/MatrixGen.xaml +++ b/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/MatrixGen.xaml @@ -21,13 +21,13 @@ - + - + - + @@ -37,7 +37,7 @@ - + diff --git a/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/ORBMatch.xaml b/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/ORBMatch.xaml index 5cb9263..e8aa64f 100644 --- a/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/ORBMatch.xaml +++ b/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/ORBMatch.xaml @@ -18,7 +18,7 @@ - + @@ -55,7 +55,7 @@ - + @@ -73,13 +73,13 @@ - + - + - + @@ -94,10 +94,10 @@ - + - + diff --git a/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/PointToLineDistance.xaml b/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/PointToLineDistance.xaml index 70c2db9..9d67626 100644 --- a/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/PointToLineDistance.xaml +++ b/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/PointToLineDistance.xaml @@ -34,7 +34,7 @@ SelectedValue="{Binding SelectVisionFlow.StringParams[LineVar], Mode=TwoWay}" SelectedValuePath="Name" DisplayMemberPath="Name" /> - + - + - + @@ -54,7 +54,7 @@ - + @@ -71,13 +71,13 @@ - + - + - + @@ -94,21 +94,21 @@ - + - + - + - + - + diff --git a/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/WeChatDecode.xaml b/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/WeChatDecode.xaml index 39bd567..a9ca115 100644 --- a/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/WeChatDecode.xaml +++ b/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/WeChatDecode.xaml @@ -32,7 +32,7 @@ + Text="{Binding SelectVisionFlow.StringParams[ResultVarName], Mode=TwoWay, UpdateSourceTrigger=LostFocus}" /> diff --git a/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Processing/HistogramEqualize.xaml b/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Processing/HistogramEqualize.xaml index 54c019e..8b66455 100644 --- a/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Processing/HistogramEqualize.xaml +++ b/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Processing/HistogramEqualize.xaml @@ -34,10 +34,10 @@ - + - + diff --git a/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Processing/ImageRotate.xaml b/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Processing/ImageRotate.xaml index 756388c..03a0be2 100644 --- a/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Processing/ImageRotate.xaml +++ b/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Processing/ImageRotate.xaml @@ -22,7 +22,7 @@ - + diff --git a/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Processing/ImageWarp.xaml b/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Processing/ImageWarp.xaml index 046d97c..b1b2b4f 100644 --- a/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Processing/ImageWarp.xaml +++ b/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Processing/ImageWarp.xaml @@ -16,7 +16,7 @@ - +