using OpenCvSharp; namespace PLCSharp.VVMs.Vision.VisionFlowHandler.Processing { /// /// 阈值Handler /// public class ThresholdHandler : IVisionFlowHandler { /// /// 类型 /// public VisionFlowType Type => VisionFlowType.阈值; /// /// 执行 /// /// 视觉功能 /// 变量项 /// 返回布尔值 public bool Execute(VisionFunction func, VisionFlow item) { if (item.IntParams.TryGetValue("ThresholdType", out int thresholdType)) { if (item.IntParams.TryGetValue("Threshold", out int threshold)) { if (item.IntParams.TryGetValue("MaxValue", out int maxValue)) { if (item.BoolParams.TryGetValue("IsOtsu", out bool isOtsu)) { if (item.BoolParams.TryGetValue("IsTriangle", out bool isTriangle)) { if (func.Src.Channels() == 3) func.Src = func.Src.CvtColor(ColorConversionCodes.BGR2GRAY); ThresholdTypes type = (ThresholdTypes)thresholdType; if (isOtsu) { type |= ThresholdTypes.Otsu; item.BoolParams["IsTriangle"] = false; isTriangle = false; } if (isTriangle) { type |= ThresholdTypes.Triangle; } if (maxValue < threshold) maxValue = threshold + 1; Cv2.Threshold(func.Src, func.Src, threshold, maxValue, type); return true; } } } } } return false; } } }