roi剪切
This commit is contained in:
parent
08d304e754
commit
e708eabd6e
@ -353,6 +353,8 @@ namespace PLCSharp.VVMs.Vision
|
||||
newFlow.DoubleParams["RotateAngle"] = 0;
|
||||
newFlow.IntParams["ResizeMode"] = 0;
|
||||
break;
|
||||
case VisionFlowType.ROI剪切:
|
||||
break;
|
||||
case VisionFlowType.ROI解码:
|
||||
newFlow.IntParams["DecodeType"] = 0;
|
||||
newFlow.BoolParams["EnableMirror"] = false;
|
||||
@ -593,6 +595,9 @@ namespace PLCSharp.VVMs.Vision
|
||||
case VisionFlowType.图像旋转:
|
||||
ContentRegion = new ImageRotate();
|
||||
break;
|
||||
case VisionFlowType.ROI剪切:
|
||||
ContentRegion = new ROICrop();
|
||||
break;
|
||||
case VisionFlowType.ROI解码:
|
||||
ContentRegion = new BarcodeDecode();
|
||||
break;
|
||||
@ -1778,6 +1783,58 @@ namespace PLCSharp.VVMs.Vision
|
||||
}
|
||||
}
|
||||
|
||||
private AsyncDelegateCommand _DrawROICropROI;
|
||||
/// <summary>
|
||||
/// 绘制ROI剪切 ROI 区域
|
||||
/// </summary>
|
||||
public AsyncDelegateCommand DrawROICropROI =>
|
||||
_DrawROICropROI ??= new AsyncDelegateCommand(ExecuteDrawROICropROIAsync);
|
||||
|
||||
private async Task ExecuteDrawROICropROIAsync()
|
||||
{
|
||||
var window = System.Windows.Application.Current.Windows.OfType<System.Windows.Window>()
|
||||
.FirstOrDefault(w => w.IsActive);
|
||||
var imageEdit = WpfTool.FindVisualChild<ImageEdit>(window);
|
||||
if (imageEdit?.ImageSource == null)
|
||||
{
|
||||
SendInfoDialog("请先获取图片!");
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var roi = await imageEdit.DrawROIAsync("ROI剪切");
|
||||
if (roi == null) return;
|
||||
|
||||
if (SelectVisionFlow == null) return;
|
||||
|
||||
SelectVisionFlow.DoubleParams["ROILeft"] = roi.Left;
|
||||
SelectVisionFlow.DoubleParams["ROITop"] = roi.Top;
|
||||
SelectVisionFlow.DoubleParams["ROIWidth"] = roi.Width;
|
||||
SelectVisionFlow.DoubleParams["ROIHeight"] = roi.Height;
|
||||
|
||||
// 画矩形边框
|
||||
var rect = new System.Windows.Shapes.Rectangle
|
||||
{
|
||||
Width = roi.Width,
|
||||
Height = roi.Height,
|
||||
Stroke = Brushes.Lime,
|
||||
StrokeThickness = 2,
|
||||
StrokeDashArray = new System.Windows.Media.DoubleCollection { 4, 2 },
|
||||
Tag = "ROI剪切",
|
||||
};
|
||||
Canvas.SetLeft(rect, roi.Left);
|
||||
Canvas.SetTop(rect, roi.Top);
|
||||
imageEdit.Draw(rect);
|
||||
|
||||
SendInfoDialog("ROI剪切 ROI 已保存");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
SendInfoDialog($"ROI 绘制失败:{ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 在 ImageEdit 上画矩形边框 + 中心十字
|
||||
/// </summary>
|
||||
|
||||
@ -0,0 +1,40 @@
|
||||
<UserControl x:Class="PLCSharp.VVMs.Vision.VisionFlowHandler.Processing.ROICrop"
|
||||
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="ROI 区域">
|
||||
<StackPanel>
|
||||
<Button Height="25" Margin="5" Command="{Binding DrawROICropROI}" Content="框选 ROI 区域" />
|
||||
<Grid Margin="5">
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition />
|
||||
<RowDefinition />
|
||||
</Grid.RowDefinitions>
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="Auto" />
|
||||
<ColumnDefinition />
|
||||
<ColumnDefinition Width="Auto" />
|
||||
<ColumnDefinition />
|
||||
</Grid.ColumnDefinitions>
|
||||
<TextBlock Margin="3,0" Text="Left" VerticalAlignment="Center" />
|
||||
<TextBox Grid.Column="1" Text="{Binding SelectVisionFlow.DoubleParams[ROILeft], TargetNullValue=—, UpdateSourceTrigger=LostFocus}" />
|
||||
<TextBlock Grid.Column="2" Margin="3,0" Text="Top" VerticalAlignment="Center" />
|
||||
<TextBox Grid.Column="3" Text="{Binding SelectVisionFlow.DoubleParams[ROITop], TargetNullValue=—, UpdateSourceTrigger=LostFocus}" />
|
||||
<TextBlock Grid.Row="1" Margin="3,0" Text="Width" VerticalAlignment="Center" />
|
||||
<TextBox Grid.Row="1" Grid.Column="1" Text="{Binding SelectVisionFlow.DoubleParams[ROIWidth], TargetNullValue=—, UpdateSourceTrigger=LostFocus}" />
|
||||
<TextBlock Grid.Row="1" Grid.Column="2" Margin="3,0" Text="Height" VerticalAlignment="Center" />
|
||||
<TextBox Grid.Row="1" Grid.Column="3" Text="{Binding SelectVisionFlow.DoubleParams[ROIHeight], TargetNullValue=—, UpdateSourceTrigger=LostFocus}" />
|
||||
</Grid>
|
||||
</StackPanel>
|
||||
</GroupBox>
|
||||
</StackPanel>
|
||||
</ScrollViewer>
|
||||
</UserControl>
|
||||
@ -0,0 +1,15 @@
|
||||
using System.Windows.Controls;
|
||||
|
||||
namespace PLCSharp.VVMs.Vision.VisionFlowHandler.Processing
|
||||
{
|
||||
/// <summary>
|
||||
/// ROICrop.xaml 的交互逻辑
|
||||
/// </summary>
|
||||
public partial class ROICrop : UserControl
|
||||
{
|
||||
public ROICrop()
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,38 @@
|
||||
using OpenCvSharp;
|
||||
|
||||
namespace PLCSharp.VVMs.Vision.VisionFlowHandler.Processing
|
||||
{
|
||||
/// <summary>
|
||||
/// ROI剪切Handler — 将ROI区域从图像中裁剪出来作为新的源图像
|
||||
/// </summary>
|
||||
public class ROICropHandler : IVisionFlowHandler
|
||||
{
|
||||
public VisionFlowType Type => VisionFlowType.ROI剪切;
|
||||
|
||||
public bool Execute(VisionFunction func, VisionFlow item)
|
||||
{
|
||||
var src = func.Src ?? throw new Exception("请先获取图片!");
|
||||
|
||||
// 读取ROI参数
|
||||
if (!item.DoubleParams.TryGetValue("ROILeft", out double roiLeft) ||
|
||||
!item.DoubleParams.TryGetValue("ROITop", out double roiTop) ||
|
||||
!item.DoubleParams.TryGetValue("ROIWidth", out double roiWidth) ||
|
||||
!item.DoubleParams.TryGetValue("ROIHeight", out double roiHeight))
|
||||
throw new Exception("ROI未配置,请先框选ROI区域!");
|
||||
|
||||
int x = Math.Clamp((int)roiLeft, 0, src.Width - 1);
|
||||
int y = Math.Clamp((int)roiTop, 0, src.Height - 1);
|
||||
int w = Math.Min((int)roiWidth, src.Width - x);
|
||||
int h = Math.Min((int)roiHeight, src.Height - y);
|
||||
|
||||
if (w <= 0 || h <= 0)
|
||||
throw new Exception("ROI超出图像范围!");
|
||||
|
||||
using Mat cropped = new Mat(src, new OpenCvSharp.Rect(x, y, w, h));
|
||||
Mat newSrc = cropped.Clone();
|
||||
func.Src = newSrc;
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -339,6 +339,7 @@ namespace PLCSharp.VVMs.Vision
|
||||
[VisionFlowType.灰度面积] = new GrayAreaHandler(),
|
||||
[VisionFlowType.图像翻转] = new ImageFlipHandler(),
|
||||
[VisionFlowType.图像旋转] = new ImageRotateHandler(),
|
||||
[VisionFlowType.ROI剪切] = new ROICropHandler(),
|
||||
|
||||
};
|
||||
[NotMapped]
|
||||
|
||||
@ -50,6 +50,8 @@
|
||||
图像翻转 = 19,
|
||||
[FlowCategory("图像处理")]
|
||||
图像旋转 = 20,
|
||||
[FlowCategory("图像处理")]
|
||||
ROI剪切 = 21,
|
||||
|
||||
[FlowCategory("图像处理")]
|
||||
坐标转换 = 18,
|
||||
|
||||
Loading…
Reference in New Issue
Block a user