//smooth salt and pepper noise
        private static Bitmap Smooth(Bitmap image)
        {
            // create filter
            var filter = new ConservativeSmoothing();

            // apply the filter
            return(filter.Apply(image));
        }
        private static Bitmap Denoiseimage(Bitmap image)
        {
            // create filter
            ConservativeSmoothing filter = new ConservativeSmoothing();

            // apply the filter
            filter.ApplyInPlace(image);
            return(image);
        }
Пример #3
0
        /*
         * 3- noise elimination to delete isolated pixel and improve the next step.
         * (median filter technique seems to be the best way);
         * */
        public void RemoveNoise(ref Bitmap image)
        {
            /*
             * FiltersSequence filterSquence = new FiltersSequence(Grayscale.CommonAlgorithms.BT709,
             *                   new Threshold(100), new FillHoles());
             * image = filterSquence.Apply(image);
             */
            // create filter
            ConservativeSmoothing filterSmoothing = new ConservativeSmoothing();

            // apply the filter
            filterSmoothing.ApplyInPlace(image);
        }
        public void doConservative(int kernel)
        {
            Bitmap img = new Bitmap(path);
            ConservativeSmoothing filter = new ConservativeSmoothing(kernel * 2 + 1);

            // apply the filter
            img = filter.Apply(img);
            // ImageEView
            if (mov != null)
            {
                this.WorkItem.Workspaces[WorkspaceNames.TabWorkspace].Close(mov);
            }
            mov = this.WorkItem.SmartParts.AddNew <ImageEView>();
            mov.panAndZoomPictureBox1.Image = img;
            SmartPartInfo spi =
                new SmartPartInfo("Conservative", "MyOwnDescription");

            this.WorkItem.Workspaces[WorkspaceNames.TabWorkspace].Show(mov, spi);
        }
Пример #5
0
        public static Bitmap GetOrcImage(Bitmap bmp)
        {
            try
            {
                // from AForge's sample code
                if (bmp.PixelFormat == PixelFormat.Format16bppGrayScale || Bitmap.GetPixelFormatSize(bmp.PixelFormat) > 32)
                {
                    throw new NotSupportedException("Unsupported image format");
                }

                var img = AForge.Imaging.Image.Clone(bmp, PixelFormat.Format24bppRgb);

                img = new Grayscale(0.2125, 0.7154, 0.0721).Apply(img);
                img = new Threshold(50).Apply(img);

                ConservativeSmoothing filter = new ConservativeSmoothing();
                // apply the filter
                filter.ApplyInPlace(img);

                //BilateralSmoothing filter = new BilateralSmoothing();
                //filter.KernelSize = 3;
                //filter.SpatialFactor = 10;
                //filter.ColorFactor = 60;
                //filter.ColorPower = 0.5;
                //// apply the filter
                //img = filter.Apply(img);

                //img = new BlobsFiltering(1, 1, img.Width, img.Height).Apply(img);

                //// create filter
                //Median filter = new Median();
                //// apply the filter
                //filter.ApplyInPlace(image)

                return(img);
            }
            finally
            {
                bmp.Dispose();
            }
        }
Пример #6
0
        public Bitmap Detect(Bitmap bitmap)
        {
            Bitmap grayscaleBitmap = Grayscale.CommonAlgorithms.BT709.Apply(bitmap);

            IFilter smoothingFilter = null;

            switch (_smoothMode)
            {
            case "None": smoothingFilter = null; break;

            case "Mean": smoothingFilter = new Mean(); break;

            case "Median": smoothingFilter = new Median(); break;

            case "Conservative": smoothingFilter = new ConservativeSmoothing(); break;

            case "Adaptive": smoothingFilter = new AdaptiveSmoothing(); break;

            case "Bilateral": smoothingFilter = new BilateralSmoothing(); break;
            }
            Bitmap smoothBitmap = smoothingFilter != null?smoothingFilter.Apply(grayscaleBitmap) : grayscaleBitmap;

            IFilter edgeFilter = null;

            switch (_edgeMode)
            {
            case "Homogenity": edgeFilter = new HomogenityEdgeDetector(); break;

            case "Difference": edgeFilter = new DifferenceEdgeDetector(); break;

            case "Sobel": edgeFilter = new SobelEdgeDetector(); break;

            case "Canny": edgeFilter = new CannyEdgeDetector(); break;
            }
            Bitmap edgeBitmap = edgeFilter != null?edgeFilter.Apply(smoothBitmap) : smoothBitmap;

            IFilter threshholdFilter = new Threshold(_threshold);
            Bitmap  thresholdBitmap  = _threshold == 0 ? edgeBitmap : threshholdFilter.Apply(edgeBitmap);

            BlobCounter blobCounter = new BlobCounter();

            blobCounter.FilterBlobs = true;
            blobCounter.MinHeight   = _minHeight;
            blobCounter.MinWidth    = _minWidth;
            blobCounter.ProcessImage(thresholdBitmap);
            Blob[] blobs = blobCounter.GetObjectsInformation();

            Bitmap   outputBitmap   = new Bitmap(thresholdBitmap.Width, thresholdBitmap.Height, PixelFormat.Format24bppRgb);
            Graphics bitmapGraphics = Graphics.FromImage(outputBitmap);
            Bitmap   inputBitmap    = null;

            switch (_drawMode)
            {
            case "Original": inputBitmap = bitmap; break;

            case "Grayscale": inputBitmap = grayscaleBitmap; break;

            case "Smooth": inputBitmap = smoothBitmap; break;

            case "Edge": inputBitmap = edgeBitmap; break;

            case "Threshold": inputBitmap = thresholdBitmap; break;
            }
            if (inputBitmap != null)
            {
                bitmapGraphics.DrawImage(inputBitmap, 0, 0);
            }

            Pen nonConvexPen = new Pen(Color.Red, 2);
            Pen nonRectPen   = new Pen(Color.Orange, 2);
            Pen cardPen      = new Pen(Color.Blue, 2);

            SimpleShapeChecker shapeChecker  = new SimpleShapeChecker();
            List <IntPoint>    cardPositions = new List <IntPoint>();

            for (int i = 0; i < blobs.Length; i++)
            {
                List <IntPoint> edgePoints = blobCounter.GetBlobsEdgePoints(blobs[i]);
                List <IntPoint> corners;

                if (shapeChecker.IsConvexPolygon(edgePoints, out corners))
                {
                    PolygonSubType subType = shapeChecker.CheckPolygonSubType(corners);

                    if ((subType == PolygonSubType.Parallelogram || subType == PolygonSubType.Rectangle) && corners.Count == 4)
                    {
                        // Check if its sideways, if so rearrange the corners so it's vertical.
                        RearrangeCorners(corners);

                        // Prevent detecting the same card twice by comparing distance against other detected cards.
                        bool sameCard = false;
                        foreach (IntPoint point in cardPositions)
                        {
                            if (corners[0].DistanceTo(point) < _minDistance)
                            {
                                sameCard = true;
                                break;
                            }
                        }
                        if (sameCard)
                        {
                            continue;
                        }

                        // Hack to prevent it from detecting smaller sections of the card instead of the whole card.
                        if (GetArea(corners) < _minArea)
                        {
                            continue;
                        }

                        cardPositions.Add(corners[0]);

                        bitmapGraphics.DrawPolygon(cardPen, ToPointsArray(corners));
                    }
                    else
                    {
                        foreach (IntPoint point in edgePoints.Take(300))
                        {
                            bitmapGraphics.DrawEllipse(nonRectPen, point.X, point.Y, 1, 1);
                        }
                    }
                }
                else
                {
                    foreach (IntPoint point in edgePoints.Take(300))
                    {
                        bitmapGraphics.DrawEllipse(nonConvexPen, point.X, point.Y, 1, 1);
                    }
                }
            }

            bitmapGraphics.Dispose();
            nonConvexPen.Dispose();
            nonRectPen.Dispose();
            cardPen.Dispose();

            return(outputBitmap);
        }
Пример #7
0
        /// <summary>
        /// <para>Pulls the image</para>
        /// <para>Runs the ocr on it</para>
        /// <para>fills in the blanks</para>
        /// <para>submits the page</para>
        /// </summary>
        /// <param name="challenge"></param>
        /// <param name="cancellationToken"></param>
        /// <param name="answer"></param>
        /// <returns></returns>
        private bool SolveCaptcha(Uri challenge, CancellationToken cancellationToken, out String answer)
        {
            answer = null;
            var tesseractEngine = this.TesseractEngine;

            if (null == tesseractEngine)
            {
                return(false);
            }

            var captchaData = this.PullCaptchaData(challenge);

            if (captchaData.ImageUri == null)
            {
                captchaData.Status = CaptchaStatus.NoImageFoundToBeSolved;
                this.UpdateCaptchaData(captchaData);
                return(false);
            }

            Console.WriteLine(Resources.Uber_SolveCaptcha_Attempting_OCR_on__0_, captchaData.ImageUri.AbsolutePath);

            captchaData.Status = CaptchaStatus.SolvingImage;
            this.UpdateCaptchaData(captchaData);

            var folder = new Folder(Path.GetTempPath());

            Document document;

            folder.TryGetTempDocument(document: out document, extension: "png");

            this.PictureBoxChallenge.Image.Save(document.FullPathWithFileName, ImageFormat.Png);

            var aforgeImage = AForge.Imaging.Image.FromFile(document.FullPathWithFileName);

            var smoothing = new ConservativeSmoothing();

            var cannyEdgeDetector = new CannyEdgeDetector();

            cannyEdgeDetector.Apply(aforgeImage);

            aforgeImage.Save(document.FullPathWithFileName, ImageFormat.Png);

            this.PictureBoxChallenge.ImageLocation = document.FullPathWithFileName;

            this.PictureBoxChallenge.Load();

            this.Throttle(Seconds.Ten);

            using (var img = Pix.LoadFromFile(document.FullPathWithFileName).Deskew()) {
                using (var page = tesseractEngine.Process(img, PageSegMode.SingleLine)) {
                    answer = page.GetText();

                    var paragraph = new Paragraph(answer);

                    answer = new Sentence(paragraph.ToStrings(" ")).ToStrings(" ");

                    FluentTimers.Create(Minutes.One, () => document.Delete()).AndStart();

                    if (!String.IsNullOrWhiteSpace(answer))
                    {
                        captchaData.Status = CaptchaStatus.SolvedChallenge;
                        this.UpdateCaptchaData(captchaData);
                        return(true);
                    }

                    return(false);
                }
            }
        }
Пример #8
0
        public mSmoothConservative()
        {
            BitmapType = BitmapTypes.None;

            filter = new ConservativeSmoothing();
        }