// получение негатива изображения public int Negativ(int index) { int i, j; if (index < 0 || index > PixelMapList.Count || PixelMapList.Count == 0) { return(-1); } int height = 0; int width = 0; Bitmap img = CreateNewMapForMap(index, ref height, ref width); for (i = 0; i < img.Width; i++) { for (j = 0; j < img.Height; j++) { int r = 0, g = 0, b = 0; GetRGB(img, i, j, ref r, ref g, ref b); r = 255 - r; g = 255 - g; b = 255 - b; img.SetPixel(i, j, Color.FromArgb(r, g, b)); } } PixelMapList.Add(img); return(0); }
// функция контрастирования изображения public int Contrast(int indexmap, int n) { int i, j; if (indexmap < 0 || indexmap > PixelMapList.Count || PixelMapList.Count == 0) { return(-1); } int height = 0; int width = 0; Bitmap newmap = CreateNewMapForMap(indexmap, ref height, ref width); if (newmap == null) { return(-1); } for (i = 0; i < width; i++) { for (j = 0; j < height; j++) { int r = 0, g = 0, b = 0; GetRGB(newmap, i, j, ref r, ref g, ref b); r = ContrastPixel(r, n); g = ContrastPixel(g, n); b = ContrastPixel(b, n); newmap.SetPixel(i, j, Color.FromArgb(r, g, b)); } } PixelMapList.Add(newmap); return(0); }
// обработка краев изображения public int BorderProcessing(int index) { int j; if (index < 0 || index > PixelMapList.Count || PixelMapList.Count == 0) { return(-1); } int height = 0; int width = 0; Bitmap newmap = CreateNewMapForMap(index, ref height, ref width); if (newmap == null) { return(-1); } for (j = 0; j < height; j++) { BorderProcessingHelper(newmap, height, width, 0, j); BorderProcessingHelper(newmap, height, width, 1, j); BorderProcessingHelper(newmap, height, width, width - 2, j); BorderProcessingHelper(newmap, height, width, width - 1, j); } for (j = 0; j < width; j++) { BorderProcessingHelper(newmap, height, width, j, 0); BorderProcessingHelper(newmap, height, width, j, 1); BorderProcessingHelper(newmap, height, width, j, height - 2); BorderProcessingHelper(newmap, height, width, j, height - 1); } PixelMapList.Add(newmap); return(0); }
public int Brightness(int indexmap, int n) { int i, j; if (indexmap < 0 || indexmap > PixelMapList.Count || PixelMapList.Count == 0) { return(-1); } int height = 0; int width = 0; Bitmap newmap = CreateNewMapForMap(indexmap, ref height, ref width); if (newmap == null) { return(-1); } for (i = 0; i < width; i++) { for (j = 0; j < height; j++) { int r = 0, g = 0, b = 0; GetRGB(newmap, i, j, ref r, ref g, ref b); r = r + n; g = g + n; b = b + n; if (r > 255) { r = 255; } if (g > 255) { g = 255; } if (b > 255) { b = 255; } if (r < 0) { r = 0; } if (g < 0) { g = 0; } if (b < 0) { b = 0; } newmap.SetPixel(i, j, Color.FromArgb(r, g, b)); } } PixelMapList.Add(newmap); return(0); }
// Метод Оцу public int MethodOtsu(int index) { if (index < 0 || index > PixelMapList.Count || PixelMapList.Count == 0) { return(-1); } int height = 0; int width = 0; Bitmap newmap = CreateNewMapForMap(index, ref height, ref width); if (newmap == null) { return(-1); } int[] p = new int[256]; // ? double[] pp = new double[256]; //? int total = 0; int r = 0, g = 0, b = 0, res = 0; for (int I = 0; I < p.Length; I++) { p[I] = 0; } for (int I = 0; I < newmap.Width; I++) { for (int J = 0; J < newmap.Height; J++) { GetRGB(newmap, I, J, ref r, ref g, ref b); res = Convert.ToInt32(RedApproximation * r + GreenApproximation * g + BlueApproximation * b); p[res]++; } } for (int I = 0; I < 256; I++) { total += p[I]; } for (int I = 0; I < 256; I++) { pp[I] = Convert.ToDouble(p[I]) / Convert.ToDouble(total); } int t = 0; double q1, q2, m1, m2, s1, s2, sw = Double.MaxValue; //? for (int I = 0; I < 256; I++) { q1 = q2 = 0; m1 = m2 = 0; s1 = s2 = 0; for (int J = 0; J <= I; J++) { q1 += pp[J]; } for (int J = I + 1; J < 256; J++) { q2 += pp[J]; } for (int J = 0; J <= I; J++) { m1 += J * pp[J] / q1; } for (int J = I + 1; J < 256; J++) { m2 += J * pp[J] / q2; } for (int J = 0; J <= I; J++) { s1 += Math.Pow((J - m1), 2) * pp[J] / q1; } for (int J = I + 1; J < 256; J++) { s2 += Math.Pow((J - m2), 2) * pp[J] / q2; } if ((q1 * s1 + q2 * s2) < sw) { sw = q1 * s1 + q2 * s2; t = I; } } for (int I = 0; I < newmap.Width; I++) { for (int J = 0; J < newmap.Height; J++) { GetRGB(newmap, I, J, ref r, ref g, ref b); res = Convert.ToInt32(RedApproximation * r + GreenApproximation * g + BlueApproximation * b); if (res > t) { newmap.SetPixel(I, J, Color.FromArgb(0, 0, 0)); } else { newmap.SetPixel(I, J, Color.FromArgb(255, 255, 255)); } } } PixelMapList.Add(newmap); return(0); }