public Bitmap Apply(BitmapData imageData)
        {
            if (!this.FormatTranslations.ContainsKey(imageData.PixelFormat))
            {
                throw new UnsupportedImageFormatException("Source pixel format is not supported by the filter.");
            }
            BlobCounter counter = new BlobCounter(imageData);

            Blob[] objectsInformation = counter.GetObjectsInformation();
            int    num    = 0;
            Blob   blob   = null;
            int    index  = 0;
            int    length = objectsInformation.Length;

            while (index < length)
            {
                int num4 = objectsInformation[index].Rectangle.Width * objectsInformation[index].Rectangle.Height;
                if (num4 > num)
                {
                    num  = num4;
                    blob = objectsInformation[index];
                }
                index++;
            }
            if (blob == null)
            {
                throw new ArgumentException("The source image does not contain any blobs.");
            }
            this.blobPosition = new IntPoint(blob.Rectangle.Left, blob.Rectangle.Top);
            if (this.originalImage == null)
            {
                counter.ExtractBlobsImage(new UnmanagedImage(imageData), blob, false);
            }
            else
            {
                if ((((this.originalImage.PixelFormat != PixelFormat.Format24bppRgb) && (this.originalImage.PixelFormat != PixelFormat.Format32bppArgb)) && ((this.originalImage.PixelFormat != PixelFormat.Format32bppRgb) && (this.originalImage.PixelFormat != PixelFormat.Format32bppPArgb))) && (this.originalImage.PixelFormat != PixelFormat.Format8bppIndexed))
                {
                    throw new UnsupportedImageFormatException("Original image may be grayscale (8bpp indexed) or color (24/32bpp) image only.");
                }
                if ((this.originalImage.Width != imageData.Width) || (this.originalImage.Height != imageData.Height))
                {
                    throw new InvalidImagePropertiesException("Original image must have the same size as passed source image.");
                }
                counter.ExtractBlobsImage(this.originalImage, blob, false);
            }
            Bitmap bitmap = blob.Image.ToManagedImage();

            blob.Image.Dispose();
            return(bitmap);
        }
Example #2
0
        public Bitmap Apply(BitmapData imageData)
        {
            if (!FormatTranslations.ContainsKey(imageData.PixelFormat))
            {
                throw new UnsupportedImageFormatException("Source pixel format is not supported by the filter.");
            }
            BlobCounter blobCounter = new BlobCounter(imageData);

            Blob[] objectsInformation = blobCounter.GetObjectsInformation();
            int    num  = 0;
            Blob   blob = null;
            int    i    = 0;

            for (int num2 = objectsInformation.Length; i < num2; i++)
            {
                int num3 = objectsInformation[i].Rectangle.Width * objectsInformation[i].Rectangle.Height;
                if (num3 > num)
                {
                    num  = num3;
                    blob = objectsInformation[i];
                }
            }
            if (blob == null)
            {
                throw new ArgumentException("The source image does not contain any blobs.");
            }
            blobPosition = new IntPoint(blob.Rectangle.Left, blob.Rectangle.Top);
            if (originalImage == null)
            {
                blobCounter.ExtractBlobsImage(new UnmanagedImage(imageData), blob, extractInOriginalSize: false);
            }
            else
            {
                if (originalImage.PixelFormat != PixelFormat.Format24bppRgb && originalImage.PixelFormat != PixelFormat.Format32bppArgb && originalImage.PixelFormat != PixelFormat.Format32bppRgb && originalImage.PixelFormat != PixelFormat.Format32bppPArgb && originalImage.PixelFormat != PixelFormat.Format8bppIndexed)
                {
                    throw new UnsupportedImageFormatException("Original image may be grayscale (8bpp indexed) or color (24/32bpp) image only.");
                }
                if (originalImage.Width != imageData.Width || originalImage.Height != imageData.Height)
                {
                    throw new InvalidImagePropertiesException("Original image must have the same size as passed source image.");
                }
                blobCounter.ExtractBlobsImage(originalImage, blob, extractInOriginalSize: false);
            }
            Bitmap result = blob.Image.ToManagedImage();

            blob.Image.Dispose();
            return(result);
        }
Example #3
0
        // U ovom zadatku koristi se aforge biblioteka za obradu slika u C# jeziku
        // Za zadanu sliku rjesenje je 5.
        // Rezultatna slika nalazi se na web adresi:
        // http://imageshack.us/scaled/landing/221/merged.png
        public static Bitmap FindObjects(System.Drawing.Bitmap image, int hueMin, int hueMax, float saturationMin, float saturationMax, float luminanceMin, float luminanceMax, int blobWidth, int blobHeight)
        {
            // HSL filter
            HSLFiltering filter = new HSLFiltering();

            // Zelimo zadrzati samo crvenu
            filter.Hue        = new IntRange(hueMin, hueMax);
            filter.Saturation = new Range(saturationMin, saturationMax);
            filter.Luminance  = new Range(luminanceMin, luminanceMax);

            // primjenimo filter
            filter.ApplyInPlace(image);

            // algoritam za trazenje nakupina (eng. blob)
            BlobCounterBase bc = new BlobCounter();

            // postavljamo filter za karakteristike nakupina koje trazimo
            bc.FilterBlobs = true;
            // Trazimo da budu odredene velicine kako bi uklonili sum na slici
            bc.MinWidth     = blobWidth;
            bc.MinHeight    = blobHeight;
            bc.ObjectsOrder = ObjectsOrder.Size;


            Bitmap redBlobs = image;

            bc.ProcessImage(redBlobs);

            //Trazimo crvene nakupine, tj crvene objekte na slici
            Blob[] blobs = bc.GetObjectsInformation();
            //Za zadanu sliku nasli smo ih pet, i spremljeni su u polje blobs, duzina polja je broj crvenih objekata na slici
            //Console.WriteLine(blobs.Length);
            bc.ExtractBlobsImage(redBlobs, blobs[0], true);

            //DODATNO: Radimo sliku samo sa pronadenim objektima, ovo nam sluzi samo kao provjera
            Merge  roses       = new Merge();
            Bitmap resultImage = blobs[0].Image.ToManagedImage();

            for (int j = 1; j < blobs.Length; j++)
            {
                bc.ExtractBlobsImage(redBlobs, blobs[j], true);
                roses.OverlayImage = blobs[j].Image.ToManagedImage();
                resultImage        = roses.Apply(resultImage);
            }
            //Odkomentirati sljedecu liniju, kako bi slika sa pronadenim objektima bila spremljena na disk, te se onda moze pregledati
            //resultImage.Save(@"C:\merged.jpg");
            return(resultImage);
        }
        public List <Spot> DetectBlobs()
        {
            BlobCounterBase counter = new BlobCounter {
                FilterBlobs  = true,
                MinHeight    = minHeight,
                MinWidth     = minWidth,
                ObjectsOrder = ObjectsOrder.Area
            };

            counter.ProcessImage(image);
            Blob[] blobs = counter.GetObjectsInformation();

            List <Spot> spots = new List <Spot>();

            foreach (Blob blob in blobs)
            {
                counter.ExtractBlobsImage(image, blob, false);
                Point position = new Point(blob.Rectangle.X, blob.Rectangle.Y);
                int   width    = blob.Rectangle.Width;
                int   heigth   = blob.Rectangle.Height;

                Spot spot = new Spot(blob.Image.ToManagedImage(), position, width, heigth, blob.Area, counter.GetBlobsEdgePoints(blob));
                spots.Add(spot);
            }

            return(spots);
        }
Example #5
0
 public override Task extractContourns()
 {
     return(Task.Run(delegate(){
         this.rgb = this.rgb.Resize(this.preferredSize.Width, this.preferredSize.Height, Emgu.CV.CvEnum.Inter.Linear, true);
         this.mask = this.mask.Resize(this.preferredSize.Width, this.preferredSize.Height, Emgu.CV.CvEnum.Inter.Linear, true);
         Bitmap maskAsBitmap = mask.ToBitmap();
         Bitmap rgbAsBitmap = new Bitmap(this.rgb.ToBitmap());
         var dt = new BinaryWatershed(0.5f, DistanceTransformMethod.Euclidean);
         Bitmap output = dt.Apply(maskAsBitmap);
         BlobCounter bc = new BlobCounter();
         bc.ObjectsOrder = ObjectsOrder.Area;
         bc.ProcessImage(output);
         Blob[] blobs = bc.GetObjectsInformation();
         foreach (Blob blob in blobs)
         {
             if (blob.Area > 100)
             {
                 List <Accord.IntPoint> border = bc.GetBlobsEdgePoints(blob);
                 Point[] borderPoints = border.Select(pt => new Point(pt.X, pt.Y)).ToArray();
                 bc.ExtractBlobsImage(rgbAsBitmap, blob, false);
                 //this.rgb.Draw(new Cross2DF(new PointF(blob.CenterOfGravity.X, blob.CenterOfGravity.Y), 15, 15), new Bgr(0, 255, 0), 3);
                 this.pollenGrains.Add(new PollenGrain(blob));
             }
         }
     }));
 }
Example #6
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);
        }
Example #7
0
        /// <summary>
        /// Process a new video frame.
        /// </summary>
        public void ProcessFrame(UnmanagedImage frame)
        {
            filterImage = filter.Apply(frame);

            Blob blob = extractBlob();

            if (blob == null)
            {
                trackingObject.Reset();
                return;
            }


            trackingObject.Rectangle = blob.Rectangle;
            trackingObject.Center    = (IntPoint)blob.CenterOfGravity;

            if (rotation)
            {
                // Locate moments
                CentralMoments moments = new CentralMoments();
                moments.Compute(filterImage, blob.Rectangle);
                trackingObject.Angle = moments.GetOrientation();
            }


            if (extract)
            {
                blobCounter.ExtractBlobsImage(filterImage, blob, false);
                trackingObject.Image = blob.Image;
            }
        }
        public void FindContourTest2()
        {
            Bitmap bmp = Properties.Resources.hand2;

            BlobCounter bc = new BlobCounter(bmp);
            bc.ObjectsOrder = ObjectsOrder.Size;
            Blob[] blobs = bc.GetObjectsInformation();
            bc.ExtractBlobsImage(bmp, blobs[0], true);
            List<IntPoint> expected = bc.GetBlobsEdgePoints(blobs[0]);
            Bitmap blob = blobs[0].Image.ToManagedImage();

            BorderFollowing bf = new BorderFollowing();
            List<IntPoint> actual = bf.FindContour(blob);

            foreach (IntPoint point in expected)
                Assert.IsTrue(actual.Contains(point));

            IntPoint prev = actual[0];
            for (int i = 1; i < actual.Count; i++)
            {
                IntPoint curr = actual[i];
                Assert.IsTrue(System.Math.Abs(prev.X - curr.X) <= 1 &&
                              System.Math.Abs(prev.Y - curr.Y) <= 1);
                prev = curr;
            }

            IntPoint first = actual[0];
            IntPoint last = actual[actual.Count - 1];
            Assert.IsTrue(System.Math.Abs(first.X - last.X) <= 1 &&
                          System.Math.Abs(first.Y - last.Y) <= 1);
        }
Example #9
0
        public void CalculateBlobs()
        {
            blobCounter             = new BlobCounter();
            blobCounter.FilterBlobs = true;

            if (MinWidth > 0)
            {
                blobCounter.MinWidth = MinWidth;
            }
            if (MaxWidth > 0)
            {
                blobCounter.MaxWidth = MaxWidth;
            }

            if (MinHeight > 0)
            {
                blobCounter.MinHeight = MinHeight;
            }
            if (MaxHeight > 0)
            {
                blobCounter.MaxHeight = MaxHeight;
            }

            blobCounter.CoupledSizeFiltering = Coupled;
            blobCounter.BackgroundThreshold  = BackgroundColor;

            blobCounter.ProcessImage(bitmap);
            blobObjects = blobCounter.GetObjectsInformation().ToList();
            foreach (Blob blob in blobObjects)
            {
                blobCounter.ExtractBlobsImage(bitmap, blob, MaintainSize);
            }
        }
Example #10
0
        public IEnumerable <Bitmap> Apply(Bitmap bitmap)
        {
            // assuming scanned background is white we need to invert for the algo to work
            var copy = new Invert().Apply(bitmap);

            copy = EnsureGrayscale(copy);
            new Threshold {
                ThresholdValue = 25
            }.ApplyInPlace(copy);
            new FillHoles().ApplyInPlace(copy);

            var blobCounter = new BlobCounter
            {
                // set filtering options
                FilterBlobs = true,
                MinWidth    = 50,
                MinHeight   = 50,
            };

            blobCounter.ProcessImage(copy);
            var blobs = blobCounter.GetObjectsInformation();

            if (blobs.Any())
            {
                var invertedOriginal = new Invert().Apply(bitmap);
                foreach (var blob in blobs)
                {
                    // use inverted source to ensure correct edge colors
                    blobCounter.ExtractBlobsImage(invertedOriginal, blob, false);
                    var blobImage = blob.Image.ToManagedImage();

                    // straighten
                    var angle          = new DocumentSkewChecker().GetSkewAngle(EnsureGrayscale(blobImage));
                    var rotationFilter = new RotateBilinear(-angle)
                    {
                        FillColor = Color.Black
                    };
                    blobImage = rotationFilter.Apply(blobImage);

                    // crop
                    blobImage = new ExtractBiggestBlob().Apply(blobImage);

                    new Invert().ApplyInPlace(blobImage);
                    yield return(blobImage);
                }
            }
            else
            {
                yield return(bitmap);
            }
        }
Example #11
0
        public static Bitmap GetBlobObject(this Bitmap iBitmap)
        {
            BlobCounterBase bc = new BlobCounter();

            bc.FilterBlobs  = true;
            bc.MinWidth     = 1;
            bc.MinHeight    = 1;
            bc.ObjectsOrder = ObjectsOrder.Size;
            bc.ProcessImage(iBitmap);
            Blob[] blobs = bc.GetObjectsInformation();
            if (blobs.Length > 0)
            {
                bc.ExtractBlobsImage(iBitmap, blobs[0], false);
            }
            return(blobs[0].Image.ToManagedImage());
        }
Example #12
0
        public void FindContourTest()
        {
            Bitmap bmp = Properties.Resources.sample_black;

            Bitmap gray = AForge.Imaging.Filters.Grayscale.CommonAlgorithms.BT709.Apply(bmp);

            BlobCounter bc = new BlobCounter(gray);

            bc.ObjectsOrder = ObjectsOrder.Size;
            Blob[] blobs = bc.GetObjectsInformation();
            bc.ExtractBlobsImage(bmp, blobs[0], true);
            List <IntPoint> expected = bc.GetBlobsEdgePoints(blobs[0]);
            Bitmap          blob     = blobs[0].Image.ToManagedImage();

            BorderFollowing bf     = new BorderFollowing();
            List <IntPoint> actual = bf.FindContour(blob);

            Assert.AreEqual(expected.Count, actual.Count);

            foreach (IntPoint point in expected)
            {
                Assert.IsTrue(actual.Contains(point));
            }

            foreach (IntPoint point in actual)
            {
                Assert.IsTrue(expected.Contains(point));
            }

            IntPoint prev = actual[0];

            for (int i = 1; i < actual.Count; i++)
            {
                IntPoint curr = actual[i];
                Assert.IsTrue(System.Math.Abs(prev.X - curr.X) <= 1 &&
                              System.Math.Abs(prev.Y - curr.Y) <= 1);
                prev = curr;
            }

            IntPoint first = actual[0];
            IntPoint last  = actual[actual.Count - 1];

            Assert.IsTrue(System.Math.Abs(first.X - last.X) <= 1 &&
                          System.Math.Abs(first.Y - last.Y) <= 1);
        }
        public void FindDefectsTest()
        {

            Bitmap bmp = Properties.Resources.hand;

            Bitmap gray = AForge.Imaging.Filters.Grayscale.CommonAlgorithms.BT709.Apply(bmp);

            BlobCounter bc = new BlobCounter(gray);
            bc.ObjectsOrder = ObjectsOrder.Size;
            Blob[] blobs = bc.GetObjectsInformation();
            bc.ExtractBlobsImage(bmp, blobs[0], true);

            Bitmap blob = blobs[0].Image.ToManagedImage();

            BorderFollowing bf = new BorderFollowing();
            List<IntPoint> contour = bf.FindContour(blob);

            GrahamConvexHull graham = new GrahamConvexHull();
            List<IntPoint> hull = graham.FindHull(contour);

            ConvexHullDefects hullDefects = new ConvexHullDefects(10);
            List<ConvexityDefect> defects = hullDefects.FindDefects(contour, hull);

          /*  PointsMarker marker = new PointsMarker(hull, Color.Green, 10);
            marker.ApplyInPlace(blob);
            ImageBox.Show(blob);
            */

            Assert.AreEqual(2, defects.Count);
            Assert.AreEqual(new IntPoint(130, 10), contour[defects[0].Start]);
            Assert.AreEqual(new IntPoint(93, 109), contour[defects[0].Point]);
            Assert.AreEqual(new IntPoint(64, 9), contour[defects[0].End]);
            Assert.AreEqual(99.549179077148438, defects[0].Depth, 1e-5);
            Assert.IsFalse(double.IsNaN(defects[0].Depth));
            //    Assert.AreEqual(9912.9531239366424, defects[0].Area);

            Assert.AreEqual(new IntPoint(49, 18), contour[defects[1].Start]);
            Assert.AreEqual(new IntPoint(61, 106), contour[defects[1].Point]);
            Assert.AreEqual(new IntPoint(18, 127), contour[defects[1].End]);
            Assert.AreEqual(35.615153852366504, defects[1].Depth, 1e-5);
            Assert.IsFalse(double.IsNaN(defects[1].Depth));
            //    Assert.AreEqual(2293.7535682510002, defects[1].Area);

        }
        public void FindDefectsTest()
        {
            Bitmap bmp = Properties.Resources.hand;

            Bitmap gray = AForge.Imaging.Filters.Grayscale.CommonAlgorithms.BT709.Apply(bmp);

            BlobCounter bc = new BlobCounter(gray);

            bc.ObjectsOrder = ObjectsOrder.Size;
            Blob[] blobs = bc.GetObjectsInformation();
            bc.ExtractBlobsImage(bmp, blobs[0], true);

            Bitmap blob = blobs[0].Image.ToManagedImage();

            BorderFollowing bf      = new BorderFollowing();
            List <IntPoint> contour = bf.FindContour(blob);

            GrahamConvexHull graham = new GrahamConvexHull();
            List <IntPoint>  hull   = graham.FindHull(contour);

            ConvexHullDefects      hullDefects = new ConvexHullDefects(10);
            List <ConvexityDefect> defects     = hullDefects.FindDefects(contour, hull);

            /*  PointsMarker marker = new PointsMarker(hull, Color.Green, 10);
             * marker.ApplyInPlace(blob);
             * ImageBox.Show(blob);
             */

            Assert.AreEqual(2, defects.Count);
            Assert.AreEqual(new IntPoint(130, 10), contour[defects[0].Start]);
            Assert.AreEqual(new IntPoint(93, 109), contour[defects[0].Point]);
            Assert.AreEqual(new IntPoint(64, 9), contour[defects[0].End]);
            Assert.AreEqual(99.549179077148438, defects[0].Depth, 1e-5);
            Assert.IsFalse(double.IsNaN(defects[0].Depth));
            //    Assert.AreEqual(9912.9531239366424, defects[0].Area);

            Assert.AreEqual(new IntPoint(49, 18), contour[defects[1].Start]);
            Assert.AreEqual(new IntPoint(61, 106), contour[defects[1].Point]);
            Assert.AreEqual(new IntPoint(18, 127), contour[defects[1].End]);
            Assert.AreEqual(35.615153852366504, defects[1].Depth, 1e-5);
            Assert.IsFalse(double.IsNaN(defects[1].Depth));
            //    Assert.AreEqual(2293.7535682510002, defects[1].Area);
        }
        //ilk önce bu butona tıklanmalı
        private void button1_Click(object sender, EventArgs e)
        {
            sayac       = 1;
            kaynakResim = (Bitmap)System.Drawing.Image.FromFile("sample2.jpg");

            //Otsu Threshold uygulandı
            OtsuThreshold otsuFiltre = new OtsuThreshold();

            //resim eğer renkliyse önce griye çeviriyor sonra filtre uyguluyor
            //resim zaten griyse direk filtreyi uyguluyor
            siyahBeyazResim = otsuFiltre.Apply(kaynakResim.PixelFormat != PixelFormat.Format8bppIndexed ? Grayscale.CommonAlgorithms.BT709.Apply(kaynakResim) : kaynakResim);

            BlobCounterBase bc = new BlobCounter();

            bc.FilterBlobs = true;
            bc.MinHeight   = 5;
            bc.MinWidth    = 5;

            bc.ProcessImage(siyahBeyazResim);
            blobs             = bc.GetObjectsInformation();
            label2.Text       = "Toplam Şekil Sayısı = " + bc.ObjectsCount.ToString();
            toplamSekilSayisi = bc.ObjectsCount;

            /*aşağıdaki for döngüsü yerine buradaki koşul ifadesi kullanısaydı eğer,
             * resimdeki en büyük şekil bulunacaktı.
             * if (blobs.Length > 0)
             * {
             *  bc.ExtractBlobsImage(siyahBeyazResim, blobs[0], true);
             * }
             * Bitmap xxx = blobs[0].Image.ToManagedImage();
             */

            //bütün şekiller bulunuyor
            for (int i = 0, n = blobs.Length; i < n; i++)
            {
                if (blobs.Length > 0)
                {
                    bc.ExtractBlobsImage(siyahBeyazResim, blobs[i], true);
                    pictureBox1.Image = siyahBeyazResim;
                    pictureBox1.Refresh();
                }
            }
        }
Example #16
0
        private void socketadd_Click(object sender, EventArgs e)
        {
            OpenFileDialog rs = new OpenFileDialog();

            rs.Title  = "Resim Seç";
            rs.Filter = "(*.jpg)|*.jpg|(*.png)|*.png";

            if (rs.ShowDialog() == DialogResult.OK)
            {
                this.pictureBox1.Image = new Bitmap(rs.OpenFile());
            }

            sayac = 1;
            Bitmap kaynakResim = new Bitmap(pictureBox1.Image);

            //Otsu Threshold uygulandı
            OtsuThreshold otsuFiltre = new OtsuThreshold();

            //resim eğer renkliyse önce griye çeviriyor sonra filtre uyguluyor
            //resim zaten griyse direk filtreyi uyguluyor
            siyahBeyazResim = otsuFiltre.Apply(kaynakResim.PixelFormat != PixelFormat.Format8bppIndexed ? Grayscale.CommonAlgorithms.BT709.Apply(kaynakResim) : kaynakResim);

            BlobCounterBase bc = new BlobCounter();

            bc.FilterBlobs = true;
            bc.MinHeight   = 5;
            bc.MinWidth    = 5;

            bc.ProcessImage(siyahBeyazResim);
            blobs = bc.GetObjectsInformation();

            for (int i = 0, n = blobs.Length; i < n; i++)
            {
                if (blobs.Length > 0)
                {
                    bc.ExtractBlobsImage(siyahBeyazResim, blobs[i], true);
                    pictureBox1.Image = siyahBeyazResim;
                    pictureBox1.Refresh();
                }
            }
        }
Example #17
0
        public void FindContourTest()
        {
            Bitmap bmp = Properties.Resources.sample_black;

            Bitmap gray = AForge.Imaging.Filters.Grayscale.CommonAlgorithms.BT709.Apply(bmp);

            BlobCounter bc = new BlobCounter(gray);
            bc.ObjectsOrder = ObjectsOrder.Size;
            Blob[] blobs = bc.GetObjectsInformation();
            bc.ExtractBlobsImage(bmp, blobs[0], true);
            List<IntPoint> expected = bc.GetBlobsEdgePoints(blobs[0]);
            Bitmap blob = blobs[0].Image.ToManagedImage();

            BorderFollowing bf = new BorderFollowing();
            List<IntPoint> actual = bf.FindContour(blob);

            Assert.AreEqual(expected.Count, actual.Count);

            foreach (IntPoint point in expected)
                Assert.IsTrue(actual.Contains(point));

            foreach (IntPoint point in actual)
                Assert.IsTrue(expected.Contains(point));

            IntPoint prev = actual[0];
            for (int i = 1; i < actual.Count; i++)
            {
                IntPoint curr = actual[i];
                Assert.IsTrue(System.Math.Abs(prev.X - curr.X) <= 1 &&
                              System.Math.Abs(prev.Y - curr.Y) <= 1);
                prev = curr;
            }

            IntPoint first = actual[0];
            IntPoint last = actual[actual.Count - 1];
            Assert.IsTrue(System.Math.Abs(first.X - last.X) <= 1 &&
                          System.Math.Abs(first.Y - last.Y) <= 1);
        }
Example #18
0
        static void Main(string[] args)
        {
            Threshold                   thresh        = new Threshold(10);
            Median                      median        = new Median(9);
            Erosion3x3                  erode         = new Erosion3x3();
            Dilatation3x3               dilate        = new Dilatation3x3();
            GrahamConvexHull            hullFinder    = new GrahamConvexHull();
            ConnectedComponentsLabeling ccLabeler     = new ConnectedComponentsLabeling();
            BorderFollowing             contourFinder = new BorderFollowing();
            GrayscaleToRGB              rgb           = new GrayscaleToRGB();
            ConvexHullDefects           defectFinder  = new ConvexHullDefects(10);

            Bitmap img = (Bitmap)Bitmap.FromFile("hand3.jpg");

            Bitmap image = Grayscale.CommonAlgorithms.BT709.Apply(img);

            thresh.ApplyInPlace(image);
            //median.ApplyInPlace(image);
            erode.ApplyInPlace(image);
            dilate.ApplyInPlace(image);

            BlobCounter counter = new BlobCounter(image);

            counter.ObjectsOrder = ObjectsOrder.Area;

            Blob[] blobs = counter.GetObjectsInformation();

            if (blobs.Length > 0)
            {
                counter.ExtractBlobsImage(image, blobs[0], true);

                UnmanagedImage hand = blobs[0].Image;

                var contour = contourFinder.FindContour(hand);

                if (contour.Count() > 0)
                {
                    var initialHull = hullFinder.FindHull(contour);

                    var defects = defectFinder.FindDefects(contour, initialHull);

                    var filteredHull = initialHull.ClusterHullPoints().FilterLinearHullPoints();

                    var palmCenter = defects.Centroid(contour);

                    var wristPoints = filteredHull.SelectWristPoints(defects, contour);

                    Bitmap color = rgb.Apply(hand).ToManagedImage();

                    //BitmapData data = color.LockBits(new Rectangle(0, 0, color.Width, color.Height), ImageLockMode.ReadWrite, color.PixelFormat);
                    //Drawing.Polyline(data, contour, Color.Blue);
                    //Drawing.Polygon(data, filteredHull, Color.Red);
                    //color.UnlockBits(data);

                    Graphics gr = Graphics.FromImage(color);

                    gr.DrawPolygon(new Pen(Brushes.Red, 3), filteredHull.ToPtArray());
                    gr.DrawLines(new Pen(Brushes.Blue, 3), contour.ToPtArray());
                    gr.DrawEllipse(new Pen(Brushes.Red, 3), palmCenter.X - 10, palmCenter.Y - 10, 20, 20);

                    foreach (ConvexityDefect defect in defects)
                    {
                        gr.DrawEllipse(new Pen(Brushes.Green, 6), contour[defect.Point].X - 10, contour[defect.Point].Y - 10, 20, 20);
                    }

                    foreach (AForge.IntPoint pt in filteredHull)
                    {
                        gr.DrawEllipse(new Pen(Brushes.Yellow, 6), pt.X - 10, pt.Y - 10, 20, 20);
                    }

                    foreach (AForge.IntPoint pt in wristPoints)
                    {
                        gr.DrawEllipse(new Pen(Brushes.PowderBlue, 6), pt.X - 10, pt.Y - 10, 20, 20);
                    }

                    ImageBox.Show(color);
                }
            }
        }
Example #19
0
        private void button1_Click(object sender, EventArgs e)
        {//picture button and save
            try
            {
                //AForge.Imaging.Filters.Invert inv = new AForge.Imaging.Filters.Invert();
                String pictureName = textBox1.Text;

                //Bitmap picture = new Bitmap(DisplayWindow.Image); //videoSourcePlayer.GetCurrentVideoFrame();
                //ResizeBilinear refilt = new ResizeBilinear(400, 300);
                Camera.Image.Save(pictureCounter + pictureName + ".jpg");

                //Bitmap picture = Camera.Image; //videoSourcePlayer.GetCurrentVideoFrame();
                Bitmap pictureCopy = new Bitmap(pictureCounter + pictureName + ".jpg");
                //refilt.Apply(pictureCopy);
                pictureBox1.Image = pictureCopy;
                //Bitmap pictureCopy = new Bitmap(picture);
                //pictureCopy.Save(pictureName + pictureCounter + ".jpg", ImageFormat.Jpeg);// save into Bin folder as a jpeg file



                if (pictureCounter == 1)//get the reference
                {
                    //referencePicture = ()videoSourcePlayer.GetCurrentVideoFrame();
                    //BlobCounter bc = new BlobCounter(pictureCopy);
                    //bc.MinHeight = 1000;//required parameters
                    //bc.MinWidth = 1000;
                    //bc.MaxHeight = 1100;
                    //bc.MaxWidth = 1100;
                    //inv.ApplyInPlace(pictureCopy);
                    //bc.BackgroundThreshold = Color.Black;
                    //bc.ProcessImage(pictureCopy);
                    //Rectangle[] rects = bc.GetObjectsRectangles(); // gets the toop left hand corner of the x and y coordinates

                    //Crop filter = new Crop(new Rectangle (400, 50, 500, 500));
                    referencePic = pictureCopy;


                    Grayscale grayScale = new Grayscale(0.2125, 0.7154, 0.0721);
                    referencePic = grayScale.Apply(referencePic);

                    Threshold threshold = new Threshold(50);
                    threshold.ApplyInPlace(referencePic);

                    BlobCounter bc = new BlobCounter(referencePic);
                    bc.FilterBlobs = true;
                    bc.MinHeight   = 900;//these values will have to change in order to fit your tests
                    bc.MinWidth    = 900;

                    bc.ProcessImage(referencePic);
                    Blob[]      blobs = bc.GetObjectsInformation();
                    Rectangle[] rects = bc.GetObjectsRectangles();

                    foreach (Blob blob in blobs)
                    {
                        bc.ExtractBlobsImage(referencePic, blob, true);
                        //Bitmap blobimage = new Bitmap(blob.Image.ToManagedImage());
                    }

                    referenceX = rects[0].X; //(rects[0].Width / 2) + rects[0].X;
                    referenceY = rects[0].Y; //(rects[0].Height / 2) - rects[0].Y;
                }
                //pictureCopy.Dispose();//free up memory in system
                //pictureCopy = null;
                pictureCounter++;

                //DirectoryInfo parentDirectory = new DirectoryInfo(Environment.CurrentDirectory);//Directory where you can find the pictures
                //FileInfo[] files = parentDirectory.GetFiles("CaptureImage.jpg", SearchOption.TopDirectoryOnly);
                //foreach (FileInfo fi in files)
                //{
                //    files[0].Delete();
                //}
            }
            catch (Exception ex)
            {
                MessageBox.Show("Something Didn't work \r\n" + ex);
            }
        }//picture button and save end
Example #20
0
        /// <summary>
        /// Apply filter to an image.
        /// </summary>
        ///
        /// <param name="imageData">Source image to get biggest blob from.</param>
        ///
        /// <returns>Returns image of the biggest blob.</returns>
        ///
        /// <exception cref="UnsupportedImageFormatException">Unsupported pixel format of the source image.</exception>
        /// <exception cref="UnsupportedImageFormatException">Unsupported pixel format of the original image.</exception>
        /// <exception cref="InvalidImagePropertiesException">Source and original images must have the same size.</exception>
        /// <exception cref="ArgumentException">The source image does not contain any blobs.</exception>
        ///
        public Bitmap Apply(BitmapData imageData)
        {
            // check pixel format of the source image
            if (!FormatTranslations.ContainsKey(imageData.PixelFormat))
            {
                throw new UnsupportedImageFormatException("Source pixel format is not supported by the filter.");
            }

            // locate blobs in the source image
            var blobCounter = new BlobCounter(imageData);
            // get information about blobs
            var blobs = blobCounter.GetObjectsInformation();
            // find the biggest blob
            var  maxSize     = 0;
            Blob biggestBlob = null;

            for (int i = 0, n = blobs.Length; i < n; i++)
            {
                var size = blobs[i].Rectangle.Width * blobs[i].Rectangle.Height;

                if (size > maxSize)
                {
                    maxSize     = size;
                    biggestBlob = blobs[i];
                }
            }

            // check if any blob was found
            if (biggestBlob == null)
            {
                throw new ArgumentException("The source image does not contain any blobs.");
            }

            blobPosition = new IntPoint(biggestBlob.Rectangle.Left, biggestBlob.Rectangle.Top);

            // extract biggest blob's image
            if (originalImage == null)
            {
                blobCounter.ExtractBlobsImage(new UnmanagedImage(imageData), biggestBlob, false);
            }
            else
            {
                // check original image's format
                if (
                    (originalImage.PixelFormat != PixelFormat.Format24bppRgb) &&
                    (originalImage.PixelFormat != PixelFormat.Format32bppArgb) &&
                    (originalImage.PixelFormat != PixelFormat.Format32bppRgb) &&
                    (originalImage.PixelFormat != PixelFormat.Format32bppPArgb) &&
                    (originalImage.PixelFormat != PixelFormat.Format8bppIndexed)
                    )
                {
                    throw new UnsupportedImageFormatException("Original image may be grayscale (8bpp indexed) or color (24/32bpp) image only.");
                }

                // check its size
                if ((originalImage.Width != imageData.Width) || (originalImage.Height != imageData.Height))
                {
                    throw new InvalidImagePropertiesException("Original image must have the same size as passed source image.");
                }

                blobCounter.ExtractBlobsImage(originalImage, biggestBlob, false);
            }

            var managedImage = biggestBlob.Image.ToManagedImage();

            // dispose unmanaged image of the biggest blob
            biggestBlob.Image.Dispose();

            return(managedImage);
        }
Example #21
0
        private void video_NewFrame(object sender, NewFrameEventArgs eventArgs)
        {
            Bitmap img = (Bitmap)eventArgs.Frame.Clone();

            byte[] ImArray = new byte[1];
            ImArray          = BmpToBytes_MemStream(img);
            img              = (Bitmap)BytesToImg(ImArray);
            pbVideo.SizeMode = PictureBoxSizeMode.StretchImage;

            HSLfilter.Saturation = new AForge.Range(satMin, satMax);
            HSLfilter.Luminance  = new AForge.Range(lumMin, lumMax);
            HSLfilter.Hue        = new AForge.IntRange(hueRange, hueReplace);
            HSLfilter.ApplyInPlace(img);

            BlobCounterBase blobCounter = new BlobCounter();

            blobCounter.FilterBlobs  = true;
            blobCounter.MinWidth     = minBlobSize;
            blobCounter.MinHeight    = minBlobSize;
            blobCounter.ObjectsOrder = ObjectsOrder.Size;
            blobCounter.ProcessImage(img);
            Blob[] blobs = blobCounter.GetObjectsInformation();

            if (blobs.Length > 0)
            {
                isBlob = true;
                Blob blob = blobs[0];
                blobCounter.ExtractBlobsImage(img, blob, true);

                location = blob.Rectangle.X + (blob.Rectangle.Width / 2);

                BeginInvoke(((Action) delegate()
                {
                    tbWidth.Clear();
                    tbWidth.Text = blob.Rectangle.Width.ToString();
                }));

                if (blob.Rectangle.Width > targetSize)
                {
                    isClose = true;
                }
                else
                {
                    isClose = false;
                }

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

                Rectangle rect = blobs[0].Rectangle;
                Drawing.Rectangle(data, rect, Color.White);

                img.UnlockBits(data);
            }
            else
            {
                isBlob = false;
            }

            if (inRoom)
            {
                LocateObject(ImArray, isBlob, abort);
            }

            pbVideo.Image = img;
        }
        private void JpegLiveSource1LiveNotificationEvent(object sender, EventArgs e)
        {
            if (this.InvokeRequired)
            {
                if (OnMainThread)
                {
                    LiveContentEventArgs args = e as LiveContentEventArgs;
                    if (args != null && args.LiveContent != null)
                    {
                        // UI thread is too busy - discard this frame from display
                        args.LiveContent.Dispose();
                    }
                    return;
                }
                OnMainThread = true;
                // Make sure we execute on the UI thread before updating UI Controls
                BeginInvoke(new EventHandler(JpegLiveSource1LiveNotificationEvent), new[] { sender, e });
            }
            else
            {
                LiveContentEventArgs args = e as LiveContentEventArgs;
                if (args != null)
                {
                    if (args.LiveContent != null)
                    {
                        // Display the received JPEG
                        //textBoxLength.Text = "" + args.LiveContent.Content.Length;

                        int width  = args.LiveContent.Width;
                        int height = args.LiveContent.Height;

                        MemoryStream ms = new MemoryStream(args.LiveContent.Content);
                        //Bitmap newBitmap = testBox();
                        Bitmap newBitmap = new Bitmap(ms);

                        if (referenceBitmap == null)
                        {
                            referenceBitmap = newBitmap;
                        }

                        textBoxResolution.Text = "" + width + "x" + height;

                        if (pictureBoxOriginal.Size.Width != 0 && pictureBoxOriginal.Size.Height != 0)
                        {
                            if ((newBitmap.Width != pictureBoxOriginal.Width || newBitmap.Height != pictureBoxOriginal.Height))
                            {
                                pictureBoxOriginal.Image = new Bitmap(newBitmap, pictureBoxOriginal.Size);
                            }
                            else
                            {
                                pictureBoxOriginal.Image = newBitmap;
                            }
                        }

                        textBoxDecodingStatus.Text = args.LiveContent.HardwareDecodingStatus;



                        ms.Close();
                        ms.Dispose();

                        _count++;
                        textBoxCount.Text = "" + _count;

                        args.LiveContent.Dispose();



                        /// Star processing frame
                        grayImage            = gfilter.Apply(newBitmap);
                        pictureBoxGray.Image = grayImage;

                        try
                        {
                            if (counter == 4000)
                            {
                                counter    = 0;
                                background = null;
                                analyticsImageProcessing.resetBackground();
                            }

                            if (background == null)
                            {
                                background = analyticsImageProcessing.GetBackGound(grayImage);

                                /// Show mensaje
                                Bitmap   bitmap = new Bitmap(320, 240);
                                Graphics g      = Graphics.FromImage(bitmap);
                                g.FillRectangle(System.Drawing.Brushes.Black, 0, 0, bitmap.Width, bitmap.Height);
                                g.DrawString("Processing Background...", new Font(FontFamily.GenericMonospace, 12), Brushes.White, new PointF(20, pictureBoxOriginal.Height / 2 - 20));
                                g.Dispose();
                                pictureBoxBackgound.Image = new Bitmap(bitmap, pictureBoxOriginal.Size);
                                bitmap.Dispose();
                            }
                            else
                            {
                                pictureBoxBackgound.Image = background;
                                background2 = gfilter.Apply(background);

                                Bitmap backgroundMask = analyticsImageProcessing.diff(grayImage, background2);
                                pictureBox1.Image = backgroundMask;


                                // create filter
                                //   Median filter = new Median();
                                // apply the filter
                                //   Bitmap backgroundMask2 = filter.Apply(backgroundMask);

                                //     pictureBox2.Image = backgroundMask2;

                                // create filter
                                Pixellate pxfilter = new Pixellate(20);
                                // apply the filter
                                Bitmap result = pxfilter.Apply(backgroundMask);
                                //      pictureBox3.Image = result;

                                // create filter
                                Threshold thfilter = new Threshold(1);
                                // apply the filter
                                Bitmap result2 = thfilter.Apply(result);

                                pictureBox4.Image = result2;

                                ApplyMask appmask = new ApplyMask(result2);
                                foreground = appmask.Apply(newBitmap);
                            }


                            /// PocessImage
                            if (foreground != null)
                            {
                                Blob[] blobs = analyticsImageProcessing.GetBlobs(foreground, blobCounter);
                                textBoxMetadata.Text = metadataHandler.SendMetadataBox(blobs, _jpegLiveSource.Width, _jpegLiveSource.Height);
                                PaintHeatMap(blobs);
                                pictureBoxHeatmap.Image = bitmapHeatMap;

                                /// Debug tool


                                if (blobs[0] != null && blobs.Length > 0)
                                {
                                    blobCounter.ExtractBlobsImage(foreground, blobs[0], false);
                                    pictureBoxBlob1.Image = blobs[0].Image.ToManagedImage();
                                    textBoxAreaBlob1.Text = blobs[0].Area.ToString();
                                    textBoxXBlob1.Text    = blobs[0].CenterOfGravity.X.ToString();
                                    textBoxYBlob1.Text    = blobs[0].CenterOfGravity.Y.ToString();
                                }
                                else
                                {
                                    pictureBoxBlob1.Image = null;
                                }

                                if (blobs[1] != null && blobs.Length > 1)
                                {
                                    blobCounter.ExtractBlobsImage(foreground, blobs[1], false);
                                    pictureBoxBlob2.Image = blobs[1].Image.ToManagedImage();
                                    textBoxAreaBlob2.Text = blobs[1].Area.ToString();
                                    textBoxXBlob2.Text    = blobs[1].CenterOfGravity.X.ToString();
                                    textBoxYBlob2.Text    = blobs[1].CenterOfGravity.Y.ToString();
                                }
                                else
                                {
                                    pictureBoxBlob2.Image = null;
                                }

                                if (blobs[2] != null && blobs.Length > 2)
                                {
                                    blobCounter.ExtractBlobsImage(foreground, blobs[2], false);
                                    pictureBoxBlob3.Image = blobs[2].Image.ToManagedImage();
                                    textBoxAreaBlob3.Text = blobs[2].Area.ToString();
                                    textBoxXBlob3.Text    = blobs[2].CenterOfGravity.X.ToString();
                                    textBoxYBlob3.Text    = blobs[2].CenterOfGravity.Y.ToString();
                                }
                                else
                                {
                                    pictureBoxBlob3.Image = null;
                                }

                                if (blobs[3] != null && blobs.Length > 3)
                                {
                                    blobCounter.ExtractBlobsImage(foreground, blobs[3], false);
                                    pictureBoxBlob4.Image = blobs[3].Image.ToManagedImage();
                                    textBoxAreaBlob4.Text = blobs[3].Area.ToString();
                                    textBoxXBlob4.Text    = blobs[3].CenterOfGravity.X.ToString();
                                    textBoxYBlob4.Text    = blobs[3].CenterOfGravity.Y.ToString();
                                }
                                else
                                {
                                    pictureBoxBlob4.Image = null;
                                }
                            }
                        }
                        catch (Exception r)
                        {
                            Console.WriteLine(r.Message);
                        }
                    }
                    else if (args.Exception != null)
                    {
                        // Handle any exceptions occurred inside toolkit or on the communication to the VMS

                        Bitmap   bitmap = new Bitmap(320, 240);
                        Graphics g      = Graphics.FromImage(bitmap);
                        g.FillRectangle(System.Drawing.Brushes.Black, 0, 0, bitmap.Width, bitmap.Height);
                        g.DrawString("Connection lost to server ...", new Font(FontFamily.GenericMonospace, 12), Brushes.White, new PointF(20, pictureBoxOriginal.Height / 2 - 20));
                        g.Dispose();
                        pictureBoxOriginal.Image = new Bitmap(bitmap, pictureBoxOriginal.Size);
                        bitmap.Dispose();
                    }
                }
                OnMainThread = false;
            }
        }
Example #23
0
        static void Main(string[] args)
        {
            Threshold thresh = new Threshold(10);
            Median median = new Median(9);
            Erosion3x3 erode = new Erosion3x3();
            Dilatation3x3 dilate = new Dilatation3x3();
            GrahamConvexHull hullFinder = new GrahamConvexHull();
            ConnectedComponentsLabeling ccLabeler = new ConnectedComponentsLabeling();
            BorderFollowing contourFinder = new BorderFollowing();
            GrayscaleToRGB rgb = new GrayscaleToRGB();
            ConvexHullDefects defectFinder = new ConvexHullDefects(10);

            Bitmap img = (Bitmap)Bitmap.FromFile("hand3.jpg");

            Bitmap image = Grayscale.CommonAlgorithms.BT709.Apply(img);
            thresh.ApplyInPlace(image);
            //median.ApplyInPlace(image);
            erode.ApplyInPlace(image);
            dilate.ApplyInPlace(image);

            BlobCounter counter = new BlobCounter(image);
            counter.ObjectsOrder = ObjectsOrder.Area;

            Blob[] blobs = counter.GetObjectsInformation();

            if (blobs.Length > 0)
            {
                counter.ExtractBlobsImage(image, blobs[0], true);

                UnmanagedImage hand = blobs[0].Image;

                var contour = contourFinder.FindContour(hand);

                if (contour.Count() > 0)
                {
                    var initialHull = hullFinder.FindHull(contour);

                    var defects = defectFinder.FindDefects(contour, initialHull);

                    var filteredHull = initialHull.ClusterHullPoints().FilterLinearHullPoints();

                    var palmCenter = defects.Centroid(contour);

                    var wristPoints = filteredHull.SelectWristPoints(defects, contour);

                    Bitmap color = rgb.Apply(hand).ToManagedImage();

                    //BitmapData data = color.LockBits(new Rectangle(0, 0, color.Width, color.Height), ImageLockMode.ReadWrite, color.PixelFormat);
                    //Drawing.Polyline(data, contour, Color.Blue);
                    //Drawing.Polygon(data, filteredHull, Color.Red);
                    //color.UnlockBits(data);

                    Graphics gr = Graphics.FromImage(color);

                    gr.DrawPolygon(new Pen(Brushes.Red, 3), filteredHull.ToPtArray());
                    gr.DrawLines(new Pen(Brushes.Blue, 3), contour.ToPtArray());
                    gr.DrawEllipse(new Pen(Brushes.Red, 3), palmCenter.X - 10, palmCenter.Y - 10, 20, 20);

                    foreach (ConvexityDefect defect in defects)
                    {
                        gr.DrawEllipse(new Pen(Brushes.Green, 6), contour[defect.Point].X - 10, contour[defect.Point].Y - 10, 20, 20);
                    }

                    foreach (AForge.IntPoint pt in filteredHull)
                    {
                        gr.DrawEllipse(new Pen(Brushes.Yellow, 6), pt.X - 10, pt.Y - 10, 20, 20);
                    }

                    foreach (AForge.IntPoint pt in wristPoints)
                    {
                        gr.DrawEllipse(new Pen(Brushes.PowderBlue, 6), pt.X - 10, pt.Y - 10, 20, 20);
                    }

                    ImageBox.Show(color);
                }
            }
        }
        /// <summary>
        /// Apply filter to an image.
        /// </summary>
        /// 
        /// <param name="imageData">Source image to get biggest blob from.</param>
        /// 
        /// <returns>Returns image of the biggest blob.</returns>
        /// 
        /// <exception cref="UnsupportedImageFormatException">Unsupported pixel format of the source image.</exception>
        /// <exception cref="UnsupportedImageFormatException">Unsupported pixel format of the original image.</exception>
        /// <exception cref="InvalidImagePropertiesException">Source and original images must have the same size.</exception>
        /// <exception cref="ArgumentException">The source image does not contain any blobs.</exception>
        ///
        public Bitmap Apply( BitmapData imageData )
        {
            // check pixel format of the source image
            if ( !FormatTranslations.ContainsKey( imageData.PixelFormat ) )
                throw new UnsupportedImageFormatException( "Source pixel format is not supported by the filter." );

            // locate blobs in the source image
            BlobCounter blobCounter = new BlobCounter( imageData );
            // get information about blobs
            Blob[] blobs = blobCounter.GetObjectsInformation( );
            // find the biggest blob
            int  maxSize = 0;
            Blob biggestBlob = null;

            for ( int i = 0, n = blobs.Length; i < n; i++ )
            {
                int size = blobs[i].Rectangle.Width * blobs[i].Rectangle.Height;

                if ( size > maxSize )
                {
                    maxSize = size;
                    biggestBlob = blobs[i];
                }
            }

            // check if any blob was found
            if ( biggestBlob == null )
            {
                throw new ArgumentException( "The source image does not contain any blobs." );
            }

            blobPosition = new IntPoint( biggestBlob.Rectangle.Left, biggestBlob.Rectangle.Top );

            // extract biggest blob's image
            if ( originalImage == null )
            {
                blobCounter.ExtractBlobsImage( new UnmanagedImage( imageData ), biggestBlob, false );
            }
            else
            {
                // check original image's format
                if (
                    ( originalImage.PixelFormat != PixelFormat.Format24bppRgb ) &&
                    ( originalImage.PixelFormat != PixelFormat.Format32bppArgb ) &&
                    ( originalImage.PixelFormat != PixelFormat.Format32bppRgb ) &&
                    ( originalImage.PixelFormat != PixelFormat.Format32bppPArgb ) &&
                    ( originalImage.PixelFormat != PixelFormat.Format8bppIndexed )
                    )
                {
                    throw new UnsupportedImageFormatException( "Original image may be grayscale (8bpp indexed) or color (24/32bpp) image only." );
                }

                // check its size
                if ( ( originalImage.Width != imageData.Width ) || ( originalImage.Height != imageData.Height ) )
                {
                    throw new InvalidImagePropertiesException( "Original image must have the same size as passed source image." );
                }

                blobCounter.ExtractBlobsImage( originalImage, biggestBlob, false );
            }

            Bitmap managedImage = biggestBlob.Image.ToManagedImage( );

            // dispose unmanaged image of the biggest blob
            biggestBlob.Image.Dispose( );

            return managedImage;
        }
Example #25
0
        public void FindContourTest2()
        {
            Bitmap bmp = Properties.Resources.hand2;

            BlobCounter bc = new BlobCounter(bmp);
            bc.ObjectsOrder = ObjectsOrder.Size;
            Blob[] blobs = bc.GetObjectsInformation();
            bc.ExtractBlobsImage(bmp, blobs[0], true);
            List<IntPoint> expected = bc.GetBlobsEdgePoints(blobs[0]);
            Bitmap blob = blobs[0].Image.ToManagedImage();

            BorderFollowing bf = new BorderFollowing();
            List<IntPoint> actual = bf.FindContour(blob);

            foreach (IntPoint point in expected)
                Assert.IsTrue(actual.Contains(point));

            IntPoint prev = actual[0];
            for (int i = 1; i < actual.Count; i++)
            {
                IntPoint curr = actual[i];
                Assert.IsTrue(System.Math.Abs(prev.X - curr.X) <= 1 &&
                              System.Math.Abs(prev.Y - curr.Y) <= 1);
                prev = curr;
            }

            IntPoint first = actual[0];
            IntPoint last = actual[actual.Count - 1];
            Assert.IsTrue(System.Math.Abs(first.X - last.X) <= 1 &&
                          System.Math.Abs(first.Y - last.Y) <= 1);
        }