I have a 3D medical image. I use scipy.ndimage.measurements.label
to get the connected voxel groups. It is very fast. But I also want to get the number of voxels of each label. I use the following code to get the number of each value (suppose image_3d
is the array after scipy.ndimage.measurements.label
). It cost about 2 minutes.
import numpy as np
from skimage.measure import label
import time
image_3d = np.random.randint(100, size=(512, 512, 1024))
t1 = time.time()
pixel_count_list = [np.sum((image_3d== i).astype(int)) for i in range(100)]
t2 = time.time()
print("used time: ", t2-t1)
# 117 seconds
Is there any efficient way to get it efficiently?