示例#1
0
        public Bitmap ExtractHandContour(Bitmap img)
        {
            // create filter to remove noise (small blobs)
            BlobsFiltering filterB = new BlobsFiltering();

            // configure filter
            //filterB.CoupledSizeFiltering = true;
            filterB.MinWidth  = 200;
            filterB.MinHeight = 400;
            // apply the filter


            this.imgHandContour = filterB.Apply(img);
            this.imgHandContour = this.Invert();
            int iVerticalOffset = 150; //cut bottom noise

            Bitmap newImg = new Bitmap(this.imgHandContour.Width, this.imgHandContour.Height - iVerticalOffset, System.Drawing.Imaging.PixelFormat.Format24bppRgb);

            for (int x = 0; x < this.imgHandContour.Width; x++)
            {
                for (int y = 0; y < this.imgHandContour.Height - iVerticalOffset; y++)
                {
                    //if(this.imgHandContour.GetPixel(x,y).Name !=  "ffffffff")
                    newImg.SetPixel(x, y, this.imgHandContour.GetPixel(x, y));
                }
            }
            return(newImg);

            //return imgHandContour;
        }
示例#2
0
        public static Bitmap ProcessBlob(Bitmap img)
        {
            BlobCounterBase bc = new BlobCounter();

            // set filtering options
            bc.FilterBlobs = true;
            bc.MinWidth    = 2;
            bc.MinHeight   = 2;
            // set ordering options
            bc.ObjectsOrder = ObjectsOrder.Size;
            // process binary image
            bc.ProcessImage(Grayscale.CommonAlgorithms.BT709.Apply(img));
            Blob[] blobs = bc.GetObjectsInformation();
            // extract the biggest blob
            if (blobs.Length > 0)
            {
                bc.ExtractBlobsImage(img, blobs[0], true);
                // create filter
                BlobsFiltering filter = new BlobsFiltering();
                // apply the filter
                Bitmap biggestBlobsImage = filter.Apply(img);
                return(biggestBlobsImage);
            }
            return(img);
        }
示例#3
0
        public static Bitmap BlobsFiltering(Bitmap Imagem)
        {
            BlobsFiltering filter = new BlobsFiltering();

            Imagem = Imagem.Clone(new Rectangle(0, 0, Imagem.Width, Imagem.Height), System.Drawing.Imaging.PixelFormat.Format24bppRgb);
            Imagem = filter.Apply(Imagem);
            return(Imagem);
        }
示例#4
0
        private Bitmap ProcessImageBlobsFiltering(Bitmap img)
        {
            BlobsFiltering blobsFiltering = new BlobsFiltering();

            blobsFiltering.CoupledSizeFiltering = true;
            blobsFiltering.MinWidth             = minWidth;
            blobsFiltering.MinHeight            = minHeight;
            return(blobsFiltering.Apply(img));
        }
示例#5
0
        //blob filter
        private void blob()
        {
            Bitmap         gambar = (Bitmap)crop_image.Image;
            BlobsFiltering filter = new BlobsFiltering();

            filter.CoupledSizeFiltering = true;
            filter.MinWidth             = 100;
            filter.MinHeight            = 150;
            crop_image.Image            = filter.Apply(gambar);
        }
示例#6
0
        private Bitmap ProcessCurrentFrame()
        {
            Bitmap bmp;

            //curr_gs = pixelFilter.Apply(curr_gs);

            if (first)
            {
                back  = (Bitmap)curr_gs.Clone();
                first = false;
            }

            /* Detection */

            diffFilter.OverlayImage = back;
            bmp = diffFilter.Apply(curr_gs);
            bmp = thresholdFilter.Apply(bmp);
            bmp = blobFilter.Apply(bmp);
            //bmp = erosionFilter.Apply(bmp);
            //bmp = openFilter.Apply(bmp);
            //bmp = edgeFilter.Apply(bmp);

            try
            {
                //blobCount.Text = blobGrabber.Apply(bmp).Size.ToString();
            }
            catch (Exception)
            {
            }


            /* Transcribe to original image */

            // extract red channel from the original image
            //IFilter extrachChannel = new ExtractChannel(RGB.R);
            //Bitmap redChannel = extrachChannel.Apply(curr_c);
            //  merge red channel with motion regions
            //Merge mergeFilter = new Merge();
            //mergeFilter.OverlayImage = bmp;
            //Bitmap tmp4 = mergeFilter.Apply(redChannel);
            // replace red channel in the original image
            //ReplaceChannel replaceChannel = new ReplaceChannel(RGB.R,tmp4);
            //curr_c = replaceChannel.Apply(curr_c);

            /* Background Update */
            //towardsFilter.OverlayImage = curr_gs;
            //back = towardsFilter.Apply(back);
            morphFilter.OverlayImage = curr_gs;
            back = morphFilter.Apply(back);


            return(bmp);
        }
示例#7
0
        public List <Bitmap> PreHandle2(Bitmap original)
        {
            var preOcr = new FiltersSequence(
                Grayscale.CommonAlgorithms.RMY,
                new BradleyLocalThresholding());

            var grayscale = preOcr.Apply(original);

            GrayImg.Source = Bitmap2BitmapImageSource(grayscale);

            var filter = new BlobsFiltering
            {
                CoupledSizeFiltering = true,
                MinHeight            = 6,
                MinWidth             = 6
            };
            var filterImage = filter.Apply(new Invert().Apply(grayscale));

            ThresholdImg.Source = Bitmap2BitmapImageSource(filterImage);

            var invertImg = new Invert().Apply(filterImage);

            DoneImg.Source = Bitmap2BitmapImageSource(invertImg);

            var labels = new ConnectedComponentsLabeling();

            labels.BlobCounter.ObjectsOrder = ObjectsOrder.XY;
            labels.Apply(filterImage);

            var bitmaps = new List <Bitmap>();

            for (int i = 0; i < labels.ObjectCount; i++)
            {
                var candidate = labels.BlobCounter.GetObjectsInformation()[i];
                var edgePoint = labels.BlobCounter.GetBlobsEdgePoints(candidate);
                labels.BlobCounter.ExtractBlobsImage(filterImage, candidate, false);
                if (candidate.Image != null)
                {
                    var charcter   = new Invert().Apply(candidate.Image.ToManagedImage());
                    var resizer    = new ResizeBilinear(10, 10);
                    var finalImage = resizer.Apply(charcter);
                    bitmaps.Add(finalImage);

                    TesseractEngine a = new TesseractEngine(@"./tessdata", "eng", EngineMode.TesseractAndCube);
                    Page            p = a.Process(finalImage);
                    TBlock.Text += p.GetText();
                }
            }
            return(bitmaps);
        }
示例#8
0
        private void button5_Click(object sender, EventArgs e)
        {
            BlobFilter.MaxHeight = Convert.ToInt16(textBox2.Text.ToString());
            BlobFilter.MinHeight = Convert.ToInt16(textBox3.Text.ToString());
            BlobFilter.MaxWidth  = Convert.ToInt16(textBox4.Text.ToString());
            BlobFilter.MinWidth  = Convert.ToInt16(textBox5.Text.ToString());

            BlobBmp = BlobFilter.Apply(BinBmp);
            BlobCountFilter.ProcessImage(BlobBmp);
            BlobArray = BlobCountFilter.GetObjects(BlobBmp, true); //apply blob counting

            process.Image = BlobBmp;
            this.Text    += ";" + BlobBmp.Width.ToString();
            //this.Text = BlobArray[0].Area.ToString();
        }
        private void extractLargestBlob()
        {
            ExtractBiggestBlob biggestBlob = new ExtractBiggestBlob();
            Bitmap             newImage    = biggestBlob.Apply(imageGot);

            int newImHeight = newImage.Height;
            int newImWidth  = newImage.Width;
            // create filter
            BlobsFiltering filter = new BlobsFiltering();

            filter.CoupledSizeFiltering = true;
            filter.MinWidth             = newImHeight;
            filter.MinHeight            = newImWidth;
            imageGot          = filter.Apply(imageGot);
            pictureBox1.Image = imageGot;
        }
        //verilen sınır değerlerden küçük şekilleri göz ardı eder
        private void button5_Click(object sender, EventArgs e)
        {
            //filtre oluşturuldu
            BlobsFiltering filtre = new BlobsFiltering();

            //filtre ayarları yapıldı
            filtre.CoupledSizeFiltering = true;
            filtre.MinWidth             = 70;
            filtre.MinHeight            = 70;

            //burada bazıları ApplyInPlace() fonksiyonunu kullanıyor
            //ben Apply() fonksiyonunu kullanıyorum
            //ApplyInPlace() fonksiyonunu daha önce hiç kullanmadım, nasıl çalıştığınıda çözemedim zaten:)
            Bitmap newImage = filtre.Apply((Bitmap)pictureBox1.Image);

            pictureBox1.Image = newImage;
        }
示例#11
0
        private void blobToolStripMenuItem_Click(object sender, EventArgs e)
        {
            BlobsFiltering filter = new BlobsFiltering();

            pictureBox2.Image = filter.Apply((Bitmap)pictureBox1.Image);
        }
        public Bitmap FilterBloobs(Bitmap bitmap, int minWidth, int minHeight)
        {
            BlobsFiltering filter = new BlobsFiltering(minWidth, minHeight, int.MaxValue, int.MaxValue);

            return(ReverseBitmapColors(filter.Apply(ReverseBitmapColors(bitmap))));
        }
示例#13
0
        public RecognizeResult Recognize(RecoginizeData data)
        {
            try
            {
                using (var src = data.CardsBitmap.Deserialize() as Bitmap)
                {
                    if (src == null)
                    {
                        throw new InvalidDataException(string.Format("待识别扑克的图片数据反序列化失败!"));
                    }
                    var result = new RecognizeResult
                    {
                        Result = new List <Card>()
                    };

                    //过滤蓝色、灰度化、二值化
                    var toRec = _seq.Apply(src);

                    //黑色与白色互换
                    ExchangeIndexColor(toRec, 255, 0);

                    //去掉干扰线
                    RemoveInterferenceLines(toRec, 0);

                    //过滤噪音
                    toRec = _blobsFiltering.Apply(toRec);

                    //获取所有的块
                    _blobCounter.ProcessImage(toRec);

                    //去掉偏移的块区域
                    var rects = RemoveOffsetBlobs(_blobCounter.GetObjectsRectangles().ToList());

                    //合并区域
                    rects = MergeBlobs(rects);

                    //识别扑克牌数字
                    rects.ForEach(rectNum =>
                    {
                        //识别牌数字
                        var bmpNum  = _resizeNumFilter.Apply(toRec.Clone(rectNum, toRec.PixelFormat));
                        var cardNum = _cardNetworkService.ComputeNum(bmpNum);
                        if (cardNum == CardNum.未知)
                        {
                            var dir = AppDomain.CurrentDomain.BaseDirectory + @"未知\数字\";
                            Directory.CreateDirectory(dir);
                            bmpNum.Save(dir + string.Format("{0}.bmp", DateTime.Now.ToString("yyyyMMddHHmmssffff")));
                        }

                        //识别牌花色(花色图片需要重新处理,所以需要用原图src)
                        var bmpColor  = GetColorBitmap(src, rectNum);
                        var cardColor = _cardNetworkService.ComputeColor(bmpColor);
                        if (cardColor == CardColor.未知)
                        {
                            var dir = AppDomain.CurrentDomain.BaseDirectory + @"未知\花色\";
                            Directory.CreateDirectory(dir);
                            bmpColor.Save(dir + string.Format("{0}.bmp", DateTime.Now.ToString("yyyyMMddHHmmssffff")));
                        }

                        //将识别出来的牌信息添加到结果集中
                        result.Result.Add(new Card
                        {
                            Num   = cardNum,
                            Color = cardColor,
                            Rect  = rectNum
                        });
                    });

                    GC.Collect();

                    return(result);
                }
            }
            catch (Exception ex)
            {
                throw new FaultException((ex.InnerException ?? ex).ToString());
            }
        }
示例#14
0
        private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
        {
            loaded            = new Bitmap(openFileDialog1.FileName);
            pictureBox1.Image = loaded;

            processed = new Bitmap(openFileDialog1.FileName);

            GrayscaleBT709 g = new GrayscaleBT709();

            processed = g.Apply((Bitmap)processed);

            Threshold t = new Threshold(128);

            processed = t.Apply((Bitmap)processed);

            BlobsFiltering b = new BlobsFiltering();

            b.MinWidth             = 147;
            b.MinHeight            = 147;
            b.CoupledSizeFiltering = true;
            processed = b.Apply((Bitmap)processed);

            HomogenityEdgeDetector h = new HomogenityEdgeDetector();

            processed = h.Apply((Bitmap)processed);

            pictureBox2.Image = processed;

            BlobCounterBase bc = new BlobCounter();

            bc.ProcessImage(processed);
            Blob[] blobs = bc.GetObjectsInformation();


            //5 cents 7 pieces 700-800 pixels
            //10 cents 11 pieces 801-1000 pixels
            //25 cents 28 pieces 1001-1200 pixels
            //1 peso 13 pieces 1200-1500 pixels
            //5 peso 5 pieces 1501-1700 pixels

            int paybcents = 0, tencents = 0, twenypaybcents = 0, wanpeso = 0, paybpeso = 0;

            foreach (Blob blob in blobs)
            {
                if (blob.Area >= 700 && blob.Area <= 800)
                {
                    paybcents++;
                }
                else if (blob.Area >= 801 && blob.Area <= 1000)
                {
                    tencents++;
                }
                else if (blob.Area >= 1001 && blob.Area <= 1200)
                {
                    twenypaybcents++;
                }
                else if (blob.Area >= 1200 && blob.Area <= 1500)
                {
                    wanpeso++;
                }
                else if (blob.Area >= 1501 && blob.Area <= 1700)
                {
                    paybpeso++;
                }
            }
            double paybcentsbalyo      = paybcents * 0.05;
            double tencentsbalyo       = tencents * 0.1;
            double twenypaybcentsbalyo = twenypaybcents * 0.25;
            double wanpesobalyo        = wanpeso * 1;
            double paybpesobalyo       = paybpeso * 5;

            double total = paybcentsbalyo + tencentsbalyo + twenypaybcentsbalyo + wanpesobalyo + paybpesobalyo;

            MessageBox.Show("5 cents: " + paybcents + " pieces value: " + paybcentsbalyo + "\n10 cents: " + tencents + " pieces value: " + tencentsbalyo + "\n25 cents: " + twenypaybcents + " pieces value:" + twenypaybcentsbalyo + "\n1 peso: " + wanpeso + " pieces value:" + wanpesobalyo + "\n5 peso: " + paybpeso + " pieces value:" + paybpesobalyo + "\nTotal Value: " + total);
        }
示例#15
0
        public static Tuple <Bitmap, Bitmap, Bitmap, Bitmap, List <Bitmap> > blob(Bitmap img, Interval w, Interval h, Boolean couple)
        {
            Bitmap sourceImage = (Bitmap)(img);

            ////////////////////////////////////////////////////

            BlobCounter blobCounter = new BlobCounter();

            blobCounter.FilterBlobs = true;
            blobCounter.MinHeight   = (int)h.Min;
            blobCounter.MaxHeight   = (int)h.Max;
            blobCounter.MinWidth    = (int)w.Min;
            blobCounter.MaxWidth    = (int)w.Max;

            blobCounter.CoupledSizeFiltering = couple;

            //blobCounter.BackgroundThreshold.

            blobCounter.ProcessImage(sourceImage);
            Blob[] blobs = blobCounter.GetObjectsInformation();
            //bitmap.UnlockBits(sourceImage);

            ////////////////////////////////////////////////////
            Bitmap newBlobImg = (Bitmap)(sourceImage).Clone();
            // create filter
            BlobsFiltering filterBlob = new BlobsFiltering();

            // configure filter
            filterBlob.CoupledSizeFiltering = couple;
            filterBlob.MinHeight            = (int)h.Min;
            filterBlob.MaxHeight            = (int)h.Max;
            filterBlob.MinWidth             = (int)w.Min;
            filterBlob.MaxWidth             = (int)w.Max;
            // apply the filter
            newBlobImg = filterBlob.Apply(newBlobImg);

            ////////////////////////////////////////////////////

            Bitmap rectImage = (Bitmap)(newBlobImg).Clone();

            GrayscaleToRGB convertToColor = new GrayscaleToRGB();

            if (rectImage.PixelFormat == System.Drawing.Imaging.PixelFormat.Format8bppIndexed)
            {
                rectImage = convertToColor.Apply(newBlobImg);
            }

            Graphics g = Graphics.FromImage(rectImage);

            Pen myPen = new Pen(Color.Red, 1);

            List <Bitmap> blobIMGs = new List <Bitmap>();

            for (int i = 0; i < blobs.Length; i++)
            {
                blobIMGs.Add((Bitmap)(newBlobImg).Clone(blobs[i].Rectangle, PixelFormat.Format32bppArgb));
                g.DrawRectangle(myPen, blobs[i].Rectangle);
            }

            ////////////////////////////////////////////////////


            Bitmap colorImage = (Bitmap)(newBlobImg).Clone();

            ConnectedComponentsLabeling filter = new ConnectedComponentsLabeling();

            // apply the filter
            colorImage = filter.Apply(colorImage);
            //////////////////////////////////////////////////

            SimpleShapeChecker shapeChecker = new SimpleShapeChecker();
            Bitmap             shapeImage   = (Bitmap)(newBlobImg).Clone();
            Graphics           gg           = Graphics.FromImage(shapeImage);
            Pen yellowPen = new Pen(Color.Yellow, 2); // circles
            Pen redPen    = new Pen(Color.Red, 2);    // quadrilateral
            Pen brownPen  = new Pen(Color.Brown, 2);  // quadrilateral with known sub-type
            Pen greenPen  = new Pen(Color.Green, 2);  // known triangle
            Pen bluePen   = new Pen(Color.Blue, 2);   // triangle

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

                AForge.Point center;
                float        radius;

                // is circle ?
                if (shapeChecker.IsCircle(edgePoints, out center, out radius))
                {
                    gg.DrawEllipse(yellowPen,
                                   (float)(center.X - radius), (float)(center.Y - radius),
                                   (float)(radius * 2), (float)(radius * 2));
                }
                else
                {
                    List <IntPoint> corners;

                    // is triangle or quadrilateral
                    if (shapeChecker.IsConvexPolygon(edgePoints, out corners))
                    {
                        // get sub-type
                        PolygonSubType subType = shapeChecker.CheckPolygonSubType(corners);

                        Pen pen;

                        if (subType == PolygonSubType.Unknown)
                        {
                            pen = (corners.Count == 4) ? redPen : bluePen;
                        }
                        else
                        {
                            pen = (corners.Count == 4) ? brownPen : greenPen;
                        }

                        gg.DrawPolygon(pen, ToPointsArray(corners));
                    }
                }
            }



            return(new Tuple <Bitmap, Bitmap, Bitmap, Bitmap, List <Bitmap> >(newBlobImg, colorImage, rectImage, shapeImage, blobIMGs));
        }