// assume k = -0.2 and R = 15x15 // needs 1 pixel width padding public static Pixel Niblack(PNM image, int index, double k = -0.2, int length = 15, int padding = 7) { byte r, g, b; float[] surrounding = new float[length * length]; int i = 0; for (int m = 0; m < length; m++) { for (int n = 0; n < length; n++) { image.GetPixel(index - ((padding - m) * image.Width) - (padding - n), out r, out g, out b); surrounding[i] = PNM.RGBToLuminosity(r, g, b); i++; } } float mean = surrounding.Average(); double threshold = mean + (k * (mean / surrounding.Length)); image.GetPixel(index, out r, out g, out b); byte luminosity = PNM.RGBToLuminosity(r, g, b); if (luminosity < threshold) { return(Pixel.Black); } return(Pixel.White); }
// pixel_intensity >= threshold == white object // pixel_intensity < threshold == black background public static Pixel Plain(byte r, byte g, byte b, byte threshold) { byte lum = PNM.RGBToLuminosity(r, g, b); if (lum >= threshold) { return(Pixel.White); } return(Pixel.Black); }
public static Pixel ToGrayscale(byte r, byte g, byte b) { byte value = PNM.RGBToLuminosity(r, g, b); return(new Pixel(value, value, value)); }