private int[] GetLocalMin(LockBitmap bmp, Square indexes) { int[] result = new int[3] { 256, 256, 256 }; for (int x = indexes.X1; x < indexes.X2; x++) { for (int y = indexes.Y1; y < indexes.Y2; y++) { Color color = bmp.GetPixel(x, y); if (color.R < result[0]) { result[0] = color.R; } if (color.G < result[1]) { result[1] = color.G; } if (color.B < result[2]) { result[2] = color.B; } } } return(result); }
private int[] GetAdaptiveLimits(LockBitmap bmp, int x, int y) { int[] result = new int[3] { -1, -1, -1 }; int windowSize = 1; for (int i = 0; i < 2; i++) { Square indexes = GetSquare(x, y, windowSize, bmp.Height, bmp.Width); int[] fmax = GetLocalMax(bmp, indexes); int[] fmin = GetLocalMin(bmp, indexes); double[] P = GetLocalAverage(bmp, indexes); double[] dfmax = new double[3]; double[] dfmin = new double[3]; for (int j = 0; j < 3; j++) { dfmax[j] = Math.Abs(fmax[j] - P[j]); dfmin[j] = Math.Abs(fmin[j] - P[j]); } for (int j = 0; j < 3; j++) { if (result[j] != -1) { continue; } if (dfmax[j] > dfmin[j]) { result[j] = (int)(adaptiveCoef * (2.0 / 3 * fmin[j] + 1.0 / 3 * P[j]) + 0.5); } else if (dfmax[j] < dfmin[j]) { result[j] = (int)(adaptiveCoef * (1.0 / 3 * fmin[j] + 2.0 / 3 * P[j]) + 0.5); //fmax?? } else if (dfmax[j] == dfmin[j]) { if (i == 1) { result[j] = (int)(adaptiveCoef * P[j] + 0.5); } if (i == 0) { windowSize += 2; } } } } return(result); }
private int[] GetPixelWithMultiply(LockBitmap bmp, int x, int y, int coef) { int[] result = new int[3]; Color color = bmp.GetPixel(x, y); result[0] = color.R * coef; result[1] = color.G * coef; result[2] = color.B * coef; return(result); }
private Bitmap localProcessingFilter(Bitmap originalBitmap, int windowSize, double coef) { Bitmap newBitmap = new Bitmap(originalBitmap.Width, originalBitmap.Height); LockBitmap original = new LockBitmap(originalBitmap); LockBitmap filtered = new LockBitmap(newBitmap); int width = originalBitmap.Width; int height = originalBitmap.Height; original.LockBits(); filtered.LockBits(); for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { Color pixel = original.GetPixel(x, y); int[] result = new int[3] { pixel.R, pixel.G, pixel.B }; Square indexes = GetSquare(x, y, windowSize, height, width); double[] average = GetLocalAverage(original, indexes); double[] square = GetLocalSquare(original, indexes); for (int i = 0; i < 3; i++) { int limit = (int)(average[i] + coef * square[i] + 0.5); if (result[i] >= limit) { result[i] = 0; } else { result[i] = 255; } } filtered.SetPixel(x, y, Color.FromArgb(result[0], result[1], result[2])); } } filtered.UnlockBits(); original.UnlockBits(); return(newBitmap); }
private Bitmap adaptiveProcessingFilter(Bitmap originalBitmap, double coef) { Bitmap newBitmap = new Bitmap(originalBitmap.Width, originalBitmap.Height); LockBitmap original = new LockBitmap(originalBitmap); LockBitmap filtered = new LockBitmap(newBitmap); int width = originalBitmap.Width; int height = originalBitmap.Height; original.LockBits(); filtered.LockBits(); for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { Color pixel = original.GetPixel(x, y); int[] result = new int[3] { pixel.R, pixel.G, pixel.B }; int[] limits = GetAdaptiveLimits(original, x, y); for (int i = 0; i < 3; i++) { if (result[i] > limits[i]) { result[i] = 255; } else { result[i] = 0; } } filtered.SetPixel(x, y, Color.FromArgb(result[0], result[1], result[2])); } } filtered.UnlockBits(); original.UnlockBits(); return(newBitmap); }
private double[] GetLocalSquare(LockBitmap bmp, Square indexes) { double[] result = new double[3]; for (int x = indexes.X1; x < indexes.X2; x++) { for (int y = indexes.Y1; y < indexes.Y2; y++) { Color color = bmp.GetPixel(x, y); result[0] += Math.Pow(color.R, 2); result[1] += Math.Pow(color.G, 2); result[2] += Math.Pow(color.B, 2); } } for (int i = 0; i < 3; i++) { result[i] = Math.Sqrt(result[i]); } return(result); }
private double[] GetLocalAverage(LockBitmap bmp, Square indexes) { int elementCount = (indexes.X2 - indexes.X1 + 1) * (indexes.Y2 - indexes.Y1 + 1); double[] result = new double[3]; for (int x = indexes.X1; x < indexes.X2; x++) { for (int y = indexes.Y1; y < indexes.Y2; y++) { Color color = bmp.GetPixel(x, y); result[0] += color.R; result[1] += color.G; result[2] += color.B; } } for (int i = 0; i < 3; i++) { result[i] /= elementCount; } return(result); }
private Bitmap highFrequenceFilter(Bitmap originalBitmap, ImageFilter filter) { Bitmap newBitmap = new Bitmap(originalBitmap.Width, originalBitmap.Height); LockBitmap original = new LockBitmap(originalBitmap); LockBitmap filtered = new LockBitmap(newBitmap); int width = originalBitmap.Width; int height = originalBitmap.Height; original.LockBits(); filtered.LockBits(); for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { int[] result = new int[3]; if (y > 0 && x > 0) { SumArrays(result, GetPixelWithMultiply(original, x - 1, y - 1, filter.Matrix[0, 0])); } if (y > 0) { SumArrays(result, GetPixelWithMultiply(original, x, y - 1, filter.Matrix[0, 1])); } if (y > 0 && x < width - 1) { SumArrays(result, GetPixelWithMultiply(original, x + 1, y - 1, filter.Matrix[0, 2])); } if (x > 0) { SumArrays(result, GetPixelWithMultiply(original, x - 1, y, filter.Matrix[1, 0])); } SumArrays(result, GetPixelWithMultiply(original, x, y, filter.Matrix[1, 1])); if (x < width - 1) { SumArrays(result, GetPixelWithMultiply(original, x + 1, y, filter.Matrix[1, 2])); } if (y < height - 1 && x > 0) { SumArrays(result, GetPixelWithMultiply(original, x - 1, y + 1, filter.Matrix[2, 0])); } if (y < height - 1) { SumArrays(result, GetPixelWithMultiply(original, x, y + 1, filter.Matrix[2, 1])); } if (y < height - 1 && x < width - 1) { SumArrays(result, GetPixelWithMultiply(original, x + 1, y + 1, filter.Matrix[2, 2])); } for (int i = 0; i < 3; i++) { if (result[i] < 0) { result[i] = 0; } if (result[i] > 255) { result[i] = 255; } } filtered.SetPixel(x, y, Color.FromArgb(result[0], result[1], result[2])); } } filtered.UnlockBits(); original.UnlockBits(); return(newBitmap); }