Beispiel #1
0
		private void ProcessImage(Bitmap image) {
			CaptchaImageProcessor proc = new CaptchaImageProcessor(image);
			ImageProcessorSettings settings = new ImageProcessorSettings() {
				ImageBlur = chkBlur.Checked,
				RemoveBorder = (int)numericRemoveBorder.Value,
				RemovePixelNoise = (int)numericNoiseKill.Value,
				InvertColors = chkInvertColors.Checked,
				LinearizeColors = chkLinarizeColors.Checked,
				BrightnessThreshold = (int)numericBrightness.Value
			};

			Bitmap imageAfter = proc.ProcessImage(settings);
			string result = CaptchaKiller.OCR(imageAfter);
			imgAfter.Image = imageAfter;

			if (result == null) {
				lblResult.BackColor = Color.Maroon;
				lblResult.ForeColor = Color.WhiteSmoke;
				lblResult.Text = "OCR Error";
			} else {
				lblResult.BackColor = Color.DarkGreen;
				lblResult.ForeColor = Color.WhiteSmoke;
				lblResult.Text = result;
			}
		}
        public Bitmap ProcessImage(ImageProcessorSettings settings)
        {
            Bitmap output = Image.Clone() as Bitmap;

            if (settings.RemoveBorder > 0)
            {
                int x = settings.RemoveBorder;
                output = new Crop(new Rectangle(x, x, Image.Width - (2 * x), Image.Height - (2 * x))).Apply(Image);
            }

            if (settings.RemovePixelNoise > 0)
            {
                for (int i = 0; i < settings.RemovePixelNoise; i++)
                {
                    // Use a copy for iteration
                    Bitmap bitmap = new Bitmap(output);

                    for (int j = 1; j < (output.Height - 1); j++)
                    {
                        for (int k = 1; k < (output.Width - 1); k++)
                        {
                            if (output.GetPixel(k, j).GetBrightness() < 0.5)
                            {
                                // Top left
                                float tl = output.GetPixel(k - 1, j).GetBrightness();
                                // Top right
                                float tr = output.GetPixel(k + 1, j).GetBrightness();
                                // Bottom left
                                float bl = output.GetPixel(k, j - 1).GetBrightness();
                                // Bottom right
                                float br = output.GetPixel(k, j + 1).GetBrightness();

                                // Test the edges
                                if (tl > 0.5 && tr > 0.5)
                                {
                                    bitmap.SetPixel(k, j, Color.White);
                                }
                                if (bl > 0.5 && br > 0.5)
                                {
                                    bitmap.SetPixel(k, j, Color.White);
                                }
                            }
                        }
                    }
                    output = bitmap;
                }
            }
            if (settings.LinearizeColors)
            {
                ImageStatistics statistics = new ImageStatistics(output);
                output = new LevelsLinear {
                    InRed   = new Range(statistics.Red.Min, statistics.Red.Max),
                    InGreen = new Range(statistics.Green.Min, statistics.Green.Max),
                    InBlue  = new Range(statistics.Blue.Min, statistics.Blue.Max)
                }.Apply(output);
            }
            if (settings.InvertColors)
            {
                output = new Invert().Apply(output);
            }
            if (settings.ImageBlur)
            {
                output = new Blur().Apply(output);
            }
            if (settings.BrightnessThreshold > 0)
            {
                // Brightness threshold is in percent..
                decimal threshold = (decimal)settings.BrightnessThreshold / 100M;
                output = new Threshold(Convert.ToByte((decimal)(threshold * 255M)), 255).Apply(output);
            }

            return(output);
        }