问题如下:提供仅包含0和1的矩阵(下面的示例),我需要能够识别(并最终提取)1的最小边界矩形。
例如
0 0 0 0 0 0
0 [0 0 1 0] 0
0 [0 1 1 0] 0
0 [1 0 0 0] 0
0 [0 0 0 1] 0
0 0 0 0 0 0
我无法提出一个好的解决方案。感谢您的帮助!
答案 0 :(得分:1)
m = [
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 0, 0],
[0, 0, 1, 1, 0, 0],
[0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0],
[0, 0, 0, 0, 0, 0],
]
min_col = max_col = min_row = max_row = None
for i, row in enumerate(m):
for j, col in enumerate(row):
if col:
if min_row is None:
min_row = i
if min_col is None or min_col > j:
min_col = j
if max_row is None or max_row < i:
max_row = i
if max_col is None or max_col < j:
max_col = j
print('starting row = %s' % min_row)
print('starting column = %s' % min_col)
print('ending row = %s' % max_row)
print('ending column = %s' % max_col)
这将输出:
starting row = 1
starting column = 1
ending row = 4
ending column = 4
答案 1 :(得分:0)
主要思想是:
答案 2 :(得分:0)
arr = [[0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0], [0, 0, 1, 1, 0, 0], [0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0], [0, 0, 0, 0, 0, 0]]
# Find all positions where arr value is 1
pos_list = [(i,j) for i,r in enumerate(arr) for j,e in enumerate(r) if e]
# [(1, 3), (2, 2), (2, 3), (3, 1), (4, 4)]
# Get min of all (x, y) values as start_pos and max of all (x, y) values as end_pos
x_pos, y_pos = zip(*[(i,j) for i,r in enumerate(arr) for j,e in enumerate(r) if e])
start_pos, end_pos = (min(x_pos), min(y_pos)), (max(x_pos), max(y_pos))
print (start_pos, end_pos)
# (1, 1), (4, 4)
消除所有空白行,转置数组(通过压缩),再次消除所有行,再次转回,以获得适合所有1的原始数组的最小子数组。
sub_arr = list(zip(*[r for r in zip(*[r for r in arr if any(r)]) if any(r)]))
print (sub_arr)
# [(0, 0, 1, 0), (0, 1, 1, 0), (1, 0, 0, 0), (0, 0, 0, 1)]