tomdoel / pulmonarytoolkit

Lung medical image analysis and visualisation software for Matlab.
GNU General Public License v3.0
84 stars 57 forks source link

How to save segmentation results in NIFTI format #56

Open Fiy-fly opened 1 year ago

Fiy-fly commented 1 year ago

Hello,I don‘t know How to save segmentation results in NIFTI format? I use the “export segmentation ” at GUI ,then I get an RGB image in DICOM format. I was wondering where I should find the original segmentation results in MATLAB.

Fiy-fly commented 1 year ago

Sorry,I already know how to save in NII format, but I'd still like to know how to see the raw segments in the MATLAB workspace

tomdoel commented 1 year ago

Here are two ways:

  1. Using the GUI, you can "Export Segmentation" and choose "Format: Matlab". This should save the segmentation matrix as a Matlab .mat file, which you can load into the workspace. The segmentation matrix is of datatype uint8, where 0 means no segmented region, and other values mean part of a segmentation, where the values used depend on the type of segmentation. For example, for lung segmentations, 1 is Right and 2 is Left.

  2. You can write a script which uses the API (see Tutorial 3 for an introduction) and fetch the result of the corresponding segmentation plugin using dataset.GetResult(). The result will usualy be a PTKImage object, whose property RawImage is the Matlab matrix containing the segmentation values (same as above). NB be aware the segmentation image you get will be cropped to the lung region of interest by default - if you don't want this and want a full size image, then you would do this by adding an additional "context" argument set to PTKOriginalImage to the GetResult() call. For example dataset.GetResult('PTKLeftAndRightLungs', 'PTKOriginalImage');

Here is a rough example script for fetching lung and lobe segmentations using the API (Note I have not tested this!)

PTKAddPaths;
ptk_main = PTKMain();
uid = `PUT_THE_UID_FOR_YOUR_DATASET_HERE`;
dataset = ptk_main.CreateDatasetFromUid(uid);

lung_segmentation = dataset.GetResult('PTKLeftAndRightLungs');
% Now lung_segmentation.RawImage contains the lung segmentation matrix

lobe_segmentation = dataset.GetResult('PTKLobes');
% Now lobe_segmentation.RawImage contains the lobe segmentation matrix