private PixelState[,] InitializePixels() { PixelState[,] pixels = new PixelState[bmp.Width, bmp.Height]; BitmapData bmData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, bmp.PixelFormat); for (int x = 0; x < bmp.Width; x++) { for (int y = 0; y < bmp.Height; y++) { int r, g, b; NuGenImageProcessor.GetPixelAt(bmData, x, y, out r, out g, out b); if ((Math.Round(((double)Math.Max(Math.Max(r, g), b)) * 255.0 / 100.0)) < 50) { pixels[x, y] = PixelState.PixelOn; } else { pixels[x, y] = PixelState.PixelOff; } } } bmp.UnlockBits(bmData); return(pixels); }
//Discretizes the image, switching through the different modes of discretization public void Discretize() { int value; BitmapData bmData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, bmp.PixelFormat); for (int x = 0; x < bmp.Width; x++) { for (int y = 0; y < bmp.Height; y++) { if (settings.discretizeMethod == DiscretizeMethod.DiscretizeForeground) { value = DiscretizeValueForeground(x, y, bmData); } else { value = DiscretizeValue(x, y, bmData); } if (PixelIsOn(value)) { NuGenImageProcessor.SetPixelAt(bmData, x, y, 0, 0, 0); } else { NuGenImageProcessor.SetPixelAt(bmData, x, y, 255, 255, 255); } } } bmp.UnlockBits(bmData); }
private static void TestGetPixels() { Image img = Image.FromFile("samples\\gridlines.gif"); Bitmap b = new Bitmap(img); BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadOnly, b.PixelFormat); NuGenImageProcessor.SetPixelAt(bmData, 242, 133, 64, 65, 66); int r, g, blue; NuGenImageProcessor.GetPixelAt(bmData, 242, 133, out r, out g, out blue); Color c = Color.FromArgb(r, g, blue); b.UnlockBits(bmData); Color cb = b.GetPixel(242, 133); if (!(c.R == 64 && c.G == 65 && c.B == 66)) { throw new Exception("Failure"); } if (!(c.R == cb.R && c.G == cb.G && c.B == cb.B)) { throw new Exception("Failure"); } }
//Discretizes a value and takes the bitmap data object so that when looping through the entire // image the bits do not need to be repeatedly locked public int DiscretizeValue(int x, int y, BitmapData bmData) { int h, s, v, r, g, b; NuGenImageProcessor.GetPixelAt(bmData, x, y, out r, out g, out b); NuGenImageProcessor.GetHSVFromRGB(r, g, b, out h, out s, out v); double intensity; // convert hue from 0 to 359, saturation from 0 to 255, value from 0 to 255 int value = 0; switch (settings.discretizeMethod) { case DiscretizeMethod.DiscretizeForeground: break; case DiscretizeMethod.DiscretizeHue: { value = h * DiscretizeHueMax / 359; break; } case DiscretizeMethod.DiscretizeIntensity: { intensity = Math.Sqrt(r * r + g * g + b * b); value = (int)(intensity * DiscretizeIntensityMax / Math.Sqrt(255 * 255 + 255 * 255 + 255 * 255) + 0.5); break; } case DiscretizeMethod.DiscretizeNone: throw new InvalidOperationException("Can not discretize, discretizing is not enabled at this time"); case DiscretizeMethod.DiscretizeSaturation: { value = s * DiscretizeSaturationMax / 255; break; } case DiscretizeMethod.DiscretizeValue: { value = v * DiscretizeValueMax / 255; break; } } if (value < 0) { value = 0; } if (ColorAttributeMax(settings.discretizeMethod) < value) { value = ColorAttributeMax(settings.discretizeMethod); } return(value); }
private static void TestSetPixelAt() { Image img = Image.FromFile("samples\\gridlines.gif"); Bitmap b = new Bitmap(img); BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, b.PixelFormat); NuGenImageProcessor.SetPixelAt(bmData, 100, 100, 255, 0, 0); b.UnlockBits(bmData); Color c = b.GetPixel(100, 100); }
// remove gridlines from original image and discretize, then break processed image into segments public void ProcessOriginialImage() { if (originalImage == null) { throw new InvalidOperationException("There is no original image to process"); } imageProcessor = new NuGenImageProcessor(this); imageProcessor.Process(); processedImage = imageProcessor.ProcessedImage; bgColor = imageProcessor.BackgroundColor; UpdateListeners(); }
//Tells whether a processed pixel is on, it is only only if it is black, off it is white public static bool ProcessedPixelIsOn(BitmapData mbData, int x, int y) { if (x > mbData.Width || y > mbData.Height) { return(false); } int r, g, b; NuGenImageProcessor.GetPixelAt(mbData, x, y, out r, out g, out b); if (r == 0 && g == 0 && b == 0) { return(true); } return(false); }
//Saves the pixel states to the image private void SavePixels(PixelState[,] pixels, Color bgColor) { BitmapData bmData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, bmp.PixelFormat); byte r = bgColor.R; byte g = bgColor.G; byte b = bgColor.B; for (int x = 0; x < bmp.Width; x++) { for (int y = 0; y < bmp.Height; y++) { PixelState state = pixels[x, y]; if (state != PixelState.PixelOff && state != PixelState.PixelOn) { NuGenImageProcessor.SetPixelAt(bmData, x, y, r, g, b); } } } bmp.UnlockBits(bmData); }
//Discretizes a value using the foreground method taking a bitmap data object, this method provides // the best performance when looping through an entire image since it doesn't require the bitmap // to be locked repeatedly public int DiscretizeValueForeground(int x, int y, Color referenceColor, BitmapData bmData) { int r, g, b; NuGenImageProcessor.GetPixelAt(bmData, x, y, out r, out g, out b); int rBg = referenceColor.R; int gBg = referenceColor.G; int bBg = referenceColor.B; double distance = Math.Sqrt((r - rBg) * (r - rBg) + (g - gBg) * (g - gBg) + (b - bBg) * (b - bBg)); int value = (int)(distance * DiscretizeForegroundMax / Math.Sqrt(255 * 255 + 255 * 255 + 255 * 255) + 0.5); if (value < 0) { value = 0; } if (ColorAttributeMax(settings.discretizeMethod) < value) { value = ColorAttributeMax(settings.discretizeMethod); } return(value); }