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
359 stars 43 forks source link

Using cc3d with multiple channels? #57

Closed sneh-debug closed 3 years ago

sneh-debug commented 3 years ago

hello, I am using 3D images ,so for that how can i apply connected component library?

william-silversmith commented 3 years ago

Hi,

Multiple channels are not supported. I'm not totally sure what that would mean as often the channels are interrelated and sometimes they represent different data planes.

You can either: Run cc3d against each channel separately and recombine them after or you can combine all the channels into a single image (say combine RGB into a uint32 image).

If you tell me more about your data I can recommend something more specific.

Will

On Mon, Nov 9, 2020 at 3:56 AM sneh-debug notifications@github.com wrote:

hello, I am using 3D images ,so for that how can i apply connected component library?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/seung-lab/connected-components-3d/issues/57, or unsubscribe https://github.com/notifications/unsubscribe-auth/AATGQSMJRV72YSVHAWMSZYTSO6VD3ANCNFSM4TPBOWKA .

sneh-debug commented 3 years ago

hello @william-silversmith ,i want to apply cc3d on segmentation result image containing multiple class and the dimensions of result image is 128128128

william-silversmith commented 3 years ago

Here's what I suspect you want to do:

# Let multichannel be the segmentation image.

output = np.zeros(multichannel.shape, dtype=np.uint32)
for i in range(multichannel.shape[3]):
   output[:,:,:,i] = cc3d.connected_components(multichannel[:,:,:,i])

If instead by multichannel, you don't mean mean a 4D image and instead mean simply an image with multiple label types (i.e. label 1 might be adjacent to label 2), just use cc3d normally, that's what it was designed for.

sneh-debug commented 3 years ago

Here's what I suspect you want to do:

# Let multichannel be the segmentation image.

output = np.zeros(multichannel.shape, dtype=np.uint32)
for i in range(multichannel.shape[3]):
   output[:,:,:,i] = cc3d.connected_components(multichannel[:,:,:,i])

If instead by multichannel, you don't mean mean a 4D image and instead mean simply an image with multiple label types (i.e. label 1 might be adjacent to label 2), just use cc3d normally, that's what it was designed for.

Thanks Capture this is 3d image showing all 3 axis, i want to apply cc3d on it. How will i pass the image to it? i have to convert it into array?

sneh-debug commented 3 years ago

@william-silversmith

output_dir='./'
PNii  = nibabel.load('./prediction/BraTS20_Training_368/prediction.nii.gz')
P  = PNii.get_fdata()
print(P.shape)
#multichannel='./prediction/BraTS20_Training_368/prediction.nii.gz'
output = np.zeros(P.shape, dtype=np.uint32)
for i in range(P.shape[2]):
   output[:,:,i] = cc3d.connected_components(output[:,:,i])
print(output.shape)
output=sitk.GetImageFromArray(output)
  #get the save path
sitk.WriteImage(output,output_dir + 'new.nii.gz') 

This is my code for apply cc3d and saving it into 3d image with .nii.gz format but the output image is blank image? Please tell me what mistake i am doing here. Thanks

william-silversmith commented 3 years ago

Hi sneh, it looks like you are passing a blank output to cc3d. However, the way you're doing it you'll run connected components on each 2D slice, which may not be what you want. The following will probably produce 3D connected components if P is a numpy array.

output_dir='./'
PNii  = nibabel.load('./prediction/BraTS20_Training_368/prediction.nii.gz')
P  = PNii.get_fdata() 
output = cc3d.connected_components(P)
output=sitk.GetImageFromArray(output)
sitk.WriteImage(output,output_dir + 'new.nii.gz') 
sneh-debug commented 3 years ago

Hi sneh, it looks like you are passing a blank output to cc3d. However, the way you're doing it you'll run connected components on each 2D slice, which may not be what you want. The following will probably produce 3D connected components if P is a numpy array.

output_dir='./'
PNii  = nibabel.load('./prediction/BraTS20_Training_368/prediction.nii.gz')
P  = PNii.get_fdata() 
output = cc3d.connected_components(P)
output=sitk.GetImageFromArray(output)
sitk.WriteImage(output,output_dir + 'new.nii.gz') 

then it gives following error: TypeError: Type float64 not currently supported.

william-silversmith commented 3 years ago

If your data is floating point, it's not supported. You'll have to find a way to convert it to integer labels. This could be as easy as P.astype(np.uint64).

sneh-debug commented 3 years ago

@william-silversmith it worked . thank you. can we increase the intensity value? The image obtained from cc3d is not clear, very difficult to visualize.

william-silversmith commented 3 years ago

I'm glad it worked! To visualize more easily, try casting the output to a float before passing it to save_images. That functions re-normalizes floats to be more visible.

william-silversmith commented 3 years ago

Closing this question due to inactivity. Please reopen if you still need help!