Beispiel #1
0
 public override Image Apply(Image img)
 {
     using (img)
     {
         ConvolutionMatrix cm = new ConvolutionMatrix();
         cm.Matrix[0, 0] = X0Y0;
         cm.Matrix[1, 0] = X1Y0;
         cm.Matrix[2, 0] = X2Y0;
         cm.Matrix[0, 1] = X0Y1;
         cm.Matrix[1, 1] = X1Y1;
         cm.Matrix[2, 1] = X2Y1;
         cm.Matrix[0, 2] = X0Y2;
         cm.Matrix[1, 2] = X1Y2;
         cm.Matrix[2, 2] = X2Y2;
         cm.Factor = Factor;
         cm.Offset = Offset;
         return cm.Apply(img);
     }
 }
 public static ConvolutionMatrix Sharpen(int weight = 11)
 {
     ConvolutionMatrix cm = new ConvolutionMatrix();
     cm.SetAll(0);
     cm.Matrix[1, 1] = weight;
     cm.Matrix[1, 0] = cm.Matrix[0, 1] = cm.Matrix[2, 1] = cm.Matrix[1, 2] = -2;
     cm.Factor = weight - 8;
     return cm;
 }
 public static ConvolutionMatrix Smooth(int weight = 1)
 {
     ConvolutionMatrix cm = new ConvolutionMatrix();
     cm.SetAll(1);
     cm.Matrix[1, 1] = weight;
     cm.Factor = weight + 8;
     return cm;
 }
 public static ConvolutionMatrix MeanRemoval(int weight = 9)
 {
     ConvolutionMatrix cm = new ConvolutionMatrix();
     cm.SetAll(-1);
     cm.Matrix[1, 1] = weight;
     cm.Factor = weight - 8;
     return cm;
 }
 public static ConvolutionMatrix GaussianBlur(int weight = 4)
 {
     ConvolutionMatrix cm = new ConvolutionMatrix();
     cm.SetAll(1);
     cm.Matrix[1, 1] = weight;
     cm.Matrix[1, 0] = cm.Matrix[0, 1] = cm.Matrix[2, 1] = cm.Matrix[1, 2] = 2;
     cm.Factor = weight + 12;
     return cm;
 }
 public static ConvolutionMatrix Emboss()
 {
     ConvolutionMatrix cm = new ConvolutionMatrix();
     cm.SetAll(-1);
     cm.Matrix[1, 1] = 4;
     cm.Matrix[1, 0] = cm.Matrix[0, 1] = cm.Matrix[2, 1] = cm.Matrix[1, 2] = 0;
     cm.Offset = 127;
     return cm;
 }
 public static ConvolutionMatrix EdgeDetect()
 {
     ConvolutionMatrix cm = new ConvolutionMatrix();
     cm.Matrix[0, 0] = cm.Matrix[1, 0] = cm.Matrix[2, 0] = -1;
     cm.Matrix[0, 1] = cm.Matrix[1, 1] = cm.Matrix[2, 1] = 0;
     cm.Matrix[0, 2] = cm.Matrix[1, 2] = cm.Matrix[2, 2] = 1;
     cm.Offset = 127;
     return cm;
 }
Beispiel #8
0
        public static Image Apply(this ConvolutionMatrix matrix, Image img)
        {
            int factor = Math.Max(matrix.Factor, 1);

            Bitmap result = (Bitmap)img.Clone();

            using (UnsafeBitmap source = new UnsafeBitmap((Bitmap)img, true, ImageLockMode.ReadOnly))
                using (UnsafeBitmap dest = new UnsafeBitmap(result, true, ImageLockMode.WriteOnly))
                {
                    int height = source.Height - 2;
                    int width  = source.Width - 2;
                    ColorBgra[,] pixelColor = new ColorBgra[3, 3];
                    int       pixel;
                    ColorBgra color = new ColorBgra();

                    for (int y = 0; y < height; y++)
                    {
                        for (int x = 0; x < width; x++)
                        {
                            pixelColor[0, 0] = source.GetPixel(x, y);
                            pixelColor[0, 1] = source.GetPixel(x, y + 1);
                            pixelColor[0, 2] = source.GetPixel(x, y + 2);
                            pixelColor[1, 0] = source.GetPixel(x + 1, y);
                            pixelColor[1, 1] = source.GetPixel(x + 1, y + 1);
                            pixelColor[1, 2] = source.GetPixel(x + 1, y + 2);
                            pixelColor[2, 0] = source.GetPixel(x + 2, y);
                            pixelColor[2, 1] = source.GetPixel(x + 2, y + 1);
                            pixelColor[2, 2] = source.GetPixel(x + 2, y + 2);

                            pixel = (((pixelColor[0, 0].Blue * matrix.Matrix[0, 0]) +
                                      (pixelColor[1, 0].Blue * matrix.Matrix[1, 0]) +
                                      (pixelColor[2, 0].Blue * matrix.Matrix[2, 0]) +
                                      (pixelColor[0, 1].Blue * matrix.Matrix[0, 1]) +
                                      (pixelColor[1, 1].Blue * matrix.Matrix[1, 1]) +
                                      (pixelColor[2, 1].Blue * matrix.Matrix[2, 1]) +
                                      (pixelColor[0, 2].Blue * matrix.Matrix[0, 2]) +
                                      (pixelColor[1, 2].Blue * matrix.Matrix[1, 2]) +
                                      (pixelColor[2, 2].Blue * matrix.Matrix[2, 2])) / factor) + matrix.Offset;

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

                            color.Blue = (byte)pixel;

                            pixel = (((pixelColor[0, 0].Green * matrix.Matrix[0, 0]) +
                                      (pixelColor[1, 0].Green * matrix.Matrix[1, 0]) +
                                      (pixelColor[2, 0].Green * matrix.Matrix[2, 0]) +
                                      (pixelColor[0, 1].Green * matrix.Matrix[0, 1]) +
                                      (pixelColor[1, 1].Green * matrix.Matrix[1, 1]) +
                                      (pixelColor[2, 1].Green * matrix.Matrix[2, 1]) +
                                      (pixelColor[0, 2].Green * matrix.Matrix[0, 2]) +
                                      (pixelColor[1, 2].Green * matrix.Matrix[1, 2]) +
                                      (pixelColor[2, 2].Green * matrix.Matrix[2, 2])) / factor) + matrix.Offset;

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

                            color.Green = (byte)pixel;

                            pixel = (((pixelColor[0, 0].Red * matrix.Matrix[0, 0]) +
                                      (pixelColor[1, 0].Red * matrix.Matrix[1, 0]) +
                                      (pixelColor[2, 0].Red * matrix.Matrix[2, 0]) +
                                      (pixelColor[0, 1].Red * matrix.Matrix[0, 1]) +
                                      (pixelColor[1, 1].Red * matrix.Matrix[1, 1]) +
                                      (pixelColor[2, 1].Red * matrix.Matrix[2, 1]) +
                                      (pixelColor[0, 2].Red * matrix.Matrix[0, 2]) +
                                      (pixelColor[1, 2].Red * matrix.Matrix[1, 2]) +
                                      (pixelColor[2, 2].Red * matrix.Matrix[2, 2])) / factor) + matrix.Offset;

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

                            color.Red = (byte)pixel;

                            color.Alpha = pixelColor[1, 1].Alpha;

                            dest.SetPixel(x + 1, y + 1, color);
                        }
                    }
                }

            return(result);
        }