This commit is contained in:
tfarcraw 2026-07-16 12:11:11 +08:00
parent f923a286f3
commit 08d304e754
7 changed files with 116 additions and 0 deletions

View File

@ -349,6 +349,10 @@ namespace PLCSharp.VVMs.Vision
case VisionFlowType.图像翻转:
newFlow.IntParams["FlipDirection"] = 0;
break;
case VisionFlowType.图像旋转:
newFlow.DoubleParams["RotateAngle"] = 0;
newFlow.IntParams["ResizeMode"] = 0;
break;
case VisionFlowType.ROI解码:
newFlow.IntParams["DecodeType"] = 0;
newFlow.BoolParams["EnableMirror"] = false;
@ -586,6 +590,9 @@ namespace PLCSharp.VVMs.Vision
case VisionFlowType.图像翻转:
ContentRegion = new ImageFlip();
break;
case VisionFlowType.图像旋转:
ContentRegion = new ImageRotate();
break;
case VisionFlowType.ROI解码:
ContentRegion = new BarcodeDecode();
break;

View File

@ -0,0 +1,36 @@
<UserControl x:Class="PLCSharp.VVMs.Vision.VisionFlowHandler.Processing.ImageRotate"
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"
xmlns:local="clr-namespace:PLCSharp.VVMs.Vision.VisionFlowHandler"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:vision="clr-namespace:PLCSharp.VVMs.Vision"
mc:Ignorable="d"
d:DataContext="{d:DesignInstance Type=vision:VisionConfigViewModel}"
d:DesignHeight="200" d:DesignWidth="300">
<ScrollViewer VerticalScrollBarVisibility="Auto">
<StackPanel>
<GroupBox Header="旋转参数">
<Grid Margin="5">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="80" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<TextBlock Text="角度" VerticalAlignment="Center" ToolTip="旋转角度(度),正数逆时针,负数顺时针" />
<TextBox Grid.Column="1" Text="{Binding SelectVisionFlow.DoubleParams[RotateAngle], TargetNullValue=0, UpdateSourceTrigger=PropertyChanged}" />
<TextBlock Grid.Row="1" Text="尺寸模式" VerticalAlignment="Center" />
<ComboBox Grid.Row="1" Grid.Column="1" SelectedIndex="{Binding SelectVisionFlow.IntParams[ResizeMode], Mode=TwoWay}">
<ComboBoxItem Content="保持原尺寸" />
<ComboBoxItem Content="完整显示" />
</ComboBox>
</Grid>
</GroupBox>
</StackPanel>
</ScrollViewer>
</UserControl>

View File

@ -0,0 +1,15 @@
using System.Windows.Controls;
namespace PLCSharp.VVMs.Vision.VisionFlowHandler.Processing
{
/// <summary>
/// ImageRotate.xaml 的交互逻辑
/// </summary>
public partial class ImageRotate : UserControl
{
public ImageRotate()
{
InitializeComponent();
}
}
}

View File

@ -0,0 +1,54 @@
using OpenCvSharp;
namespace PLCSharp.VVMs.Vision.VisionFlowHandler.Processing
{
/// <summary>
/// 图像旋转Handler — 按指定角度旋转图像,可选择保持原尺寸或扩展画布
/// </summary>
public class ImageRotateHandler : IVisionFlowHandler
{
public VisionFlowType Type => VisionFlowType.图像旋转;
public bool Execute(VisionFunction func, VisionFlow item)
{
var src = func.Src ?? throw new Exception("请先获取图片!");
// 读取旋转角度
double angle = item.DoubleParams.TryGetValue("RotateAngle", out double a) ? a : 0;
// 读取尺寸模式:0=保持原尺寸,1=扩展画布(完整显示)
int resizeMode = item.IntParams.TryGetValue("ResizeMode", out int rm) ? rm : 0;
int w = src.Width, h = src.Height;
var center = new Point2f(w / 2f, h / 2f);
var rot = Cv2.GetRotationMatrix2D(center, angle, 1.0);
Size dstSize;
if (resizeMode == 1)
{
// 计算旋转后最小外接矩形尺寸
double rad = angle * Math.PI / 180.0;
double cos = Math.Abs(Math.Cos(rad));
double sin = Math.Abs(Math.Sin(rad));
int newW = (int)(w * cos + h * sin);
int newH = (int)(w * sin + h * cos);
dstSize = new Size(newW, newH);
// 调整平移量使图像居中
rot.Set<double>(0, 2, rot.Get<double>(0, 2) + (newW - w) / 2.0);
rot.Set<double>(1, 2, rot.Get<double>(1, 2) + (newH - h) / 2.0);
}
else
{
dstSize = new Size(w, h);
}
using Mat dst = new Mat();
Cv2.WarpAffine(src, dst, rot, dstSize);
dst.CopyTo(src);
return true;
}
}
}

View File

@ -338,6 +338,7 @@ namespace PLCSharp.VVMs.Vision
[VisionFlowType.颜色面积] = new ColorAreaHandler(),
[VisionFlowType.灰度面积] = new GrayAreaHandler(),
[VisionFlowType.图像翻转] = new ImageFlipHandler(),
[VisionFlowType.图像旋转] = new ImageRotateHandler(),
};
[NotMapped]

View File

@ -48,6 +48,8 @@
阈值 = 17,
[FlowCategory("图像处理")]
图像翻转 = 19,
[FlowCategory("图像处理")]
图像旋转 = 20,
[FlowCategory("图像处理")]
坐标转换 = 18,

View File

@ -154,6 +154,7 @@ namespace PLCSharp.VVMs.Vision
{
VisionsModel = this,
ImageDatas = ImageDatas,
GlobalModel = GlobalModel,
};
VisionFunctions.Add(newFunction);