// Image Preprocessing private Bitmap ImagePreprocessing(Bitmap image, int threshold) { // Grayscale image = Grayscale.CommonAlgorithms.RMY.Apply(image); // Edge Detection (using CannyEdgeDetector) image = new CannyEdgeDetector().Apply(image); // Noise Removal int startX, startY, endX, endY; startX = image.Width; startY = image.Height; endX = 0; endY = 0; for (int i = 0; i < image.Width; i++) { for (int j = 0; j < image.Height; j++) { if (image.GetPixel(i, j).R > threshold) { if (i < startX) { startX = i; } if (j < startY) { startY = j; } if (i > endX) { endX = i; } if (j > endY) { endY = j; } } } } image = image.Clone(new Rectangle(startX, startY, endX - startX, endY - startY), PixelFormat.Format8bppIndexed); // Resize image = new ResizeBilinear(32, 32).Apply(image); return(image); }
public Bitmap preprocessing(Bitmap image) { image = image.Clone(new Rectangle(0, 0, image.Width, image.Height), System.Drawing.Imaging.PixelFormat.Format24bppRgb); image = Grayscale.CommonAlgorithms.BT709.Apply(image); image = new Threshold(127).Apply(image); image = new CannyEdgeDetector().Apply(image); int x = image.Width; int y = image.Height; int width = 0; int height = 0; for (int i = 0; i < image.Height; i++) { for (int j = 0; j < image.Width; j++) { if (image.GetPixel(j, i).R > 127) { if (x > j) { x = j; } if (y > i) { y = i; } if (width < j) { width = j; } if (height < i) { height = i; } } } } image = image.Clone(new Rectangle(x, y, width - x, height - y), System.Drawing.Imaging.PixelFormat.Format24bppRgb); image = new ResizeBilinear(10, 10).Apply(image); return(image); }