Пример #1
0
 public static Bitmap ApplySmooth(Bitmap bitmapImage, double weight)
 {
     ConvolutionMatrix matrix = new ConvolutionMatrix(3);
     matrix.SetAll(1);
     matrix.Matrix[1, 1] = weight;
     matrix.Factor = weight + 8;
     return Convolution3x3(bitmapImage, matrix);
 }
Пример #2
0
 public static Bitmap ApplySharpen(Bitmap bitmapImage, double weight)
 {
     ConvolutionMatrix matrix = new ConvolutionMatrix(3);
     matrix.SetAll(1);
     matrix.Matrix[0, 0] = 0;
     matrix.Matrix[1, 0] = -2;
     matrix.Matrix[2, 0] = 0;
     matrix.Matrix[0, 1] = -2;
     matrix.Matrix[1, 1] = weight;
     matrix.Matrix[2, 1] = -2;
     matrix.Matrix[0, 2] = 0;
     matrix.Matrix[1, 2] = -2;
     matrix.Matrix[2, 2] = 0;
     matrix.Factor = weight - 8;
     return Convolution3x3(bitmapImage, matrix);
 }
Пример #3
0
 public static Bitmap ApplyEmboss(Bitmap bitmapImage, double weight)
 {
     ConvolutionMatrix matrix = new ConvolutionMatrix(3);
     matrix.SetAll(1);
     matrix.Matrix[0, 0] = -1;
     matrix.Matrix[1, 0] = 0;
     matrix.Matrix[2, 0] = -1;
     matrix.Matrix[0, 1] = 0;
     matrix.Matrix[1, 1] = weight;
     matrix.Matrix[2, 1] = 0;
     matrix.Matrix[0, 2] = -1;
     matrix.Matrix[1, 2] = 0;
     matrix.Matrix[2, 2] = -1;
     matrix.Factor = 4;
     matrix.Offset = 127;
     return Convolution3x3(bitmapImage, matrix);
 }
Пример #4
0
 public static Bitmap ApplyGaussianBlur(Bitmap bitmapImage, double peakValue)
 {
     ConvolutionMatrix matrix = new ConvolutionMatrix(3);
     matrix.SetAll(1);
     matrix.Matrix[0, 0] = peakValue / 4;
     matrix.Matrix[1, 0] = peakValue / 2;
     matrix.Matrix[2, 0] = peakValue / 4;
     matrix.Matrix[0, 1] = peakValue / 2;
     matrix.Matrix[1, 1] = peakValue;
     matrix.Matrix[2, 1] = peakValue / 2;
     matrix.Matrix[0, 2] = peakValue / 4;
     matrix.Matrix[1, 2] = peakValue / 2;
     matrix.Matrix[2, 2] = peakValue / 4;
     matrix.Factor = peakValue * 4;
     return Convolution3x3(bitmapImage, matrix);
 }
Пример #5
0
        public static Bitmap Convolution3x3(Bitmap b, ConvolutionMatrix m)
        {
            Bitmap newImg = (Bitmap)b.Clone();

            //LockBitmap lockBitmap = new LockBitmap(newImg);
            //lockBitmap.LockBits();

            Color[,] pixelColor = new Color[3, 3];
            int A, R, G, B;

            for (int y = 0; y < b.Height - 2; y++)
            {
                for (int x = 0; x < b.Width - 2; x++)
                {
                    pixelColor[0, 0] = b.GetPixel(x, y);
                    pixelColor[0, 1] = b.GetPixel(x, y + 1);
                    pixelColor[0, 2] = b.GetPixel(x, y + 2);
                    pixelColor[1, 0] = b.GetPixel(x + 1, y);
                    pixelColor[1, 1] = b.GetPixel(x + 1, y + 1);
                    pixelColor[1, 2] = b.GetPixel(x + 1, y + 2);
                    pixelColor[2, 0] = b.GetPixel(x + 2, y);
                    pixelColor[2, 1] = b.GetPixel(x + 2, y + 1);
                    pixelColor[2, 2] = b.GetPixel(x + 2, y + 2);

                    A = pixelColor[1, 1].A;

                    R = (int)((((pixelColor[0, 0].R * m.Matrix[0, 0]) +
                                 (pixelColor[1, 0].R * m.Matrix[1, 0]) +
                                 (pixelColor[2, 0].R * m.Matrix[2, 0]) +
                                 (pixelColor[0, 1].R * m.Matrix[0, 1]) +
                                 (pixelColor[1, 1].R * m.Matrix[1, 1]) +
                                 (pixelColor[2, 1].R * m.Matrix[2, 1]) +
                                 (pixelColor[0, 2].R * m.Matrix[0, 2]) +
                                 (pixelColor[1, 2].R * m.Matrix[1, 2]) +
                                 (pixelColor[2, 2].R * m.Matrix[2, 2]))
                                        / m.Factor) + m.Offset);

                    if (R < 0)
                    {
                        R = 0;
                    }
                    else if (R > 255)
                    {
                        R = 255;
                    }

                    G = (int)((((pixelColor[0, 0].G * m.Matrix[0, 0]) +
                                 (pixelColor[1, 0].G * m.Matrix[1, 0]) +
                                 (pixelColor[2, 0].G * m.Matrix[2, 0]) +
                                 (pixelColor[0, 1].G * m.Matrix[0, 1]) +
                                 (pixelColor[1, 1].G * m.Matrix[1, 1]) +
                                 (pixelColor[2, 1].G * m.Matrix[2, 1]) +
                                 (pixelColor[0, 2].G * m.Matrix[0, 2]) +
                                 (pixelColor[1, 2].G * m.Matrix[1, 2]) +
                                 (pixelColor[2, 2].G * m.Matrix[2, 2]))
                                        / m.Factor) + m.Offset);

                    if (G < 0)
                    {
                        G = 0;
                    }
                    else if (G > 255)
                    {
                        G = 255;
                    }

                    B = (int)((((pixelColor[0, 0].B * m.Matrix[0, 0]) +
                                 (pixelColor[1, 0].B * m.Matrix[1, 0]) +
                                 (pixelColor[2, 0].B * m.Matrix[2, 0]) +
                                 (pixelColor[0, 1].B * m.Matrix[0, 1]) +
                                 (pixelColor[1, 1].B * m.Matrix[1, 1]) +
                                 (pixelColor[2, 1].B * m.Matrix[2, 1]) +
                                 (pixelColor[0, 2].B * m.Matrix[0, 2]) +
                                 (pixelColor[1, 2].B * m.Matrix[1, 2]) +
                                 (pixelColor[2, 2].B * m.Matrix[2, 2]))
                                        / m.Factor) + m.Offset);

                    if (B < 0)
                    {
                        B = 0;
                    }
                    else if (B > 255)
                    {
                        B = 255;
                    }
                    newImg.SetPixel(x + 1, y + 1, Color.FromArgb(A, R, G, B));
                }
            }

            //lockBitmap.UnlockBits();

            return newImg;
        }