seung-lab / connected-components-3d

Connected components on discrete and continuous multilabel 3D & 2D images. Handles 26, 18, and 6 connected variants; periodic boundaries (4, 8, & 6)
GNU Lesser General Public License v3.0
367 stars 43 forks source link

How to access/extract the connected components using this package ? #4

Closed tkseneee closed 5 years ago

tkseneee commented 5 years ago

Thank you for making this package windows compatible. I segmented the region of interest from Lung CT scans. My segmented output size 512x512x130, where 130 is the total number of cross sectional images in the Lung CT scan and 512x512 is the size of each cross sectional image. I use the following code to find the connected components (connected tissue clusters) across the cross section of the CT scan. import numpy as np from cc3d import connected_components nod_arr=np.load('nodule_arr.npy') nod_3d = connected_components(nod_arr)

It doesn't throw any error, but I can't able to extract any label information from nod_3d. How can I extract the tissues clusters which are connected across the cross section of CT scan ? For example, if one tissue cluster is exist from slice number 40 to 43 (4 cross section images), I need to extract that tissue cluster alone separately. image

william-silversmith commented 5 years ago

Hi Senthil,

I'm glad that first step worked for you! I have a few questions about the issue to help me diagnose the problem:

1) What is the data type of the input image? 2) What is the data type of the output image? 3) What is meant by "extract any label information"? Do you mean that the output is boolean? 4) What is the output of nod_arr.flags? 5) What is the output of nod_3d.flags?

If your issue is you need to separate the intermingled labels, here's what I'd recommend:

import numpy as np

segids = np.unique(nod_3d)[1:] # [1:] skips segid 0

for segid in segids:
    isolated = nod_3d * (segid == nod_3d)

If you know the label of the segid, you can skip the unique and for loop steps.

william-silversmith commented 5 years ago

I updated the README with instructions on how to extract individual labels.

tkseneee commented 5 years ago

Thank you for your replies and sorry for my late response.

nod_3d = connected_components(nod_arr)    
segids = np.unique(nod_3d)[1:]

for the above code I am getting 30846 segids.

segid=10 #some random id I took
p=(segid==nod_3d)
po=np.where(p==True)
iso=nod_3d*p 

for all the segid, I am getting 'po' values is True at only one position (x,y,z). So it means that I am not able extract the connected components in 3D right ?.

my nod_arr size is (512x512x130), data type is 'uint8' (also I can generate nod_arr in uint32)

nod_arr.flags output is: C_CONTIGUOUS : True F_CONTIGUOUS : False OWNDATA : True WRITEABLE : True ALIGNED : True WRITEBACKIFCOPY : False UPDATEIFCOPY : False

nod_3d.flags output is: C_CONTIGUOUS : True F_CONTIGUOUS : False OWNDATA : False WRITEABLE : True ALIGNED : True WRITEBACKIFCOPY : False UPDATEIFCOPY : False

william-silversmith commented 5 years ago

Hi Senthil,

The datasets my lab works with are a bit weird in the sense that we default our arrays to Fortran (Column-major) order. I suspect that the support for C (Row major) arrays is lacking. C order is the numpy default, so you're not doing anything wrong.

I had originally written cc3d.hpp such that it expected an array with the X index varying most quickly. This is equivalent to Fortran order. Therefore, I'm not surprised that it gave incorrect results when the Z index was varying most quickly. You can probably fix this by calling np.asfortranarray(nod_arr) before passing it to connected_components with version 1.0.2.

However, I just pushed an update to master that includes support for C order arrays. I'm going to deploy it when the tests pass.

william-silversmith commented 5 years ago

1.0.3 is live. :)

william-silversmith commented 5 years ago

Hi Senthil,

Let me know if 1.0.3 resolved your problem, otherwise I'll close this issue in a few days. You can reopen it if you're still having trouble.

I hope your research is going well. :)

Will