/// <summary>
		/// Apply a ConvolutionMatrix to the array of values
		/// </summary>
		/// <param name="values">Values to be processed</param>
		/// <param name="matrix">ConvolutionMatrix to be applied</param>
		/// <returns>Array containing the altered values</returns>
		public static byte[,] Apply(byte[,] values, ConvolutionMatrix matrix)
		{
			if (values == null)
				return null;

			// store the dimensions of the array
			int ArrayWidth = values.GetLength(0);
			int ArrayHeight = values.GetLength(1);

			// create the new array to be returned
			byte[,] Result = new byte[ArrayWidth, ArrayHeight];

			int iPixel;

			for (int y = 0; y < ArrayHeight - 2; y++)
			{
				for (int x = 0; x < ArrayWidth - 2; x++)
				{
					iPixel =
						(values[x, y] * matrix.TopLeft) + (values[x + 1, y] * matrix.TopMiddle) + (values[x + 2, y] * matrix.TopRight) +
						(values[x, y + 1] * matrix.MiddleLeft) + (values[x + 1, y + 1] * matrix.Pixel) + (values[x + 2, y] * matrix.MiddleRight) +
						(values[x, y + 2] * matrix.BottomLeft) + (values[x + 1, y + 2] * matrix.BottomMiddle) + (values[x + 2, y] * matrix.BottomRight);

					iPixel = (int)(((float)iPixel / (float)matrix.Factor) + 0.5) + matrix.Offset;

					if (iPixel < 0) iPixel = 0;
					if (iPixel > 255) iPixel = 255;

					Result[x + 1, y + 1] = (byte)iPixel;
				}
			}

			int position = ArrayHeight - 1;

			// TODO: Apply the matrix to the edges
			for (int x = 0; x < ArrayWidth; x++)
			{
				Result[x, 0] = values[x, 0];
				Result[x, position] = values[x, position];
			}


			position = ArrayWidth - 1;

			for (int y = 1; y < ArrayHeight - 1; y++)
			{
				Result[0, y] = values[0, y];
				Result[position, y] = values[position, y];
			}

			return Result;
		}
        public ConvolutionFilter(ConvolutionMatrix matrix)

        {
            _matrix = matrix;
        }
Example #3
0
        static private void ApplyConvolution3x3(ref ImageData Img, ConvolutionMatrix m)
        {
            ImageData newImg = Img.Clone();

            Color[,] pixelColor = new Color[3, 3];
            int A, RedChannel, GreenChannel, BlueChannel;

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

                    A = pixelColor[1, 1].A;

                    RedChannel = (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);



                    GreenChannel = (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);


                    BlueChannel = (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);

                    RedChannel   = (RedChannel > 255) ? 255 : RedChannel;
                    RedChannel   = (RedChannel < 0) ? 0 : RedChannel;
                    GreenChannel = (GreenChannel > 255) ? 255 : GreenChannel;
                    GreenChannel = (GreenChannel < 0) ? 0 : GreenChannel;
                    BlueChannel  = (BlueChannel > 255) ? 255 : BlueChannel;
                    BlueChannel  = (BlueChannel < 0) ? 0 : BlueChannel;


                    newImg.SetPixel(x + 1, y + 1, Color.FromArgb(A, RedChannel, GreenChannel, BlueChannel));
                }
            }
            Img = newImg.Clone();
        }
Example #4
0
        private static Bitmap Convolution3x3(Bitmap b, ConvolutionMatrix m)
        {
            var newImg = (Bitmap)b.Clone();
            var 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));
                }
            }
            return(newImg);
        }
Example #5
0
 /// <summary>
 /// blur the image
 /// </summary>
 /// <param name="Img">image to manipulate</param>
 /// <param name="weight">weight of effect</param>
 /// <example>
 /// <code>
 /// <![CDATA[
 /// ImageData Img = new ImageData(...);
 /// // apply filter to image
 /// Filter.Blur(ref Img, 30.0);
 /// ]]>
 /// </code>
 /// </example>
 /// <see cref="blur"/>
 /// <returns></returns>
 public static void Blur(ref ImageData Img, double weight)
 {
     ConvolutionMatrix CMatrix = new ConvolutionMatrix(3);
     CMatrix.SetAll(1);
     CMatrix.Matrix[1, 1] = weight;
     CMatrix.Factor = weight + 8;
     ApplyConvolution3x3(ref Img, CMatrix);
 }
Example #6
0
        private static void ApplyConvolution3x3(ref ImageData Img, ConvolutionMatrix m)
        {
            ImageData newImg = Img.Clone();
            Color[,] pixelColor = new Color[3, 3];
            int A, RedChannel, GreenChannel, BlueChannel;

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

                    A = pixelColor[1, 1].A;

                    RedChannel = (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);

                    GreenChannel = (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);

                    BlueChannel = (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);

                    RedChannel = (RedChannel > 255) ? 255 : RedChannel;
                    RedChannel = (RedChannel < 0) ? 0 : RedChannel;
                    GreenChannel = (GreenChannel > 255) ? 255 : GreenChannel;
                    GreenChannel = (GreenChannel < 0) ? 0 : GreenChannel;
                    BlueChannel = (BlueChannel > 255) ? 255 : BlueChannel;
                    BlueChannel = (BlueChannel < 0) ? 0 : BlueChannel;

                    newImg.SetPixel(x + 1, y + 1, Color.FromArgb(A, RedChannel, GreenChannel, BlueChannel));
                }
            }
            Img = newImg.Clone();
        }
Example #7
0
 /// <summary>
 /// sharpen an image
 /// </summary>
 /// <example>
 /// <code>
 /// <![CDATA[
 /// ImageData Img = new ImageData(...);
 /// // apply filter to image
 /// Filter.Sharpen(ref Img, 4.0);
 /// ]]>
 /// </code>
 /// </example>
 /// <param name="Img">image to manipulate</param>
 /// <param name="weight">weight</param>
 /// <see cref="sharpen"/>
 /// <returns></returns>
 public static void Sharpen(ref ImageData Img, double weight)
 {
     ConvolutionMatrix CMatrix = new ConvolutionMatrix(3);
     CMatrix.SetAll(1);
     CMatrix.Matrix[0, 0] = 0;
     CMatrix.Matrix[1, 0] = -2;
     CMatrix.Matrix[2, 0] = 0;
     CMatrix.Matrix[0, 1] = -2;
     CMatrix.Matrix[1, 1] = weight;
     CMatrix.Matrix[2, 1] = -2;
     CMatrix.Matrix[0, 2] = 0;
     CMatrix.Matrix[1, 2] = -2;
     CMatrix.Matrix[2, 2] = 0;
     CMatrix.Factor = weight - 8;
     ApplyConvolution3x3(ref Img, CMatrix);
 }
Example #8
0
 /// <summary>
 /// apply gaussianblur to image
 /// </summary>
 /// <example>
 /// <code>
 /// <![CDATA[
 /// ImageData Img = new ImageData(...);
 /// // apply filter to image
 /// Filter.GaussianBlur(ref Img, 20.0);
 /// ]]>
 /// </code>
 /// </example>
 /// <param name="Img">image to manipulate</param>
 /// <param name="peakValue">parameter</param>
 /// <see cref="gaussian"/>
 /// <returns></returns>
 public static void GaussianBlur(ref ImageData Img, double peakValue)
 {
     ConvolutionMatrix CMatrix = new ConvolutionMatrix(3);
     CMatrix.SetAll(1);
     CMatrix.Matrix[0, 0] = peakValue / 4;
     CMatrix.Matrix[1, 0] = peakValue / 2;
     CMatrix.Matrix[2, 0] = peakValue / 4;
     CMatrix.Matrix[0, 1] = peakValue / 2;
     CMatrix.Matrix[1, 1] = peakValue;
     CMatrix.Matrix[2, 1] = peakValue / 2;
     CMatrix.Matrix[0, 2] = peakValue / 4;
     CMatrix.Matrix[1, 2] = peakValue / 2;
     CMatrix.Matrix[2, 2] = peakValue / 4;
     CMatrix.Factor = peakValue * 4;
     ApplyConvolution3x3(ref Img, CMatrix);
 }
Example #9
0
 /// <summary>
 /// apply emboss effect on image
 /// </summary>
 /// <param name="Img">image to manipulate</param>
 /// <param name="weight">weight of emboss effect</param>
 /// <example>
 /// <code>
 /// <![CDATA[
 /// ImageData Img = new ImageData(...);
 /// // apply filter to image
 /// Filter.Emboss(ref Img, 4.0);
 /// ]]>
 /// </code>
 /// </example>
 /// <see cref="emboss"/>
 /// <returns></returns>
 public static void Emboss(ref ImageData Img, double weight)
 {
     ConvolutionMatrix CMatrix = new ConvolutionMatrix(3);
     CMatrix.SetAll(1);
     CMatrix.Matrix[0, 0] = -1;
     CMatrix.Matrix[1, 0] = 0;
     CMatrix.Matrix[2, 0] = -1;
     CMatrix.Matrix[0, 1] = 0;
     CMatrix.Matrix[1, 1] = weight;
     CMatrix.Matrix[2, 1] = 0;
     CMatrix.Matrix[0, 2] = -1;
     CMatrix.Matrix[1, 2] = 0;
     CMatrix.Matrix[2, 2] = -1;
     CMatrix.Factor = 4;
     CMatrix.Offset = 127;
     ApplyConvolution3x3(ref Img, CMatrix);
 }