예제 #1
0
        private void StretchHistogram()
        {
            int[] redHistogram   = HistogramCreator.GetHistogramFromByteArray(modifiedPixels, HistogramCreator.ColorMode.RED);
            int[] greenHistogram = HistogramCreator.GetHistogramFromByteArray(modifiedPixels, HistogramCreator.ColorMode.GREEN);
            int[] blueHistogram  = HistogramCreator.GetHistogramFromByteArray(modifiedPixels, HistogramCreator.ColorMode.BLUE);

            int minRed   = HistogramCreator.GetIndexOfMinValue(redHistogram);
            int minGreen = HistogramCreator.GetIndexOfMinValue(greenHistogram);
            int minBlue  = HistogramCreator.GetIndexOfMinValue(blueHistogram);
            int maxRed   = HistogramCreator.GetIndexOfMaxValue(redHistogram);
            int maxGreen = HistogramCreator.GetIndexOfMaxValue(greenHistogram);
            int maxBlue  = HistogramCreator.GetIndexOfMaxValue(blueHistogram);

            byte[] LUTred   = new byte[256];
            byte[] LUTgreen = new byte[256];
            byte[] LUTblue  = new byte[256];

            for (int i = 0; i < 256; i++)
            {
                LUTred[i]   = (byte)(255f / (maxRed - minRed) * (i - minRed));
                LUTgreen[i] = (byte)(255f / (maxGreen - minGreen) * (i - minGreen));
                LUTblue[i]  = (byte)(255f / (maxBlue - minBlue) * (i - minBlue));
            }

            for (int i = 0; i < modifiedPixels.Length; i += 4)
            {
                modifiedPixels[i + 2] = LUTred[modifiedPixels[i + 2]];
                modifiedPixels[i + 1] = LUTgreen[modifiedPixels[i + 1]];
                modifiedPixels[i]     = LUTblue[modifiedPixels[i]];
            }

            UpdateModifiedImageSource();
        }
예제 #2
0
        private void EqualizeHistogram()
        {
            int[] redHistogram   = HistogramCreator.GetHistogramFromByteArray(modifiedPixels, HistogramCreator.ColorMode.RED);
            int[] greenHistogram = HistogramCreator.GetHistogramFromByteArray(modifiedPixels, HistogramCreator.ColorMode.GREEN);
            int[] blueHistogram  = HistogramCreator.GetHistogramFromByteArray(modifiedPixels, HistogramCreator.ColorMode.BLUE);

            int pixelsAmount = redHistogram.Sum();

            double redSum   = 0;
            double greenSum = 0;
            double blueSum  = 0;

            int minRed   = HistogramCreator.GetIndexOfMinValue(redHistogram);
            int minGreen = HistogramCreator.GetIndexOfMinValue(greenHistogram);
            int minBlue  = HistogramCreator.GetIndexOfMinValue(blueHistogram);

            int redK   = redHistogram.Count(x => x != 0);
            int greenK = greenHistogram.Count(x => x != 0);
            int blueK  = blueHistogram.Count(x => x != 0);

            double[] distributionRedHistogram   = new double[256];
            double[] distributionGreenHistogram = new double[256];
            double[] distributionBlueHistogram  = new double[256];

            for (int i = 0; i < 256; i++)
            {
                redSum += (double)redHistogram[i] / (double)pixelsAmount;
                distributionRedHistogram[i] = redSum;

                greenSum += (double)greenHistogram[i] / (double)pixelsAmount;
                distributionGreenHistogram[i] = greenSum;

                blueSum += (double)blueHistogram[i] / (double)pixelsAmount;
                distributionBlueHistogram[i] = blueSum;
            }

            int[] LUTred   = new int[256];
            int[] LUTgreen = new int[256];
            int[] LUTblue  = new int[256];

            for (int i = 0; i < 256; i++)
            {
                LUTred[i]   = (int)Math.Round((distributionRedHistogram[i] - distributionRedHistogram[minRed]) / (1 - distributionRedHistogram[minRed]) * (redK - 1));
                LUTgreen[i] = (int)Math.Round((distributionGreenHistogram[i] - distributionGreenHistogram[minGreen]) / (1 - distributionGreenHistogram[minGreen]) * (greenK - 1));
                LUTblue[i]  = (int)Math.Round((distributionBlueHistogram[i] - distributionBlueHistogram[minBlue]) / (1 - distributionBlueHistogram[minBlue]) * (blueK - 1));
            }

            for (int i = 0; i < modifiedPixels.Length; i += 4)
            {
                modifiedPixels[i + 2] = (byte)LUTred[modifiedPixels[i + 2]];
                modifiedPixels[i + 1] = (byte)LUTgreen[modifiedPixels[i + 1]];
                modifiedPixels[i]     = (byte)LUTblue[modifiedPixels[i]];
            }

            UpdateModifiedImageSource();
        }
예제 #3
0
        public Histogram(byte[] imagePixels, bool grayScale)
        {
            redHistogram      = HistogramCreator.GetHistogramFromByteArray(imagePixels, HistogramCreator.ColorMode.RED);
            greenHistogram    = HistogramCreator.GetHistogramFromByteArray(imagePixels, HistogramCreator.ColorMode.GREEN);
            blueHistogram     = HistogramCreator.GetHistogramFromByteArray(imagePixels, HistogramCreator.ColorMode.BLUE);
            averagedHistogram = HistogramCreator.GetHistogramFromByteArray(imagePixels, HistogramCreator.ColorMode.GRAYSCALE);

            InitializeComponent();

            if (grayScale)
            {
                RedValuesRadioBtn.IsEnabled   = false;
                GreenValuesRadioBtn.IsEnabled = false;
                BlueValuesRadioBtn.IsEnabled  = false;
            }
        }
예제 #4
0
        private void OtsuThresholdCalculate()
        {
            int[] histogram = HistogramCreator.GetHistogramFromByteArray(pixels, HistogramCreator.ColorMode.GRAYSCALE);

            int    threshold        = 0;
            double minClassVariance = double.MaxValue;

            for (int T = 0; T < 256; T++)
            {
                double classVariance = CalculateClassVariance(ref histogram, T);
                if (classVariance < minClassVariance)
                {
                    threshold        = T;
                    minClassVariance = classVariance;
                }
            }

            MessageBox.Show($"Znaleziony optymalny próg metodą Otsu wynosi {threshold}.");

            ThresholdSpinValue.Value = (byte)threshold;
        }