簡體   English   中英

如何從外部邊界框中找到重疊和內部邊界框?

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

我正在嘗試注釋圖像,這是我所做的注釋中的one邊界框詳細信息,

原始圖像- 注釋基於此文檔圖像。

# 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]]}

現在我還有另外五個小邊界框,它們也由我注釋,

[{'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]]}
 ]

我正在尋找一種方法來查找上述 5 個邊界框坐標中有多少重疊或進入我位於最頂部的第一個主邊界框內。

我還需要一個選項 select 有多少重疊百分比將其視為重疊。 假設如果兩個盒子有輕微的接觸,我不想要它們。 至少 10-15% 的 bbox 應該是內部的,以將其視為重疊或內部。

任何人都可以幫助我如何實現這一目標嗎?

所需的 Output:

在我的示例中,這兩個較小的邊界框位於主邊界框內部或與主邊界框重疊。

[{'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]]}]

我用來從center points, width, height創建邊界框points的 function 是這樣的,

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]]

您可以通過檢查一個框的任何角是否落在另一個框的范圍內來檢查兩個框是否有任何重疊(反之亦然)。

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

編輯:

抱歉,澄清一下:我將框視為四個點 [左上、右上、右下、左下],我假設這些框沒有旋轉或任何東西(我認為這是一個安全的假設大多數注釋)。

編輯2:

這是一個如何使用它的示例。 請記住:我不知道您將使用它的所有不同方式。 如果您發現某些內容不起作用,則必須對其進行修改以供自己使用。

# 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");

編輯3:

哎呀。 我只是想到了這個 function 不起作用的情況。 請暫時忽略這個答案。 我今天沒有太多時間來解決這個問題,如果我稍后想出另一個答案,我會發布它。 但現在你不應該使用這個,對不起。 我應該更努力地檢查邊緣情況。

編輯4:

好的,這次我查找了一個實際的算法,而不是試圖當場制作一個。

# 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");

這是我從https 中提取方程式的來源://www.geeksforgeeks.org/find-two-rectangles-overlap/

那太尷尬了,我應該一開始就使用一個既定的算法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM