user2097314
user2097314

Reputation: 59

OpenCV - C++ - Axis aligned bounding box of a binary object

So I have this image with an orange traffic cone

Original Image

I have filtered out all of the colors I don't want

Filtered Image

Now what I want to do is draw a box around the cone. I would like to do this by determining the maximum upper and lower bounds of the cone, and the maximum left and right bounds of the cone. Basically, the location of the highest white pixel, lowest white pixel, left most white pixel and right most white pixel.

I know how to draw the lines, but I don't know how to find the bounds of the cone.

The idea is to find a box around the cone so that I can determine the centroid of the cone.

Any help is appreciated.

Upvotes: 1

Views: 1524

Answers (1)

Abhijit Thatte
Abhijit Thatte

Reputation: 116

Assuming that the images is loaded in an array ... you can use following algorithm.

long top, bottom, right, left;
bottom = right = -1;
top = maxrows;
left = maxcolumns;

for(long row = 0; row < maxrows; row++)
{
    for(long column = 0; column < maxcolumns; column++)
    {
        if(true == IsPixelWhite(image[row][column])
        {
        if(row > bottom)  bottom = row;
        if(column > right) right = column;
        if(row < top)  top = row;
        if(column < left) left = column;
        }
    }
}

Upvotes: 1

Related Questions