泛洪填充算法的一般实现会遇到堆栈溢出,有没有办法对其进行优化?我正在使用此算法在建筑模型中找到明显的空白区域。我对这些模型进行了体素化,然后通过简化版本的0和1来重新解析体素化结果。 0和1表示体素是否存在。 0表示存在,1表示不存在。然后我必须找到连接0的不同子集,换句话说,3D建筑内的连接空隙空间。
示例2D输入数据示例存储在列表中,3D将是列表中的多个条目。 (Z,Y,X)=(0,4,9)
11000111
11000000
10001110
10111110
维基百科提出了几种补救方法,但我不知道如何实施它们。这是现有的算法,我已经为更密集的数据设置了“sys.setrecursionlimit(10000)”。对于某些人来说这是好的,但是对于更密集的(Z,Y,X)=(50,46,22)或更大,因为建筑模型变得更复杂,有数百个房间,我得到堆栈溢出消息
错误堆栈溢出将在递归函数中发生:
File "ZoneFinding3D_Baselined.py", line 104, in findZero
if (0 <= row < row_len) and (0 <= col < col_len) and (0 <= z < height_len) and (col, row, z) not in walked:
MemoryError: Stack overflow
代码:
def findZero(subset_in, col, row, z, height_len, col_len, row_len, layers3D, walked, output):
if (0 <= row < row_len) and (0 <= col < col_len) and (0 <= z < height_len) and (col, row, z) not in walked:
walked.append((col, row, z))
if layers3D[z][row][col] == 0: #layers3D is in format (z, row, col) which is the actual hierarchy of input data, Z, Y, X
if subset_in is not None:
subset = subset_in
else:
subset = []
subset.append((col, row, z))
findZero(subset, col+1, row, z, height_len, col_len, row_len, layers3D, walked, output)
findZero(subset, col, row+1, z, height_len, col_len, row_len, layers3D, walked, output)
findZero(subset, col-1, row, z, height_len, col_len, row_len, layers3D, walked, output)
findZero(subset, col, row-1, z, height_len, col_len, row_len, layers3D, walked, output)
findZero(subset, col, row, z+1, height_len, col_len, row_len, layers3D, walked, output)
findZero(subset, col, row, z-1, height_len, col_len, row_len, layers3D, walked, output)
if subset_in is None:
output.append(subset)
def checkThrough(layers3D, gridSizes):
walked = []
output = []
countX=0; countY=0; countZ=0
for z in range(0, gridSizes[2]):
for row in range (countY, countY+gridSizes[1]):
for col in range (0, gridSizes[0]):
col_len = gridSizes[0]
row_len = gridSizes[1]
height_len = gridSizes[2]
if (col, row, z) not in walked: #walked takes format of (col, row, z), modified from (z, row, col)
findZero(None, col, row, z, height_len, col_len, row_len, layers3D, walked, output)
return output
答案 0 :(得分:1)
您可以使用scipy.ndimage.label
快速执行此操作:
import numpy as np
from scipy.ndimage import label
a = np.random.randint(0, 2, (4, 6))
b = label(a)
print a
print b
输出是:
[[1 0 1 1 0 0]
[1 0 0 0 0 0]
[1 1 0 0 0 1]
[0 1 1 0 1 1]]
(array([[1, 0, 2, 2, 0, 0],
[1, 0, 0, 0, 0, 0],
[1, 1, 0, 0, 0, 3],
[0, 1, 1, 0, 3, 3]]), 3)
label()
找到所有连接的1,所以你需要反转0&amp; 1首先为您的数据。