Ejemplo n.º 1
0
        private WriteableBitmap FindPlate(IEnumerable<Rect> rects, WriteableBitmap image)
        {
            WriteableBitmap bestCandidate = null;
            
            foreach (var rect in rects)
            {
                var croppedImage = image.Crop(rect);
                var edgeFilter = new CannyEdgeDetector();
                var smoothFilter = new Median();
                var grayFilter = new Grayscale(0.2125, 0.7154, 0.0721);
                var blobCounter = new BlobCounter();
                var cutTop = croppedImage.PixelHeight * 0.3;

                croppedImage = croppedImage.Crop(new Rect(0, cutTop, croppedImage.PixelWidth, croppedImage.PixelHeight));

                var bitmap = (Bitmap)croppedImage;
                var grayImage = grayFilter.Apply(bitmap);

                bitmap = smoothFilter.Apply(grayImage);
                edgeFilter.ApplyInPlace(bitmap);
                blobCounter.ProcessImage(bitmap);

                var blobs = blobCounter.GetObjectsInformation();
                var possibleChars = new List<Rectangle>();

                foreach (var blob in blobs)
                {
                    var objRectangle = blob.Rectangle;
                    var ratio = (double)objRectangle.Height / (double)objRectangle.Width;
                    
                    if (ratio >= 1.16d && ratio <= 6.3d)
                    {
                        possibleChars.Add(objRectangle);
                    }
                }

                if (possibleChars.Count == 0)
                {
                    continue;
                }

                bestCandidate = croppedImage;
            }

            return bestCandidate;
        }
Ejemplo n.º 2
0
        // =========================================================
        private void Edge_detectFunc(ref Bitmap frame, int par_int, double par_d, int par_R, int par_G, int par_B)
        {
            frame = Grayscale.CommonAlgorithms.RMY.Apply(frame);	// Make gray
            switch (par_int)
            {
                case 1:
                    SobelEdgeDetector SobelFilter = new SobelEdgeDetector();
                    SobelFilter.ApplyInPlace(frame);
                    break;

                case 2:
                    DifferenceEdgeDetector DifferenceFilter = new DifferenceEdgeDetector();
                    DifferenceFilter.ApplyInPlace(frame);
                    break;

                case 3:
                    HomogenityEdgeDetector HomogenityFilter = new HomogenityEdgeDetector();
                    HomogenityFilter.ApplyInPlace(frame);
                    break;

                case 4:
                    CannyEdgeDetector Cannyfilter = new CannyEdgeDetector();
                    // apply the MirrFilter
                    Cannyfilter.ApplyInPlace(frame);
                    break;

                default:
                    HomogenityEdgeDetector filter = new HomogenityEdgeDetector();
                    filter.ApplyInPlace(frame);
                    break;
            }
            GrayscaleToRGB RGBfilter = new GrayscaleToRGB();	// back to color format
            frame = RGBfilter.Apply(frame);
        }
Ejemplo n.º 3
0
        private Bitmap GetEdgedImage(WriteableBitmap writeableBitmap)
        {
            var edgeFilter = new CannyEdgeDetector(255, 0);
            var smoothFilter = new Median();
            var grayFilter = new Grayscale(0.2125, 0.7154, 0.0721);
            var bitmap = (Bitmap)writeableBitmap;

            bitmap = grayFilter.Apply(bitmap);
            smoothFilter.ApplyInPlace(bitmap);
            edgeFilter.ApplyInPlace(bitmap);

            return bitmap;
        }
Ejemplo n.º 4
0
        // =========================================================
        private void Edge_detectFunc(ref Bitmap frame, int par_int)
        {
            frame = Grayscale.CommonAlgorithms.RMY.Apply(frame);	// Make gray
            switch (par_int) {
                case 1:
                    SobelEdgeDetector SobelFilter = new SobelEdgeDetector();
                    SobelFilter.ApplyInPlace(frame);
                    break;

                case 2:
                    DifferenceEdgeDetector DifferenceFilter = new DifferenceEdgeDetector();
                    DifferenceFilter.ApplyInPlace(frame);
                    break;

                case 3:
                    HomogenityEdgeDetector HomogenityFilter = new HomogenityEdgeDetector();
                    HomogenityFilter.ApplyInPlace(frame);
                    break;

                case 4:
                    // can we not have references to canny in the code. gives me ptsd flashbacks
                    CannyEdgeDetector Nightmare = new CannyEdgeDetector();
                    // apply the filter
                    Nightmare.ApplyInPlace(frame);
                    break;

                default:
                    HomogenityEdgeDetector filter = new HomogenityEdgeDetector();
                    filter.ApplyInPlace(frame);
                    break;
            }
            GrayscaleToRGB RGBfilter = new GrayscaleToRGB();	// back to color format
            frame = RGBfilter.Apply(frame);
        }
Ejemplo n.º 5
0
        private void button2_Click(object sender, EventArgs e)
        {
            button2.Text = "处理中";
            switch (comboBox4.SelectedIndex)
            {
            case 0:
            {
                Bitmap      temp    = (Bitmap)pictureBox1.Image;
                OilPainting filter3 = new OilPainting(10);
                // apply the filter
                filter3.ApplyInPlace(temp);
                this.pictureBox2.Image = ResizeBitmap(temp);
                break;
            }

            case 1:
            {
                Bitmap temp = (Bitmap)pictureBox1.Image;
                temp = new Grayscale(0.2125, 0.7154, 0.0721).Apply(temp);
                DifferenceEdgeDetector edgeDetector = new DifferenceEdgeDetector();
                temp = edgeDetector.Apply(temp);
                temp = new Threshold((int)numericUpDown1.Value).Apply(temp);

                //FillHoles filter2 = new FillHoles();
                //filter2.MaxHoleHeight = MinHeight;
                //filter2.MaxHoleWidth = MaxWidth;
                //filter2.CoupledSizeFiltering = false;
                // apply the filter
                //temp = filter2.Apply(temp);
                //HorizontalRunLengthSmoothing hrls = new HorizontalRunLengthSmoothing(40);
                // apply the filter
                //hrls.ApplyInPlace(temp);

                /*AForge.Imaging.Filters.BlobsFiltering filter = new AForge.Imaging.Filters.BlobsFiltering();
                 * // 设置过滤条件(对象长、宽至少为70)
                 * filter.CoupledSizeFiltering = true;
                 * filter.MaxWidth = (int)numericUpDown3.Value;
                 * filter.MaxHeight = (int)numericUpDown4.Value;
                 * filter.MinWidth = (int)numericUpDown5.Value;
                 * filter.MinHeight = (int)numericUpDown6.Value;
                 * filter.ApplyInPlace(temp);*/



                BlobCounter blobCounter = new BlobCounter();

                blobCounter.MinHeight    = 32;
                blobCounter.MinWidth     = 32;
                blobCounter.FilterBlobs  = true;
                blobCounter.ObjectsOrder = ObjectsOrder.Size;

                // 4 - find all stand alone blobs
                blobCounter.ProcessImage(temp);
                Blob[]             blobs        = blobCounter.GetObjectsInformation();
                SimpleShapeChecker shapeChecker = new SimpleShapeChecker();

                List <IntPoint> corners  = null;
                List <IntPoint> corners2 = null;
                for (int i = 0, n = blobs.Length; i < n; i++)
                {
                    List <IntPoint> edgePoints = blobCounter.GetBlobsEdgePoints(blobs[i]);
                    // does it look like a quadrilateral ?
                    if (shapeChecker.IsQuadrilateral(edgePoints, out corners))
                    {
                        // get edge points on the left and on the right side
                        List <IntPoint> leftEdgePoints, rightEdgePoints;
                        blobCounter.GetBlobsLeftAndRightEdges(blobs[i],
                                                              out leftEdgePoints, out rightEdgePoints);
                        listBox1.DataSource = leftEdgePoints;
                        listBox2.DataSource = rightEdgePoints;
                    }
                }
                //listBox1.DataSource = corners;
                //listBox2.DataSource = corners2;
                this.pictureBox1.Image = temp;
                break;
            }

            case 2:
            {
                Bitmap bt2 = new Bitmap(@"D:\TCL条码\截图01.bmp");
                Bitmap bt1 = new Bitmap(@"D:\TCL条码\截图03.bmp");
                //Bitmap bt1 = new Bitmap(pictureBox2.Image);
                ExhaustiveTemplateMatching tm = new ExhaustiveTemplateMatching(0.80f);
                //基于一定的相似性阈值获得匹配块
                TemplateMatch[] matchings = tm.ProcessImage(bt1, bt2);
                BitmapData      data      = bt1.LockBits(
                    new Rectangle(0, 0, bt1.Width, bt1.Height),
                    ImageLockMode.ReadWrite, bt1.PixelFormat);
                foreach (TemplateMatch m in matchings)
                {
                    Drawing.Rectangle(data, m.Rectangle, Color.Red);
                }
                bt1.UnlockBits(data);
                pictureBox2.Image = bt1;
                break;
            }

            case 3:
            {
                Bitmap bt2 = new Bitmap(@"D:\TCL条码\Canny算法.png");
                AForge.Imaging.Filters.BlobsFiltering filter = new AForge.Imaging.Filters.BlobsFiltering();
                // 设置过滤条件(对象长、宽至少为70)
                filter.CoupledSizeFiltering = true;
                filter.MaxWidth             = (int)numericUpDown3.Value;
                filter.MaxHeight            = (int)numericUpDown4.Value;
                filter.MinWidth             = (int)numericUpDown5.Value;
                filter.MinHeight            = (int)numericUpDown6.Value;
                filter.ApplyInPlace(bt2);
                pictureBox1.Image = bt2;
                byte[] RESULT = BitmapToBytes(bt2);
                break;
            }

            case 4:
            {
                Bitmap temp = (Bitmap)pictureBox1.Image;
                temp = new Grayscale(0.2125, 0.7154, 0.0721).Apply(temp);
                AForge.Imaging.Filters.CannyEdgeDetector filter = new AForge.Imaging.Filters.CannyEdgeDetector();
                filter.ApplyInPlace(temp);
                pictureBox2.Image = temp;
                break;
            }
            }

            button2.Text = "处理";
        }
Ejemplo n.º 6
0
        private void videoNewFrame(object sender, NewFrameEventArgs args)
        {
            Bitmap temp = args.Frame.Clone() as Bitmap;

            switch (comboBox3.SelectedIndex)
            {
            case 0:

                break;

            case 1:
                temp = new Grayscale(0.2125, 0.7154, 0.0721).Apply(temp);
                break;

            case 2:
                temp = new Grayscale(0.2125, 0.7154, 0.0721).Apply(temp);
                temp = new Threshold((int)numericUpDown1.Value).Apply(temp);
                break;

            case 3:
                temp = new Grayscale(0.2125, 0.7154, 0.0721).Apply(temp);
                AForge.Imaging.Filters.CannyEdgeDetector filter = new AForge.Imaging.Filters.CannyEdgeDetector();
                filter.ApplyInPlace(temp);
                break;

            case 4:
                for (int i = 0; i < (int)numericUpDown2.Value; i++)
                {
                    temp = new Dilatation().Apply(temp);
                }
                break;

            case 5:
                for (int i = 0; i < (int)numericUpDown2.Value; i++)
                {
                    temp = new Erosion().Apply(temp);
                }
                break;

            case 6:
                temp = new Grayscale(0.2125, 0.7154, 0.0721).Apply(temp);
                AForge.Imaging.Filters.BradleyLocalThresholding filter1 = new AForge.Imaging.Filters.BradleyLocalThresholding();
                filter1.ApplyInPlace(temp);
                break;
            }

            switch (comboBox2.SelectedIndex)
            {
            case 0:
            {
                BarcodeReader reader = new BarcodeReader();
                reader.Options.CharacterSet = "UTF-8";
                Result result = reader.Decode(temp);
                if (result != null)
                {
                    if (wait == false)
                    {
                        MessageBox.Show(result.ToString());
                        wait = true;
                    }
                }
                else
                {
                    wait = false;
                }
                break;
            }

            case 1:
            {
                /*Bitmap pImg = MakeGrayscale3((Bitmap)temp);
                 * using (ZBar.ImageScanner scanner = new ZBar.ImageScanner())
                 * {
                 *  scanner.SetConfiguration(ZBar.SymbolType.None, ZBar.Config.Enable, 0);
                 *  scanner.SetConfiguration(ZBar.SymbolType.CODE39, ZBar.Config.Enable, 1);
                 *  scanner.SetConfiguration(ZBar.SymbolType.CODE128, ZBar.Config.Enable, 1);
                 *
                 *  List<ZBar.Symbol> symbols = new List<ZBar.Symbol>();
                 *  symbols = scanner.Scan((System.Drawing.Image)pImg);
                 *  if (symbols != null && symbols.Count > 0)
                 *  {
                 *      string result = string.Empty;
                 *      symbols.ForEach(s => result += s.Data);
                 *      if (wait == false)
                 *      {
                 *          MessageBox.Show(result);
                 *          wait = true;
                 *      }
                 *  }
                 *  else
                 *      wait = false;
                 * }*/
                break;
            }

            case 2:
            {
                break;
            }

            case 3:
                break;
            }
            this.pictureBox1.Image = ResizeBitmap(temp);
        }