点点/点线增加标定转换
This commit is contained in:
parent
c0ad2d4087
commit
939984681b
@ -488,9 +488,16 @@ public partial class VisionConfigViewModel
|
||||
break;
|
||||
case VisionFlowType.点到点距离:
|
||||
newFlow.StringParams["ResultVar"] = "点到点距离";
|
||||
newFlow.StringParams["PtPt_TransOutVar"] = "点到点距离";
|
||||
break;
|
||||
case VisionFlowType.点到线距离:
|
||||
newFlow.StringParams["ResultVar"] = "点到线距离";
|
||||
newFlow.StringParams["PtLine_TransOutVar"] = "点到线距离";
|
||||
break;
|
||||
case VisionFlowType.两线交点:
|
||||
newFlow.StringParams["IntersectResultVar"] = "两线交点";
|
||||
newFlow.StringParams["IntersectOffsetVar"] = "两线交点_Offset";
|
||||
newFlow.BoolParams["TwoLine_OutOffset"] = true;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
|
||||
@ -18,10 +18,13 @@
|
||||
<RowDefinition />
|
||||
<RowDefinition />
|
||||
<RowDefinition />
|
||||
<RowDefinition />
|
||||
<RowDefinition />
|
||||
</Grid.RowDefinitions>
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="80" />
|
||||
<ColumnDefinition />
|
||||
<ColumnDefinition Width="Auto" />
|
||||
</Grid.ColumnDefinitions>
|
||||
<TextBlock Grid.Row="0" Text="点变量" VerticalAlignment="Center" />
|
||||
<ComboBox Grid.Row="0" Grid.Column="1"
|
||||
@ -34,15 +37,26 @@
|
||||
SelectedValue="{Binding SelectVisionFlow.StringParams[LineVar], Mode=TwoWay}"
|
||||
SelectedValuePath="Name" DisplayMemberPath="Name" />
|
||||
<TextBlock Grid.Row="2" Text="结果变量" VerticalAlignment="Center" />
|
||||
<TextBox Grid.Row="2" Grid.Column="1" Text="{Binding SelectVisionFlow.StringParams[ResultVar], Mode=TwoWay}" />
|
||||
<TextBox Grid.Row="2" Grid.Column="1" Grid.ColumnSpan="2" Text="{Binding SelectVisionFlow.StringParams[ResultVar], Mode=TwoWay}" />
|
||||
<TextBlock Grid.Row="3" Text="距离" VerticalAlignment="Center" />
|
||||
<TextBox Grid.Row="3" Grid.Column="1"
|
||||
<TextBox Grid.Row="3" Grid.Column="1" Grid.ColumnSpan="2"
|
||||
Text="{Binding SelectedVisionFunction.Params.ResultDoubles[PointLineDist_Distance], Mode=OneWay}"
|
||||
IsReadOnly="True" Background="#FFF0F0F0" />
|
||||
<TextBlock Grid.Row="4" Text="垂足X,Y" VerticalAlignment="Center" />
|
||||
<TextBox Grid.Row="4" Grid.Column="1"
|
||||
<TextBox Grid.Row="4" Grid.Column="1" Grid.ColumnSpan="2"
|
||||
Text="{Binding SelectedVisionFunction.Params.ResultDoubles[PointLineDist_FootX], Mode=OneWay}"
|
||||
IsReadOnly="True" Background="#FFF0F0F0" />
|
||||
|
||||
<TextBlock Grid.Row="5" Text="坐标转换" VerticalAlignment="Center" />
|
||||
<ComboBox Grid.Row="5" Grid.Column="1" Margin="5,0"
|
||||
ItemsSource="{Binding SystemVariables}"
|
||||
SelectedValue="{Binding SelectVisionFlow.StringParams[PtLine_TransMat], Mode=TwoWay}"
|
||||
SelectedValuePath="Name" DisplayMemberPath="Name" />
|
||||
<CheckBox Grid.Row="5" Grid.Column="2" Content="启用" VerticalAlignment="Center"
|
||||
IsChecked="{Binding SelectVisionFlow.BoolParams[PtLine_EnableTrans], Mode=TwoWay}" />
|
||||
<TextBlock Grid.Row="6" Text="转换变量" VerticalAlignment="Center" />
|
||||
<TextBox Grid.Row="6" Grid.Column="1" Grid.ColumnSpan="2"
|
||||
Text="{Binding SelectVisionFlow.StringParams[PtLine_TransOutVar], Mode=TwoWay}" />
|
||||
</Grid>
|
||||
</GroupBox>
|
||||
</StackPanel>
|
||||
|
||||
@ -47,6 +47,61 @@ namespace PLCSharp.VVMs.Vision.VisionFlowHandler.Algorithm
|
||||
func.Params.ResultDoubles["PointLineDist_FootX"] = footX;
|
||||
func.Params.ResultDoubles["PointLineDist_FootY"] = footY;
|
||||
|
||||
// 坐标转换
|
||||
bool transformApplied = false;
|
||||
if (item.BoolParams.TryGetValue("PtLine_EnableTrans", out bool doXf) && doXf)
|
||||
{
|
||||
if (item.StringParams.TryGetValue("PtLine_TransMat", out var xfName) && !string.IsNullOrEmpty(xfName))
|
||||
{
|
||||
var sysVar = func.GlobalModel.VariablesModel.SystemVariables.FirstOrDefault(v => v.Name == xfName);
|
||||
if (sysVar?.Value != null)
|
||||
{
|
||||
Mat transformMat = MatExtension.DeserializeAffineMat((string)sysVar.Value);
|
||||
using (transformMat)
|
||||
{
|
||||
Point2f XfPt(double px, double py)
|
||||
{
|
||||
if (transformMat.Rows == 2)
|
||||
return new Point2f(
|
||||
(float)(px * transformMat.At<double>(0, 0) + py * transformMat.At<double>(0, 1) + transformMat.At<double>(0, 2)),
|
||||
(float)(px * transformMat.At<double>(1, 0) + py * transformMat.At<double>(1, 1) + transformMat.At<double>(1, 2)));
|
||||
double w = px * transformMat.At<double>(2, 0) + py * transformMat.At<double>(2, 1) + transformMat.At<double>(2, 2);
|
||||
if (Math.Abs(w) < 1e-10) return new Point2f(0, 0);
|
||||
return new Point2f(
|
||||
(float)((px * transformMat.At<double>(0, 0) + py * transformMat.At<double>(0, 1) + transformMat.At<double>(0, 2)) / w),
|
||||
(float)((px * transformMat.At<double>(1, 0) + py * transformMat.At<double>(1, 1) + transformMat.At<double>(1, 2)) / w));
|
||||
}
|
||||
|
||||
var tf = XfPt(line.From.X, line.From.Y);
|
||||
var tt = XfPt(line.To.X, line.To.Y);
|
||||
var tp = XfPt(point.X, point.Y);
|
||||
|
||||
// 变换后重新计算距离
|
||||
double nx1 = tf.X, ny1 = tf.Y, nx2 = tt.X, ny2 = tt.Y, nx0 = tp.X, ny0 = tp.Y;
|
||||
double nA = ny2 - ny1, nB = nx1 - nx2, nC = nx2 * ny1 - nx1 * ny2;
|
||||
double nDenom = Math.Sqrt(nA * nA + nB * nB);
|
||||
if (nDenom >= 1e-10)
|
||||
{
|
||||
double tDist = Math.Abs(nA * nx0 + nB * ny0 + nC) / nDenom;
|
||||
double nDot = (nx0 - nx1) * (nx2 - nx1) + (ny0 - ny1) * (ny2 - ny1);
|
||||
double nT = nDot / (nDenom * nDenom);
|
||||
double nFootX = Math.Round(nx1 + nT * (nx2 - nx1), 3);
|
||||
double nFootY = Math.Round(ny1 + nT * (ny2 - ny1), 3);
|
||||
|
||||
// 覆盖结果
|
||||
func.Params.ResultDoubles["PointLineDist_Distance"] = Math.Round(tDist, 3);
|
||||
func.Params.ResultDoubles["PointLineDist_FootX"] = nFootX;
|
||||
func.Params.ResultDoubles["PointLineDist_FootY"] = nFootY;
|
||||
transformApplied = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (transformApplied)
|
||||
dist = func.Params.ResultDoubles["PointLineDist_Distance"];
|
||||
|
||||
var resultVarName = item.StringParams.TryGetValue("ResultVar", out var rv) && !string.IsNullOrEmpty(rv) ? rv : "点到线距离";
|
||||
var resultVar = func.Params.Variables.FirstOrDefault(v => v.Name == resultVarName);
|
||||
if (resultVar == null)
|
||||
|
||||
@ -19,35 +19,48 @@
|
||||
<RowDefinition />
|
||||
<RowDefinition />
|
||||
<RowDefinition />
|
||||
<RowDefinition />
|
||||
<RowDefinition />
|
||||
</Grid.RowDefinitions>
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="80" />
|
||||
<ColumnDefinition />
|
||||
<ColumnDefinition Width="Auto" />
|
||||
</Grid.ColumnDefinitions>
|
||||
<TextBlock Grid.Row="0" Text="点1变量" VerticalAlignment="Center" />
|
||||
<ComboBox Grid.Row="0" Grid.Column="1"
|
||||
<ComboBox Grid.Row="0" Grid.Column="1" Grid.ColumnSpan="2"
|
||||
ItemsSource="{Binding AllVariables}"
|
||||
SelectedValue="{Binding SelectVisionFlow.StringParams[Point1Var], Mode=TwoWay}"
|
||||
SelectedValuePath="Name" DisplayMemberPath="Name" />
|
||||
<TextBlock Grid.Row="1" Text="点2变量" VerticalAlignment="Center" />
|
||||
<ComboBox Grid.Row="1" Grid.Column="1"
|
||||
<ComboBox Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="2"
|
||||
ItemsSource="{Binding AllVariables}"
|
||||
SelectedValue="{Binding SelectVisionFlow.StringParams[Point2Var], Mode=TwoWay}"
|
||||
SelectedValuePath="Name" DisplayMemberPath="Name" />
|
||||
<TextBlock Grid.Row="2" Text="结果变量" VerticalAlignment="Center" />
|
||||
<TextBox Grid.Row="2" Grid.Column="1" Text="{Binding SelectVisionFlow.StringParams[ResultVar], Mode=TwoWay}" />
|
||||
<TextBox Grid.Row="2" Grid.Column="1" Grid.ColumnSpan="2" Text="{Binding SelectVisionFlow.StringParams[ResultVar], Mode=TwoWay}" />
|
||||
<TextBlock Grid.Row="3" Text="距离" VerticalAlignment="Center" />
|
||||
<TextBox Grid.Row="3" Grid.Column="1"
|
||||
<TextBox Grid.Row="3" Grid.Column="1" Grid.ColumnSpan="2"
|
||||
Text="{Binding SelectedVisionFunction.Params.ResultDoubles[PointDist_Distance], Mode=OneWay}"
|
||||
IsReadOnly="True" Background="#FFF0F0F0" />
|
||||
<TextBlock Grid.Row="4" Text="ΔX" VerticalAlignment="Center" />
|
||||
<TextBox Grid.Row="4" Grid.Column="1"
|
||||
<TextBox Grid.Row="4" Grid.Column="1" Grid.ColumnSpan="2"
|
||||
Text="{Binding SelectedVisionFunction.Params.ResultDoubles[PointDist_DeltaX], Mode=OneWay}"
|
||||
IsReadOnly="True" Background="#FFF0F0F0" />
|
||||
<TextBlock Grid.Row="5" Text="ΔY" VerticalAlignment="Center" />
|
||||
<TextBox Grid.Row="5" Grid.Column="1"
|
||||
<TextBox Grid.Row="5" Grid.Column="1" Grid.ColumnSpan="2"
|
||||
Text="{Binding SelectedVisionFunction.Params.ResultDoubles[PointDist_DeltaY], Mode=OneWay}"
|
||||
IsReadOnly="True" Background="#FFF0F0F0" />
|
||||
<TextBlock Grid.Row="6" Text="坐标转换" VerticalAlignment="Center" />
|
||||
<ComboBox Grid.Row="6" Grid.Column="1" Margin="5,0"
|
||||
ItemsSource="{Binding SystemVariables}"
|
||||
SelectedValue="{Binding SelectVisionFlow.StringParams[PtPt_TransMat], Mode=TwoWay}"
|
||||
SelectedValuePath="Name" DisplayMemberPath="Name" />
|
||||
<CheckBox Grid.Row="6" Grid.Column="2" Content="启用" VerticalAlignment="Center"
|
||||
IsChecked="{Binding SelectVisionFlow.BoolParams[PtPt_EnableTrans], Mode=TwoWay}" />
|
||||
<TextBlock Grid.Row="7" Text="转换变量" VerticalAlignment="Center" />
|
||||
<TextBox Grid.Row="7" Grid.Column="1" Grid.ColumnSpan="2"
|
||||
Text="{Binding SelectVisionFlow.StringParams[PtPt_TransOutVar], Mode=TwoWay}" />
|
||||
</Grid>
|
||||
</GroupBox>
|
||||
</StackPanel>
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
using OpenCvSharp;
|
||||
using OpenCvSharp;
|
||||
using System.Windows.Media;
|
||||
|
||||
namespace PLCSharp.VVMs.Vision.VisionFlowHandler.Algorithm
|
||||
@ -29,11 +29,47 @@ namespace PLCSharp.VVMs.Vision.VisionFlowHandler.Algorithm
|
||||
func.Params.ResultDoubles["PointDist_DeltaX"] = Math.Round(pos2.X - pos1.X, 3);
|
||||
func.Params.ResultDoubles["PointDist_DeltaY"] = Math.Round(pos2.Y - pos1.Y, 3);
|
||||
|
||||
// 坐标转换
|
||||
if (item.BoolParams.TryGetValue("PtPt_EnableTrans", out bool doXf) && doXf)
|
||||
{
|
||||
if (item.StringParams.TryGetValue("PtPt_TransMat", out var xfName) && !string.IsNullOrEmpty(xfName))
|
||||
{
|
||||
var sysVar = func.GlobalModel.VariablesModel.SystemVariables.FirstOrDefault(v => v.Name == xfName);
|
||||
if (sysVar?.Value != null)
|
||||
{
|
||||
Mat transformMat = MatExtension.DeserializeAffineMat((string)sysVar.Value);
|
||||
using (transformMat)
|
||||
{
|
||||
Point2f XfPt(double px, double py)
|
||||
{
|
||||
if (transformMat.Rows == 2)
|
||||
return new Point2f(
|
||||
(float)(px * transformMat.At<double>(0, 0) + py * transformMat.At<double>(0, 1) + transformMat.At<double>(0, 2)),
|
||||
(float)(px * transformMat.At<double>(1, 0) + py * transformMat.At<double>(1, 1) + transformMat.At<double>(1, 2)));
|
||||
double w = px * transformMat.At<double>(2, 0) + py * transformMat.At<double>(2, 1) + transformMat.At<double>(2, 2);
|
||||
if (Math.Abs(w) < 1e-10) return new Point2f(0, 0);
|
||||
return new Point2f(
|
||||
(float)((px * transformMat.At<double>(0, 0) + py * transformMat.At<double>(0, 1) + transformMat.At<double>(0, 2)) / w),
|
||||
(float)((px * transformMat.At<double>(1, 0) + py * transformMat.At<double>(1, 1) + transformMat.At<double>(1, 2)) / w));
|
||||
}
|
||||
|
||||
var tp1 = XfPt(pos1.X, pos1.Y);
|
||||
var tp2 = XfPt(pos2.X, pos2.Y);
|
||||
double tDist = Math.Sqrt((tp2.X - tp1.X) * (tp2.X - tp1.X) + (tp2.Y - tp1.Y) * (tp2.Y - tp1.Y));
|
||||
func.Params.ResultDoubles["PointDist_Distance"] = Math.Round(tDist, 3);
|
||||
func.Params.ResultDoubles["PointDist_DeltaX"] = Math.Round(tp2.X - tp1.X, 3);
|
||||
func.Params.ResultDoubles["PointDist_DeltaY"] = Math.Round(tp2.Y - tp1.Y, 3);
|
||||
dist = Math.Round(tDist, 3);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var resultVarName = item.StringParams.TryGetValue("ResultVar", out var rv) && !string.IsNullOrEmpty(rv) ? rv : "点到点距离";
|
||||
var resultVar = func.Params.Variables.FirstOrDefault(v => v.Name == resultVarName);
|
||||
if (resultVar == null)
|
||||
{
|
||||
resultVar = new LocalVariableItem(resultVarName, "double", 0.0);
|
||||
resultVar = new LocalVariableItem(resultVarName, "Double", 0.0);
|
||||
System.Windows.Application.Current.Dispatcher.Invoke(() => func.Params.Variables.Add(resultVar));
|
||||
}
|
||||
resultVar.RawValue = dist;
|
||||
@ -45,7 +81,7 @@ namespace PLCSharp.VVMs.Vision.VisionFlowHandler.Algorithm
|
||||
func.DrawCommands.Add(DrawCommand.TextBlock(
|
||||
(pos1.X + pos2.X) / 2 + 5, (pos1.Y + pos2.Y) / 2 - 5,
|
||||
$"{dist:F3}", Colors.Lime, 16));
|
||||
|
||||
_ = func.RenderDrawAsync();
|
||||
item.Flow.Done = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -103,9 +103,12 @@
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="75" />
|
||||
<ColumnDefinition />
|
||||
<ColumnDefinition Width="Auto" />
|
||||
</Grid.ColumnDefinitions>
|
||||
<TextBlock Text="偏移变量" VerticalAlignment="Center" />
|
||||
<TextBox Grid.Column="1" Text="{Binding SelectVisionFlow.StringParams[IntersectOffsetVar], Mode=TwoWay}" />
|
||||
<CheckBox Grid.Column="2" Margin="5,0,0,0" Content="输出" VerticalAlignment="Center"
|
||||
IsChecked="{Binding SelectVisionFlow.BoolParams[TwoLine_OutOffset], Mode=TwoWay}" />
|
||||
</Grid>
|
||||
</StackPanel>
|
||||
</GroupBox>
|
||||
|
||||
@ -62,11 +62,14 @@ namespace PLCSharp.VVMs.Vision.VisionFlowHandler.Algorithm
|
||||
func.Params.ResultDoubles["IntersectOffY"] = offY;
|
||||
func.Params.ResultDoubles["IntersectOffAngle"] = offAngle;
|
||||
|
||||
var offVarName = item.StringParams.TryGetValue("IntersectOffsetVar", 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(offX, offY, 0, offAngle);
|
||||
if (!item.BoolParams.TryGetValue("TwoLine_OutOffset", out bool skipOff) || skipOff)
|
||||
{
|
||||
var offVarName = item.StringParams.TryGetValue("IntersectOffsetVar", 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(offX, offY, 0, offAngle);
|
||||
}
|
||||
}
|
||||
|
||||
// 画交点 — 写入 DrawCommands
|
||||
|
||||
Loading…
Reference in New Issue
Block a user