Exemplo n.º 1
0
        private void contrast(int contrast)
        {
            Bitmap     b          = new Bitmap(pictureBox1.Image.Width, pictureBox1.Image.Height);
            LockBitmap lockBitmap = new LockBitmap(b);
            LockBitmap orig       = new LockBitmap((Bitmap)original);

            float factor = (259 * (contrast + 255)) / (255 * (259 - contrast));

            lockBitmap.LockBits();
            orig.LockBits();
            for (int i = 0; i < pictureBox1.Image.Height; i++)
            {
                for (int j = 0; j < pictureBox1.Image.Width; j++)
                {
                    var   colour   = orig.GetPixel(j, i);
                    float newRed   = Truncate(factor * (colour.R - 128) + 128);
                    float newGreen = Truncate(factor * (colour.G - 128) + 128);
                    float newBlue  = Truncate(factor * (colour.B - 128) + 128);

                    lockBitmap.SetPixel(j, i, Color.FromArgb((int)newRed, (int)newGreen, (int)newBlue));
                }
            }
            lockBitmap.UnlockBits();
            orig.UnlockBits();
            pictureBox1.Image = b;
        }
Exemplo n.º 2
0
        private void EdgeDetectionEvent(object sender, EventArgs e)
        {
            Bitmap     bitmap = new Bitmap(pictureBox1.Image.Width, pictureBox1.Image.Height);
            LockBitmap old    = new LockBitmap((Bitmap)pictureBox1.Image);
            LockBitmap n      = new LockBitmap((Bitmap)bitmap);

            old.LockBits();
            n.LockBits();

            for (int i = 1; i < pictureBox1.Image.Height; i++)
            {
                for (int j = 1; j < pictureBox1.Image.Width; j++)
                {
                    var p1 = old.GetPixel(j, i);
                    var p2 = old.GetPixel(j, i - 1);
                    var p3 = old.GetPixel(j - 1, i);
                    var p4 = old.GetPixel(j - 1, i - 1);

                    int r = Math.Min(Math.Abs(p1.R - p2.R) + Math.Abs(p1.R - p3.R) + Math.Abs(p1.R - p4.R), 255);
                    int g = Math.Min(Math.Abs(p1.G - p2.G) + Math.Abs(p1.G - p3.G) + Math.Abs(p1.G - p4.G), 255);
                    int b = Math.Min(Math.Abs(p1.B - p2.B) + Math.Abs(p1.B - p3.B) + Math.Abs(p1.B - p4.B), 255);

                    n.SetPixel(j, i, Color.FromArgb(r, g, b));
                }
            }

            old.UnlockBits();
            n.UnlockBits();
            pictureBox1.Image = bitmap;
            grayImage         = bitmap;
            currentImageCp    = new Bitmap(bitmap);
        }
Exemplo n.º 3
0
        private void gaussianFilterTrackBar_ValueChanged(object sender, EventArgs e)
        {
            int param = gaussianFilterTrackBar.Value;

            int[,] matrix = new int[, ] {
                { 1, 1, param, 1, 1 },
                { 1, param, param *param, param, 1 },
                { param, param *param, param *param *param, param *param, param },
                { 1, param, param *param, param, 1 },
                { 1, 1, param, 1, 1 },
            };
            int sum = 0;

            for (int i = 0; i < 5; i++)
            {
                for (int j = 0; j < 5; j++)
                {
                    sum += matrix[i, j];
                }
            }

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

            n.LockBits();
            orig.LockBits();

            for (int i = 2; i < pictureBox1.Image.Height - 2; i++)
            {
                for (int j = 2; j < pictureBox1.Image.Width - 2; j++)
                {
                    var p = orig.GetPixel(j, i);
                    int r = 0, g = 0, b = 0;
                    for (int k = i - 2; k <= i + 2; k++)
                    {
                        for (int l = j - 2; l < j + 3; l++)
                        {
                            r += matrix[k - (i - 2), l - (j - 2)] * orig.GetPixel(l, k).R;
                            g += matrix[k - (i - 2), l - (j - 2)] * orig.GetPixel(l, k).G;
                            b += matrix[k - (i - 2), l - (j - 2)] * orig.GetPixel(l, k).B;
                        }
                    }
                    r /= sum;
                    g /= sum;
                    b /= sum;
                    n.SetPixel(j, i, Color.FromArgb(r, g, b));
                }
            }
            orig.UnlockBits();
            n.UnlockBits();
            pictureBox1.Image = bitmap;
        }
Exemplo n.º 4
0
        private void LaplacianClick(object sender, EventArgs e)
        {
            int param = gaussianFilterTrackBar.Value;

            int[,] matrix = new int[, ] {
                { 0, -1, 0 },
                { -1, 4, 1 },
                { 0, -1, 0 }
            };


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

            n.LockBits();
            orig.LockBits();

            for (int i = 1; i < pictureBox1.Image.Height - 1; i++)
            {
                for (int j = 1; j < pictureBox1.Image.Width - 1; j++)
                {
                    var p = orig.GetPixel(j, i);
                    int r = 0, g = 0, b = 0;
                    for (int k = i - 1; k <= i + 1; k++)
                    {
                        for (int l = j - 1; l <= j + 1; l++)
                        {
                            r += matrix[k - (i - 1), l - (j - 1)] * orig.GetPixel(l, k).R;
                            g += matrix[k - (i - 1), l - (j - 1)] * orig.GetPixel(l, k).G;
                            b += matrix[k - (i - 1), l - (j - 1)] * orig.GetPixel(l, k).B;
                        }
                    }

                    r = (int)Truncate(r);
                    g = (int)Truncate(g);
                    b = (int)Truncate(b);
                    n.SetPixel(j, i, Color.FromArgb(r, g, b));
                }
            }
            orig.UnlockBits();
            n.UnlockBits();
            pictureBox1.Image = bitmap;
        }
Exemplo n.º 5
0
        private void button7_Click(object sender, EventArgs e)
        {
            Bitmap     b = new Bitmap(pictureBox1.Image.Width, pictureBox1.Image.Height);
            LockBitmap o = new LockBitmap((Bitmap)pictureBox1.Image);

            o.LockBits();
            int minR = 255;
            int minG = 255;
            int minB = 255;
            int maxR = 0;
            int maxG = 0;
            int maxB = 0;

            for (int i = 0; i < pictureBox1.Image.Height; i++)
            {
                for (int j = 0; j < pictureBox1.Image.Width; j++)
                {
                    var p = o.GetPixel(j, i);
                    minR = Math.Min(minR, p.R);
                    minG = Math.Min(minG, p.G);
                    minB = Math.Min(minB, p.B);
                    maxR = Math.Max(maxR, p.R);
                    maxG = Math.Max(maxG, p.G);
                    maxB = Math.Max(maxB, p.B);
                }
            }
            for (int i = 0; i < pictureBox1.Image.Height; i++)
            {
                for (int j = 0; j < pictureBox1.Image.Width; j++)
                {
                    var p = o.GetPixel(j, i);

                    o.SetPixel(j, i, Color.FromArgb(255 * (p.R - minR) / (maxR - minR),
                                                    255 * (p.G - minG) / (maxG - minG),
                                                    255 * (p.B - minB) / (maxB - minB)));
                }
            }
            o.UnlockBits();
            pictureBox1.Refresh();
        }
Exemplo n.º 6
0
        private void nonlinearNormalization_ValueChanged(object sender, EventArgs e)
        {
            //double alpha = Math.Log10(nonlinearNormalization.Value + 1.01);
            double     alpha = nonlinearNormalization.Value;
            Bitmap     b     = new Bitmap(pictureBox1.Image.Width, pictureBox1.Image.Height);
            LockBitmap n     = new LockBitmap((Bitmap)b);
            LockBitmap orig  = new LockBitmap((Bitmap)original);

            n.LockBits();
            orig.LockBits();
            double maxR = 0;
            double maxG = 0;
            double maxB = 0;

            for (int i = 0; i < pictureBox1.Image.Height; i++)
            {
                for (int j = 0; j < pictureBox1.Image.Width; j++)
                {
                    var p = orig.GetPixel(j, i);
                    maxR = Math.Max(maxR, p.R);
                    maxG = Math.Max(maxG, p.G);
                    maxB = Math.Max(maxB, p.B);
                }
            }
            for (int i = 0; i < pictureBox1.Image.Height; i++)
            {
                for (int j = 0; j < pictureBox1.Image.Width; j++)
                {
                    var p = orig.GetPixel(j, i);

                    n.SetPixel(j, i, Color.FromArgb((int)(255 * Math.Pow((p.R / maxR), alpha)),
                                                    (int)(255 * Math.Pow((p.G / maxG), alpha)),
                                                    (int)(255 * Math.Pow((p.B / maxB), alpha))));
                }
            }
            orig.UnlockBits();
            n.UnlockBits();
            pictureBox1.Image = b;
        }
Exemplo n.º 7
0
        private void lightening(int alpha)
        {
            Bitmap b = new Bitmap(pictureBox1.Image.Width, pictureBox1.Image.Height);
            //LockBitmap bLock = new LockBitmap(b);
            LockBitmap o = new LockBitmap((Bitmap)pictureBox1.Image);

            o.LockBits();
            //bLock.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);

                    o.SetPixel(j, i, Color.FromArgb(Math.Max(0, Math.Min(p.R + alpha, 255)),
                                                    Math.Max(0, Math.Min(p.G + alpha, 255)),
                                                    Math.Max(0, Math.Min(p.B + alpha, 255))));
                }
            }
            //bLock.UnlockBits();
            o.UnlockBits();
            pictureBox1.Refresh();
        }
Exemplo n.º 8
0
        private void greyClick(object sender, EventArgs e)
        {
            LockBitmap o          = new LockBitmap((Bitmap)pictureBox1.Image);
            Bitmap     b          = new Bitmap(pictureBox1.Image.Width, pictureBox1.Image.Height);
            LockBitmap lockBitmap = new LockBitmap(b);

            lockBitmap.LockBits();
            o.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);
                    int color = (p.R + p.B + p.G) / 3;
                    lockBitmap.SetPixel(j, i, Color.FromArgb(color, color, color));
                }
            }
            o.UnlockBits();
            lockBitmap.UnlockBits();
            pictureBox1.Image = b;
            grayImage         = new Bitmap(pictureBox1.Image);
        }
Exemplo n.º 9
0
        private void TrashHolding(object sender, EventArgs e)
        {
            Bitmap     b          = new Bitmap(pictureBox1.Image.Width, pictureBox1.Image.Height);
            LockBitmap lockBitmap = new LockBitmap(b);
            LockBitmap orig       = new LockBitmap((Bitmap)grayImage);

            int label = trackBar2.Value;

            lockBitmap.LockBits();
            orig.LockBits();
            for (int i = 0; i < pictureBox1.Image.Height; i++)
            {
                for (int j = 0; j < pictureBox1.Image.Width; j++)
                {
                    var colour   = orig.GetPixel(j, i);
                    int newcolor = colour.R >= label ? 255 : 0;
                    lockBitmap.SetPixel(j, i, Color.FromArgb(newcolor, newcolor, newcolor));
                }
            }
            lockBitmap.UnlockBits();
            orig.UnlockBits();
            pictureBox1.Image = b;
            currentImageCp    = new Bitmap(b);
        }
Exemplo n.º 10
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;
        }