int GetDimensionResults(int dimensionMin, int dimensionMax, bool isHighSetting, Func <int, MCvScalar, MCvScalar> scalarUpdator) { var results = new List <AutoThresholdResult>(); var requiredArea = _targetRegion.Area() * RequiredMomentAreaInRoiPercent / 100; Func <ColourDetectorOutput, bool> meetsMinimumAreaRequired = o => o.MomentArea >= requiredArea; for (int i = dimensionMin; i < dimensionMax; i++) { var detectorInput = new ColourDetectorInput(); detectorInput.Captured = _input; detectorInput.ErodeDilateIterations = ErodeDilateIterations; detectorInput.Settings.Absorb(_settings); detectorInput.Settings.MomentArea = new RangeF(0, float.MaxValue); if (isHighSetting) { detectorInput.Settings.HighThreshold = scalarUpdator(i, detectorInput.Settings.HighThreshold); } else { detectorInput.Settings.LowThreshold = scalarUpdator(i, detectorInput.Settings.LowThreshold); } var tickResult = new AutoThresholdResult(); tickResult.DimensionValue = i; detectorInput.Settings.Roi = _targetRegion; tickResult.RoiOutput = _colourDetector.Process(detectorInput); // Save processing the full screen if it doesn't meet minimum ROI requirements if (meetsMinimumAreaRequired(tickResult.RoiOutput)) { detectorInput.Settings.Roi = Rectangle.Empty; tickResult.FullOutput = _colourDetector.Process(detectorInput); results.Add(tickResult); } ColourCheckTick?.Invoke(this, tickResult); Intercept?.Invoke(detectorInput); } // Remove any where region of interest isn't highlighted results.RemoveAll(r => r.RoiOutput.MomentArea < requiredArea); // Pick the smallest thresholded area left in full screen var result = results .OrderBy(r => r.FullOutput.MomentArea) .FirstOrDefault(); if (result == null) { return(isHighSetting ? dimensionMax : dimensionMin); } return(result.DimensionValue); }
private void ThresholdSelector_ColourCheckTick(object sender, AutoThresholdResult e) { if (e.FullOutput ==null || e.FullOutput.ThresholdImage == null) { return; } var processed = e.FullOutput.ThresholdImage; var enhanced = processed.Mat.ToImage<Bgr, byte>(); enhanced.Draw(_readyRectangle, new Bgr(Color.Blue)); imageBoxFiltered.Image = enhanced; }