Exemplo n.º 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);
     }
 }
Exemplo n.º 2
0
        public static Image Apply(this ConvolutionMatrix kernel, Image img)
        {
            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 originX = (kernel.Width - 1) / 2;
                    int originY = (kernel.Height - 1) / 2;

                    Parallel.For(0, source.Height, y =>
                    {
                        Parallel.For(0, source.Width, x =>
                        {
                            double r = 0.0;
                            double g = 0.0;
                            double b = 0.0;
                            double a = 0.0;

                            // Apply each matrix multiplier to the color components for each pixel.
                            for (int fy = 0; fy < kernel.Height; fy++)
                            {
                                int fyr     = fy - originY;
                                int offsetY = y + fyr;

                                offsetY = offsetY.Clamp(0, source.Height - 1);

                                for (int fx = 0; fx < kernel.Width; fx++)
                                {
                                    int fxr     = fx - originX;
                                    int offsetX = x + fxr;

                                    offsetX = offsetX.Clamp(0, source.Width - 1);

                                    ColorBgra currentColor = source.GetPixel(offsetX, offsetY);

                                    r += kernel[fy, fx] * currentColor.Red;
                                    g += kernel[fy, fx] * currentColor.Green;
                                    b += kernel[fy, fx] * currentColor.Blue;
                                    if (kernel.ConsiderAlpha)
                                    {
                                        a += kernel[fy, fx] * currentColor.Alpha;
                                    }
                                }
                            }

                            r += kernel.Offset;
                            r  = r.Clamp(0, 255);

                            g += kernel.Offset;
                            g  = g.Clamp(0, 255);

                            b += kernel.Offset;
                            b  = b.Clamp(0, 255);

                            if (kernel.ConsiderAlpha)
                            {
                                a += kernel.Offset;
                                a  = a.Clamp(0, 255);
                            }

                            dest.SetPixel(
                                x,
                                y,
                                new ColorBgra(
                                    (byte)b,
                                    (byte)g,
                                    (byte)r,
                                    kernel.ConsiderAlpha
                                    ? (byte)a
                                    : source.GetPixel(x, y).Alpha
                                    )
                                );
                        });
                    });
                }

            return(result);
        }
        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);
        }
Exemplo n.º 4
0
 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;
 }
Exemplo n.º 5
0
 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;
 }
Exemplo n.º 6
0
 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;
 }
Exemplo n.º 7
0
 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;
 }
Exemplo n.º 8
0
 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;
 }
Exemplo n.º 9
0
 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;
 }