/// <summary> /// blur the image /// </summary> /// <param name="img"> /// image to manipulate /// </param> /// <param name="weight"> /// weight of effect /// </param> 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); }
/// <summary> /// The apply convolution 3 x 3. /// </summary> /// <param name="img"> /// The img. /// </param> /// <param name="matrix"> /// The matrix. /// </param> private static void ApplyConvolution3X3(ref ImageData img, ConvolutionMatrix matrix) { ImageData newImg = img.Clone(); Color[,] pixelColor = new Color[3, 3]; 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]; int alphaChannel = pixelColor[1, 1].A; int redChannel = (int) ((((pixelColor[0, 0].R * matrix.Matrix[0, 0]) + (pixelColor[1, 0].R * matrix.Matrix[1, 0]) + (pixelColor[2, 0].R * matrix.Matrix[2, 0]) + (pixelColor[0, 1].R * matrix.Matrix[0, 1]) + (pixelColor[1, 1].R * matrix.Matrix[1, 1]) + (pixelColor[2, 1].R * matrix.Matrix[2, 1]) + (pixelColor[0, 2].R * matrix.Matrix[0, 2]) + (pixelColor[1, 2].R * matrix.Matrix[1, 2]) + (pixelColor[2, 2].R * matrix.Matrix[2, 2])) / matrix.Factor) + matrix.Offset); int greenChannel = (int) ((((pixelColor[0, 0].G * matrix.Matrix[0, 0]) + (pixelColor[1, 0].G * matrix.Matrix[1, 0]) + (pixelColor[2, 0].G * matrix.Matrix[2, 0]) + (pixelColor[0, 1].G * matrix.Matrix[0, 1]) + (pixelColor[1, 1].G * matrix.Matrix[1, 1]) + (pixelColor[2, 1].G * matrix.Matrix[2, 1]) + (pixelColor[0, 2].G * matrix.Matrix[0, 2]) + (pixelColor[1, 2].G * matrix.Matrix[1, 2]) + (pixelColor[2, 2].G * matrix.Matrix[2, 2])) / matrix.Factor) + matrix.Offset); int blueChannel = (int) ((((pixelColor[0, 0].B * matrix.Matrix[0, 0]) + (pixelColor[1, 0].B * matrix.Matrix[1, 0]) + (pixelColor[2, 0].B * matrix.Matrix[2, 0]) + (pixelColor[0, 1].B * matrix.Matrix[0, 1]) + (pixelColor[1, 1].B * matrix.Matrix[1, 1]) + (pixelColor[2, 1].B * matrix.Matrix[2, 1]) + (pixelColor[0, 2].B * matrix.Matrix[0, 2]) + (pixelColor[1, 2].B * matrix.Matrix[1, 2]) + (pixelColor[2, 2].B * matrix.Matrix[2, 2])) / matrix.Factor) + matrix.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(alphaChannel, redChannel, greenChannel, blueChannel)); } } img = newImg.Clone(); }
/// <summary> /// apply gaussianblur to image /// </summary> /// <param name="img"> /// image to manipulate /// </param> /// <param name="peakValue"> /// parameter /// </param> 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); }
/// <summary> /// sharpen an image /// </summary> /// <param name="img"> /// image to manipulate /// </param> /// <param name="weight"> /// weight /// </param> 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); }
/// <summary> /// apply emboss effect on image /// </summary> /// <param name="img"> /// image to manipulate /// </param> /// <param name="weight"> /// weight of emboss effect /// </param> 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); }