public byte CalculateAdaptiveVerticalThreshold(HistogramModes histogramMode) { int[] array = null; byte min = 0; byte max = 0; switch (histogramMode) { case HistogramModes.Intensity: array = Intensity; min = IntensityMin; max = IntensityMax; break; case HistogramModes.Red: array = Red; min = RedMin; max = RedMax; break; case HistogramModes.Green: array = Green; min = GreenMin; max = GreenMax; break; case HistogramModes.Blue: array = Blue; min = BlueMin; max = BlueMax; break; default: array = Intensity; min = IntensityMin; max = IntensityMax; break; } byte previousThreshold = 0; byte threshold = Calculations.ClampToByte((max - min) / 2); while (previousThreshold != threshold) { byte lessAverage = CalculateAverageByte(array, min, threshold); byte greatAverage = CalculateAverageByte(array, Calculations.ClampToByte(threshold + 1), max); previousThreshold = threshold; threshold = Calculations.ClampToByte((lessAverage + greatAverage) / 2); } return(threshold); }
private void ChangeHistogramMode() { HistogramMode = (HistogramModes)histogramModeComboBox.SelectedIndex; switch (HistogramMode) { case HistogramModes.Intensity: histogramChart.Stroke = new SolidColorBrush(Colors.Black); histogramChart.Color = histogramChart.Stroke; histogramChart.Description = "Intensity"; histogramChart.Plot(Histogram.enumerationX, Image.ImageHistogram.Intensity); horizontalThresholdSlider.Maximum = Image.ImageHistogram.IntensityMaxValue; break; case HistogramModes.Red: histogramChart.Stroke = new SolidColorBrush(Colors.Red); histogramChart.Color = histogramChart.Stroke; histogramChart.Description = "Red"; histogramChart.Plot(Histogram.enumerationX, Image.ImageHistogram.Red); horizontalThresholdSlider.Maximum = Image.ImageHistogram.RedMaxValue; break; case HistogramModes.Green: histogramChart.Stroke = new SolidColorBrush(Colors.Green); histogramChart.Color = histogramChart.Stroke; histogramChart.Description = "Green"; histogramChart.Plot(Histogram.enumerationX, Image.ImageHistogram.Green); horizontalThresholdSlider.Maximum = Image.ImageHistogram.GreenMaxValue; break; case HistogramModes.Blue: histogramChart.Stroke = new SolidColorBrush(Colors.Blue); histogramChart.Color = histogramChart.Stroke; histogramChart.Description = "Blue"; histogramChart.Plot(Histogram.enumerationX, Image.ImageHistogram.Blue); horizontalThresholdSlider.Maximum = Image.ImageHistogram.BlueMaxValue; break; } }