Search code examples
pythonnumpymultidimensional-arrayscipyblob

How to extract 3D arrays of points (blobs) from a binary 3D array?


I have a numpy array of shape (260,311, 260) of points. It represents 8 electrodes (value 1) in an empty space (surrounded by zeroes). How can I detect these blobs and individualize them one by one? (I'm expecting 8 arrays of 260,311,260 with only one electrode each)

I've tried to extract the 1 values with np.nonzero but I'm stuck right after. I have also tried skimage.feature.blob_log but it doesn't seem to work on numpy arrays; and Kmeans clustering, but I would like to do it with a variable number of electrodes.

This is the 3d plot of np.array(np.nonzero(electrodes_data))

fig = plt.figure(figsize=(6, 6))
    ax = fig.add_subplot(111, projection='3d')
    ax.scatter(all_coordinates[0], all_coordinates[1], all_coordinates[2],
               linewidths=1, alpha=.7,
               edgecolor='k',`your text`
               s = 200,
               c=all_coordinates[0])
    ax.view_init(elev=0, azim=0)
    plt.show()

visualization of blobs

It's weird that there isn't a simple function to individualize blobs?!


Solution

  • If you had a 3D array with binary blobs. You can separate them with scipy.ndimage.label

    blobs                         #A 3D array of 1s and zeros.
    labelled = ndimage.label(blobs)
    

    Now you want to separate each value into an array?

    separated = []
    
    labels = np.unique(labelled)
    
    for lbl in labels[1:]: #the first value is 0
        z, y, x = np.where(labelled==lbl)
        one = np.zeros( labels.shape )
        one[z, y, x] = 1
        separated.add(one)
    

    Now separated should have N volumes with each blob a binary label.