diff --git a/PLCSharpWpf/Core/UserControls/ImageEdit.xaml.cs b/PLCSharpWpf/Core/UserControls/ImageEdit.xaml.cs index 11d0684..df0002b 100644 --- a/PLCSharpWpf/Core/UserControls/ImageEdit.xaml.cs +++ b/PLCSharpWpf/Core/UserControls/ImageEdit.xaml.cs @@ -297,6 +297,13 @@ namespace PLCSharp.Core.UserControls double width = _tempRoi.Width; double height = _tempRoi.Height; + // 点击不拖动(宽度或高度 < 1)视为误操作,不创建 pending,保持 drawing=true 让用户重试 + if (width < 1 || height < 1) + { + isMouseLeftButtonDown = false; + return; + } + if (_roiMode == ROIType.RotateRect) { _pendingRotateRoi = new RotateRectROI diff --git a/PLCSharpWpf/VVMs/Vision/VisionConfigViewModel.cs b/PLCSharpWpf/VVMs/Vision/VisionConfigViewModel.cs index 22abd5d..99f5339 100644 --- a/PLCSharpWpf/VVMs/Vision/VisionConfigViewModel.cs +++ b/PLCSharpWpf/VVMs/Vision/VisionConfigViewModel.cs @@ -357,6 +357,17 @@ namespace PLCSharp.VVMs.Vision newFlow.BoolParams["EnableMirror"] = false; newFlow.StringParams["ResultVarName"] = "微信解码_Result"; break; + case VisionFlowType.颜色面积: + newFlow.IntParams["HMin"] = 0; + newFlow.IntParams["HMax"] = 180; + newFlow.IntParams["SMin"] = 0; + newFlow.IntParams["SMax"] = 255; + newFlow.IntParams["VMin"] = 0; + newFlow.IntParams["VMax"] = 255; + newFlow.DoubleParams["AreaMinPercent"] = 0; + newFlow.DoubleParams["AreaMaxPercent"] = 100; + newFlow.StringParams["ResultVarName"] = "颜色面积_Result"; + break; default: break; } @@ -568,6 +579,9 @@ namespace PLCSharp.VVMs.Vision case VisionFlowType.微信解码: ContentRegion = new WeChatDecode(); break; + case VisionFlowType.颜色面积: + ContentRegion = new ColorArea(); + break; } @@ -1637,6 +1651,58 @@ namespace PLCSharp.VVMs.Vision } } + private AsyncDelegateCommand _DrawColorAreaROI; + /// + /// 绘制颜色面积 ROI 区域 + /// + public AsyncDelegateCommand DrawColorAreaROI => + _DrawColorAreaROI ??= new AsyncDelegateCommand(ExecuteDrawColorAreaROIAsync); + + private async Task ExecuteDrawColorAreaROIAsync() + { + var window = System.Windows.Application.Current.Windows.OfType() + .FirstOrDefault(w => w.IsActive); + var imageEdit = WpfTool.FindVisualChild(window); + if (imageEdit?.ImageSource == null) + { + SendInfoDialog("请先获取图片!"); + return; + } + + try + { + var roi = await imageEdit.DrawROIAsync("颜色面积"); + 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 = "颜色面积", + }; + Canvas.SetLeft(rect, roi.Left); + Canvas.SetTop(rect, roi.Top); + imageEdit.Draw(rect); + + SendInfoDialog("颜色面积 ROI 已保存"); + } + catch (Exception ex) + { + SendInfoDialog($"ROI 绘制失败:{ex.Message}"); + } + } + /// /// 在 ImageEdit 上画矩形边框 + 中心十字 /// diff --git a/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/ColorArea.xaml b/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/ColorArea.xaml new file mode 100644 index 0000000..58063f9 --- /dev/null +++ b/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/ColorArea.xaml @@ -0,0 +1,136 @@ + + + + + + +