/// <summary> /// Perform a Median filter using a 3x3 mask /// </summary> /// <param name="b">Image to Process</param> /// <returns>Filtered Image</returns> public static Bitmap Median3x3(Bitmap b) { ImagerBitmap i = new ImagerBitmap(b.Clone() as Bitmap); ImagerBitmap i2 = new ImagerBitmap(b.Clone() as Bitmap); for (int column = 1; column < i.Bitmap.Width - 1; column++) { for (int row = 1; row < i.Bitmap.Height - 1; row++) { Color[,] c = i.Get3x3(row, column); int red = Median(c[0, 0].R, c[0, 1].R, c[0, 2].R, c[1, 0].R, c[1, 1].R, c[1, 2].R, c[2, 0].R, c[2, 1].R, c[2, 2].R); int green = Median(c[0, 0].G, c[0, 1].G, c[0, 2].G, c[1, 0].G, c[1, 1].G, c[1, 2].G, c[2, 0].G, c[2, 1].G, c[2, 2].G); int blue = Median(c[0, 0].B, c[0, 1].B, c[0, 2].B, c[1, 0].B, c[1, 1].B, c[1, 2].B, c[2, 0].B, c[2, 1].B, c[2, 2].B); i2.SetPixel(column, row, Color.FromArgb(red, green, blue)); } } i.UnlockBitmap(); i2.UnlockBitmap(); return i2.Bitmap.Clone() as Bitmap; }
/// <summary> /// Perform a Bionomial filter using a 3x3 Mask /// </summary> /// <param name="b">Image to Process</param> /// <returns>Filtered Image</returns> public static Bitmap Binomial3x3(Bitmap b) { ImagerBitmap i = new ImagerBitmap(b.Clone() as Bitmap); ImagerBitmap i2 = new ImagerBitmap(b.Clone() as Bitmap); for (int column = 1; column < i.Bitmap.Width - 1; column++) { for (int row = 1; row < i.Bitmap.Height - 1; row++) { Color[,] c = i.Get3x3(row, column); int red = ((c[0, 0].R * 1) + (c[0, 1].R * 2) + (c[0, 2].R * 1) + (c[1, 0].R * 2) + (c[1, 1].R * 4) + (c[1, 2].R * 2) + (c[2, 0].R * 1) + (c[2, 1].R * 2) + (c[2, 2].R * 1)) / 16; int green = ((c[0, 0].G * 1) + (c[0, 1].G * 2) + (c[0, 2].G * 1) + (c[1, 0].G * 2) + (c[1, 1].G * 4) + (c[1, 2].G * 2) + (c[2, 0].G * 1) + (c[2, 1].G * 2) + (c[2, 2].G * 1)) / 16; int blue = ((c[0, 0].B * 1) + (c[0, 1].B * 2) + (c[0, 2].B * 1) + (c[1, 0].B * 2) + (c[1, 1].B * 4) + (c[1, 2].B * 2) + (c[2, 0].B * 1) + (c[2, 1].B * 2) + (c[2, 2].B * 1)) / 16; i2.SetPixel(column, row, Color.FromArgb(red, green, blue)); } } i.UnlockBitmap(); i2.UnlockBitmap(); return i2.Bitmap.Clone() as Bitmap; }
/// <summary> /// Perform laplace edge detection on the image /// </summary> /// <param name="b">Source Image</param> /// <returns>Edges</returns> public static Bitmap Laplace(Bitmap b) { ImagerBitmap i = new ImagerBitmap(b.Clone() as Bitmap); ImagerBitmap i2 = new ImagerBitmap(b.Clone() as Bitmap); for (int column = 1; column < i.Bitmap.Width - 1; column++) { for (int row = 1; row < i.Bitmap.Height - 1; row++) { Color[,] c = i.Get3x3(row, column); int red = (((c[0, 0].R + c[0, 1].R + c[0, 2].R + c[1, 0].R + c[1, 2].R + c[2, 0].R + c[2, 1].R + c[2, 2].R) * -1) + (c[1, 1].R * 8)) + 128; int green = (((c[0, 0].G + c[0, 1].G + c[0, 2].G + c[1, 0].G + c[1, 2].G + c[2, 0].G + c[2, 1].G + c[2, 2].G) * -1) + (c[1, 1].G * 8)) + 128; int blue = (((c[0, 0].B + c[0, 1].B + c[0, 2].B + c[1, 0].B + c[1, 2].B + c[2, 0].B + c[2, 1].B + c[2, 2].B) * -1) + (c[1, 1].B * 8)) + 128; if (red >= 128) red = 0; else red = 255; if (green >= 128) green = 0; else green = 255; if (blue >= 128) blue = 0; else blue = 255; i2.SetPixel(column, row, Color.FromArgb(red, green, blue)); } } i.UnlockBitmap(); i2.UnlockBitmap(); return i2.Bitmap.Clone() as Bitmap; }