private void process()
        {
            stopwatch.Reset();
            stopwatch.Start();
            plates = new List <string>();
            frame  = contrastCorrectionFilter.Apply(originalImage);
            frame  = grayscaleFilter.Apply(frame);

            BitmapData     frameData = frame.LockBits(new Rectangle(0, 0, frame.Width, frame.Height), ImageLockMode.ReadWrite, frame.PixelFormat);
            UnmanagedImage data      = new UnmanagedImage(frameData);

            bradleyLocalFilter.ApplyInPlace(data);
            fillHoles.ApplyInPlace(data);
            openingFilter.ApplyInPlace(data);

            blobCounter.ProcessImage(data);

            data.Dispose();
            frame.UnlockBits(frameData);

            Graphics g = Graphics.FromImage(originalImage);

            Blob[] blobs = blobCounter.GetObjectsInformation();
            foreach (Blob blob in blobs)
            {
                List <IntPoint> edgePoints = blobCounter.GetBlobsEdgePoints(blob);
                List <IntPoint> corners    = null;

                // da li je četverougao?
                if (!shapeChecker.IsQuadrilateral(edgePoints, out corners))
                {
                    continue;
                }

                if (FindNewCornersAndCheckAspectRatio(corners))
                {
                    SimpleQuadrilateralTransformation sqt = new SimpleQuadrilateralTransformation(corners, 300, 66);
                    Bitmap plate = sqt.Apply(originalImage);
                    plate = grayscaleFilter.Apply(plate);
                    otsuThresholdFilter.ApplyInPlace(plate);

                    if (!IsLicensePlate(plate))
                    {
                        continue;
                    }

                    String plateText;
                    if (FindText(plate, out plateText))
                    {
                        g.DrawPolygon(pen, ToPointsArray(corners));
                        frame = plate;
                        plates.Add(plateText);
                    }
                }
            }
            g.Dispose();
            stopwatch.Stop();
        }
Exemplo n.º 2
0
        public static Bitmap PixelDiff(Bitmap a, Bitmap b)
        {
            var difference = new Difference(a);
            var dif        = difference.Apply(b);

            gaussianBlur.ApplyInPlace(dif);

            Bitmap clone = Grayscale.CommonAlgorithms.Y.Apply(dif);

            threshold.ApplyInPlace(clone);
            fillHoles.ApplyInPlace(clone);

            return(clone);
        }
        public static Bitmap ProcessImage(string imagePath)
        {
            var img = AForge.Imaging.Image.FromFile(imagePath);

            ContrastStretch filterContrastStretch = new ContrastStretch();

            filterContrastStretch.ApplyInPlace(img);

            try
            {
                img = Grayscale.CommonAlgorithms.BT709.Apply(img);
            }
            catch
            {
                System.Windows.Forms.MessageBox.Show("The image should not be grayscale");
            }

            Opening filterOpening = new Opening();

            filterOpening.ApplyInPlace(img);

            SobelEdgeDetector sobel = new SobelEdgeDetector();

            sobel.ApplyInPlace(img);

            Closing filterClosing = new Closing();

            filterClosing.ApplyInPlace(img);

            Threshold threshold = new Threshold(100);

            threshold.ApplyInPlace(img);

            FillHoles fillHoles = new FillHoles();

            fillHoles.MaxHoleWidth         = img.Width;
            fillHoles.MaxHoleHeight        = img.Height;
            fillHoles.CoupledSizeFiltering = false;
            fillHoles.ApplyInPlace(img);

            filterOpening.ApplyInPlace(img);

            Erosion filterErosion = new Erosion();

            filterErosion.ApplyInPlace(img);

            return(img);
        }
Exemplo n.º 4
0
        public Binarize(UISettings ui, FileData file) : base(ui, file)
        {
            try {
                Invert AFinvert = new Invert();
                switch (ui.ThresholdIndex)         // threshold method selection "Global mean" / "Local adaptive"
                {
                case 0:                            // Global
                    if (ui.ThreshGlobalIsAbsolute) // use absolute
                    {
                        Threshold AFglobalbinary = new Threshold(ui.ThreshGlobalAbsolute);
                        UnmanagedBlackWhite = AFglobalbinary.Apply(UnmanagedGray);
                    }
                    else                                 // use relative
                    {
                        ImageStatistics stats          = new ImageStatistics(UnmanagedGray, AFinvert.Apply(UnmanagedExclude));
                        Threshold       AFglobalbinary = new Threshold(stats.Gray.Center2QuantileValue(1.0d * ui.ThreshGlobalRelative / 255.0d));
                        UnmanagedBlackWhite = AFglobalbinary.Apply(UnmanagedGray);
                    }
                    break;

                case 1:                         // Local
                    BradleyLocalThresholdingX AFlocalbinary = new BradleyLocalThresholdingX()
                    {
                        PixelBrightnessDifferenceLimit = ui.ThreshLocalBrightnessDifference,
                        WindowSize = ui.ThreshLocalWindowSize, UpperLimit = 250
                    };
                    UnmanagedBlackWhite = AFlocalbinary.Apply(UnmanagedGray);
                    break;
                }
                if (ui.FillHoleAirspaceSwitch && ui.FillHoleAirspace != 0)                 // fill holes of airspaces
                {
                    FillHoles AFfillinair = new FillHoles()
                    {
                        CoupledSizeFiltering = true, MaxHoleHeight = ui.FillHoleAirspace, MaxHoleWidth = ui.FillHoleAirspace
                    };
                    //FillHolesArea AFfillinair=new FillHolesArea() { MaxHoleArea=ui.FillHoleAirspace };
                    AFfillinair.ApplyInPlace(UnmanagedBlackWhite);
                }
                UnmanagedBlackWhite = AFinvert.Apply(UnmanagedBlackWhite);
                if (ui.FillHoleTissueSwitch && ui.FillHoleTissue != 0)                 // fill holes of tissue
                {
                    FillHoles AFfillintissue = new FillHoles()
                    {
                        CoupledSizeFiltering = true, MaxHoleHeight = ui.FillHoleTissue, MaxHoleWidth = ui.FillHoleTissue
                    };
                    //FillHolesArea AFfillintissue=new FillHolesArea() { MaxHoleArea=ui.FillHoleTissue };
                    AFfillintissue.ApplyInPlace(UnmanagedBlackWhite);
                }
                if (ui.MorphoDilateSwitch && ui.MorphoDilate != 0)               // Morphological Dilate
                {
                    int n = (Math.Max(ui.MorphoDilate, 0) * 2 + 1); short[,] morphmatrix = new short[n, n];
                    for (int i = 0; i < n; i++)
                    {
                        for (int j = 0; j < n; j++)
                        {
                            morphmatrix[i, j] = 1;
                        }
                    }
                    Dilatation AFdilate = new Dilatation(morphmatrix);
                    AFdilate.ApplyInPlace(UnmanagedBlackWhite);
                }
                if (ui.MorphoErodeSwitch && ui.MorphoErode != 0)               // Morphological Erode

                {
                    int n = (Math.Max(ui.MorphoErode, 0) * 2 + 1); short[,] morphmatrix = new short[n, n];
                    for (int i = 0; i < n; i++)
                    {
                        for (int j = 0; j < n; j++)
                        {
                            morphmatrix[i, j] = 1;
                        }
                    }
                    Erosion AFerode = new Erosion(morphmatrix);
                    AFerode.ApplyInPlace(UnmanagedBlackWhite);
                }
                if (ui.ExcludeColorSwitch)
                {
                    NonParen_SumArea = ui.um2px2 * UnmanagedExclude.NonBlackArea();
                }
                if (ui.BlobMinSwitch)
                {
                    Low_Threshold = Math.Pow(10.0d, ui.BlobMin) - 1;
                }
                else
                {
                    Low_Threshold = 0.0d;
                }
                if (ui.BlobMaxSwitch)
                {
                    High_Threshold = Math.Pow(10.0d, ui.BlobMax) - 1;
                }
                else
                {
                    High_Threshold = int.MaxValue;
                }

                if (ui.BlobMinSwitch || ui.BlobMaxSwitch)
                {
                    Merge       AFmerge1  = new Merge(UnmanagedExclude);
                    ExcludeSize AFexcsize = new ExcludeSize()
                    {
                        Low = (int)Math.Round(Low_Threshold / ui.um2px2), High = (int)Math.Round(Math.Min(int.MaxValue, High_Threshold / ui.um2px2))
                    };
                    Merge AFmerge2 = new Merge(AFexcsize.Apply(AFinvert.Apply(AFmerge1.Apply(UnmanagedBlackWhite))));
                    AFmerge2.ApplyInPlace(UnmanagedExclude);
                    Low_SumArea  = ui.um2px2 * AFexcsize.LowCount;
                    High_SumArea = ui.um2px2 * AFexcsize.HighCount;
                }
            } catch { throw new Exception("Error Occured During Binarization"); }
        }