TheAlPaca02
TheAlPaca02

Reputation: 523

Size of separate bounding boxes?

I want to make a piece of software that detects all the objects in an image and deletes everything except for the biggest one. I got all the separate bounding boxes & contours drawn but how do I go about comparing the size of each different bounding box / contour to make out which one is the biggest?

Upvotes: 0

Views: 300

Answers (1)

Cory Kramer
Cory Kramer

Reputation: 117856

Are your bounding boxes CvRect objects? Assuming "biggest" means largest area, you can do something like this.

std::vector<CvRect*> vBoundingBoxes;    // assume this has all your boxes
int largestArea = 0;
CvRect* pLargestBox = NULL;

for (auto it = vBoundingBoxes.begin(); it != vBoundingBoxes.end(); ++it)
{
    CvRect* pCurrentBox = *it;
    int iArea = pCurrentBox->width * pCurrentBox->length;
    if (iArea > largestArea)
    {
        largestArea = iArea;
        pLargestBox = pCurrentBox;
    }
}

Upvotes: 1

Related Questions