public Bitmap applyBernzenThersholding(Bitmap image, Point imageSize, Point filterSize, bool convert = true) { Bitmap rezult = new Bitmap(image); Bitmap tempImage; if (convert) { tempImage = convertImageToGrayscale(image); } else { tempImage = new Bitmap(image); } ApertureService service = new ApertureService(); List <Aperture> apertures = service.getApertureMatrixGenerator(imageSize, filterSize); for (int x = 0; x < apertures.Count; x++) { int minValue = 255, maxValue = 0; double average; List <List <Point> > matrix = apertures[x].matrix; foreach (var matrixLine in matrix) { foreach (var Point in matrixLine) { int pixelPosX = Point.X; int pixelPosY = Point.Y; //т.к. картинка черно белая, то значения РГБ одинаковы, можно брыть любое из трех. if (tempImage.GetPixel(pixelPosX, pixelPosY).R > maxValue) { maxValue = tempImage.GetPixel(pixelPosX, pixelPosY).R; } if (tempImage.GetPixel(pixelPosX, pixelPosY).R < minValue) { minValue = tempImage.GetPixel(pixelPosX, pixelPosY).R; } } } average = ((double)minValue + maxValue) / 2; foreach (var matrixLine in matrix) { foreach (var Point in matrixLine) { int pixelPosX = Point.X; int pixelPosY = Point.Y; if (tempImage.GetPixel(pixelPosX, pixelPosY).R < average) { rezult.SetPixel(pixelPosX, pixelPosY, Color.FromArgb(0, 0, 0)); } else { rezult.SetPixel(pixelPosX, pixelPosY, Color.FromArgb(255, 255, 255)); } } } } service = null; for (int i = 0; i < apertures.Count; i++) { apertures[i].Dispose(); } apertures.Clear(); return(rezult); }
public Bitmap applyMedianFilter(string colorMode, Bitmap image, Point imageSize, Point filterSize, int colorChannelIndex = 0) { Bitmap rezult = new Bitmap(image); ApertureService service = new ApertureService(); List <Aperture> apertures = service.getApertureMatrixGenerator(imageSize, filterSize); List <int> redList = new List <int>(); List <int> greenList = new List <int>(); List <int> blueList = new List <int>(); for (int x = 0; x < apertures.Count; x++) { redList.Clear(); blueList.Clear(); greenList.Clear(); List <List <Point> > matrix = apertures[x].matrix; foreach (var matrixLine in matrix) { foreach (var Point in matrixLine) { int pixelPosX = Point.X; int pixelPosY = Point.Y; Color color = image.GetPixel(pixelPosX, pixelPosY); redList.Add(color.R); greenList.Add(color.G); blueList.Add(color.B); } } redList.Sort(); greenList.Sort(); blueList.Sort(); int apertureCenter = redList.Count / 2; int rValue = redList[apertureCenter]; int gValue = greenList[apertureCenter]; int bValue = blueList[apertureCenter]; int aperturePosX = matrix.Count / 2; int aperturePosY = matrix[aperturePosX].Count / 2; Color oldColor = image.GetPixel(matrix[aperturePosX][aperturePosY].X, matrix[aperturePosX][aperturePosY].Y); if (colorChannelIndex == 0) { rezult.SetPixel(matrix[aperturePosX][aperturePosY].X, matrix[aperturePosX][aperturePosY].Y, Color.FromArgb(rValue, gValue, bValue)); } else if (colorChannelIndex == 1) { rezult.SetPixel(matrix[aperturePosX][aperturePosY].X, matrix[aperturePosX][aperturePosY].Y, Color.FromArgb(rValue, oldColor.G, oldColor.B)); } else if (colorChannelIndex == 2) { rezult.SetPixel(matrix[aperturePosX][aperturePosY].X, matrix[aperturePosX][aperturePosY].Y, Color.FromArgb(oldColor.R, gValue, oldColor.B)); } else if (colorChannelIndex == 3) { rezult.SetPixel(matrix[aperturePosX][aperturePosY].X, matrix[aperturePosX][aperturePosY].Y, Color.FromArgb(oldColor.R, oldColor.G, bValue)); } } service = null; for (int i = 0; i < apertures.Count; i++) { apertures[i].Dispose(); } apertures.Clear(); return(rezult); }
public static Bitmap applyGaussianBlurFilter(string colorMode, Bitmap image, Point imageSize, Point filterSize, int colorChannelIndex = 0) { Bitmap rezult = new Bitmap(image); ApertureService service = new ApertureService(); List <Aperture> apertures = service.getApertureMatrixGenerator(imageSize, filterSize); double sigma = 0.5; for (int x = 0; x < apertures.Count; x++) { double rSum = 0, gSum = 0, bSum = 0, kSum = 0; List <List <Point> > matrix = apertures[x].matrix; int line = 0; foreach (var matrixLine in matrix) { foreach (var Point in matrixLine) { int pixelPosX = Point.X; int pixelPosY = Point.Y; Color color = image.GetPixel(pixelPosX, pixelPosY); double kVal = calculateGaussian(line, sigma); rSum += color.R * kVal; gSum += color.G * kVal; bSum += color.B * kVal; kSum += kVal; } line++; } if (kSum <= 0) { kSum = 1; } rSum = rSum / kSum; if (rSum < 0) { rSum = 0; } else if (rSum > 255) { rSum = 255; } gSum = gSum / kSum; if (gSum < 0) { gSum = 0; } else if (gSum > 255) { gSum = 255; } bSum = bSum / kSum; if (bSum < 0) { bSum = 0; } else if (bSum > 255) { bSum = 255; } int aperturePosX = matrix.Count / 2; if (matrix.Count != 0 && matrix[aperturePosX].Count / 2 != 0) { int aperturePosY = matrix[aperturePosX].Count / 2; Color oldColor = image.GetPixel(matrix[aperturePosX][aperturePosY].X, matrix[aperturePosX][aperturePosY].Y); if (colorChannelIndex == 0) { rezult.SetPixel(matrix[aperturePosX][aperturePosY].X, matrix[aperturePosX][aperturePosY].Y, Color.FromArgb((int)rSum, (int)gSum, (int)bSum)); } else if (colorChannelIndex == 1) { rezult.SetPixel(matrix[aperturePosX][aperturePosY].X, matrix[aperturePosX][aperturePosY].Y, Color.FromArgb((int)rSum, oldColor.G, oldColor.B)); } else if (colorChannelIndex == 2) { rezult.SetPixel(matrix[aperturePosX][aperturePosY].X, matrix[aperturePosX][aperturePosY].Y, Color.FromArgb(oldColor.R, (int)gSum, oldColor.B)); } else if (colorChannelIndex == 3) { rezult.SetPixel(matrix[aperturePosX][aperturePosY].X, matrix[aperturePosX][aperturePosY].Y, Color.FromArgb(oldColor.R, oldColor.G, (int)bSum)); } } } service = null; for (int i = 0; i < apertures.Count; i++) { apertures[i].Dispose(); } apertures.Clear(); return(rezult); }
public Bitmap applyRecursiveFilter(string colorMode, Bitmap image, Point imageSize, Point filterSize, int colorChannelIndex = 0) { Bitmap rezult = new Bitmap(image); ApertureService service = new ApertureService(); List <Aperture> apertures = service.getApertureMatrixGenerator(imageSize, filterSize); List <int> redList = new List <int>(); List <int> greenList = new List <int>(); List <int> blueList = new List <int>(); double[] rSumsArray = new double[apertures.Count]; double[] gSumsArray = new double[apertures.Count]; double[] bSumsArray = new double[apertures.Count]; for (int x = 0; x < apertures.Count; x++) { redList.Clear(); blueList.Clear(); greenList.Clear(); int rSum = 0, gSum = 0, bSum = 0, kSum = 0; List <List <Point> > matrix = apertures[x].matrix; bool isSideAperture = false; foreach (var matrixLine in matrix) { foreach (var Point in matrixLine) { int pixelPosX = Point.X; int pixelPosY = Point.Y; //проверяем, выйдем ли мы за границы изображения слева, при расчетах, // ведь идем от -r до +r потом. if (pixelPosX <= filterSize.X || pixelPosY <= filterSize.Y) { isSideAperture = true; } Color color = image.GetPixel(pixelPosX, pixelPosY); redList.Add(color.R); greenList.Add(color.G); blueList.Add(color.B); kSum += 1; } } //если при расчетах выйдем за рамки, считаем просто стреднее арифметическое. if (isSideAperture) { for (int i = 0; i < redList.Count; i++) { rSum += redList[i]; gSum += greenList[i]; bSum += blueList[i]; } rSumsArray[x] = rSum; gSumsArray[x] = gSum; bSumsArray[x] = bSum; } else { int rSum1 = 0, gSum1 = 0, bSum1 = 0, kSum1 = 0; int rSum2 = 0, gSum2 = 0, bSum2 = 0, kSum2 = 0; //крайне правый элемент предыдущего окна for (int i = 0; i < matrix.Count; i++) { Color color = image.GetPixel(matrix[i][0].X - 1, matrix[i][0].Y); rSum1 += color.R; gSum1 += color.G; bSum1 += color.B; kSum1 += 1; } for (int i = 0; i < matrix.Count; i++) { Color color = image.GetPixel(matrix[i][filterSize.X - 1].X, matrix[i][filterSize.X - 1].Y); rSum2 += color.R; gSum2 += color.G; bSum2 += color.B; kSum2 += 1; } //вычислять рекурсию только если предыдущая апертура на той же линии if (apertures[x].y == apertures[x - 1].y) { double rSumReal = rSumsArray[x - 1] - rSum1 + rSum2; double gSumReal = gSumsArray[x - 1] - rSum1 + rSum2; double bSumReal = gSumsArray[x - 1] - rSum1 + rSum2; rSumsArray[x] = rSumReal; gSumsArray[x] = gSumReal; bSumsArray[x] = bSumReal; } else { for (int i = 0; i < redList.Count; i++) { rSum += redList[i]; gSum += greenList[i]; bSum += blueList[i]; } rSumsArray[x] = rSum; gSumsArray[x] = gSum; bSumsArray[x] = bSum; } } // int pixelCount = filterSize.X * filterSize.Y; rSum = (int)rSumsArray[x] / kSum; if (rSum < 0) { rSum = 0; } else if (rSum > 255) { rSum = 255; } gSum = (int)gSumsArray[x] / kSum; if (gSum < 0) { gSum = 0; } else if (gSum > 255) { gSum = 255; } bSum = (int)bSumsArray[x] / kSum; if (bSum < 0) { bSum = 0; } else if (bSum > 255) { bSum = 255; } int aperturePosX = matrix.Count / 2; if (matrix.Count != 0 && matrix[aperturePosX].Count / 2 != 0) { int aperturePosY = matrix[aperturePosX].Count / 2; Color oldColor = image.GetPixel(matrix[aperturePosX][aperturePosY].X, matrix[aperturePosX][aperturePosY].Y); if (colorChannelIndex == 0) { rezult.SetPixel(matrix[aperturePosX][aperturePosY].X, matrix[aperturePosX][aperturePosY].Y, Color.FromArgb((int)rSum, (int)gSum, (int)bSum)); } else if (colorChannelIndex == 1) { rezult.SetPixel(matrix[aperturePosX][aperturePosY].X, matrix[aperturePosX][aperturePosY].Y, Color.FromArgb((int)rSum, oldColor.G, oldColor.B)); } else if (colorChannelIndex == 2) { rezult.SetPixel(matrix[aperturePosX][aperturePosY].X, matrix[aperturePosX][aperturePosY].Y, Color.FromArgb(oldColor.R, (int)gSum, oldColor.B)); } else if (colorChannelIndex == 3) { rezult.SetPixel(matrix[aperturePosX][aperturePosY].X, matrix[aperturePosX][aperturePosY].Y, Color.FromArgb(oldColor.R, oldColor.G, (int)bSum)); } } } service = null; for (int i = 0; i < apertures.Count; i++) { apertures[i].Dispose(); } apertures.Clear(); return(rezult); }