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();
        }
예제 #2
0
        /// <summary>
        /// Crop image
        /// </summary>
        /// <param name="src">orignal image</param>
        /// <param name="displaycornerPoints"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <returns></returns>
        public Bitmap CroppedImage(Bitmap src, List <IntPoint> displaycornerPoints, int width, int height)
        {
            //Create crop filter
            SimpleQuadrilateralTransformation filter
                = new SimpleQuadrilateralTransformation(displaycornerPoints, width, height);
            //Create cropped display image
            Bitmap des = filter.Apply(src);

            return(des);
        }
예제 #3
0
        private static Bitmap _CropToQuad(Bitmap bitmap, List <IntPoint> corners)
        {
            if (corners == null)
            {
                return(null);
            }

            // create filter
            SimpleQuadrilateralTransformation filter =
                new SimpleQuadrilateralTransformation(corners, (int)(500 * _pokemonCardRatio), 500);

            return(filter.Apply(bitmap));
        }
예제 #4
0
        public static SegaSaturnTexture ConvertFrom(Bitmap image, string name, SegaSaturnTextureCoordinates p1, SegaSaturnTextureCoordinates p2, SegaSaturnTextureCoordinates p3, SegaSaturnTextureCoordinates p4, bool isTriangleMapping)
        {
            SegaSaturnTexture toReturn = new SegaSaturnTexture
            {
                Name = name,
                Hash = String.Format("{0}¤{1}¤{2}¤{3}", p1.Hash, p2.Hash, p3.Hash, p4.Hash)
            };
            Rectangle rect = SegaSaturnTexture.GetRectangleFromTextureCoordinates(p1, p2, p3, p4);

            if (!isTriangleMapping)
            {
                toReturn.Image = JoMapEditorTools.CropBitmap(image, rect);
            }
            else
            {
                List <IntPoint> corners = new List <IntPoint>();
                corners.Add(new IntPoint(p1.X, p1.Y));
                corners.Add(new IntPoint(p2.X, p2.Y));
                corners.Add(new IntPoint(p3.X, p3.Y));
                corners.Add(new IntPoint(p4.X, p4.Y));
                SimpleQuadrilateralTransformation filter = new SimpleQuadrilateralTransformation(corners, rect.Width, rect.Height);
                toReturn.Image = filter.Apply(image);
            }
            if ((toReturn.Image.Width % 8) == 0)
            {
                toReturn.Image = toReturn.Image;
            }
            else
            {
                int width = (toReturn.Image.Width / 8) * 8;
                if (width <= 0)
                {
                    width = 8;
                }
                toReturn.Image = JoMapEditorTools.ResizeImage(toReturn.Image, width, image.Height);
            }
            return(toReturn);
        }
예제 #5
0
 public void TransformImage(VideoFrame vf)
 {
     lastFrame = new VideoFrame(vf.Number, _filter.Apply(vf.Bitmap), vf.Timestamp);
     FrameReady(this, new FrameReadyEventArgs(lastFrame));
 }
예제 #6
0
        private void PatternRecognition(Bitmap bitmap) {
            // Prvi korak - grayscalling originalne slike
            Bitmap frame = grayscaleFilter.Apply(bitmap);

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

            // Drugi korak - detekcija ivica pomocu Sobel filtra
            sobelFilter.ApplyInPlace(frameData);

            // Treći korak - konvertuj sliku u crno-bijelu pri čemu je threshold = 100 odnoso od 0 do 155 je crna boja, a od 156 do 255 je bijela boja
            thresholdFilter.ApplyInPlace(frameData);

            // Četvrti korak - dilitacija / pojacavanje bijele boje jer
            dilitationFilter.ApplyInPlace(frameData);

            // Peti korak - kreiranje binarne slike
            frame = frame.Clone(new Rectangle(0, 0, frame.Width, frame.Height), PixelFormat.Format8bppIndexed);

            // Šesti korak - pronalazak potencijalnih oblika na slici
            blobCounter.ProcessImage(frameData);
            Blob[] blobs = blobCounter.GetObjectsInformation();

            // za crtanje po originalnoj slici
            Graphics g = Graphics.FromImage(bitmap);

            // Sedmi korak - provjeri svaki oblik
            foreach (Blob blob in blobs) {
                List<IntPoint> edgePoints = blobCounter.GetBlobsEdgePoints(blob);
                List<IntPoint> hullPoints = hullFinder.FindHull(edgePoints);
                List<IntPoint> corners = null;

                // da li je četverougao?
                if (shapeChecker.IsQuadrilateral(hullPoints, out corners))
                    // da li je kvadrat?
                    if (shapeChecker.CheckPolygonSubType(corners) == PolygonSubType.Square) {
                        if (!detected) {
                            // Osmi korak - odrđivanje centra gravitacije i gornjeg lijevog tjemena
                            FindNewCorners(corners);

                            // Deveti korak - ekstrakcija prepoznatog kvadrata sa originalne slike u novu sliku dimenzija 100x100
                            SimpleQuadrilateralTransformation quadrilateralTransformation = new SimpleQuadrilateralTransformation(corners, 100, 100);
                            Bitmap recognizedSquare = quadrilateralTransformation.Apply(bitmap);
                            recognizedSquare = recognizedSquare.Clone(new Rectangle(0, 0, recognizedSquare.Width, recognizedSquare.Height), PixelFormat.Format8bppIndexed);

                            // Deseti korak - od nove slike ponovo napravi crno-bijelu
                            otsuThresholdFilter.ApplyInPlace(recognizedSquare);

                            // Jedanaesti korak - invertuj boje
                            invertFilter.ApplyInPlace(recognizedSquare);

                            //Dvanaesti korak - prepoznaj oblik (formiraj matricu)
                            float confidence; // vjerovatnoća da je prepoznat pravi oblik (odnos borja crnih i bijelih piksela u ćeliji
                            byte[,] pattern = binaryGlyphRecognizer.Recognize(recognizedSquare, new Rectangle(0, 0, recognizedSquare.Width, recognizedSquare.Height), out confidence);
                            recognizedSquare.Dispose();

                            if (confidence >= 0.6) {
                                oldPatterns.Add(pattern);
                                Boolean canDraw = CheckPrevious();
                                if (canDraw) {
                                    // Trinaesti korak - iscrtaj matricu
                                    DrawPattern(pattern);
                                    detected = true;

                                    // pravim delay od 3s nakon što prepozna pattern
                                    new Task(() => {
                                        Thread.Sleep(3*1000);
                                        detected = false;
                                    }).Start();

                                    // Komunikacija sa warehouse uređajem.
                                    //new Thread(new RS232Communication(shape).Run).Start();
                                }
                            }
                        }
                        // iscrtaj ivice oko prepoznatog kvadrata
                        g.DrawPolygon(pen, ToPointsArray(hullPoints));
                    }
            }

            g.Dispose();
            frame.UnlockBits(frameData);
            frame.Dispose();
        }
        private void Button1_Click(object sender, EventArgs e)
        {
            PB1.Image = System.Drawing.Image.FromFile(bilde);
            Bitmap bitmap = new Bitmap(bilde);

            ColorFiltering colorFilter = new ColorFiltering();

            colorFilter.Red              = new IntRange(0, 64);
            colorFilter.Green            = new IntRange(0, 64);
            colorFilter.Blue             = new IntRange(0, 30);
            colorFilter.FillOutsideRange = false;

            colorFilter.ApplyInPlace(bitmap);
            PB1.Image = bitmap;



            // locate objects using blob counter
            BlobCounter blobCounter = new BlobCounter();

            blobCounter.FilterBlobs          = true;
            blobCounter.CoupledSizeFiltering = true;
            blobCounter.MinHeight            = bitmap.Height / 2;
            blobCounter.MinWidth             = bitmap.Width / 2;

            blobCounter.ProcessImage(bitmap);
            Blob[]          blobs   = blobCounter.GetObjectsInformation();
            List <IntPoint> corners = new List <IntPoint>();

            for (int i = 0, n = blobs.Length; i < n; i++)
            {
                List <IntPoint> edgePoints = blobCounter.GetBlobsEdgePoints(blobs[i]);
                if (corners.Count < 4)
                {
                    corners = PointsCloud.FindQuadrilateralCorners(edgePoints);
                }
                else
                {
                    break;
                }
            }
            CornersTB.Text = corners.Count.ToString();


            if (corners.Count == 4)
            {
                //croper bildet etter hvilke kanter den finner
                SimpleQuadrilateralTransformation filter2 = new SimpleQuadrilateralTransformation(corners);
                bitmap    = filter2.Apply(bitmap);
                PB1.Image = bitmap;
            }

            //fjerner 2% av kantene for å ungå feil.
            int  i1      = bitmap.Height / 100;
            int  i2      = bitmap.Width / 100;
            Crop Cfilter = new Crop(new Rectangle(i2, i1, bitmap.Width - (2 * i2), bitmap.Height - (2 * i1)));

            bitmap    = Cfilter.Apply(bitmap);
            PB1.Image = bitmap;

            //lager en grey scale av bildet. kan flyttes når vi finner firkant 90%+ av tida
            Grayscale filter = new Grayscale(0.2125, 0.7154, 0.0721);
            Bitmap    GrayBM = filter.Apply(bitmap);

            PB1.Image = GrayBM;


            // gather statistics
            ImageStatistics stat  = new ImageStatistics(bitmap);
            ImageStatistics stat2 = new ImageStatistics(GrayBM);
            Histogram       red   = stat.Red;
            Histogram       blue  = stat.Blue;
            Histogram       green = stat.Green;
            Histogram       gray  = stat2.Gray;


            redTB.Text   = Convert.ToString(red.Mean);
            blueTB.Text  = Convert.ToString(blue.Mean);
            greenTB.Text = Convert.ToString(green.Mean);
            double Gmean = gray.Mean;

            GavgTB.Text = Convert.ToString(Gmean);
            GminTB.Text = Convert.ToString(gray.Min);
            GmaxTB.Text = Convert.ToString(gray.Max);


            int temp = 0;

            for (int i = 0; i < (Gmean / 2); i++)
            {
                temp += gray.Values[i];
            }
            MessageBox.Show(temp.ToString() + " sorte piksel verdier under " + Convert.ToString(Gmean / 2));

            if (temp > 3)
            {
                MessageBox.Show("Feil er detektert");
            }
            else
            {
                MessageBox.Show("Platen har ingen feil");
            }
        }
예제 #8
0
        private void button2_Click(object sender, EventArgs e)
        {
            Orignal = ConvertTo24bpp(CDC.Apply(Orignal, 0.6));


            if (Template.PixelFormat != PixelFormat.Format8bppIndexed)
            {
                Template = Grayscale.CommonAlgorithms.BT709.Apply(Template);
            }
            if (Orignal.PixelFormat != PixelFormat.Format8bppIndexed)
            {
                Orignal = Grayscale.CommonAlgorithms.BT709.Apply(Orignal);
            }

            BradleyLocalThresholding FilterBrad = new BradleyLocalThresholding();

            FilterBrad.WindowSize = 100;
            FilterBrad.PixelBrightnessDifferenceLimit = 0.2f;
            Template = FilterBrad.Apply(Template);
            Orignal  = FilterBrad.Apply(Orignal);
            //TemplatImage.Save( @"D:\Temp.jpg" );
            //SourceImage.Save( @"D:\src.jpg" );
            if (Template.PixelFormat != PixelFormat.Format24bppRgb)
            {
                GrayscaleToRGB FilterRGB = new GrayscaleToRGB();
                Template = FilterRGB.Apply(Template);
            }
            if (Orignal.PixelFormat != PixelFormat.Format24bppRgb)
            {
                GrayscaleToRGB FilterRGB = new GrayscaleToRGB();
                Orignal = FilterRGB.Apply(Orignal);
            }

            int divisor                    = int.Parse(textBox1.Text);
            int CellsizeLength             = int.Parse(textBox2.Text);
            ExhaustiveTemplateMatching etm = new ExhaustiveTemplateMatching(0.1f);

            if (Orignal.PixelFormat != PixelFormat.Format24bppRgb)
            {
                GrayscaleToRGB FilterRGB = new GrayscaleToRGB();
                Orignal = FilterRGB.Apply(Orignal);
            }

            ResizeNearestNeighbor Resize_filter2 = new ResizeNearestNeighbor(Orignal.Width / divisor, Orignal.Height / divisor);
            Bitmap Resize_Org_Image = Resize_filter2.Apply(AForge.Imaging.Image.Clone(Orignal));



            ResizeNearestNeighbor Resize_filter3 = new ResizeNearestNeighbor(Template.Width / divisor, Template.Height / divisor);
            Bitmap Resize_Template = Resize_filter3.Apply(AForge.Imaging.Image.Clone(Template));


            TemplateMatch[] tm = etm.ProcessImage(Resize_Org_Image, Resize_Template);

            if (tm.Length > 0)
            {
                List <IntPoint> cornersRect = new List <IntPoint>
                {
                    new IntPoint(tm[0].Rectangle.X * divisor - CellsizeLength, tm[0].Rectangle.Y * divisor - CellsizeLength),
                    new IntPoint((tm[0].Rectangle.X * divisor) + (tm[0].Rectangle.Width * divisor) + CellsizeLength, tm[0].Rectangle.Y * divisor - CellsizeLength),
                    new IntPoint((tm[0].Rectangle.X * divisor) + (tm[0].Rectangle.Width * divisor) + CellsizeLength, (tm[0].Rectangle.Y * divisor) + (tm[0].Rectangle.Height * divisor) + CellsizeLength),
                    new IntPoint(tm[0].Rectangle.X * divisor - CellsizeLength, (tm[0].Rectangle.Y * divisor) + (tm[0].Rectangle.Height * divisor) + CellsizeLength)
                };
                SimpleQuadrilateralTransformation squadtran = new SimpleQuadrilateralTransformation(cornersRect, Orignal.Width + CellsizeLength * 2, Orignal.Height + CellsizeLength * 2)
                {
                    AutomaticSizeCalculaton = true
                };
                Bitmap ExhaustiveTemplate24bit = squadtran.Apply(AForge.Imaging.Image.Clone(Orignal));
                ExhaustiveTemplate24bit = new ResizeNearestNeighbor(Template.Width, Template.Height).Apply(ExhaustiveTemplate24bit);
                pictureBox3.Image       = ExhaustiveTemplate24bit;
            }
            GC.Collect();
        }