Import labelmap to segmentation node - in batch processing

slicer.modules.segmentations.logic().ImportLabelmapToSegmentationNode(labelmapVolumeNode, seg)
Traceback (most recent call last):
File “”, line 1, in
TypeError: arguments do not match any overloaded methods

i’m trying to make segmentation please help…

There are four signatures for that function:

You need to make sure the arguments match one of these.

Also the functions are static, so it’s possible that the error you get is because in python when you call a member function the first argument that is passed is self. Try to call the function as static, like this:

sorry but i need more help in this, actually im trying this simple code from the nightly documentation.

Create a segmentation from a labelmap volume and display in 3D
labelmapVolumeNode = getNode(‘label’)
seg = slicer.mrmlScene.AddNewNodeByClass(‘vtkMRMLSegmentationNode’)
slicer.modules.segmentations.logic().ImportLabelmapToSegmentationNode(labelmapVolumeNode, seg)
seg.CreateClosedSurfaceRepresentation()
slicer.mrmlScene.RemoveNode(labelmapVolumeNode)

Do you have a labelmap node with the name ‘label’ in your scene?
Is labelmapVolumeNode a valid labelmap volume node?
What do you see on the console if you type labelmapVolumeNode and press enter?

I have a problem related to this. The import works in Slicer Python Interactor but not in the terminal i.e.

    `Slicer --no-main-window --python-script  myScript.py`

I get an error

arguments do not match any overloaded methods

Here is the part from the script, I also tried the commented line as well:

[success, labelmapVolumeNode] = slicer.util.loadVolume(labelmapPath, returnNode=True)
segVolumeNode = slicer.vtkMRMLSegmentationNode()

#slicer.modules.segmentations.logic().ImportLabelmapToSegmentationNode(labelmapVolumeNode, segVolumeNode)
slicer.vtkSlicerSegmentationsModuleLogic.ImportLabelmapToSegmentationNode(labelmapVolumeNode, segVolumeNode)

When I pass None as the first argument to the ImportLabelmapToSegmentationNode call I get the error you’re seeing, but if I pass a valid volume node, it returns True.
Did the label map volume load? What’s the value of “success”?

The label map volume is loaded successfully, I get “True” when I print “success” .

I needed the import to use the segmentation later for getting the statistics. I did a workaround and computed it manually and it works .

    tmpSegArray = slicer.util.array(labelmapVolumeNode.GetID())
    tmpSegArraySpacing = labelmapVolumeNode.GetSpacing()

    vxSize = reduce(lambda x,y: x*y, tmpSegArraySpacing)
    
    S1Count= len(tmpSegArray[tmpSegArray==7])
    S2Count= len(tmpSegArray[tmpSegArray==300])
    
    S1Size= S1Count * vxSize
    S2Size= S2Count * vxSize

We can investigate if you post a complete example script that reproduces the error.

Here is a complete script:

# myScript  
# to run: ~/sw/Slicer-4.8.1/Slicer --no-main-window --python-script myScript.py "

from __main__ import vtk, qt, ctk, slicer
from copy import deepcopy
import Tkinter, tkFileDialog, sys ,math 
import os, re , time , datetime
import sitkUtils,   numpy as np, SimpleITK as sitk
from shutil import copyfile
print("                   myScript ")        
[success, labelmapVolumeNode] = slicer.util.loadVolume("./myImg-label.nrrd", returnNode=True)    
print ("labelmapVolumeNoderesult : "  + str(success))
segVolumeNode = slicer.vtkMRMLSegmentationNode()   slicer.modules.segmentations.logic().ImportLabelmapToSegmentationNode(labelmapVolumeNode,segVolumeNode)
 #slicer.vtkSlicerSegmentationsModuleLogic.ImportLabelmapToSegmentationNode(labelmapVolumeNode, segVolumeNode)
print(" All tasks are done!  ") 
exit() 

And here is the output:

  Slicer --no-main-window --python-script ./myScript.py
  Number of registered modules: 150 
  Number of instantiated modules: 150 
  Number of loaded modules: 150 
               myScript 
  Loaded volume from file: ~/myImg-label.nrrd. 
  Dimensions: 80x80x20. Number of components: 1. Pixel type: short.
 "Volume" Reader has successfully read the file "./myImg-label.nrrd" "[0.02s]" 
 labelmapVolumeNoderesult : True
 Traceback (most recent call last):
  File "<string>", line 7, in <module>
  File "./myScript.py", line 20, in <module>    slicer.modules.segmentations.logic().ImportLabelmapToSegmentationNode(labelmapVolumeNode,segVolumeNode)
  TypeError: arguments do not match any overloaded methods

There were a couple of problems:

  • It is not enough to create the segmentation node but you need to also add to the scene. You can use slicer.mrmlScene.AddNewNodeByClass to create a node and add it to the scene in one step.
  • slicer.util.loadVolume loads file as scalar volume, while ImportLabelmapToSegmentationNode expects a labelmap volume; you need to use slicer.util.loadLabelVolume instead
  • Relative path was used for the input image. I’m not sure what is the working directory of Slicer. It is safer to use absolute path.

After fixing these issues, a full working example:

[success, labelmapVolumeNode] = slicer.util.loadLabelVolume(r"c:\Users\msliv\OneDrive\Projects\SlicerTesting3\20180410-StartupSegmentation\Segmentation-label.nrrd", returnNode=True)
segNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLSegmentationNode")
slicer.modules.segmentations.logic().ImportLabelmapToSegmentationNode(labelmapVolumeNode, segNode)
2 Likes

It is not enough to create the segmentation node but you need to also add to the scene. You can use slicer.mrmlScene.AddNewNodeByClass to create a node and add it to the scene in one step.

I thought we don’t need a scene when we run from a script, I was wrong.

slicer.util.loadVolume loads file as scalar volume, while ImportLabelmapToSegmentationNode expects a labelmap volume; you need to use slicer.util.loadLabelVolume instead

I guess this is the main cause of the error.

It works now thanks for investigating the error.

Modules usually require all input and output nodes to be in the same scene. Without a scene, you cannot specify parent transforms or any other relationship between nodes.

2 Likes