Esempio n. 1
0
        private void EqualizeHistogram(EqualizeMethod method)
        {
            FastBitmap bmp  = ((ImageForm)ActiveMdiChild).Image;
            Histogram  hist = ((ImageForm)ActiveMdiChild).Histogram;

            ImageHelper.EqualizeHistogram(bmp, hist, method);
            ((ImageForm)ActiveMdiChild).Refresh();
        }
Esempio n. 2
0
        public static void EqualizeHistogram(FastBitmap bmp, Histogram hist, EqualizeMethod method)
        {
            int    R    = 0;
            double hInt = 0.0;

            double[] left     = new double[256];
            double[] right    = new double[256];
            int[]    newValue = new int[256];

            for (int i = 0; i < 256; ++i)
            {
                left[i] = R;
                hInt   += hist.HistogramTable[i];
                while (hInt > hist.Average)
                {
                    hInt -= hist.Average;
                    if (R < 255)
                    {
                        R++;
                    }
                }

                right[i] = R;
                switch (method)
                {
                case EqualizeMethod.Averages:
                    newValue[i] = (int)((left[i] + right[i]) / 2.0);
                    break;

                case EqualizeMethod.Random:
                    newValue[i] = (int)(right[i] - left[i]);
                    break;

                case EqualizeMethod.Own:
                    newValue[i] = (int)((left[i] + right[i]) / 2.0);
                    break;
                }
            }

            for (int i = 0; i < bmp.Size.Width; ++i)
            {
                for (int j = 0; j < bmp.Size.Height; ++j)
                {
                    Color color = bmp[i, j];
                    if (left[color.R] == right[color.R])
                    {
                        bmp[i, j] = Color.FromArgb(color.A, (int)left[color.R], (int)left[color.R], (int)left[color.R]);
                    }
                    else
                    {
                        switch (method)
                        {
                        case EqualizeMethod.Averages:
                            bmp[i, j] = Color.FromArgb(color.A, (int)newValue[color.R], (int)newValue[color.R], (int)newValue[color.R]);
                            break;

                        case EqualizeMethod.Random:
                            Random rnd   = new Random();
                            int    value = (int)left[color.R] + rnd.Next(newValue[color.R] + 1);
                            bmp[i, j] = Color.FromArgb(color.A, value, value, value);
                            break;

                        case EqualizeMethod.Neighborhood8:
                            double average = 0;
                            int    count   = 0;
                            foreach (Point offset in new Point[] { new Point(1, 0), new Point(-1, 0), new Point(0, 1), new Point(0, -1), new Point(1, 1), new Point(-1, -1), new Point(-1, 1), new Point(1, -1) })
                            {
                                if (i + offset.X >= 0 && i + offset.X < bmp.Width && j + offset.Y >= 0 && j + offset.Y < bmp.Height)
                                {
                                    average += bmp[i + offset.X, j + offset.Y].R;
                                    ++count;
                                }
                            }
                            average /= count;
                            if (average > right[color.R])
                            {
                                bmp[i, j] = Color.FromArgb(color.A, (int)right[color.R], (int)right[color.R], (int)right[color.R]);
                            }
                            else if (average < left[color.R])
                            {
                                bmp[i, j] = Color.FromArgb(color.A, (int)left[color.R], (int)left[color.R], (int)left[color.R]);
                            }
                            else
                            {
                                bmp[i, j] = Color.FromArgb(color.A, (int)average, (int)average, (int)average);
                            }
                            break;

                        case EqualizeMethod.Own:
                            bmp[i, j] = Color.FromArgb(color.A, (int)newValue[color.R], (int)newValue[color.R], (int)newValue[color.R]);
                            break;
                        }
                    }
                }
            }
        }