slicing label images

61 views
Skip to first unread message

jken...@gmail.com

unread,
Jun 5, 2021, 8:14:47 PM6/5/21
to itksnap-users
I've been struggling with how to use c3d to generate slices of a segmentation with the appropriate label colors. I've been playing around with trying to combine the -oli function with slicing with something like this:

c3d 2021-05-04_AZBA_segmentation.nii.gz 2021-05-04_AZBA_segmentation.nii.gz -oli 2021-05-04_AZBA_Label_descriptions.txt 1 -slice y 60:1:800 -foreach -type uchar -endfor -oomc coronal/2021-05-04_AZBA_segmentation_%0d.png

When I try to run this on my windows machine with 32GB of RAM I get a 'segmentation fault (core dumped)' error. I've tried running this on a Linux machine in the lab w/ 128 GB of RAM, but I run into the following error: 

Unknown command -oomc
Unknown exception caught by convert3d
  When processing command: -oomc

I've downloaded the most recent nightly build for Linux, so was surprised to see this error. I get the same error if I try foreach-comp command as well.

My thinking for the code above is that I'm trying to slice the multicomponent image. The best I've been able to come up with is to loop through this via a bash script: 

for c in $(seq -f "%03g" 60 800); do
c3d 2021-05-04_AZBA_segmentation_${i}_only.nii.gz -slice y $c -as S 2021-05-04_AZBA_segmentation_${i}_only.nii.gz -clip 0 0 -reslice-identity -push S -oli 2021-05-04_AZBA_Label_descriptions.txt 1.0 -type uchar -omc coronal/2021-05-04_AZBA_segmentation_${i}_only_coronal_${c}.png;
done

but this is super slow since everything needs to be constantly reloaded. Any help or suggestions would be much appreciated!

Justin 

jken...@gmail.com

unread,
Jun 5, 2021, 9:33:34 PM6/5/21
to itksnap-users
Okay, I figured out one part of this. The reason the -oomc and -foreach-comp commands weren't working on the linux machine was because the default download at sourceforge was giving version 1.1. I downloaded the version posted about a month ago and can run these commands now on Linux. However, I still run into the 'segmentation fault (core dumped)' error, and I think it's unlikely that it's because I'm running out of memory so I haven't figured out how to get this command to do as I wish.

Paul Yushkevich

unread,
Jun 6, 2021, 7:24:28 AM6/6/21
to itksna...@googlegroups.com
Hi Justin

Could you add the -verbose flag at the beginning, to help see where it crashes?

Thanks!

--
You received this message because you are subscribed to the Google Groups "itksnap-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to itksnap-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/itksnap-users/b42e081d-2d2e-4255-acbb-411971e0067an%40googlegroups.com.


--
Paul A. Yushkevich, Ph.D.
Professor of Radiology
Penn Image Computing and Science Laboratory
University of Pennsylvania Perelman School of Medicine

Paul Yushkevich

unread,
Jun 6, 2021, 7:29:31 AM6/6/21
to itksna...@googlegroups.com
Hmm.. It could be because -oomc needs a number of components (3 for RGB) to be supplied as the first argument:

-oomc                           : Output multiple multi-component images to multiple files

Syntax: `-oomc n_comp image_list` or `-oomc n_comp image_spec`

Write all images on the **convert3d** stack as multiple multi-component image files. The command is a mixture of the **-omc** and **-oo** commands. There must be a multiple of 'n_comp' images on the stack. Every consecutive 'n_comp' images on the stack will be written to a separate multi-component image.

jken...@gmail.com

unread,
Jun 6, 2021, 11:10:47 AM6/6/21
to itksnap-users
Hi Paul,

Here is the output with the verbose flag:

c3d -verbose 2021-05-04_AZBA_segmentation.nii.gz 2021-05-04_AZBA_segmentation.nii.gz -oli 2021-05-04_AZBA_Label_descriptions.txt 1 -slice y 400:1:402 -foreach -type uchar -endfor -oomc coronal/2021-05-04_AZBA_segmentation_%02d.png
Reading #1 from 2021-05-04_AZBA_segmentation.nii.gz
Reading #2 from 2021-05-04_AZBA_segmentation.nii.gz
Overlaying labels in #1 over image #0
Extracting slice 400 along Y axis in image #1
  Extracting bounding box [0, 400, 0] [470, 1, 670]
Extracting slice 401 along Y axis in image #2
  Extracting bounding box [0, 401, 0] [470, 1, 670]
Extracting slice 402 along Y axis in image #3
  Extracting bounding box [0, 402, 0] [470, 1, 670]
Repeating commands for all 5 images
Segmentation fault (core dumped)

I tried adding 3 immediately after the -oomc argument (i.e. and I get (I also tried 2 and 6 with the same result):
c3d -verbose 2021-05-04_AZBA_segmentation.nii.gz 2021-05-04_AZBA_segmentation.nii.gz -oli 2021-05-04_AZBA_Label_descriptions.txt 1 -slice y 400:1:402 -foreach -type uchar -endfor -oomc 3 coronal/2021-05-04_AZBA_segmentation_%02d.png
Reading #1 from 2021-05-04_AZBA_segmentation.nii.gz
Reading #2 from 2021-05-04_AZBA_segmentation.nii.gz
Overlaying labels in #1 over image #0
Extracting slice 400 along Y axis in image #1
  Extracting bounding box [0, 400, 0] [470, 1, 670]
Extracting slice 401 along Y axis in image #2
  Extracting bounding box [0, 401, 0] [470, 1, 670]
Extracting slice 402 along Y axis in image #3
  Extracting bounding box [0, 402, 0] [470, 1, 670]
Repeating commands for all 5 images
Exception caught of type 16ConvertException
  Exception detail: Multicomponent output error: mismatch in image dimensions

So it looks like it doesn't get to the 'segmentation fault' error. 

Thanks for having a look!

Justin

Paul Yushkevich

unread,
Jun 7, 2021, 12:15:30 PM6/7/21
to itksna...@googlegroups.com
Please try the following command:

c3d -verbose \
  2021-05-04_AZBA_segmentation.nii.gz -as X \
  -slice 400:1:402 \
  -push X \
  -slice 400:1:402 \
  -reorder 0.5 \
  -foreach-comp 2 -oli 2021-05-04_AZBA_Label_descriptions.txt 1 -endfor \
  -type uchar -oomc 3 coronal/2021-05-04_AZBA_segmentation_%02d.png

What I am suggesting is to first extract slices from the "main" image and the "segmentation" image (in your case they are both the same image), regroup them so that you have a sequence main slice / segmentation slice / main slice / segmentation slice / ... and then apply -oli to each main/segmentation pair in the for loop. 

Let me know how that works 

jken...@gmail.com

unread,
Jun 8, 2021, 12:49:16 PM6/8/21
to itksnap-users
Hi Paul,

Thanks for the suggestion. I tried what you suggested (with slight modification for the slice command). It does output 2 png files, however they are not colored based on the label descriptions, but are all grayscale. I've tried removing the '3' after -oomc and I get the same segmentation fault (core dumped) error. I did this because the only time I've ever been able to get this to output the right colors is from the code I noted above when I used a bash script to loop through everything. 

c3d -verbose   2021-05-04_AZBA_segmentation.nii.gz -as X   -slice y 400:1:402   -push X   -slice y 400:1:402   -reorder 0.5   -foreach-comp 2 -oli 2021-05-04_AZBA_Label_descriptions.txt 1 -endfor   -type uchar -oomc 3 coronal/2021-05-04_AZBA_segmentation_%02d.png
Reading #1 from 2021-05-04_AZBA_segmentation.nii.gz
Extracting slice 400 along Y axis in image #18446744073709551615
  Extracting bounding box [0, 400, 0] [470, 1, 670]
Extracting slice 401 along Y axis in image #0
  Extracting bounding box [0, 401, 0] [470, 1, 670]
Extracting slice 402 along Y axis in image #1
  Extracting bounding box [0, 402, 0] [470, 1, 670]
Extracting slice 400 along Y axis in image #2
  Extracting bounding box [0, 400, 0] [470, 1, 670]
Extracting slice 401 along Y axis in image #3
  Extracting bounding box [0, 401, 0] [470, 1, 670]
Extracting slice 402 along Y axis in image #4
  Extracting bounding box [0, 402, 0] [470, 1, 670]
Reordering 6 images with stride of 3
Repeating commands for all 6 images in batches of 2
Overlaying labels in #2 over image #1
Overlaying labels in #2 over image #1
Writing Images 1 to 3 to multicomponent file coronal/2021-05-04_AZBA_segmentation_00.png
  Output voxel type: uchar[h]
  Rounding off: Enabled
Writing Images 4 to 6 to multicomponent file coronal/2021-05-04_AZBA_segmentation_01.png
  Output voxel type: uchar[h]
  Rounding off: Enabled
Writing Images 7 to 9 to multicomponent file coronal/2021-05-04_AZBA_segmentation_02.png
  Output voxel type: uchar[h]
  Rounding off: Enabled
Not enough images on the stack for the requested command.
  Requested command:
  Note: C3D requires image operands to precede commands.
        message can be caused by incorrect usage, such as
           c3d -command image.nii
        instead of
           c3d image.nii -command

Thanks for having a look!

Best,

Justin

Reply all
Reply to author
Forward
0 new messages