删除坐标列表中的重叠值

2024-10-03 19:29:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个1920 x 1080的图像,以及相应的坐标,它们表示图像上具有随机形状(x,y)的边界框

bb_rects= [[412, 130, 507, 234], [521, 82, 647, 188], [521, 314, 747, 469], [820, 372, 943, 473]]

我正在尝试从1920 x 1080中提取不在bb_rects中那些边界框内的100 x 100图像。我在1920 x 1080图像上使用蛮力,每100步用一个窗口提取100x100坐标。如何排除all_rects中与bb_rects中的坐标重叠的坐标

all_rects= [(0, 0, 100, 100), (100, 0, 200, 100), (200, 0, 300, 100), (300, 0, 400, 100), (400, 0, 500, 100), (500, 0, 600, 100), (600, 0, 700, 100), (700, 0, 800, 100), (800, 0, 900, 100), (900, 0, 1000, 100), (1000, 0, 1100, 100), (1100, 0, 1200, 100), (1200, 0, 1300, 100), (1300, 0, 1400, 100), (1400, 0, 1500, 100), (1500, 0, 1600, 100), (1600, 0, 1700, 100), (1700, 0, 1800, 100), (1800, 0, 1900, 100), (0, 100, 100, 200), (100, 100, 200, 200), (200, 100, 300, 200), (300, 100, 400, 200), (400, 100, 500, 200), (500, 100, 600, 200), (600, 100, 700, 200), (700, 100, 800, 200), (800, 100, 900, 200), (900, 100, 1000, 200), (1500, 900, 1600, 1000), (1600, 900, 1700, 1000), (1700, 900, 1800, 1000), (1800, 900, 1900, 1000)]

输出应该是rects_filtered,它们是all_rects坐标,与bb_rects不重叠


Tags: 图像allfiltered边界蛮力形状bbrects
1条回答
网友
1楼 · 发布于 2024-10-03 19:29:37

所以,我想到的是:这个概念是建立一种“网格”,并确定哪些正方形实际上完全包含在任何一个bb_矩形中。不确定它在处理时间方面是否更好,但您可能希望尝试以下方法:

import math

bb_rects= [[412, 130, 507, 234], [521, 314, 747, 512]]


def does_rectangle_contain(rect1, rect2):
   return rect1[0] <= rect2[0] and rect1[1] <= rect2[1] and rect1[2] >= rect2[2] and rect1[3] >= rect2[3]


for rect in bb_rects:
    min_x = int(rect[0] / 100)
    max_x = math.ceil(rect[2] / 100)

    min_y = int(rect[1] / 100)
    max_y = math.ceil(rect[3] / 100)
    possible_overlapping_rectangles_y = range(min_y * 100, 100 * (max_y + 1), 100)
    possible_overlapping_rectangles_y_pairs = []
    for first, second in zip(possible_overlapping_rectangles_y, possible_overlapping_rectangles_y[1:]):
        possible_overlapping_rectangles_y_pairs.append((first, second))

    possible_overlapping_rectangles_x = range(min_x * 100, 100 * (max_x + 1), 100)
    possible_overlapping_rectangles_x_pairs = []
    for first, second in zip(possible_overlapping_rectangles_x, possible_overlapping_rectangles_x[1:]):
        possible_overlapping_rectangles_x_pairs.append((first, second))

    possible_rectangles = []

    for a in possible_overlapping_rectangles_x_pairs:
        for b in possible_overlapping_rectangles_y_pairs:
            possible_rectangles.append([a[0], b[0], a[1], b[1]])

    print(possible_rectangles)

    for grid_rect in possible_rectangles:
        if does_rectangle_contain(rect, grid_rect):
            print(f"fully contained: ({grid_rect[0]}, {grid_rect[1]}, {grid_rect[2]}, {grid_rect[3]}) in "
                  f"({rect[0]}, {rect[1]}, {rect[2]}, {rect[3]})")

对于我的测试数据,它产生:

[[400, 100, 500, 200], [400, 200, 500, 300], [500, 100, 600, 200], [500, 200, 600, 300]]
[[500, 300, 600, 400], [500, 400, 600, 500], [500, 500, 600, 600], [600, 300, 700, 400], [600, 400, 700, 500], [600, 500, 700, 600], [700, 300, 800, 400], [700, 400, 800, 500], [700, 500, 800, 600]]
fully contained: (600, 400, 700, 500) in (521, 314, 747, 512)

您可以稍后简单地过滤掉“所有错误”

注意:代码当然应该重构。这只是一个快速原型

相关问题 更多 >