How to find if a blob crossed a line

Ask question on using Emgu CV with C#
Post Reply
rhpillai
Posts: 4
Joined: Thu Jul 05, 2018 2:45 am

How to find if a blob crossed a line

Post by rhpillai » Mon Dec 09, 2019 6:24 am

Hi,

Im trying to program a counting application using a webcam. I draw a line on the picturebox, and if any blob crosses the line, the count increments. But for some reason, when a blob crosses a line, it just keeps incrementing till another blob is introduced in the frame. I want that once a blob crosses the line it should be incremented only by 1. Here is the code:

Code: Select all

  Current_Frame = m.ToImage<Bgr, byte>();
              
               var imgSmoothed = Current_Frame.PyrDown().PyrUp();										//Gaussian pyramid decomposition
               imgSmoothed._SmoothGaussian(5);
               var imgGrayColorFiltered = imgSmoothed.InRange(new Bgr(tblue.Value, tgreen.Value, tred.Value), new Bgr(255, 255, 255));

               Image<Gray, byte> invert = imgGrayColorFiltered;
                
               Image<Gray, byte> threshedimg = new Image<Gray, byte>(invert.Bitmap);
               Image<Bgr, byte> newimg = new Image<Bgr, byte>(threshedimg.Bitmap);

               CvInvoke.AdaptiveThreshold(threshedimg, threshedimg, 255, AdaptiveThresholdType.GaussianC, ThresholdType.BinaryInv, Convert.ToInt32(tthreshold.Value) + Convert.ToInt32(tthreshold.Value) % 2 + 1,1d);
               threshedimg._Not();
               //closing
               Mat kernelCl = CvInvoke.GetStructuringElement(ElementShape.Rectangle, new Size(1,1), new System.Drawing.Point(-1, -1));
               CvInvoke.MorphologyEx(threshedimg, threshedimg, MorphOp.Close, kernelCl, new System.Drawing.Point(-1, -1), 1, BorderType.Default, new MCvScalar());
               threshedimg._Not();  
               Image<Gray, byte> speck = threshedimg;
               CvInvoke.FilterSpeckles(speck, 10.0, 5, 10.5);
                             

               Image<Gray, byte> imginput = new Image<Gray, byte>(speck.Bitmap);
         
//draw line on mouse move and click event
               for (int x = 0; x < p1List.Count; x++)
               {
                   CvInvoke.Line(newimg, p1List[x], p2List[x], new MCvScalar(0, 255, 0), 2);

                   x0 = p1List[x].X;
                   y0 = p1List[x].Y;
                   x1 = p2List[x].X;
                   y1 = p2List[x].Y;

                centroid.X = Convert.ToInt32( (x0 + x1) / 2);
                centroid.Y = Convert.ToInt32((y0 + y1) / 2);
                CvInvoke.PutText(newimg, centroid.X.ToString() + "," + centroid.Y.ToString(), new System.Drawing.Point((int)centroid.X, (int)centroid.Y), FontFace.HersheyPlain, 3.0, new MCvScalar(0, 0, 255), 2);                  
               }
                //DETECT BLOB
             
               _blobDetector22.Detect(imginput, blobs1);
               blobs1.FilterByArea((int)nsizefilter.Value, int.MaxValue);

               float scale1 = (threshedimg.Width + threshedimg.Width) / 2.0f;
               _tracker22.Update(blobs1, 0.01 * scale1, 5, 5);

               Bitmap drawrect = new Bitmap(threshedimg.Bitmap);
               foreach (var pair1 in _tracker22)
               {
                  
                   b1 = pair1.Value;
                   CvInvoke.Rectangle(newimg, b1.BoundingBox, new MCvScalar(255, 0, 0), 5);
                   CvInvoke.PutText(newimg, b1.Id.ToString() + "," + Math.Round(b1.Centroid.Y).ToString(), new System.Drawing.Point((int)Math.Round(b1.Centroid.X), (int)Math.Round(b1.Centroid.Y)), FontFace.HersheyPlain, 3.0, new MCvScalar(0, 0, 255), 2);

                   if (b1.Centroid.Y > centroid.Y )
                   {                   
                      increment++;
                       label1.Text = increment.ToString();
                   }
               }
             
               pictureBox1.Image = newimg.Bitmap;
               }

Post Reply