AForge, ColorTracking multiple Objects

1k Views Asked by At

I'm currently working on Tracking Object by Color on my WebCam, and i have it going so far but i want to add an option to draw multiple Objects. Until now it only Draws a Rectangle around the biggest Object.

BlobCounter blobcounter = new BlobCounter();
blobcounter.MinHeight = 100;
blobcounter.MinWidth = 100;
blobcounter.ObjectsOrder = ObjectsOrder.Size;
blobcounter.ProcessImage(grayImage);
Rectangle[] rects = blobcounter.GetObjectsRectangles();

if (checkBox1.Checked == false)
{
    if (rects.Length > 0)
    {
        Rectangle objectRect1 = rects[0];
        Graphics g = Graphics.FromImage(video);
        using (Pen pen = new Pen(Color.LightGreen, 3))
        {
            g.DrawRectangle(pen, objectRect1);
            PointF drawPoin = new PointF(objectRect1.X, objectRect1.Y);
            int objectX = objectRect1.X + objectRect1.Width / 2 - video.Width / 2;
            int objectY = video.Height / 2 - (objectRect1.Y + objectRect1.Height / 2);
            PointF drawPoin2 = new PointF(objectRect1.X, objectRect1.Y + objectRect1.Height + 4);
            String Blobinformation = "X= " + objectX.ToString() + "  Y= " + objectY.ToString() + "\nSize=" + objectRect1.Width + ", " + objectRect1.Height;
            g.DrawString(Blobinformation, new Font("Arial", 12), new SolidBrush(Color.LightSkyBlue), drawPoin2);
        }
        g.Dispose();
    }
}
else
{
 ??????????
}
1

There are 1 best solutions below

1
On

Adding a simple foreach loop should suffice. I don't know how efficient the drawing is, but I'm almost certain it wont be a problem with a few rectangles.

else
{
    if (rects.Length > 0)
    {
        foreach (Rectangle objectRect in rects)
        {
            Graphics g = Graphics.FromImage(video);
            using (Pen pen = new Pen(Color.LightGreen, 3))
            {
                g.DrawRectangle(pen, objectRect);
                PointF drawPoin = new PointF(objectRect.X, objectRect.Y);
                int objectX = objectRect.X + objectRect.Width / 2 - video.Width / 2;
                int objectY = video.Height / 2 - (objectRect.Y + objectRect.Height / 2);
                PointF drawPoin2 = new PointF(objectRect.X, objectRect.Y + objectRect.Height + 4);
                String Blobinformation = "X= " + objectX.ToString() + "  Y= " + objectY.ToString() + "\nSize=" + objectRect.Width + ", " + objectRect.Height;
                g.DrawString(Blobinformation, new Font("Arial", 12), new SolidBrush(Color.LightSkyBlue), drawPoin2);
            }
            g.Dispose();
        }
    }
}