Extract point data from label map in CLI module

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!

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 and here.

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());

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.

// 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();

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

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();

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

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).

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

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

Ohhhhhhhhhhh Yes, outVolume.