我正在处理大型3D图像(大约8000x5000x50),其中包含大约1000到10000个标记的分段,即整数的连接区域。我需要找到每个物体的重心,但是我尝试的速度太慢了。使用scipy的center_of_mass()
function,我像这样遍历所有标签:
coords = []
for label in range(1, n):
print("label " + str(label))
coord = ndimage.center_of_mass(labels, labels, label)
coords.append(coord)
这很好用,但每个标签很容易花费五秒钟。有什么更快的方法?
答案 0 :(得分:0)
我找到了一种方法,可以通过从大型源图像(称为labels
)中获取每个标记对象的边界框,分别对每个对象调用center_of_mass()
并将结果转换回去来加快处理速度进入源图像的坐标空间:
coords = []
# Get bounding boxes for all objects in the form of slices
objects = ndimage.find_objects(labels)
# Loop over all objects:
for l in range(1, n):
# Get individual bounding box
bb_slices = objects[l]
# Extract object
obj = labels[bb_slices]
# Compute centroid
coord = ndimage.center_of_mass(obj)
# Translate result from coordinate space of the bounding box back to the source image by simply adding the starting coordinate of each slice
coord_translated = (np.around(coord[0]).astype(np.uint8) + slices[0].start, np.around(coord[1]).astype(np.uint8) +
slices[1].start, np.around(coord[2]).astype(np.uint8) + slices[2].start)
coords.append(coord_translated)
> 900个对象仅需约16秒,而原始方法至少需要一个小时。