简体   繁体   中英

how to find overlapping and inner bounding boxes from a outer bounding box?

I am trying to annotate a image and here is the one bounding box details from a annotation I have made,

Original Image - The annotations are based on this document image.

# annotation
{'x': 267.9,
 'y': 40.3,
 'height': 116.7,
 'width': 672.7,
 'center': [604.25, 98.65],
 'points': [[267.9, 40.3], [267.9, 157], [940.6, 157], [940.6, 40.3]]}

Now I have five other small bounding boxes which are also annotated by me,

[{'x': 290,
  'y': 66,
  'width': 309,
  'height': 72,
  'center': [444.5, 102.0],
  'points': [[290.0, 66.0], [290.0, 138.0], [599.0, 138.0], [599.0, 66.0]]},
 {'x': 626,
  'y': 68,
  'width': 295,
  'height': 72,
  'center': [773.5, 104.0],
  'points': [[626.0, 68.0], [626.0, 140.0], [921.0, 140.0], [921.0, 68.0]]},
 {'x': 960,
  'y': 69,
  'width': 20,
  'height': 71,
  'center': [970.0, 104.5],
  'points': [[960.0, 69.0], [960.0, 140.0], [980.0, 140.0], [980.0, 69.0]]},
 {'x': 1000,
  'y': 72,
  'width': 881,
  'height': 72,
  'center': [1440.5, 108.0],
  'points': [[1000.0, 72.0], [1000.0, 144.0], [1881.0, 144.0], [1881.0, 72.0]]},
 {'x': 1904,
  'y': 73,
  'width': 5,
  'height': 71,
  'center': [1906.5, 108.5],
  'points': [[1904.0, 73.0], [1904.0, 144.0], [1909.0, 144.0], [1909.0, 73.0]]}
 ]

I am looking for a way to find how many of the above 5 bounding box coordinates overlap or come inside the first main bounding box that I have at the very top.

I also need an option to select how much percent of overlapping to consider it as overlap. Let's say if two boxes are slightly touching I don't want them. Atleast 10-15% of bbox should be insider to consider it overlapping or inside.

Can anyone help me out on how I can achieve that?

Desired Output:

In my example, these two below smaller bounding boxes are inside or overlapping with the main bounding box.

[{'x': 290,
  'y': 66,
  'width': 309,
  'height': 72,
  'center': [444.5, 102.0],
  'points': [[290.0, 66.0], [290.0, 138.0], [599.0, 138.0], [599.0, 66.0]]},
 {'x': 626,
  'y': 68,
  'width': 295,
  'height': 72,
  'center': [773.5, 104.0],
  'points': [[626.0, 68.0], [626.0, 140.0], [921.0, 140.0], [921.0, 68.0]]}]

The function I used to create points for bounding box from center points, width, height is this,

def convert_points(center_x, center_y, width, height):
    x = center_x
    y = center_y
    return [[x - width / 2, y - height / 2], [x - width / 2, y + height / 2],
            [x + width / 2, y + height / 2], [x + width / 2, y - height / 2]]

You can check to see if two boxes have any overlap by checking if any of the corners of one box fall within the bounds of the other box (you have to do this vice-versa as well).

# check if a point falls within bounds
def inBounds(point, tl, br):
    x, y = point;
    left, top = tl;
    right, bottom = br;
    if left < x and x < right and top < y and y < bottom:
        return True;
    return False;

# check if these two boxes have any overlap
def boxOverlap(box1, box2):
    # check box2 in box1
    tl = box1[0];
    br = box1[2];
    for point in box2:
        if inBounds(point, tl, br):
            return True;
    
    # check box1 in box2
    tl = box2[0];
    br = box2[2];
    for point in box1:
        if inBounds(point, tl, br):
            return True;

    # no overlap
    return False;

Edit:

Sorry, to clarify: I'm thinking of boxes as four points [top-left, top-right, bottom-right, bottom-left] and I assume the boxes aren't rotated or anything (I think that's a safe assumption for most annotations).

Edit 2:

Here's an example of how to use this. Remember: I don't know all of the different ways you're going to use this. You'll have to modify it for your own use if you find something that isn't working.

# main box annotation
main_annot = {'x': 267.9,
 'y': 40.3,
 'height': 116.7,
 'width': 672.7,
 'center': [604.25, 98.65],
 'points': [[267.9, 40.3], [267.9, 157], [940.6, 157], [940.6, 40.3]]};

# other box annotation
other_annot = {'x': 290,
  'y': 66,
  'width': 309,
  'height': 72,
  'center': [444.5, 102.0],
  'points': [[290.0, 66.0], [290.0, 138.0], [599.0, 138.0], [599.0, 66.0]]};

if boxOverlap(main_annot['points'], other_annot['points']):
    print("OVERLAP DETECTED");
else:
    print("NO OVERLAP");

Edit 3:

WOOPS. I just thought of a case where this function doesn't work. Please disregard this answer for now. I don't have much time left today to work on this, If I come up with another answer later I'll post it. but for now you shouldn't use this, Sorry. I should've been more diligent with checking edge cases.

Edit 4:

Ok, I looked up an actual algorithm this time instead of trying to make one on the spot.

# check if these two boxes have any overlap
def boxOverlap(box1, box2):
    # get corners
    tl1 = box1[0];
    br1 = box1[2];
    tl2 = box2[0];
    br2 = box2[2];

    # separating axis theorem
    # left/right
    if tl1[0] >= br2[0] or tl2[0] >= br1[0]:
        return False;

    # top/down
    if tl1[1] >= br2[1] or tl2[1] >= br1[1]:
        return False;

    # overlap
    return True;

# main box annotation
main_annot = {'x': 267.9,
 'y': 40.3,
 'height': 116.7,
 'width': 672.7,
 'center': [604.25, 98.65],
 'points': [[267.9, 40.3], [267.9, 157], [940.6, 157], [940.6, 40.3]]};

# other box annotation
other_annot = {'x': 290,
  'y': 66,
  'width': 309,
  'height': 72,
  'center': [444.5, 102.0],
  'points': [[290.0, 66.0], [290.0, 138.0], [599.0, 138.0], [599.0, 66.0]]};

if boxOverlap(main_annot['points'], other_annot['points']):
    print("OVERLAP DETECTED");
else:
    print("NO OVERLAP");

Here's the source I pulled the equation from https://www.geeksforgeeks.org/find-two-rectangles-overlap/

That was pretty embarrassing, I should've just used an established algorithm to begin with.

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