How to export segmentation as masks (.jpgs?)

Operating system: Linux
Slicer version: 4.10.1
I have as input DICOM files and I want to export the segmentations on those files in the form of a mask. The intended use is to load the masks into a numpy array for further use.

Using Segmentation module you can convert your segments to a label map, and then save it as a JPG/PNG series at the Save Dialog box.

1 Like

Why do you need a stack of jpgs just to load them into a numpy array? You can do all that in Slicer using the python interactor. To get the numpy array for a volume you can use this function


(slicer.util.arrayFromVolume)
You’ll need to do this on the labelmap you exported from the segmentation.

Btw you can also export segmentations to labelmaps in the Data module (right-click segmentation and you’ll find the option in the menu).

1 Like

I see, thank you so much! I wasn’t familiar with the python interactor.

I saved the input image (2D) and the segment as mask both in nrrd format and I load them in python for further processing but the dimension of these two images are different and so the mask actually does not match the original image. Could you please tell me how I can save them separately in the same dimension so that they are perfectly registered as shown in the Slicer viewer? Thanks in advance

It is solved, I had to deactivate compressing while saving data!

That’s strange. Compression should have no impact on the dimensions of the volume saved. Did you change the Slicer version recently? The newer versions should save the labelmap to the full extend of the volume.

I am using the nightly 4.11.0 (2020.06.20). And I am saving the segmentation itself (in nrrd format) not the labelmap.

I am actually having problem saving the segmentation in other formats (e.g. .png). I can easily save the input dicom file (2D image) as .png image but when I want to save the labelmap in .png I get this error: “cannot write data file”.
I appreciate if you would let me know how to solve this issue.

I would not recommend to save 3D image or segmentation as a series of jpg/png/bmp. Instead, use the following file formats:

  • for lossless saving (save all metadata, including position, orientation, spacing), save as nrrd
  • for saving training data for deep learning network, save into numpy array file from script
  • for papers, presentations, you can save as image series or video using Screen Capture module