Python 删除坐标列表中的重叠值
我有一个图像是1920 x 1080,对应的坐标表示该图像上具有随机形状(x,y)的边界框 我试图从Python 删除坐标列表中的重叠值,python,list,image,image-processing,coordinates,Python,List,Image,Image Processing,Coordinates,我有一个图像是1920 x 1080,对应的坐标表示该图像上具有随机形状(x,y)的边界框 我试图从1920 x 1080中提取不在bb\u rects中边界框内的100 x 100图像。我在1920 x 1080图像上施加了蛮力,窗口每100步提取100x100坐标。如何排除与bb\u-rects中的坐标重叠的所有中的坐标 all_rects= [(0, 0, 100, 100), (100, 0, 200, 100), (200, 0, 300, 100), (300, 0, 400, 10
1920 x 1080
中提取不在bb\u rects
中边界框内的100 x 100图像。我在1920 x 1080
图像上施加了蛮力,窗口每100步提取100x100
坐标。如何排除与bb\u-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\u filtered
,它们是所有与bb\u rects
不重叠的坐标,因此,我想到的是:这个概念是建立一种“网格”,并确定哪些方块实际上完全包含在任何bb\u rects中。不确定它在处理时间方面是否更好,但您可能希望尝试以下方法:
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]})")
您可以稍后简单地过滤掉“所有错误”
注意:代码当然应该重构。这只是一个快速原型
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)