假设每个矩形都有x、y、宽度和高度,您需要对每两个矩形进行成对比较,通过比较 if 来检查重叠
-
矩形 #1 的
x 介于 x 和 x + 矩形 #2 的宽度之间,或者
-
矩形#1 的
y 介于y 和y + 矩形#2 的高度之间,或者
- ...
幸运的是,您可以使用 NumPy 的矢量化功能来避免嵌套循环。
在下面的代码中,我生成了一些随机矩形,并过滤掉了那些重叠的:
import cv2
import numpy as np
# Randomly generate n white rectangles on black background
n = 20
rects = [[np.random.randint(0, 350),
np.random.randint(0, 250),
np.random.randint(10, 50),
np.random.randint(10, 50)] for i in range(n)]
img = np.zeros((300, 400), np.uint8)
for rect in rects:
img = cv2.rectangle(img, (rect[0], rect[1]),
(rect[0] + rect[2], rect[1] + rect[3]), 255, 1)
# Calculate left, right, top, bottom limits
rects = np.array(rects)
left = np.expand_dims(rects[:, 0], axis=1)
right = np.expand_dims(rects[:, 0] + rects[:, 2], axis=1)
top = np.expand_dims(rects[:, 1], axis=1)
bottom = np.expand_dims(rects[:, 1] + rects[:, 3], axis=1)
# Check for left limit intrusions, right limit intrusions, ...
check_l = (left <= left.T) & (left.T <= right)
check_r = (left <= right.T) & (right.T <= right)
check_t = (top <= top.T) & (top.T <= bottom)
check_b = (top <= bottom.T) & (bottom.T <= bottom)
# Check for combinations of left-top intrusions, left-bottom intrusions, ...
check_lt = check_l & check_t
check_lb = check_l & check_b
check_rt = check_r & check_t
check_rb = check_r & check_b
# Get all combinations; get rid of self identical matches
check = check_lt | check_lb | check_rt | check_rb
check = np.bitwise_xor(check, np.eye(n).astype(bool))
check = np.argwhere(check)
# Get unique indices of corrupted rectangles
corrupted = np.unique(check)
# Draw cleaned image
img_clean = np.zeros_like(img)
for i, rect in enumerate(rects):
if i not in corrupted:
img_clean = cv2.rectangle(img_clean, (rect[0], rect[1]),
(rect[0] + rect[2], rect[1] + rect[3]), 255, 1)
# Output
cv2.imshow('Original image', img)
cv2.imshow('Cleaned image', img_clean)
cv2.waitKey(0)
cv2.destroyAllWindows()
首先,我们来看看“输入”和“输出”:
基本上,计算每个矩形的左、右、上和下限。然后,计算矩形#1 的每个组合从左侧、顶部或右侧“侵入”到矩形#2,依此类推。
几个bool大小为(n, n)的矩阵,其中n是矩形的个数,
中间存储,但我想,对于n <= 2000 左右,内存消耗应该可以忽略不计。由于矢量化,这种方法非常快。
----------------------------------------
System information
----------------------------------------
Platform: Windows-10-10.0.16299-SP0
Python: 3.9.1
NumPy: 1.20.2
OpenCV: 4.5.1
----------------------------------------