Exemple #1
0
        private void Equalization_Click(object sender, EventArgs e)
        {
            int[] R = new int[256];
            int[] G = new int[256];
            int[] B = new int[256];

            Bitmap     bitmap = new Bitmap(pictureBox1.Image.Width, pictureBox1.Image.Height);
            LockBitmap n      = new LockBitmap((Bitmap)bitmap);
            LockBitmap o      = new LockBitmap((Bitmap)pictureBox1.Image);

            o.LockBits();
            n.LockBits();
            for (int i = 0; i < pictureBox1.Image.Height; i++)
            {
                for (int j = 0; j < pictureBox1.Image.Width; j++)
                {
                    var p = o.GetPixel(j, i);
                    R[p.R]++;
                    G[p.G]++;
                    B[p.B]++;
                }
            }

            int cdfRMin = 0;
            int cdfGMin = 0;
            int cdfBMin = 0;

            for (int i = 1; i < 256; i++)
            {
                if (R[i - 1] > 0 && cdfRMin == 0)
                {
                    cdfRMin = R[i - 1];
                }
                if (G[i - 1] > 0 && cdfGMin == 0)
                {
                    cdfGMin = G[i - 1];
                }
                if (B[i - 1] > 0 && cdfBMin == 0)
                {
                    cdfBMin = B[i - 1];
                }

                R[i] += R[i - 1];
                G[i] += G[i - 1];
                B[i] += B[i - 1];
            }
            int NxM = pictureBox1.Image.Height * pictureBox1.Image.Width;

            for (int i = 0; i < pictureBox1.Image.Height; i++)
            {
                for (int j = 0; j < pictureBox1.Image.Width; j++)
                {
                    var p = o.GetPixel(j, i);
                    int r = (int)(255 * (R[p.R] - cdfRMin) / (NxM - cdfRMin));
                    int g = (int)(255 * (G[p.G] - cdfGMin) / (NxM - cdfGMin));
                    int b = (int)(255 * (B[p.B] - cdfBMin) / (NxM - cdfBMin));
                    n.SetPixel(j, i, Color.FromArgb(r, g, b));
                }
            }

            o.UnlockBits();
            n.UnlockBits();
            pictureBox1.Image = bitmap;
        }