Beispiel #1
0
		public void GetValueIncrement()
		{
			var provider = GlobalSetup.Container.Resolve<IBlobStorageProvider>();
			provider.CreateContainer(ContainerName);

			var counter = new BlobCounter(provider, ContainerName, BlobName);

			var val = (int)counter.GetValue();

			if (0 != val) counter.Delete();

			counter.Increment(10);
			val = (int) counter.GetValue();
			Assert.AreEqual(10, val, "#A00");

			var val2 = counter.Increment(-5);
			val = (int)counter.GetValue();
			Assert.AreEqual(5, val, "#A01");
			Assert.AreEqual(val, val2, "#A02");

			var flag1 = counter.Delete();
			var flag2 = counter.Delete();

			Assert.IsTrue(flag1, "#A03");
			Assert.IsFalse(flag2, "#A04");
		}
        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);

        }
Beispiel #3
0
        private static void GetDominantColor(object o)
        {
            System.Drawing.Bitmap image = (System.Drawing.Bitmap)(o as Bitmap).Clone();

            Grayscale filter = new Grayscale(0.2125, 0.7154, 0.0721);

            image = filter.Apply(o as Bitmap);
            Threshold filterGray = new Threshold(120);

            filterGray.ApplyInPlace(image);

            BlobCounterBase bc = new BlobCounter();

            bc.FilterBlobs  = true;
            bc.MinWidth     = 90;
            bc.MinHeight    = 90;
            bc.MaxHeight    = 380;
            bc.ObjectsOrder = ObjectsOrder.Size;
            bc.ProcessImage(image);
            Blob[] blobs = bc.GetObjectsInformation();
            FoundObjects = GetObjectListFromBlobs((o as Bitmap), blobs);
            newObjectImage(GetBitmapImagesFromBlobs((o as Bitmap), blobs));
            CheckForEqualsInDataBase(FoundObjects);
            //newObject(FindedObjects);

            //BitmapImage btm = new BitmapImage();
            //using (MemoryStream memStream2 = new MemoryStream())
            //{
            //    (image).Save(memStream2, System.Drawing.Imaging.ImageFormat.Png);
            //    memStream2.Position = 0;
            //    btm.BeginInit();
            //    btm.CacheOption = BitmapCacheOption.OnLoad;
            //    btm.UriSource = null;
            //    btm.StreamSource = memStream2;
            //    btm.EndInit();
            //}

            //newFrame(btm);
        }
Beispiel #4
0
        private void Button4_Click(object sender, EventArgs e)
        {
            int neural_start = 0;

            while (neural_start == 0)
            {
                if (first_try == 0)
                {
                    button1.PerformClick();
                }
                try
                {
                    pictureBox4.Image = pictureBox3.Image;
                    neural_start      = 1;
                }
                catch
                {
                }
            }
            Bitmap      d  = new Bitmap(pictureBox4.Image);
            BlobCounter bc = new BlobCounter();

            bc.ProcessImage(d);
            Blob[] blobs = bc.GetObjectsInformation();
            foreach (var bloob in blobs)
            {
                if (bloob.Area > 500)
                {
                    double[][] treinamento =
                    {
                        new double[] { bloob.Area, bloob.Rectangle.Height, bloob.Rectangle.Width, bloob.Fullness }
                    };
                    label9.Text  = bloob.Area.ToString();
                    label11.Text = bloob.Rectangle.Height.ToString();
                    label13.Text = bloob.Rectangle.Width.ToString();
                    label15.Text = bloob.Fullness.ToString();
                }
            }
        }
Beispiel #5
0
        /// <summary>
        /// 扑克识别
        /// </summary>
        /// <param name="bmp"></param>
        public void PokerDetection(Bitmap bmp)
        {
            FiltersSequence seq = new FiltersSequence();

            seq.Add(Grayscale.CommonAlgorithms.BT709); // 添加灰度滤镜
            seq.Add(new OtsuThreshold());              // 添加二值化滤镜
            bmp = seq.Apply(bmp);                      // 应用滤镜

            // 从图像中提取宽度和高度大于150的blob
            BlobCounter extractor = new BlobCounter();

            extractor.FilterBlobs = true;
            extractor.MinWidth    = extractor.MinHeight = 150;
            extractor.MaxWidth    = extractor.MaxHeight = 350;
            extractor.ProcessImage(bmp);

            // 用于从原始图像提取扑克牌
            QuadrilateralTransformation quadTransformer = new QuadrilateralTransformation();
            int CardWidth  = 200;
            int CardHeight = 300;
            // 用于调整扑克牌大小
            ResizeBilinear resizer = new ResizeBilinear(CardWidth, CardHeight);

            foreach (Blob blob in extractor.GetObjectsInformation())
            {
                // 获取扑克牌的边缘点
                List <IntPoint> edgePoints = extractor.GetBlobsEdgePoints(blob);
                // 利用边缘点,在原始图像上找到四角
                List <IntPoint> corners = PointsCloud.FindQuadrilateralCorners(edgePoints);
                Bitmap          cardImg = quadTransformer.Apply(bmp);    // 提取扑克牌图像

                if (cardImg.Width > cardImg.Height)                      // 如果扑克牌横放
                {
                    cardImg.RotateFlip(RotateFlipType.Rotate90FlipNone); // 旋转之
                }
                cardImg = resizer.Apply(cardImg);                        // 归一化(重设大小)扑克牌
                RefreshBitmap(3, cardImg);
            }
        }
Beispiel #6
0
        public void FindBlobs()
        {
            BlobCounterBase bc = new BlobCounter();

            bc.FilterBlobs = true;
            bc.MinWidth    = 1;
            bc.MinHeight   = 1;
            bc.MaxHeight   = 1000;
            bc.MaxWidth    = 1000;
            // process binary image
            bc.ProcessImage(Img);
            Blob[] blobs = bc.GetObjects(Img, false);
            if (blobs.Length > 0)
            {
                X      = blobs[0].Rectangle.X;
                Y      = blobs[0].Rectangle.Y;
                Width  = blobs[0].Rectangle.Width;
                Height = blobs[0].Rectangle.Height;
            }
            // x =blobs[0].CenterOfGravity.X;
            //y = blobs[0].CenterOfGravity.Y;
        }
Beispiel #7
0
        public void GetDisplayCornerfrombmp(Bitmap processbmp, out List <IntPoint> displaycornerPoints)
        {
            BlobCounter bbc = new BlobCounter();

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

            bbc.ProcessImage(processbmp);

            Blob[]             blobs        = bbc.GetObjectsInformation();
            SimpleShapeChecker shapeChecker = new SimpleShapeChecker();

            foreach (var blob in blobs)
            {
                List <IntPoint> edgePoints = bbc.GetBlobsEdgePoints(blob);
                List <IntPoint> cornerPoints;


                // use the shape checker to extract the corner points
                if (shapeChecker.IsQuadrilateral(edgePoints, out cornerPoints))
                {
                    // only do things if the corners from a rectangle
                    if (shapeChecker.CheckPolygonSubType(cornerPoints) == PolygonSubType.Rectangle)
                    {
                        flagPoints = cornerPoints;
                        continue;
                    }
                    else
                    {
                        MessageBox.Show("Cannot Find the Display");
                        flagPoints = null;
                        //                       picturebox_test.Image = m;
                        continue;
                    }
                }
            }
            displaycornerPoints = flagPoints;
        }
Beispiel #8
0
        public static void DetectBigBlobs(Bitmap bitmap)
        {
            //bitmap = Grayscale.CommonAlgorithms.BT709.Apply(bitmap);
            BlobCounter blobCounter = new BlobCounter();

            blobCounter.ProcessImage(Grayscale.CommonAlgorithms.BT709.Apply(bitmap));
            Rectangle[] rects = blobCounter.GetObjectsRectangles();
            //Graphics object to draw
            Pen      pen = new Pen(Color.Red, 2);
            Graphics g   = Graphics.FromImage(bitmap);

            foreach (Rectangle rect in rects)
            {
                if (rect.Width > 200 && rect.Height > 150)
                {
                    g.DrawRectangle(pen, rect);
                }
            }

            pen.Dispose();
            g.Dispose();
        }
Beispiel #9
0
        private void hücreToolStripMenuItem_Click(object sender, EventArgs e)
        {
            islem = new ExtractNormalizedRGBChannel(RGB.G).Apply(kaynak);
            islem = new OtsuThreshold().Apply(islem);
            islem = new Invert().Apply(islem);
            islem = new Opening().Apply(islem);

            BlobCounter bc = new BlobCounter();

            bc.FilterBlobs = true;
            bc.MinHeight   = 5;
            bc.MinWidth    = 5;
            bc.ProcessImage(islem);
            Rectangle[] kare = bc.GetObjectsRectangles();
            listBox1.Items.Add(kare.Length + "tane hücre var");
            foreach (Rectangle rect in kare)
            {
                listBox1.Items.Add(rect.Location + " " + rect.Size);
            }

            KaynakBox.Image = islem;
        }
Beispiel #10
0
        public static IntPoint[] DetectSurface(Bitmap image)
        {
            var blobCounter = new BlobCounter
            {
                FilterBlobs = true,
                MinHeight   = 10,
                MinWidth    = 10
            };

            var shapes       = new List <Shape>();
            var shapeChecker = new SimpleShapeChecker();

            var threshold = GetThreshold(image);

            blobCounter.BackgroundThreshold = Color.FromArgb(255, threshold, threshold, threshold);
            blobCounter.ProcessImage(image);
            var blobs = blobCounter.GetObjectsInformation();

            foreach (var blob in blobs)
            {
                if (blob.Fullness >= 1)
                {
                    continue;
                }
                var edgePoints = blobCounter.GetBlobsEdgePoints(blob);
                if (shapeChecker.IsQuadrilateral(edgePoints, out List <IntPoint> corners))
                {
                    shapes.Add(new Shape {
                        Blob = blob, Corners = corners
                    });
                }
            }

            var bestShape = shapes.OrderBy(s => s.Blob.Area)
                            .Skip(shapes.Count / 2)
                            .Take(1).FirstOrDefault();

            return(bestShape?.Corners?.ToArray());
        }
        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);
        }
        // private static readonly string modelsDirectory = Path.Combine(Environment.CurrentDirectory, @"ML\OnnxModels");


        /*
         * private void LoadModel()
         * {
         *  // Check for an Onnx model exported from Custom Vision
         *  var customVisionExport = Directory.GetFiles(modelsDirectory, "*.zip").FirstOrDefault();
         *
         *  // If there is one, use it.
         *  if (customVisionExport != null)
         *  {
         *      var customVisionModel = new CustomVisionModel(customVisionExport);
         *      var modelConfigurator = new OnnxModelConfigurator(customVisionModel);
         *
         *      outputParser = new OnnxOutputParser(customVisionModel);
         *      customVisionPredictionEngine = modelConfigurator.GetMlNetPredictionEngine<CustomVisionPrediction>();
         *  }
         *  else // Otherwise default to Tiny Yolo Onnx model
         *  {
         *      var tinyYoloModel = new TinyYoloModel(Path.Combine(modelsDirectory, "TinyYolo2_model.onnx"));
         *      var modelConfigurator = new OnnxModelConfigurator(tinyYoloModel);
         *
         *      outputParser = new OnnxOutputParser(tinyYoloModel);
         *      tinyYoloPredictionEngine = modelConfigurator.GetMlNetPredictionEngine<TinyYoloPrediction>();
         *  }
         * }
         *
         *
         *
         */



        public HeatMapPluginService()
        {
            //load Model
            //LoadModel();


            InitializeComponent();


            // Test Parameters
            //Item _newItem = Configuration.Instance.GetItem(new Guid("d198ae21-1aba-48fa-83d5-f0aa191439f9"), new Guid("5135ba21-f1dc-4321-806a-6ce2017343c0"));
            Item _newItem = Configuration.Instance.GetItem(new Guid("D198AE21-1ABA-48FA-83D5-F0AA191439F9"), new Guid("5135ba21-f1dc-4321-806a-6ce2017343c0"));

            //Item _newItem = Configuration.Instance.GetItem(new Guid("DD826D1C-E703-4FED-8124-7D67ECEAA317"), new Guid("5135ba21-f1dc-4321-806a-6ce2017343c0"));



            OpenStream(_newItem);

            blobCounter = new BlobCounter();

            // Initialize Analyc Image Process
            analyticsImageProcessing = new AnalyticsImageProcessing();

            // Start Metadata Device service
            metadataHandler          = new MetadataHandler();
            _metadataProviderChannel = metadataHandler.OpenHTTPService();

            _metadataProviderChannel.SessionOpening += MetadataProviderSessionOpening;
            _metadataProviderChannel.SessionClosed  += MetadataProviderSessionClosed;

            // Start Communication Manager
            MessageCommunicationManager.Start(EnvironmentManager.Instance.MasterSite.ServerId);
            _messageCommunication = MessageCommunicationManager.Get(EnvironmentManager.Instance.MasterSite.ServerId);

            // Create a fiter to get messages from Smart Client Plugin
            _messageCommunication.RegisterCommunicationFilter(HeatMapSearchHandler, new VideoOS.Platform.Messaging.CommunicationIdFilter("analyticsHeatMapSearch"));
        }
        /// <summary>
        /// We process the image applying all the filters, then we filter the blobs and we find the border or the biggest one
        /// </summary>
        /// <param name="bitmap"></param>
        /// <returns></returns>
        private List <IntPoint> FindCorners(Bitmap bitmap)
        {
            List <IntPoint> corners = new List <IntPoint>();

            using (var clone = bitmap.Clone() as Bitmap)
            {
                new EuclideanColorFiltering(new AForge.Imaging.RGB((byte)Red, (byte)Green, (byte)Blue), Radius).ApplyInPlace(clone);
                using (var grayscaledBitmap = Grayscale.CommonAlgorithms.BT709.Apply(clone))
                {
                    new Threshold(Threshold).ApplyInPlace(grayscaledBitmap);
                    if (Inverted)
                    {
                        new Invert().ApplyInPlace(grayscaledBitmap);
                    }
                    BlobCounter blobCounter = new BlobCounter();
                    blobCounter.FilterBlobs  = true;
                    blobCounter.MinWidth     = 50;
                    blobCounter.MinHeight    = 50;
                    blobCounter.ObjectsOrder = ObjectsOrder.Size;
                    blobCounter.ProcessImage(grayscaledBitmap);
                    Blob[] blobs = blobCounter.GetObjectsInformation();
                    // create convex hull searching algorithm
                    GrahamConvexHull hullFinder = new GrahamConvexHull();
                    for (int i = 0, n = blobs.Length; i < n; i++)
                    {
                        List <IntPoint> leftPoints, rightPoints;
                        List <IntPoint> edgePoints = new List <IntPoint>();
                        // get blob's edge points
                        blobCounter.GetBlobsLeftAndRightEdges(blobs[i], out leftPoints, out rightPoints);
                        edgePoints.AddRange(leftPoints);
                        edgePoints.AddRange(rightPoints);
                        // blob's convex hull
                        corners = hullFinder.FindHull(edgePoints);
                    }
                }
            }
            return(corners);
        }
Beispiel #14
0
        void FinalVideo_NewFrame(object sender, NewFrameEventArgs eventArgs)
        {
            Icon                    newIcon = new Icon(@"c:\users\gregster\documents\visual studio 2012\Projects\WebCamTrack\WebCamTrack\bin\Debug\favicon.ico");
            BlobCounter             bc      = new BlobCounter();
            EuclideanColorFiltering filter  = new EuclideanColorFiltering();
            Bitmap                  video   = (Bitmap)eventArgs.Frame.Clone(); //sem filtro
            Bitmap                  video1  = (Bitmap)eventArgs.Frame.Clone(); // imagem com filtro

            //
            filter.CenterColor = new RGB(0, 0, 0);
            filter.Radius      = 100;
            filter.ApplyInPlace(video1);//aplicando o filtro
            bc.MinWidth    = 5;
            bc.MinHeight   = 5;
            bc.FilterBlobs = true;
            //  bc.ObjectsOrder = ObjectsOrder.Size;
            bc.ProcessImage(video1);// processando a imagem que ja foi filtrada para identificar objetos
            Rectangle[] rects = bc.GetObjectsRectangles();
            foreach (Rectangle recs in rects)
            {
                if (rects.Length > 0)
                {
                    Rectangle objectRect = rects[0];
                    Graphics  g          = Graphics.FromImage(video);//identificar objetos a partir da imagem com filtro
                    Graphics  h          = Graphics.FromImage(video1);
                    using (Pen pen = new Pen(Color.FromArgb(160, 255, 160), 5))
                    {
                        g.DrawIcon(newIcon, objectRect);
                        // g.DrawRectangle(pen, objectRect);
                        h.DrawRectangle(pen, objectRect);
                    }
                    g.Dispose();
                    h.Dispose();
                }
            }
            pictureBox1.Image = video;
            pictureBox2.Image = video1;
        }
        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);
        }
Beispiel #16
0
        private static void DrawMainObjectEdges(ref Bitmap mainObject, BlobCounter blobCounter)
        {
            List <IntPoint> corners = new();
            // create convex hull searching algorithm
            GrahamConvexHull hullFinder = new();

            foreach (Blob blob in blobCounter.GetObjectsInformation())
            {
                // get blob's edge points
                List <IntPoint> edgePoints = blobCounter.GetBlobsEdgePoints(blob);
                corners.AddRange(edgePoints);
                // blob's convex hull
                corners = hullFinder.FindHull(corners);
            }

            if (corners.Any())
            {
                //using (Graphics g = Graphics.FromImage(mainObject))
                //using (Pen pen = new Pen(Color.FromArgb(160, 255, 160), 3))
                //{
                //    g.DrawPolygon(pen, corners.Select(c => new Point(c.X, c.Y)).ToArray());
                //}

                GraphicsPath graphicsPath = new();
                graphicsPath.AddPolygon(corners.Select(c => new Point(c.X, c.Y)).ToArray());
                RectangleF mainObjectBounds = graphicsPath.GetBounds();

                Region clipRegion = new(graphicsPath);
                clipRegion.Translate(-mainObjectBounds.X, -mainObjectBounds.Y);

                Bitmap clippedImage = new((int)mainObjectBounds.Width, (int)mainObjectBounds.Height);                   //, PixelFormat.Format32bppArgb);
                using var g = Graphics.FromImage(clippedImage);
                g.Clip      = clipRegion;                                                                               // restrict drawing region
                g.DrawImage(mainObject, -mainObjectBounds.X, -mainObjectBounds.Y, mainObject.Width, mainObject.Height); // draw clipped

                mainObject = clippedImage;
            }
        }
Beispiel #17
0
        public BlobCounter AnalyzePicture(TargetProfile target, System.Drawing.Bitmap porcessedImg)
        {
            EuclideanColorFiltering filter = new EuclideanColorFiltering();

            filter.CenterColor = new RGB(target.Color);
            filter.Radius      = target.FilterRadius;
            filter.ApplyInPlace(porcessedImg);

            BlobCounter blobCounter = new BlobCounter();

            blobCounter.MinWidth     = target.MinSize;
            blobCounter.MinHeight    = target.MinSize;
            blobCounter.FilterBlobs  = true;
            blobCounter.ObjectsOrder = ObjectsOrder.Size;
            blobCounter.MaxWidth     = blobCounter.MaxHeight = target.MaxSize;

            System.Drawing.Imaging.BitmapData objectsData = porcessedImg.LockBits(new System.Drawing.Rectangle(0, 0, porcessedImg.Width, porcessedImg.Height), ImageLockMode.ReadOnly, porcessedImg.PixelFormat);
            Grayscale      grayscaleFilter = new Grayscale(0.2125, 0.7154, 0.0721);
            UnmanagedImage grayImage       = grayscaleFilter.Apply(new UnmanagedImage(objectsData));

            porcessedImg.UnlockBits(objectsData);
            return(blobCounter);
        }
Beispiel #18
0
        private void nesnebul(Bitmap image)
        {
            BlobCounter bc = new BlobCounter();

            bc.MinWidth     = 10;
            bc.MinHeight    = 10;
            bc.FilterBlobs  = true;
            bc.ObjectsOrder = ObjectsOrder.Size;

            bc.ProcessImage(image);
            Rectangle[] rects = bc.GetObjectsRectangles();
            Blob[]      blobs = bc.GetObjectsInformation();
            pictureBox2.Image = image;

            foreach (Rectangle rect in rects)
            {
                Rectangle obje = rects[i];
                int       obX  = obje.X + (obje.Width / 2);
                int       obY  = obje.Y + (obje.Height / 2);
                x = obX;
                y = obY;
            }
        }
Beispiel #19
0
        public static List <Rectangle> FindRectangles(Bitmap image, bool invert, int?minWidth, int?maxWidth, int?minHeight, int?maxHeight)
        {
            List <Rectangle> rectangles   = new List <Rectangle>();
            Bitmap           workingImage = new Bitmap(image);

            if (invert)
            {
                workingImage = InvertImage(image);
            }
            BlobCounter blobCounter = new BlobCounter();

            blobCounter.ProcessImage((Bitmap)workingImage);
            var foundRectangles = blobCounter.GetObjectsRectangles();

            foreach (var foundRectangle in foundRectangles)
            {
                if (RectangleFitInBounds(foundRectangle, workingImage, minWidth, maxWidth, minHeight, maxHeight))
                {
                    rectangles.Add(foundRectangle);
                }
            }
            return(rectangles);
        }
Beispiel #20
0
        private static Bitmap ClipMainObject(Bitmap image, BlobCounter blobCounter)
        {
            List <IntPoint> corners = new();

            // Create convex hull searching algorithm
            GrahamConvexHull hullFinder = new();

            foreach (Blob blob in blobCounter.GetObjectsInformation())
            {
                // Get blob's edge points
                List <IntPoint> edgePoints = blobCounter.GetBlobsEdgePoints(blob);
                corners.AddRange(edgePoints);

                // Blob's convex hull
                corners = hullFinder.FindHull(corners);
            }

            if (corners.Any())
            {
                GraphicsPath graphicsPath = new GraphicsPath();
                graphicsPath.AddPolygon(corners.Select(c => new Point(c.X, c.Y)).ToArray());
                RectangleF mainObjectBounds = graphicsPath.GetBounds();

                // Move region to top left corner
                Region clipRegion = new Region(graphicsPath);
                clipRegion.Translate(-mainObjectBounds.X, -mainObjectBounds.Y);

                // Draw selected region
                Bitmap clippedImage = new((int)mainObjectBounds.Width, (int)mainObjectBounds.Height);    //, PixelFormat.Format32bppArgb);
                using var g = Graphics.FromImage(clippedImage);
                g.Clip      = clipRegion;                                                                // Restrict drawing region
                g.DrawImage(image, -mainObjectBounds.X, -mainObjectBounds.Y, image.Width, image.Height); // Draw clipped
                return(clippedImage);
            }

            return(image);
        }
Beispiel #21
0
        /// <summary>
        /// Finds circles - zoom is NOT compensated for in returned results
        /// </summary>
        /// <returns>Graphic coordinates with zoom not accounted for for found circles</returns>
        public static List <Shapes.Circle> FindCircles(VideoProcessing vp, Bitmap frame)
        {
            // locating objects
            if (frame == null)
            {
                return(null);
            }
            BlobCounter blobCounter = new BlobCounter();

            blobCounter.FilterBlobs = true;
            blobCounter.MinHeight   = 5;
            blobCounter.MinWidth    = 5;
            blobCounter.ProcessImage(frame);
            Blob[] blobs = blobCounter.GetObjectsInformation();

            List <Shapes.Circle> Circles = new List <Shapes.Circle>();

            for (int i = 0, n = blobs.Length; i < n; i++)
            {
                SimpleShapeChecker shapeChecker = new SimpleShapeChecker();
                List <IntPoint>    edgePoints   = blobCounter.GetBlobsEdgePoints(blobs[i]);
                Point center;
                float radius;

                // is circle ?
                if (shapeChecker.IsCircle(edgePoints, out center, out radius))
                {
                    if (radius > 3)    // filter out some noise
                    {
                        var circle = new Shapes.Circle(center.X, center.Y, radius);
                        Circles.Add(circle);
                    }
                }
            }
            SetVideoProcessing(Circles, vp);
            return(Circles);
        }
Beispiel #22
0
        public List <List <System.Drawing.Point> > GetEdges()
        {
            // lock image
            BitmapData bitmapData = bitmap.LockBits(
                new Rectangle(0, 0, bitmap.Width, bitmap.Height),
                ImageLockMode.ReadWrite, bitmap.PixelFormat);

            // step 1 - turn background to black
            ColorFiltering colorFilter = new ColorFiltering();

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

            colorFilter.ApplyInPlace(bitmapData);

            // step 2 - locating objects
            BlobCounter blobCounter = new BlobCounter();

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

            blobCounter.ProcessImage(bitmapData);
            Blob[] blobs = blobCounter.GetObjectsInformation();
            bitmap.UnlockBits(bitmapData);
            List <List <System.Drawing.Point> > edges = new List <List <System.Drawing.Point> >();

            for (int i = 0; i < blobs.Length; i++)
            {
                List <IntPoint>             edgePoints       = blobCounter.GetBlobsEdgePoints(blobs[i]);
                List <System.Drawing.Point> edgePointsNormal = ToPointsArray(edgePoints);
                edges.Add(edgePointsNormal);
            }
            return(edges);
        }
        /////////Burdan sonra ekrandaki color etrafina Dikdörtgen cizdiriyoruz/////////////
        public void cevresiniciz(Bitmap image)
        {
            BlobCounter blobCounter = new BlobCounter();

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

            Grayscale grayFilter = new Grayscale(0.2125, 0.7154, 0.0721);
            Bitmap    grayImage  = grayFilter.Apply(image);

            blobCounter.ProcessImage(grayImage);
            Rectangle[] rects = blobCounter.GetObjectsRectangles();
            foreach (Rectangle recs in rects)
            {
                if (rects.Length > 0)
                {
                    Rectangle objectRect = rects[0];
                    //Graphics g = Graphics.FromImage(image);
                    Graphics g = pictureBox1.CreateGraphics();
                    using (Pen pen = new Pen(Color.FromArgb(0, 0, 0), 2))
                    {
                        g.DrawRectangle(pen, objectRect);
                    }

                    //Cizdirilen Dikdörtgenin Koordinatlari aliniyor.
                    int objectX = objectRect.X + (objectRect.Width / 2);
                    int objectY = objectRect.Y + (objectRect.Height / 2);
                    //int objectX = objectRect.X;
                    //int objectY = objectRect.Y;
                    g.DrawString(objectX.ToString() + "X" + objectY.ToString(), new Font("Arial", 12), Brushes.Red, new System.Drawing.Point(250, 1));
                    g.Dispose();
                }
            }
        }
Beispiel #24
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);
        }
Beispiel #25
0
        /// <summary>
        /// Based off www.codeproject.com/Articles/10248/Motion-Detection-Algorithms
        /// </summary>
        /// <param name="thresholdImage"></param>
        /// <returns></returns>
        private List <Rectangle> GetLocationRectangles(Bitmap thresholdImage)
        {
            int size = thresholdImage.Size.Width / 20;

            BlobCounter blobCounter = new BlobCounter();

            blobCounter.ProcessImage(thresholdImage);
            Rectangle[] rects = blobCounter.GetObjectsRectangles();


            List <Rectangle> largeBlobRects = new List <Rectangle>();

            foreach (Rectangle rc in rects)
            {
                if ((rc.Width < size) && (rc.Height < size * 2.5))
                {
                    continue;
                }

                largeBlobRects.Add(rc);
            }

            return(largeBlobRects.OrderBy(x => x.Height * x.Width).ToList());
        }
Beispiel #26
0
        private void button6_Click(object sender, EventArgs e)
        {
            Bitmap      İmage       = (Bitmap)pictureBox5.Image;
            BlobCounter blobCounter = new BlobCounter();

            blobCounter.FilterBlobs = true;
            blobCounter.MinHeight   = 10;
            blobCounter.MinWidth    = 10;
            blobCounter.MaxWidth    = 350;
            blobCounter.MaxHeight   = 350;
            blobCounter.ProcessImage(İmage);
            Rectangle[] rects = blobCounter.GetObjectsRectangles();
            Graphics    g     = Graphics.FromImage(İmage);

            if (rects.Length == 0)
            {
                System.Windows.Forms.MessageBox.Show("No rectangle found in image ");
            }
            else if (rects.Length > 1)
            {
                Console.WriteLine("Using largest rectangle found in image ");
                var       r2         = rects.OrderByDescending(r => r.Height * r.Width).ToList();
                Rectangle objectRect = r2[1];
                using (Pen pen = new Pen(Color.FromArgb(160, 255, 160), 5))
                {
                    g.DrawRectangle(pen, objectRect);
                }
                g.Dispose();
            }
            else
            {
                Console.WriteLine("Huh? on image ");
            }

            pictureBox6.Image = İmage;
        }
Beispiel #27
0
        private Bitmap Select(Bitmap İmage)
        {
            BlobCounter blobCounter = new BlobCounter();

            blobCounter.FilterBlobs = true;
            blobCounter.ProcessImage(İmage);
            Rectangle[] rects = blobCounter.GetObjectsRectangles();
            if (rects.Length == 0)
            {
                System.Windows.Forms.MessageBox.Show("No rectangle found in image ");
            }
            else if (rects.Length > 1)
            {
                // get largets rect
                Console.WriteLine("Using largest rectangle found in image ");
                var r2 = rects.OrderByDescending(r => r.Height * r.Width).ToList();
                İmage = İmage.Clone(r2[3], İmage.PixelFormat);
            }
            else
            {
                Console.WriteLine("Huh? on image ");
            }
            return(İmage);
        }
Beispiel #28
0
        public List <IntPoint> GetDisplayCorner(Bitmap bitmap)
        {
            BlobCounter bbc = new BlobCounter();

            bbc.FilterBlobs = true;
            bbc.MinHeight   = 5;
            bbc.MinWidth    = 5;
            bbc.ProcessImage(bitmap);

            Blob[]             blobs        = bbc.GetObjectsInformation();
            SimpleShapeChecker shapeChecker = new SimpleShapeChecker();

            foreach (var blob in blobs)
            {
                List <IntPoint> edgePoints = bbc.GetBlobsEdgePoints(blob);
                List <IntPoint> cornerPoints;

                // use the shape checker to extract the corner points
                if (shapeChecker.IsQuadrilateral(edgePoints, out cornerPoints))
                {
                    // only do things if the corners from a rectangle
                    if (shapeChecker.CheckPolygonSubType(cornerPoints) == PolygonSubType.Rectangle)
                    {
                        flagPoints = cornerPoints;
                        continue;
                    }
                    else
                    {
                        flagPoints = null;
                        continue;
                    }
                }
            }

            return(flagPoints);
        }
Beispiel #29
0
		public void IncrementMultiThread()
		{
			var provider = GlobalSetup.Container.Resolve<IBlobStorageProvider>();
			provider.CreateContainer(ContainerName);

			//creating thread parameters
			var count = new BlobCounter(provider, ContainerName, "SomeBlobName");
			count.Reset(0);

			var random = new Random();
			const int threadsCount = 4;
			var increments = Range.Array(threadsCount).Convert(e => Range.Array(5).Convert(i => random.Next(20) - 10));
			var localSums = increments.SelectInParallel(e =>
				{
					var counter = new BlobCounter(provider, ContainerName, "SomeBlobName");
					foreach (var increment in e)
					{
						counter.Increment(increment);
					}
					return e.Sum();
				}, threadsCount);

			Assert.AreEqual(localSums.Sum(), count.GetValue(), "values should be equal, BlobCounter supposed to be thread-safe");
		}
        private void process_image(PictureBox box, bool toinvert)
        {
            Bitmap original = (Bitmap)Bitmap.FromFile(_fname);

            original.Save("C:\\users\\alberto geniola\\desktop\\dbg\\original_" + toinvert + ".bmp");

            // Setup the Blob counter
            BlobCounter blobCounter = new BlobCounter();

            blobCounter.BackgroundThreshold  = Color.FromArgb(10, 10, 10);
            blobCounter.FilterBlobs          = true;
            blobCounter.CoupledSizeFiltering = false;
            blobCounter.MinHeight            = minHeight;
            blobCounter.MinWidth             = minWidth;

            List <Blob> blobs = new List <Blob>();

            // Button scanning
            // Apply the grayscale. This is needed for AForge's filters
            using (Bitmap grey_scaled = Grayscale.CommonAlgorithms.BT709.Apply(original))
            {
                // Invert the image if requested
                if (toinvert)
                {
                    Invert invert = new Invert();
                    invert.ApplyInPlace(grey_scaled);
                }

                using (Bitmap t1 = new Threshold(64).Apply(grey_scaled))
                {
                    using (var tmp = new Bitmap(t1.Width, t1.Height))
                    {
                        using (Graphics g = Graphics.FromImage(tmp))
                        {
                            g.DrawImage(t1, 0, 0);
                        }

                        tmp.Save("C:\\users\\alberto geniola\\desktop\\dbg\\filtered_" + toinvert + ".bmp");

                        // The blob counter will analyze the bitmap looking for shapes
                        blobCounter.ProcessImage(tmp);
                        var tmparr = blobCounter.GetObjectsInformation();
                        blobs.AddRange(tmparr);

                        for (int i = 0, n = tmparr.Length; i < n; i++)
                        {
                            List <IntPoint> edgePoints = blobCounter.GetBlobsEdgePoints(blobs[i]);
                            if (edgePoints.Count > 1)
                            {
                                IntPoint p0, p1;
                                PointsCloud.GetBoundingRectangle(edgePoints, out p0, out p1);
                                var r = new Rectangle(p0.X, p0.Y, p1.X - p0.X, p1.Y - p0.Y);

                                // Skip any shape representing the border of the whole window ( +10px padding)
                                if (r.Width >= (original.Width - 10))
                                {
                                    continue;
                                }

                                using (var g = Graphics.FromImage(tmp))
                                {
                                    g.DrawRectangle(_marker, r);
                                }
                            }
                        }

                        tmp.Save("C:\\users\\alberto geniola\\desktop\\dbg\\processed_" + toinvert + ".bmp");
                    }
                }

                using (Bitmap t2 = new SISThreshold().Apply(grey_scaled))
                {
                    using (var tmp = new Bitmap(t2.Width, t2.Height))
                    {
                        using (Graphics g = Graphics.FromImage(tmp))
                        {
                            g.DrawImage(t2, 0, 0);
                        }
                        tmp.Save("C:\\users\\alberto geniola\\desktop\\dbg\\t2_" + toinvert + ".bmp");
                        // The blob counter will analyze the bitmap looking for shapes
                        blobCounter.ProcessImage(tmp);
                        var tmparr = blobCounter.GetObjectsInformation();
                        blobs.AddRange(tmparr);

                        for (int i = 0, n = tmparr.Length; i < n; i++)
                        {
                            List <IntPoint> edgePoints = blobCounter.GetBlobsEdgePoints(blobs[i]);
                            if (edgePoints.Count > 1)
                            {
                                IntPoint p0, p1;
                                PointsCloud.GetBoundingRectangle(edgePoints, out p0, out p1);
                                var r = new Rectangle(p0.X, p0.Y, p1.X - p0.X, p1.Y - p0.Y);

                                // Skip any shape representing the border of the whole window ( +10px padding)
                                if (r.Width >= (original.Width - 10))
                                {
                                    continue;
                                }

                                using (var g = Graphics.FromImage(tmp))
                                {
                                    g.DrawRectangle(_marker, r);
                                }
                            }
                        }

                        tmp.Save("C:\\users\\alberto geniola\\desktop\\dbg\\t1_" + toinvert + ".bmp");
                    }
                }
            }


            Bitmap test = (Bitmap)original.Clone();

            // Let's analyze every single shape
            for (int i = 0, n = blobs.Count; i < n; i++)
            {
                List <IntPoint> edgePoints = blobCounter.GetBlobsEdgePoints(blobs[i]);
                if (edgePoints.Count > 1)
                {
                    IntPoint p0, p1;
                    PointsCloud.GetBoundingRectangle(edgePoints, out p0, out p1);
                    var r = new Rectangle(p0.X, p0.Y, p1.X - p0.X, p1.Y - p0.Y);

                    // Skip any shape representing the border of the whole window ( +10px padding)
                    if (r.Width >= (original.Width - 10))
                    {
                        continue;
                    }

                    using (var g = Graphics.FromImage(test))
                    {
                        g.DrawRectangle(_marker, r);
                    }

                    // This is most-likely a button!
                    // Crop the image and pass it to the OCR engine for text recognition
                    using (Bitmap button = new Bitmap(r.Width, r.Height))
                    {
                        // Scan the original shape
                        String txt = null;
                        using (var g1 = Graphics.FromImage(button))
                        {
                            g1.DrawImage(original, 0, 0, r, GraphicsUnit.Pixel);
                        }

                        // Process OCR on that image
                        txt = scanButton(button);

                        if (String.IsNullOrEmpty(txt))
                        {
                            using (Bitmap tmp = Grayscale.CommonAlgorithms.BT709.Apply(button))
                            {
                                if (toinvert)
                                {
                                    new Invert().ApplyInPlace(tmp);
                                }

                                new SISThreshold().ApplyInPlace(tmp);
                                txt = scanButton(tmp);
                            }
                        }

                        // If still nothing is found, repeat the analysis with the second version of the filter
                        if (String.IsNullOrEmpty(txt))
                        {
                            using (Bitmap tmp = Grayscale.CommonAlgorithms.BT709.Apply(button))
                            {
                                if (toinvert)
                                {
                                    new Invert().ApplyInPlace(tmp);
                                }
                                new Threshold(64).ApplyInPlace(tmp);
                                txt = scanButton(tmp);
                            }
                        }

                        if (!String.IsNullOrEmpty(txt))
                        {
                            using (var g = Graphics.FromImage(test))
                            {
                                int    SPACING = 5;
                                double angle   = 45 * 2 * Math.PI / 360; // 45 degrees to radiants
                                for (int x = 0; x < r.Width; x += SPACING)
                                {
                                    PointF start = new PointF(r.X + x, r.Y);
                                    PointF end   = new PointF((float)(r.X + x + r.Height * Math.Tan(angle)), r.Y + r.Height);
                                    if (end.X > (r.X + r.Width))
                                    {
                                        // Calculate midpoint
                                        var delta = end.X - r.Width;
                                        end.X = r.X + r.Width;
                                        end.Y = r.Y + (float)(Math.Tan(angle) * r.Width) - x;

                                        // Draw the overflow line
                                        g.DrawLine(_marker2, r.X, end.Y, delta, r.Y + r.Height);
                                    }

                                    g.DrawLine(_marker2, start, end);
                                }

                                g.FillRectangle(_b, r);
                                var dim = g.MeasureString(txt.Trim(), _f);
                                g.DrawString(txt.Trim().ToUpper(), _f, _b2, r.X + (r.Width - dim.Width) / 2, r.Y + (r.Height - dim.Height) / 2);
                            }
                        }

                        test.Save("C:\\users\\alberto geniola\\desktop\\dbg\\processed_" + toinvert + ".bmp");

                        /*
                         * // At this point we should have a result. Add it to list if it does not overlap any UIAutomated element
                         * UIControlCandidate t = new UIControlCandidate();
                         * t.PositionWindowRelative = r;
                         * var winLoc = w.WindowLocation;
                         * t.PositionScreenRelative = new Rectangle(r.X + winLoc.X, r.Y + winLoc.Y, r.Width, r.Height);
                         * t.Text = txt;
                         * t.Score = policy.RankElement(t);
                         *
                         * // If the item falls into the same area of a UI element, ignore it.
                         * bool overlaps = false;
                         * foreach (var el in res)
                         * {
                         *  if (el.AutoElementRef != null && el.PositionScreenRelative.IntersectsWith(t.PositionScreenRelative))
                         *  {
                         *      overlaps = true;
                         *      break;
                         *  }
                         * }
                         * if (!overlaps)
                         *  res.Add(t);
                         */
                    }
                }

                box.Image = test;
            }
        }
Beispiel #31
0
        //kameradan alınan görüntünün işleme kısmı

        private void FinalFrame_newFrame(object sender, NewFrameEventArgs eventArgs)
        {
            video_1 = (Bitmap)eventArgs.Frame.Clone();
            video_2 = (Bitmap)eventArgs.Frame.Clone();



            Mirror filter2 = new Mirror(false, true);

            filter2.ApplyInPlace(video_1);

            EuclideanColorFiltering filter = new EuclideanColorFiltering();

            filter.CenterColor = new RGB(Color.FromArgb(R, G, B));
            filter.Radius      = 100;
            filter.ApplyInPlace(video_1);
            BitmapData     objectsData = video_1.LockBits(new Rectangle(0, 0, video_1.Width, video_1.Height), ImageLockMode.ReadOnly, video_1.PixelFormat);
            Grayscale      grifiltresi = new Grayscale(0.2125, 0.7154, 0.0721); //Gri filtresi
            UnmanagedImage griresim    = grifiltresi.Apply(new UnmanagedImage(objectsData));

            video_1.UnlockBits(objectsData);
            BlobCounter blobcounter = new BlobCounter();  //birleşik pikselleri numaralandırma işlemi, nesne sayma

            blobcounter.MinWidth     = 18;                //sayılan nesnelerin minimum genişliği
            blobcounter.MinHeight    = 18;                //"" yüksekliği
            blobcounter.FilterBlobs  = true;              //şartlara uyanların işaretlenmesi
            blobcounter.ObjectsOrder = ObjectsOrder.Size; //boyutlarına göre sıralamaya sokma işlemi


            blobcounter.ProcessImage(griresim);                     //videodaki bloblar sayılıp işleniyor
            Rectangle[] rects = blobcounter.GetObjectsRectangles(); //blobcounter içerisindeki blobların verileri alınıyor
            Blob[]      blobs = blobcounter.GetObjectsInformation();
            pictureBox2.Image = video_2;

            foreach (Rectangle recs in rects)
            {
                Graphics g = Graphics.FromImage(video_1);
                if (rects.Length > 0)
                {
                    Rectangle objectRect = rects[0];
                    x = objectRect.X + (objectRect.Width / 2);
                    y = objectRect.Y + (objectRect.Height / 2);

                    if (x < 213 && y < 160)
                    {
                        serialPort1.Write("1");
                    }
                    if ((213 < x && x < 426) && y < 160)
                    {
                        serialPort1.Write("2");
                    }
                    if (426 < x && x < 640 && y < 160)
                    {
                        serialPort1.Write("3");
                    }
                    if ((x < 213 && (160 < y && y < 320)))
                    {
                        serialPort1.Write("4");
                    }
                    if ((213 < x && x < 426) && (160 < y && y < 320))
                    {
                        serialPort1.Write("5");
                    }
                    if ((426 < x && x < 639) && (160 < y && y < 320))
                    {
                        serialPort1.Write("6");
                    }
                    if (x < 213 && (320 < y && y < 480))
                    {
                        serialPort1.Write("7");
                    }
                    if ((213 < x && x < 426) && (320 < y && y < 480))
                    {
                        serialPort1.Write("8");
                    }
                    if ((426 < x && x < 639) && (320 < y && y < 480))
                    {
                        serialPort1.Write("9");
                    }


                    using (Pen pen = new Pen(Color.FromArgb(252, 3, 26), 2))
                    {
                        g.DrawRectangle(pen, objectRect);
                    }

                    g.DrawString(x.ToString() + "X" + y.ToString(), new Font("Arial", 12), Brushes.Red, new System.Drawing.Point(x, y));
                    g.Dispose();
                }
            }
            pictureBox1.Image = video_1;
        }
        /// <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;
        }
Beispiel #33
0
        public void nesnebul(Bitmap image)
        {
            BlobCounter blobCounter = new BlobCounter();

            blobCounter.MinWidth     = 10;
            blobCounter.MinHeight    = 10;
            blobCounter.FilterBlobs  = true; // Parazitleri atar
            blobCounter.ObjectsOrder = ObjectsOrder.Size;
            BitmapData     objectsData     = image.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadOnly, image.PixelFormat);
            Grayscale      grayscaleFilter = new Grayscale(0.2125, 0.7154, 0.0721);
            UnmanagedImage grayImage       = grayscaleFilter.Apply(new UnmanagedImage(objectsData));

            image.UnlockBits(objectsData);
            blobCounter.ProcessImage(image);
            Rectangle[] rects = blobCounter.GetObjectsRectangles();
            Blob[]      blobs = blobCounter.GetObjectsInformation();
            pictureBox2.Image = image;
            if (radioButton1.Checked)
            {
                for (int i = 0; rects.Length > i; i++)
                {
                    Rectangle objectRect = rects[i];
                    Graphics  g          = pictureBox1.CreateGraphics();
                    if (objectRect.Width * objectRect.Height < 6500 && objectRect.Width * objectRect.Height > 3250)
                    {
                        using (Pen pen = new Pen(Color.FromArgb(252, 3, 26), 2))
                        {
                            g.DrawRectangle(pen, objectRect);
                            g.DrawString((i + 1).ToString(), new Font("Arial", 12), Brushes.Red, objectRect);
                        }

                        int objectX = objectRect.X + (objectRect.Width / 2);
                        int objectY = objectRect.Y + (objectRect.Height / 2);

                        if (objectY < 100)
                        {
                            if (objectX < 250)
                            {
                                WriteToPort("a");
                            }
                            else
                            {
                                WriteToPort("d");
                            }
                        }
                        else if (objectY > 200)
                        {
                            if (objectX > 250)
                            {
                                WriteToPort("c");
                            }
                            else
                            {
                                WriteToPort("f");
                            }
                        }
                        else
                        {
                            if (objectX < 250)
                            {
                                WriteToPort("b");
                            }
                            else
                            {
                                WriteToPort("e");
                            }
                        }
                    }
                    g.Dispose();
                }
            }
        }
        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);
        }
Beispiel #35
0
        private void recognize_Click(object sender, EventArgs e)
        {
            label15.Text = "";
            if (etalonimages.Count == 0)
            {
                foreach (PictureBox pb in etalonpanel.Controls.OfType <PictureBox>())
                {
                    if (pb.Image != null)
                    {
                        etalonimages.Add((Bitmap)pb.Image);
                    }
                }

                //чтение из файлов параметров

                ContourEtalCentr = ReadFromFile("ContourCentr.txt", 18);
                HullEtalCentr    = ReadFromFile("HullCentr.txt", 28);

                ContourEtalMedoid = ReadFromFile("ContourMed.txt", 18);
                HullEtalMedoid    = ReadFromFile("HullCentrMed.txt", 28);
            }
            Grayscale grayfilter = new Grayscale(0.2125, 0.7154, 0.0721);

            // применяем фильтр
            grayImage = grayfilter.Apply((Bitmap)vebpb.Image);
            //выводим на пичербокс
            //pictureBox1.Image = grayImage;

            //Application.Idle -= GetVideo;
            mainImage = grayImage.Clone(
                new Rectangle(0, 0, grayImage.Width, grayImage.Height),
                System.Drawing.Imaging.PixelFormat.Format8bppIndexed);


            //медианный фильтр для шумов границ размытых объектов
            Median filter1 = new Median();

            filter1.ApplyInPlace(mainImage);


            OtsuThreshold filter = new OtsuThreshold();

            // apply the filter

            filter.ApplyInPlace(mainImage);
            // check threshold value
            Invert filter2 = new Invert();

            // apply the filter
            filter2.ApplyInPlace(mainImage);


            // исправили потомучто надо
            //RecursiveBlobCounter bc = new RecursiveBlobCounter();
            BlobCounter bc = new BlobCounter();

            // process binary image
            bc.ProcessImage(mainImage);

            Rectangle[] rects = bc.GetObjectsRectangles();

            List <Bitmap> images         = new List <Bitmap>();
            List <Bitmap> imagesWithEdge = new List <Bitmap>();
            Bitmap        bp;
            int           i = 0;

            foreach (Rectangle rect in rects)
            {
                images.Add(new Bitmap(mainImage.Width, mainImage.Height));
                Graphics   g = Graphics.FromImage(images[i]);                               //получаю объект графики из битмап
                SolidBrush b = new SolidBrush(Color.Black);                                 //кисть для заливки
                g.FillRectangle(b, new Rectangle(0, 0, images[i].Width, images[i].Height)); //заполняю
                bp = mainImage.Clone(rects[i], System.Drawing.Imaging.PixelFormat.Format8bppIndexed);
                g.DrawImage(bp, rects[i].X, rects[i].Y, rects[i].Width, rects[i].Height);
                i++;
            }

            //List <List<System.Drawing.Point>> convexHull = new List<List<System.Drawing.Point>>();
            ProcessingImage             pi      = new ProcessingImage();
            QuickHull                   qh      = new QuickHull();
            Contur                      cont    = new Contur();
            List <System.Drawing.Point> localPt = new List <System.Drawing.Point>();

            foreach (Bitmap img in images)
            {
                //формирование выпуклой оболочки
                localPt = pi.GetPoints(img);
                List <System.Drawing.Point> ConvexHullLocal = qh.quickHull(localPt);
                ConvexHullLocal = qh.DeleteAnglePoints(ConvexHullLocal);
                convexHull.Add(ConvexHullLocal);
                //и контура
                List <System.Drawing.Point> ConturLocal = cont.kontur(img);
                ConturLocal = cont.DeleteAnglePoints(ConturLocal);
                edgePoint.Add(ConturLocal);
                imagesWithEdge.Add(img.Clone(new Rectangle(0, 0, img.Width, img.Height), System.Drawing.Imaging.PixelFormat.Format32bppRgb));
            }

            int hullimagenum = 0;

            foreach (PictureBox pb in Hullpanel.Controls)
            {
                if (hullimagenum < images.Count)
                {
                    pb.Image = images[hullimagenum];
                }
                hullimagenum++;
            }


            //выделение векторов признаков
            List <Primary>  marks                 = new List <Primary>();
            List <double[]> objectMarksHull       = new List <double[]>();
            List <double[]> objectMarksContur     = new List <double[]>();
            List <double[]> etallMarksCentrHull   = new List <double[]>();
            List <double[]> etallMarksCentrContur = new List <double[]>();
            List <double[]> etallMarksMedHull     = new List <double[]>();
            List <double[]> etallMarksMedContur   = new List <double[]>();

            for (i = 0; i < images.Count; i++)
            {
                marks.Add(new Primary(images[i], edgePoint[i], convexHull[i]));
                objectMarksContur.Add(marks[i].Contour());
                objectMarksHull.Add(marks[i].Convex());
            }

            //Отрисовка выпуклой оболочки
            for (i = 0; i < convexHull.Count; i++)
            {
                Graphics gr1 = Graphics.FromImage(images[i]);
                List <System.Drawing.Point> pt = new List <System.Drawing.Point>(convexHull[i]);
                for (int j = 0; j < pt.Count; j++)
                {
                    qh.PutPixel(gr1, Color.Red, pt[j].X, pt[j].Y, 255);
                }
                gr1 = Graphics.FromImage(imagesWithEdge[i]);
                List <System.Drawing.Point> pt1 = new List <System.Drawing.Point>(edgePoint[i]);
                for (int j = 0; j < pt1.Count; j++)
                {
                    cont.PutPixel(gr1, Color.Red, pt1[j].X, pt1[j].Y, 255);
                }
            }

            int contourimagenum = 0;

            foreach (PictureBox pb in Contourpanel.Controls)
            {
                if (contourimagenum < imagesWithEdge.Count)
                {
                    pb.Image = imagesWithEdge[contourimagenum];
                }
                contourimagenum++;
            }

            //int hullimagenum = 0;
            //foreach (PictureBox pb in Hullpanel.Controls)
            //{
            //    if (hullimagenum < images.Count)
            //        pb.Image = images[hullimagenum];
            //    hullimagenum++;
            //}


            for (int j = 0; j < etalonimages.Count; j++)
            {
                etallMarksCentrContur.Add(ContourEtalCentr[j]);
                etallMarksMedContur.Add(ContourEtalMedoid[j]);
                etallMarksCentrHull.Add(HullEtalCentr[j]);
                etallMarksMedHull.Add(HullEtalMedoid[j]);
            }


            for (int l = 0; l < images.Count; l++)
            {
                compareobjects(objectMarksHull,
                               objectMarksContur,
                               etallMarksCentrContur,
                               etallMarksMedContur,
                               etallMarksCentrHull,
                               etallMarksMedHull, l);
            }


            images.Clear();
            convexHull.Clear();
            edgePoint.Clear();
            imagesWithEdge.Clear();
            marks.Clear();
            //contourMarks.Clear();
            i = 0;
        }
Beispiel #36
0
        private void etal1btn_Click(object sender, EventArgs e)
        {
            Grayscale grayfilter = new Grayscale(0.2125, 0.7154, 0.0721);

            // применяем фильтр
            grayImage = grayfilter.Apply((Bitmap)vebpb.Image);
            //выводим на пичербокс
            //pictureBox1.Image = grayImage;

            mainImage = grayImage.Clone(
                new Rectangle(0, 0, grayImage.Width, grayImage.Height),
                System.Drawing.Imaging.PixelFormat.Format8bppIndexed);
            Bitmap Image24 = grayImage.Clone(
                new Rectangle(0, 0, grayImage.Width, grayImage.Height),
                System.Drawing.Imaging.PixelFormat.Format24bppRgb);
            Bitmap Image8 = grayImage.Clone(
                new Rectangle(0, 0, grayImage.Width, grayImage.Height),
                System.Drawing.Imaging.PixelFormat.Format8bppIndexed);
            ProcessingImage pi     = new ProcessingImage();
            OtsuThreshold   filter = new OtsuThreshold();

            // apply the filter
            filter.ApplyInPlace(Image8);

            // check threshold value
            Invert filter1 = new Invert();

            ////// apply the filter
            filter1.ApplyInPlace(Image8);


            //изменить блобкаунтер
            BlobCounter bc = new BlobCounter();

            // process binary image
            bc.ProcessImage(Image8);

            Rectangle[] rects = bc.GetObjectsRectangles();

            Bitmap bp      = new Bitmap(Image8);
            Bitmap final   = new Bitmap(Image24.Width, Image24.Height);
            Bitmap bp8     = new Bitmap(Image8);
            int    xCenter = Image8.Width / 2;
            int    yCenter = Image8.Height / 2;

            Bitmap tempImage = grayImage.Clone(
                new Rectangle(0, 0, grayImage.Width, grayImage.Height),
                System.Drawing.Imaging.PixelFormat.Format24bppRgb);
            Graphics g = Graphics.FromImage(tempImage);  //получаю объект графики из битмап

            SolidBrush b = new SolidBrush(Color.Black);  //кисть для заливки

            bp  = tempImage.Clone(rects[0], System.Drawing.Imaging.PixelFormat.Format24bppRgb);
            bp8 = Image8.Clone(rects[0], System.Drawing.Imaging.PixelFormat.Format8bppIndexed);
            g.FillRectangle(b, new Rectangle(0, 0, tempImage.Width, tempImage.Height)); //заполняю
            for (int x = 0; x < bp8.Width; x++)
            {
                for (int y = 0; y < bp8.Height; y++)
                {
                    if (bp8.GetPixel(x, y).R == 0)
                    {
                        bp.SetPixel(x, y, Color.Black);
                    }
                }
            }
            int shiftX = xCenter - rects[0].Width / 2;
            int shiftY = yCenter - rects[0].Height / 2;

            g.DrawImage(bp, shiftX, shiftY, rects[0].Width, rects[0].Height);
            //etalonimages.Add(tempImage);

            foreach (PictureBox pb in etalonpanel.Controls.OfType <PictureBox>())
            {
                if (pb.Image == null)
                {
                    pb.Image = tempImage;
                    break;
                }
            }
        }
Beispiel #37
0
        /// <summary>
        ///   Initializes a new instance of the <see cref="HslBlobTracker"/> class.
        /// </summary>
        /// <param name="filter">The filter.</param>
        public HslBlobTracker(HSLFiltering filter)
        {
            this.filter = filter;
            this.blobCounter = new BlobCounter();
            this.trackingObject = new TrackingObject();

            blobCounter.CoupledSizeFiltering = false;
            blobCounter.FilterBlobs = true;
        }
Beispiel #38
0
        /// <summary>
        /// Process the filter on the specified image.
        /// </summary>
        /// 
        /// <param name="image">Source image data.</param>
        ///
        protected override unsafe void ProcessFilter( UnmanagedImage image )
        {
            int width  = image.Width;
            int height = image.Height;

            // 1 - invert the source image
            Invert invertFilter = new Invert( );
            UnmanagedImage invertedImage = invertFilter.Apply( image );

            // 2 - use blob counter to find holes (they are white objects now on the inverted image)
            BlobCounter blobCounter = new BlobCounter( );
            blobCounter.ProcessImage( invertedImage );
            Blob[] blobs = blobCounter.GetObjectsInformation( );

            // 3 - check all blobs and determine which should be filtered
            byte[] newObjectColors = new byte[blobs.Length + 1];
            newObjectColors[0] = 255; // don't touch the objects, which have 0 ID

            for ( int i = 0, n = blobs.Length; i < n; i++ )
            {
                Blob blob = blobs[i];

                if ( ( blob.Rectangle.Left == 0 ) || ( blob.Rectangle.Top == 0 ) ||
                     ( blob.Rectangle.Right == width ) || ( blob.Rectangle.Bottom == height ) )
                {
                    newObjectColors[blob.ID] = 0;
                }
                else
                {
                    if ( ( ( coupledSizeFiltering ) && ( blob.Rectangle.Width <= maxHoleWidth ) && ( blob.Rectangle.Height <= maxHoleHeight ) ) |
                         ( ( !coupledSizeFiltering ) && ( ( blob.Rectangle.Width <= maxHoleWidth ) || ( blob.Rectangle.Height <= maxHoleHeight ) ) ) )
                    {
                        newObjectColors[blob.ID] = 255;
                    }
                    else
                    {
                        newObjectColors[blob.ID] = 0;
                    }
                }
            }

            // 4 - process the source image image and fill holes
            byte* ptr = (byte*) image.ImageData.ToPointer( );
            int offset = image.Stride - width;

            int[] objectLabels = blobCounter.ObjectLabels;

            for ( int y = 0, i = 0; y < height; y++ )
            {
                for ( int x = 0; x < width; x++, i++, ptr++ )
                {
                    *ptr = newObjectColors[objectLabels[i]];
                }
                ptr += offset;
            }
        }
        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);
                }
            }
        }