# Extract point data from label map in CLI module

**URL:** <https://discourse.slicer.org/t/extract-point-data-from-label-map-in-cli-module/405>\
**Category:** Development\
**Created:** [May 30, 2017, 9:27pm UTC](https://discourse.slicer.org/t/extract-point-data-from-label-map-in-cli-module/405 "2017-05-30T21:27:00Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![DanC](https://avatars.discourse-cdn.com/v4/letter/d/77aa72/32.png) [@DanC](https://discourse.slicer.org/u/DanC)\
**Post date:** [May 30, 2017, 9:27pm UTC](https://discourse.slicer.org/t/extract-point-data-from-label-map-in-cli-module/405/1 "2017-05-30T21:27:00Z")

</div>

Hi all.  
I would like to extract the point data from a 3d label map in a CLI module.  
Seems I could save to nrrd -\> open with itkreader -\> iterate over voxels and find filled voxels.  
Is there a way to directly get the data as an array?

Thanks!

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [May 30, 2017, 11:12pm UTC](https://discourse.slicer.org/t/extract-point-data-from-label-map-in-cli-module/405/2 "2017-05-30T23:12:53Z")

</div>

Image is read into an ITK image, so the cleanest and most reusable solution is to implement your processing as in ITK filter.

If for any reason you decide to work outside ITK, then you have to export the image to a raw buffer, process it, and import it back into an ITK image. Details are described in the [ITK Software Guide](https://itk.org/ItkSoftwareGuide.pdf) and [here](https://stackoverflow.com/questions/26296870/writing-itk-insight-toolkit-results-to-local-buffer).

---

<div class="post-metadata">

**Author:** ![DanC](https://avatars.discourse-cdn.com/v4/letter/d/77aa72/32.png) [@DanC](https://discourse.slicer.org/u/DanC)\
**Post date:** [May 31, 2017, 10:13pm UTC](https://discourse.slicer.org/t/extract-point-data-from-label-map-in-cli-module/405/3 "2017-05-31T22:13:36Z")

</div>

Thanks Andras.  
I was able to save it into an ITK image.  
Now I am trying to apply filters and save it back to nrrd file format using the following but the saved file will load in slicer, but is a blank image. Is there an additional step I should take? Thanks!

```
typedef itk::ImageFileWriter<OriginalImageType> WriterType;
WriterType::Pointer writer = WriterType::New();
writer->SetImageIO(itk::NrrdImageIO::New());
```

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [June 2, 2017, 2:22pm UTC](https://discourse.slicer.org/t/extract-point-data-from-label-map-in-cli-module/405/4 "2017-06-02T14:22:20Z")

</div>

You can check it out with other viewers (ITK Snap, ImageJ, etc), but most likely then the image is actually empty. For further help, you need to either share some sample images (make sure no patient-identifiable information is included) or part of the source code that generates and write the image.

---

<div class="post-metadata">

**Author:** ![DanC](https://avatars.discourse-cdn.com/v4/letter/d/77aa72/32.png) [@DanC](https://discourse.slicer.org/u/DanC)\
**Post date:** [June 2, 2017, 9:21pm UTC](https://discourse.slicer.org/t/extract-point-data-from-label-map-in-cli-module/405/5 "2017-06-02T21:21:09Z")

</div>

```
// declare the image class
typedef itk::Image<float, 3> OriginalImageType;

//// Do -Read in label map
typedef itk::ImageFileReader<OriginalImageType> itkReaderType;
itkReaderType::Pointer itkreader = itkReaderType::New();
itkreader->SetFileName("label.nrrd");
itkreader->Update();

OriginalImageType::Pointer outImage = itkreader->GetOutput();

typedef itk::ImageFileWriter<OriginalImageType> WriterType;
WriterType::Pointer writer = WriterType::New();
writer->SetImageIO(itk::NrrdImageIO::New());
writer->SetFileName("output.nrrd");
writer->SetInput(outImage);
writer->Update();
```

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [June 2, 2017, 9:42pm UTC](https://discourse.slicer.org/t/extract-point-data-from-label-map-in-cli-module/405/6 "2017-06-02T21:42:39Z")

</div>

I don’t see any obvious problem in the code. Can you send an example output image? (through dropbox or similar service)

---

<div class="post-metadata">

**Author:** ![DanC](https://avatars.discourse-cdn.com/v4/letter/d/77aa72/32.png) [@DanC](https://discourse.slicer.org/u/DanC)\
**Post date:** [June 2, 2017, 9:53pm UTC](https://discourse.slicer.org/t/extract-point-data-from-label-map-in-cli-module/405/7 "2017-06-02T21:53:17Z")

</div>

My bad. I stripped down the code, and it seems that the IO part is working without a problem. This is the full code. Seems I am doing something wrong with the resample filter.

```
//// Read in label map
typedef itk::ImageFileReader<OriginalImageType> itkReaderType;
itkReaderType::Pointer itkreader = itkReaderType::New();
itkreader->SetFileName("label.nrrd");
itkreader->Update();
// resample image to reduce number of points
OriginalImageType::SizeType outputSize = itkreader->GetOutput()->GetLargestPossibleRegion().GetSize();
outputSize[0] = (int) (outputSize[0]/10);
outputSize[1] = (int) (outputSize[1]/10);

OriginalImageType::SpacingType outputSpacing = itkreader->GetOutput()->GetSpacing();
outputSpacing[0] *= 10;
outputSpacing[1] *= 10;

typedef itk::IdentityTransform<double, 3> TransformType;
typedef itk::ResampleImageFilter<OriginalImageType, OriginalImageType> ResampleImageFilterType;
ResampleImageFilterType::Pointer resample = ResampleImageFilterType::New();
resample->SetInput(itkreader->GetOutput());
resample->SetSize(outputSize);
resample->SetTransform(TransformType::New());
resample->SetOutputSpacing(outputSpacing);
resample->SetOutputOrigin(itkreader->GetOutput()->GetOrigin());
resample->UpdateLargestPossibleRegion();

OriginalImageType::Pointer outImage = resample->GetOutput();

typedef itk::ImageFileWriter<OriginalImageType> WriterType;
WriterType::Pointer writer = WriterType::New();
writer->SetImageIO(itk::NrrdImageIO::New());
writer->SetFileName("output.nrrd");
writer->SetInput(outImage);
writer->Update();
```

---

<div class="post-metadata">

**Author:** ![Saima](https://avatars.discourse-cdn.com/v4/letter/s/9d8465/32.png) [@Saima](https://discourse.slicer.org/u/Saima)\
**Post date:** [September 13, 2018, 5:35am UTC](https://discourse.slicer.org/t/extract-point-data-from-label-map-in-cli-module/405/8 "2018-09-13T05:35:45Z")

</div>

How to extract data from the cli results. the cli module return this but I need to get the output volume node. how to get that output volume node?

vtkMRMLCommandLineModuleNode (00000178FF7638D0)  
ID: vtkMRMLCommandLineModuleNode1  
ClassName: vtkMRMLCommandLineModuleNode  
Name: Swiss Skull Stripper  
Debug: false  
MTime: 1380510  
Description: (none)  
SingletonTag: (none)  
HideFromEditors: true  
Selectable: true  
Selected: false  
Attributes:  
CommandLineModule:Swiss Skull Stripper  
UpdateDisplay:true  
Status: Completed  
AutoRun:0  
AutoRunMode:17  
Parameter values:  
patientVolume = vtkMRMLScalarVolumeNode1  
patientOutputVolume = vtkMRMLScalarVolumeNode4  
patientMaskLabel = vtkMRMLLabelMapVolumeNode2  
atlasMRIVolume = vtkMRMLScalarVolumeNode2  
atlasMaskVolume = vtkMRMLScalarVolumeNode3

I tried:  
cliResult.GetParameterValue(1,0) this gives only value  
I thought of this then  
scene = cliResult.GetScene()  
n = scene.GetNodes()  
I was thinking of passing the parameter value to get the node by calling n.getnode because it has collection of vtk elements but this isnt working.

Dont know what to do to access the volume created by the module

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [September 13, 2018, 5:44am UTC](https://discourse.slicer.org/t/extract-point-data-from-label-map-in-cli-module/405/9 "2018-09-13T05:44:24Z")

</div>

> [@Saima](#):
>
> I need to get the output volume node

You specify the patient output volume as an input for the CommandLineModule node, so you know which volume it is (in your example above, it is vtkMRMLScalarVolumeNode4).

---

<div class="post-metadata">

**Author:** ![Saima](https://avatars.discourse-cdn.com/v4/letter/s/9d8465/32.png) [@Saima](https://discourse.slicer.org/u/Saima)\
**Post date:** [September 13, 2018, 5:46am UTC](https://discourse.slicer.org/t/extract-point-data-from-label-map-in-cli-module/405/10 "2018-09-13T05:46:04Z")

</div>

Hi Andras,  
But it will not be always scalar volume node 4. how to do it dynamically?

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [September 13, 2018, 5:27pm UTC](https://discourse.slicer.org/t/extract-point-data-from-label-map-in-cli-module/405/11 "2018-09-13T17:27:12Z")

</div>

You create the output volume node, so you know which one it is.

---

<div class="post-metadata">

**Author:** ![Saima](https://avatars.discourse-cdn.com/v4/letter/s/9d8465/32.png) [@Saima](https://discourse.slicer.org/u/Saima)\
**Post date:** [September 15, 2018, 9:28am UTC](https://discourse.slicer.org/t/extract-point-data-from-label-map-in-cli-module/405/12 "2018-09-15T09:28:27Z")

</div>

Ohhhhhhhhhhh Yes, outVolume.
