# Import labelmap to segmentation node - in batch processing

**URL:** <https://discourse.slicer.org/t/import-labelmap-to-segmentation-node-in-batch-processing/2360>\
**Category:** Development\
**Created:** [March 19, 2018, 2:10pm UTC](https://discourse.slicer.org/t/import-labelmap-to-segmentation-node-in-batch-processing/2360 "2018-03-19T14:10:58Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![anandmulay3](https://avatars.discourse-cdn.com/v4/letter/a/ac8455/32.png) [@anandmulay3](https://discourse.slicer.org/u/anandmulay3)\
**Post date:** [March 19, 2018, 2:10pm UTC](https://discourse.slicer.org/t/import-labelmap-to-segmentation-node-in-batch-processing/2360/1 "2018-03-19T14:10:58Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![cpinter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/cpinter/32/7995_2.png) [@cpinter](https://discourse.slicer.org/u/cpinter)\
**Post date:** [March 19, 2018, 2:43pm UTC](https://discourse.slicer.org/t/import-labelmap-to-segmentation-node-in-batch-processing/2360/2 "2018-03-19T14:43:41Z")

</div>

There are four signatures for that function:

> <https://github.com/Slicer/Slicer/blob/master/Modules/Loadable/Segmentations/Logic/vtkSlicerSegmentationsModuleLogic.h#L197-L221>

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:

> <https://github.com/Slicer/Slicer/blob/master/Utilities/Templates/Modules/ScriptedSegmentEditorEffect/SegmentEditorTemplateKeyLib/SegmentEditorEffect.py#L122>

---

<div class="post-metadata">

**Author:** ![anandmulay3](https://avatars.discourse-cdn.com/v4/letter/a/ac8455/32.png) [@anandmulay3](https://discourse.slicer.org/u/anandmulay3)\
**Post date:** [March 20, 2018, 6:46am UTC](https://discourse.slicer.org/t/import-labelmap-to-segmentation-node-in-batch-processing/2360/3 "2018-03-20T06:46:20Z")

</div>

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)

---

<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:** [March 20, 2018, 6:08pm UTC](https://discourse.slicer.org/t/import-labelmap-to-segmentation-node-in-batch-processing/2360/4 "2018-03-20T18:08:25Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![brhoom](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/brhoom/32/1228_2.png) [@brhoom](https://discourse.slicer.org/u/brhoom)\
**Post date:** [April 10, 2018, 8:26pm UTC](https://discourse.slicer.org/t/import-labelmap-to-segmentation-node-in-batch-processing/2360/5 "2018-04-10T20:26:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Nicole\_Aucoin](https://avatars.discourse-cdn.com/v4/letter/n/ebca7d/32.png) [@Nicole\_Aucoin](https://discourse.slicer.org/u/Nicole_Aucoin)\
**Post date:** [April 10, 2018, 8:46pm UTC](https://discourse.slicer.org/t/import-labelmap-to-segmentation-node-in-batch-processing/2360/6 "2018-04-10T20:46:31Z")

</div>

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”?

---

<div class="post-metadata">

**Author:** ![brhoom](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/brhoom/32/1228_2.png) [@brhoom](https://discourse.slicer.org/u/brhoom)\
**Post date:** [April 10, 2018, 9:04pm UTC](https://discourse.slicer.org/t/import-labelmap-to-segmentation-node-in-batch-processing/2360/7 "2018-04-10T21:04:49Z")

</div>

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
```

---

<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:** [April 10, 2018, 9:16pm UTC](https://discourse.slicer.org/t/import-labelmap-to-segmentation-node-in-batch-processing/2360/8 "2018-04-10T21:16:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![brhoom](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/brhoom/32/1228_2.png) [@brhoom](https://discourse.slicer.org/u/brhoom)\
**Post date:** [April 10, 2018, 9:40pm UTC](https://discourse.slicer.org/t/import-labelmap-to-segmentation-node-in-batch-processing/2360/9 "2018-04-10T21:40:19Z")

</div>

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
```

---

<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:** [April 11, 2018, 2:20am UTC](https://discourse.slicer.org/t/import-labelmap-to-segmentation-node-in-batch-processing/2360/10 "2018-04-11T02:20:22Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![brhoom](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/brhoom/32/1228_2.png) [@brhoom](https://discourse.slicer.org/u/brhoom)\
**Post date:** [April 11, 2018, 8:39am UTC](https://discourse.slicer.org/t/import-labelmap-to-segmentation-node-in-batch-processing/2360/11 "2018-04-11T08:39:57Z")

</div>

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

---

<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:** [April 11, 2018, 1:26pm UTC](https://discourse.slicer.org/t/import-labelmap-to-segmentation-node-in-batch-processing/2360/12 "2018-04-11T13:26:11Z")

</div>

> [@brhoom](#):
>
> I thought we don’t need a scene when we run from a script

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.
