/// <summary> /// Turns a bitmap black and white /// </summary> /// <param name="thresh"></param> /// <returns></returns> public ImagePreprocessor Binarization(BinarizationThreshold thresh = BinarizationThreshold.Light) { try { // Establish a color object. Color curColor; int ret; // The width of the image. for (int iX = 0; iX < Image.Width; iX++) { // The height of the image. for (int iY = 0; iY < Image.Height; iY++) { // Get the pixel from bitmap object. curColor = Image.GetPixel(iX, iY); // Transform RGB to Y (gray scale) ret = (int)(curColor.R * 0.299 + curColor.G * 0.578 + curColor.B * 0.114); // This is our threshold, you can change it and to try what are different. if (ret > (int)thresh) { ret = 255; } else { ret = 0; } // Set the pixel into the bitmap object. Image.SetPixel(iX, iY, Color.FromArgb(ret, ret, ret)); } // The closing 'The height of the image'. } // The closing 'The width of the image'. } catch (Exception e) { log.LogWarning("Binarization Failed: " + e.Message); } return(this); }
public ProcessedBitmap Process(ParsedBitmap parsed, Location location, Rectangle cropLocation, BinarizationThreshold binarization = BinarizationThreshold.Light) { this.Url = parsed.Url; this.Location = location; Bitmap clone = new Bitmap(parsed.Image); this.Image = imagePreprocessor .SetImage(clone) .Crop(cropLocation) .FloodFill() .Resize() .AddBorder(Color.White) .Binarization(binarization) .Image; return(this); }