简体   繁体   中英

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

So I have this image with an orange traffic cone

原始图片

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

过滤图像

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.

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;
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM