public static Bitmap DetectEdges(Bitmap original, ExchangeMask maskType) { var mask = masks[(int)maskType]; var current = new Bitmap(original.Width, original.Height); var gsBytes = original.GetGrayscaleBytes(); for (var i = 0; i < current.Width; i++) for (var j = 0; j < current.Height; j++) { var newVal = GetSiblings(gsBytes, i, j) .Zip(mask, (a, b) => a * b) .Sum(); newVal = (newVal > limit) ? 255 : 0; current.SetPixel(i, j, Color.FromArgb(255, newVal, newVal, newVal)); } return current; }
public static Bitmap Equalize(Bitmap original) { var current = new Bitmap(original.Width, original.Height); var oldLevels = original.GetGrayscaleBytes(); var newLevels = GetNewBrightnessLevels(oldLevels.Get1dArray()); for (var i = 0; i < original.Width; i++) { for (var j = 0; j < original.Height; j++) { var oldLevel = oldLevels[i, j]; var newLevel = newLevels[oldLevel]; current.SetPixel(i, j, Color.FromArgb(255, newLevel, newLevel, newLevel)); } } return current; }
public static Bitmap Normalize(Bitmap original) { var current = new Bitmap(original.Width, original.Height); var gsBytes = original.GetGrayscaleBytes(); var gsBytesArr = gsBytes.Get1dArray(); var oldMin = gsBytesArr.Min(); var oldMax = gsBytesArr.Max(); var newMin = 0; var newMax = 255; for (var i = 0; i < current.Width; i++) { for (var j = 0; j < current.Height; j++) { var oldVal = gsBytes[i, j]; var newVal = (oldVal - oldMin) * (newMax - newMin) / (oldMax - oldMin) + newMin; current.SetPixel(i, j, Color.FromArgb(255, newVal, newVal, newVal)); } } return current; }
public static Bitmap Process(Bitmap image) { var current = new Bitmap(image.Width, image.Height); var currentBin = new Bitmap(image.Width, image.Height); var gs = image.GetGrayscaleBytes(); var binarized = Binarize(gs); for (var i = 0; i < currentBin.Width; i++) { for (var j = 0; j < currentBin.Height; j++) { var tmp = binarized[i, j] == 1 ? Color.White : Color.Black; currentBin.SetPixel(i, j, tmp); } } var scanned = Scan(binarized); var curColor = 0; var colorsDict = new Dictionary<int, Color>(); for (var i = 0; i < current.Width; i++) { for (var j = 0; j < current.Height; j++) { var mark = scanned[i, j]; if (mark == 0) current.SetPixel(i, j, Color.White); if (mark >= 2) { Color tmpCol; if (colorsDict.ContainsKey(mark)) tmpCol = colorsDict[mark]; else { curColor = (curColor == colors.Count() - 1) ? 0 : curColor + 1; colorsDict.Add(mark, colors[curColor]); tmpCol = colors[curColor]; } current.SetPixel(i, j, tmpCol); } } } currentBin.Save("SAVEDBIN.bmp"); current.Save("SAVED.bmp"); return current; }