public Bitmap FindTextBitmap() { Bitmap textBmp = new Bitmap(sets.BmpTextWidth, sets.BmpTextHeight); textBmp = sourceImage.Clone(new Rectangle(match.X + sets.TextMarginRight, match.Y + sets.TextMarginTop, sets.BmpTextWidth, sets.BmpTextHeight), PixelFormat.Format24bppRgb); Grayscale gs = new Grayscale(0.2125, 0.7154, 0.0721); SISThreshold th = new SISThreshold(); BinaryErosion3x3 bs = new BinaryErosion3x3(); ColorFiltering cf = new ColorFiltering(); cf.Red = new IntRange(90, 160); cf.Green = new IntRange(55, 110); cf.Blue = new IntRange(15, 85); cf.ApplyInPlace(textBmp); //for (int i = 0; i < textBmp.Width; i++) //{ // for (int j = 0; j < textBmp.Height; j++) // { // if (((textBmp.GetPixel(i, j).R > 95) && (textBmp.GetPixel(i, j).R < 155)) && ((textBmp.GetPixel(i, j).G > 60) && (textBmp.GetPixel(i, j).G > 105)) && ((textBmp.GetPixel(i, j).B > 20 && (textBmp.GetPixel(i, j).B < 80)))) // { // textBmp.SetPixel(i, j, Color.Black); // } // else // { // textBmp.SetPixel(i, j, Color.White); // } // } //} BilateralSmoothing bss = new BilateralSmoothing(); bss.KernelSize = 7; bss.SpatialFactor = 5; bss.ColorFactor = 30; bss.ColorPower = 0.5; bss.ApplyInPlace(textBmp); textBmp = gs.Apply(textBmp); th.ApplyInPlace(textBmp); //bs.ApplyInPlace(textBmp); //erose.ApplyInPlace(textBmp); return(textBmp); }
//TODO //FIXME public static string CalculateHash(Bitmap original, string save_filtered_to = null) { // Hash calculation is very discriminating. If a pixel changes in color, the whole hash will be different. // For us this is too rigid: we might support some "noise" or "error", manly because of animation of buttons. // For this reason, it is a good idea to apply some filter to the image in order to flattern common pizels. // A way to do this is to lower the image quality // Apply some common filter and then calculate the hash. if (original == null) { return(null); } using (Bitmap bmp = Grayscale.CommonAlgorithms.BT709.Apply(original)) { // Apply the Threshold var threshold = new SISThreshold(); threshold.ApplyInPlace(bmp); Erosion f = new Erosion(); f.ApplyInPlace(bmp); ImageConverter converter = new ImageConverter(); byte[] rawImageData = converter.ConvertTo(bmp, typeof(byte[])) as byte[]; MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider(); byte[] hash = md5.ComputeHash(rawImageData); StringBuilder sb = new StringBuilder(); for (int i = 0; i < hash.Length; i++) { sb.Append(hash[i].ToString("X2")); } string strhash = sb.ToString(); if (save_filtered_to != null) { bmp.Save(string.Format(save_filtered_to, strhash + "_filtered")); } return(strhash); } }
private void Button_HSV_Filter_Click(object sender, EventArgs e) { Bitmap image = new Bitmap(pictureBox1.Image); HSLFiltering filterHsl = new HSLFiltering(); Mean filterMean = new Mean(); filterHsl.Luminance = new AForge.Range(0.1f, 1); filterHsl.UpdateHue = false; filterHsl.UpdateSaturation = false; filterHsl.UpdateLuminance = true; filterHsl.ApplyInPlace(image); filterMean.ApplyInPlace(image); SISThreshold filterThresold = new SISThreshold(); GrayscaleBT709 filterGray = new GrayscaleBT709(); image = filterGray.Apply(image); Bitmap clone = image.Clone(new Rectangle(0, 0, image.Width, image.Height), PixelFormat.Format8bppIndexed); filterThresold.ApplyInPlace(clone); image = clone; Bitmap clone2normal = image.Clone(new Rectangle(0, 0, image.Width, image.Height), PixelFormat.Format32bppRgb); image = clone2normal; BlobCounter bc = new BlobCounter(); bc.FilterBlobs = true; bc.MinWidth = 5; bc.MinHeight = 5; bc.ProcessImage(image); Blob[] blobs = bc.GetObjects(image, false); var rectanglesToClear = from blob in blobs select blob.Rectangle; using (var gfx = Graphics.FromImage(image)) { foreach (var rect in rectanglesToClear) { if (rect.Height < 20 && rect.Width < 20) { gfx.FillRectangle(Brushes.White, rect); } } gfx.Flush(); } Dilatation filterDilation = new Dilatation(); image = image.Clone(new Rectangle(0, 0, image.Width, image.Height), PixelFormat.Format48bppRgb); filterDilation.ApplyInPlace(image); filterDilation.ApplyInPlace(image); Erosion filterErosion = new Erosion(); filterErosion.ApplyInPlace(image); pictureBox2.Image = image; }