using PLCSharp.VVMs.GlobalVariables; using PLCSharp.VVMs.Vision.Camera; using PLCSharp.VVMs.Vision.Models; using PLCSharp.VVMs.Vision.VisionFlowHandler.Access; using PLCSharp.VVMs.Vision.VisionFlowHandler.Algorithm; using PLCSharp.VVMs.Vision.VisionFlowHandler.Processing; using Prism.Commands; using System.Reflection; using System.Threading; namespace PLCSharp.VVMs.Vision; /// /// 视觉流程配置界面 —— 流程步骤的增删、测试和参数编辑 /// public partial class VisionConfigViewModel { #region 视觉流程 private VisionFlow _SelectVisionFlow; /// /// 选择VisionFlow /// public VisionFlow SelectVisionFlow { get { return _SelectVisionFlow; } set { SetProperty(ref _SelectVisionFlow, value); ContentRegion = null; if (_SelectVisionFlow != null) { _SelectVisionFlow_TypeChanged(); } } } private void _SelectVisionFlow_TypeChanged() { ContentRegion = null; switch (_SelectVisionFlow.Type) { case VisionFlowType.从全局图像获取图片: case VisionFlowType.存到全局图像: ContentRegion = new ImageFromGlobal(); break; case VisionFlowType.图像相减: ContentRegion = new ImageFromLocal(); break; case VisionFlowType.图像校正: ContentRegion = new ImageWarp(); break; case VisionFlowType.从局部图像获取图片: case VisionFlowType.存到局部图像: ContentRegion = new ImageFromLocal(); break; case VisionFlowType.从文件获取图片: ContentRegion = new ImageFromFile(); break; case VisionFlowType.拍照: ContentRegion = new ImageFromCamera(); break; case VisionFlowType.阈值: ContentRegion = new Threshold(); break; case VisionFlowType.GRAY2BGR: break; case VisionFlowType.BGR2GRAY: break; case VisionFlowType.存到文件: ContentRegion = new ImageToFile(); break; case VisionFlowType.各通道最小值: break; case VisionFlowType.各通道最大值: break; case VisionFlowType.显示图像到主页: ContentRegion = new ImageToHomePage(); break; case VisionFlowType.卡尺寻边: ContentRegion = new CaliperFindEdge(); break; case VisionFlowType.卡尺找圆: ContentRegion = new CaliperFindCircle(); break; case VisionFlowType.卡尺找旋转矩形: ContentRegion = new CaliperFindRect(); break; case VisionFlowType.两线交点: ContentRegion = new TwoLineIntersect(); break; case VisionFlowType.ORB匹配: ContentRegion = new ORBMatch(); break; case VisionFlowType.取通道: ContentRegion = new SplitChannel(); break; case VisionFlowType.坐标转换: ContentRegion = new CoordTransform(); break; case VisionFlowType.图像翻转: ContentRegion = new ImageFlip(); break; case VisionFlowType.图像旋转: ContentRegion = new ImageRotate(); break; case VisionFlowType.ROI剪切: ContentRegion = new ROICrop(); break; case VisionFlowType.直方图均衡化: ContentRegion = new HistogramEqualize(); break; case VisionFlowType.ROI解码: ContentRegion = new BarcodeDecode(); break; case VisionFlowType.微信解码: ContentRegion = new WeChatDecode(); break; case VisionFlowType.颜色面积: ContentRegion = new ColorArea(); break; case VisionFlowType.灰度面积: ContentRegion = new GrayArea(); break; case VisionFlowType.灰度模板匹配: ContentRegion = new GrayTemplateMatch(); break; case VisionFlowType.SIFT匹配: ContentRegion = new SiftMatch(); break; case VisionFlowType.矩阵生成: ContentRegion = new MatrixGen(); break; case VisionFlowType.点到点距离: ContentRegion = new PointToPointDistance(); break; case VisionFlowType.点到线距离: ContentRegion = new PointToLineDistance(); break; case VisionFlowType.画廊分类: ContentRegion = new VisionFlowHandler.Algorithm.GalleryClassifier(); break; } if (ContentRegion != null) ContentRegion.DataContext = this; } private AsyncDelegateCommand _TestFunction; /// /// 测试Function /// public AsyncDelegateCommand TestFunction => _TestFunction ??= new AsyncDelegateCommand(ExecuteTestFunctionAsync); private async Task ExecuteTestFunctionAsync() { try { SelectedVisionFunction.Flow.Reset(); await SelectedVisionFunction.RunAllAsync(); } catch (Exception ex) { SendErr(ex.Message); } if (SelectedVisionFunction.Src == null) return; ShowMat = SelectedVisionFunction.Src; } private DelegateCommand _Save; /// /// 保存 /// public DelegateCommand Save => _Save ??= new DelegateCommand(ExecuteSave); private void ExecuteSave() { VisionsModel.Manage.Execute("Save"); } private AsyncDelegateCommand _TestSelect; /// /// 测试选择流程 /// public AsyncDelegateCommand TestSelect => _TestSelect ??= new AsyncDelegateCommand(ExecuteTestSelectAsync); private async Task ExecuteTestSelectAsync() { try { if (SelectVisionFlow == null) return; await Task.Run(() => { SelectVisionFlow.Flow.Reset(); while (true) { Thread.Sleep(1); if (SelectedVisionFunction.RunItem(SelectVisionFlow)) { return; } else if (SelectVisionFlow.Flow.CheckFlowTime(10)) { throw new Exception("流程超时!"); } } }); } catch (Exception ex) { SendInfoDialog(ex.Message); } if (SelectedVisionFunction.Src == null) return; ShowMat = SelectedVisionFunction.Src; } private DelegateCommand _Manage; /// /// 流程管理 /// public DelegateCommand Manage => _Manage ??= new DelegateCommand(ExecuteManage); private void ExecuteManage(string cmd) { switch (cmd) { case "删除": if (SelectVisionFlow != null && SelectedVisionFunction.VisionFlows.Contains(SelectVisionFlow)) { SelectedVisionFunction.VisionFlows.Remove(SelectVisionFlow); } break; case "上移": if (SelectVisionFlow != null && SelectedVisionFunction.VisionFlows.IndexOf(SelectVisionFlow) > 0) { var currentIndex = SelectedVisionFunction.VisionFlows.IndexOf(SelectVisionFlow); SelectedVisionFunction.VisionFlows.Move(currentIndex, currentIndex - 1); } break; case "下移": if (SelectVisionFlow != null && SelectedVisionFunction.VisionFlows.IndexOf(SelectVisionFlow) < SelectedVisionFunction.VisionFlows.Count - 1) { var currentIndex = SelectedVisionFunction.VisionFlows.IndexOf(SelectVisionFlow); SelectedVisionFunction.VisionFlows.Move(currentIndex, currentIndex + 1); } break; } } /// /// 添加流程菜单项——按 分组 /// public List FlowMenuGroups { get; } private static List BuildFlowMenuGroups() { return [.. Enum.GetValues() .Select(t => (Type: t, Category: GetCategory(t))) .GroupBy(x => x.Category) .Select(g => new FlowMenuGroup { Header = g.Key, Items = [.. g.Select(x => new FlowMenuItem { Header = x.Type.ToString(), FlowType = x.Type })] })]; } private static string GetCategory(VisionFlowType type) { var field = typeof(VisionFlowType).GetField(type.ToString()); return field?.GetCustomAttribute()?.Category ?? "其他"; } private DelegateCommand _AddFlow; /// /// 添加Flow /// public DelegateCommand AddFlow => _AddFlow ??= new DelegateCommand(ExecuteAddFlow); private void ExecuteAddFlow(string cmd) { if (Enum.TryParse(cmd, out VisionFlowType visionFlowType)) { var newFlow = new VisionFlow() { Type = visionFlowType }; SelectedVisionFunction.VisionFlows.Add(newFlow); switch (visionFlowType) { case VisionFlowType.从全局图像获取图片: break; case VisionFlowType.从文件获取图片: break; case VisionFlowType.拍照: break; case VisionFlowType.阈值: newFlow.IntParams["ThresholdType"] = 0; newFlow.IntParams["Threshold"] = 100; newFlow.IntParams["MaxValue"] = 255; newFlow.BoolParams["IsOtsu"] = false; newFlow.BoolParams["IsTriangle"] = false; break; case VisionFlowType.GRAY2BGR: break; case VisionFlowType.BGR2GRAY: break; case VisionFlowType.存到全局图像: break; case VisionFlowType.存到文件: break; case VisionFlowType.各通道最小值: break; case VisionFlowType.各通道最大值: break; case VisionFlowType.显示图像到主页: break; case VisionFlowType.卡尺寻边: newFlow.IntParams["Threshold"] = 30; newFlow.IntParams["Direction"] = 0; newFlow.IntParams["EdgeSelector"] = 0; newFlow.IntParams["MinScore"] = 10; newFlow.DoubleParams["RansacTh"] = 5; newFlow.IntParams["CaliperLength"] = 100; newFlow.IntParams["NumCalipers"] = 50; newFlow.BoolParams["Caliper_OutOffset"] = true; newFlow.StringParams["EdgeName"] = "卡尺寻边_Edge"; newFlow.StringParams["LineOffsetVar"] = "找线_Offset"; newFlow.StringParams["Caliper_TransformOutVar"] = "找线_转换"; break; case VisionFlowType.卡尺找圆: newFlow.StringParams["CircleResultVar"] = "卡尺找圆_Circle"; newFlow.StringParams["CircleOffsetVar"] = "找圆_Offset"; newFlow.StringParams["CircleFind_TransOutVar"] = "卡尺找圆_转换"; newFlow.IntParams["Threshold"] = 30; newFlow.IntParams["Direction"] = 0; newFlow.IntParams["CaliperDirection"] = 0; newFlow.IntParams["EdgeSelector"] = 0; newFlow.IntParams["MinInliers"] = 10; newFlow.DoubleParams["RansacTh"] = 5; newFlow.IntParams["MinScore"] = 10; newFlow.IntParams["NumCalipers"] = 50; newFlow.IntParams["CaliperLength"] = 100; break; case VisionFlowType.卡尺找旋转矩形: newFlow.StringParams["RectFind_TransOutVar"] = "卡尺旋转矩形_转换"; newFlow.IntParams["Threshold"] = 30; newFlow.IntParams["Direction"] = 0; newFlow.IntParams["CaliperDirection"] = 0; newFlow.IntParams["EdgeSelector"] = 0; newFlow.IntParams["MinInliers"] = 10; newFlow.DoubleParams["RansacTh"] = 5; newFlow.IntParams["MinScore"] = 10; newFlow.IntParams["NumCalipers"] = 50; newFlow.IntParams["CaliperLength"] = 100; newFlow.StringParams["ORBMatch_PosVar"] = "ORB匹配_Pos"; newFlow.StringParams["ORBMatch_OffsetVar"] = "ORB匹配_Offset"; break; case VisionFlowType.ORB匹配: newFlow.StringParams["Orb_TransOutVar"] = "ORB匹配_转换"; newFlow.StringParams["OrbMatch_OffsetTransVar"] = "ORB匹配_偏移转换"; newFlow.StringParams["ROIShape"] = "矩形"; newFlow.IntParams["NFeatures"] = 500; newFlow.IntParams["MinMatches"] = 8; newFlow.DoubleParams["MinGoodRatio"] = 0.3; newFlow.StringParams["TemplateName"] = "ORB模板"; break; case VisionFlowType.取通道: newFlow.IntParams["ChannelIndex"] = 0; break; case VisionFlowType.坐标转换: newFlow.DoubleParams["CalibSpacing"] = 5; newFlow.DoubleParams["ImageX"] = 0; newFlow.DoubleParams["ImageY"] = 0; newFlow.StringParams["TransformMat"] = "标定矩阵"; newFlow.StringParams["OutputVar"] = "转换结果"; break; case VisionFlowType.图像翻转: newFlow.IntParams["FlipDirection"] = 0; break; case VisionFlowType.图像旋转: newFlow.DoubleParams["RotateAngle"] = 0; newFlow.IntParams["ResizeMode"] = 0; break; case VisionFlowType.ROI剪切: break; case VisionFlowType.直方图均衡化: newFlow.IntParams["Method"] = 0; newFlow.DoubleParams["ClipLimit"] = 2.0; newFlow.IntParams["TileSize"] = 8; break; case VisionFlowType.图像相减: break; case VisionFlowType.图像校正: newFlow.StringParams["MatrixVar"] = "SIFT匹配矩阵"; break; case VisionFlowType.ROI解码: newFlow.IntParams["DecodeType"] = 0; newFlow.BoolParams["EnableMirror"] = false; newFlow.BoolParams["EnableUpscale"] = true; newFlow.BoolParams["UsePureBarcode"] = false; newFlow.IntParams["DM_MinEdge"] = 0; newFlow.IntParams["DM_MaxEdge"] = 0; newFlow.StringParams["ResultVarName"] = "解码结果"; break; case VisionFlowType.微信解码: newFlow.BoolParams["EnableMirror"] = false; newFlow.StringParams["ResultVarName"] = "微信解码结果"; 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"] = "颜色面积结果"; break; case VisionFlowType.灰度面积: newFlow.IntParams["GrayMin"] = 0; newFlow.IntParams["GrayMax"] = 255; newFlow.DoubleParams["AreaMinPercent"] = 0; newFlow.DoubleParams["AreaMaxPercent"] = 100; newFlow.StringParams["ResultVarName"] = "灰度面积结果"; break; case VisionFlowType.灰度模板匹配: newFlow.StringParams["ROIShape"] = "矩形"; newFlow.DoubleParams["MinAngle"] = -10; newFlow.DoubleParams["MaxAngle"] = 10; newFlow.DoubleParams["AngleStep"] = 1; newFlow.DoubleParams["MatchScoreMax"] = 0.9; newFlow.StringParams["TemplateName"] = "GrayMatch_Template"; newFlow.StringParams["GrayTemplateMatch_PosVar"] = "灰度模板匹配结果"; newFlow.StringParams["GrayMatch_OffsetVar"] = "灰度模板匹配_偏移"; newFlow.BoolParams["GrayMatch_OutOffset"] = true; newFlow.StringParams["GrayMatch_TransOutVar"] = "灰度模板匹配_转换"; newFlow.StringParams["GrayMatch_OffsetTransVar"] = "灰度模板匹配_偏移转换"; newFlow.StringParams["ROIShape"] = "矩形"; break; case VisionFlowType.SIFT匹配: newFlow.IntParams["NFeatures"] = 1000; newFlow.StringParams["GrayMatch_OffsetTransVar"] = "灰度模板匹配_偏移转换"; newFlow.IntParams["MinMatches"] = 8; newFlow.DoubleParams["MinGoodRatio"] = 0.3; newFlow.StringParams["TemplateName"] = "SIFT模板"; newFlow.StringParams["SiftMatch_PosVar"] = "SIFT匹配结果"; newFlow.StringParams["SiftMatch_OffsetVar"] = "SIFT匹配偏移"; newFlow.StringParams["SiftMatch_MatVar"] = "SIFT匹配矩阵"; newFlow.StringParams["Sift_TransOutVar"] = "SIFT匹配_转换"; newFlow.StringParams["SiftMatch_OffsetTransVar"] = "SIFT匹配_偏移转换"; newFlow.BoolParams["SiftMatch_OutPos"] = true; newFlow.BoolParams["SiftMatch_OutOffset"] = true; newFlow.BoolParams["SiftMatch_OutMat"] = true; newFlow.StringParams["ROIShape"] = "矩形"; break; case VisionFlowType.矩阵生成: newFlow.DoubleParams["MoveX"] = 0; newFlow.DoubleParams["MoveY"] = 0; newFlow.DoubleParams["Angle"] = 0; newFlow.StringParams["MatrixVar"] = "生成矩阵"; 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["TwoLine_TransOutVar"] = "两线交点_转换"; newFlow.StringParams["IntersectResultVar"] = "两线交点"; newFlow.StringParams["IntersectOffsetVar"] = "两线交点_Offset"; newFlow.BoolParams["TwoLine_OutOffset"] = true; break; default: break; } SelectVisionFlow = null; } } #endregion 视觉流程 #region 流程设置引用 private CameraBase _SelectedCamera; /// /// 选中的相机 /// public CameraBase SelectedCamera { get { return _SelectedCamera; } set { SetProperty(ref _SelectedCamera, value); } } private Variable _FlowSelectedVariable; /// /// 在流程中选择的全局变量 /// public Variable FlowSelectedVariable { get { return _FlowSelectedVariable; } set { SetProperty(ref _FlowSelectedVariable, value); } } #endregion 流程设置引用 }