User0967
User0967

Reputation: 131

Bounding rectangle on foreground objects for classification

I have the the following program that draws bounding boxes on foreground objects. This bounding boxes will aid in classification of objects by length, by measuring the length on one side of the rectangle.

Now, this only draws rectangle on only one object an instance, I would like to make it draw all of them simultaneously. But I'm stuck. Guidance or assistance needed. Please see images attached. Foreground Mask![Original Image with Bounding Box drawn

Rect boundingRect(InputArray contours);
    // Finds the contour with the largest area
    int area = 200;
    int idx = 0;
    for(int i=0; i<contours.size() ;i++) 
    {
        if(area < contours[i].size())
            idx = i; 
    }

    //cout<< contours.size();
    // Calculates the bounding rect of the largest area contour
    Rect rect = boundingRect(contours[idx]);
    Point pt1, pt2;
    pt1.x = rect.x;
    pt1.y = rect.y;
    pt2.x = rect.x + rect.width;
    pt2.y = rect.y + rect.height;
    cout<< idx<< "\t \t";
    // Draws the rect in the original image and show it
    rectangle(frame_Original, pt1, pt2, CV_RGB(0,0,255), 2);
    //cout << pt1; cout << pt2;

Upvotes: 0

Views: 1725

Answers (1)

luhb
luhb

Reputation: 666

If you want to draw all the rectangles in countours whose size are larger than area, you should save the index and then use a loop to draw all of them.

vector<int> idx;
for(int i=0; i < contours.size() ;i++)
{
    if(area < contours[i].size())
        idx.push_back(i);
}
for((int i=0; i < idx.size() ;i++)
    \\ Draw every contours[idx[i]]  

Upvotes: 1

Related Questions