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

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.

1 Like

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
1 Like

I am facing the same issue of saving the label maps as a series of (jpg or png) images. I mainly need this kind of series for training a neural network . How do I save the label maps in jpg series ?

Slicer does not allow you saving volumes as 2D image sequences. See @lassoan answer for your options.

You will find a lot of tutorials that use PNG images as input, because using PNGs as inputs makes sense when the images are natively stored as individual 8 or 24-bit RGB images.

However, once a medical image is reconstructed into 3D volume from DICOM files, it is natively stored in nrrd or nifti format. I would recommend to check out monai and its tutorials to learn how to use 3D medical images for deep learning training.