# Change spacing of segment using Python

**URL:** <https://discourse.slicer.org/t/change-spacing-of-segment-using-python/30499>\
**Category:** Development\
**Tags:** segmentation, python\
**Created:** [July 10, 2023, 2:07pm UTC](https://discourse.slicer.org/t/change-spacing-of-segment-using-python/30499 "2023-07-10T14:07:50Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![ahmad\_alminnawi](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/ahmad_alminnawi/32/66700_2.png) [@ahmad\_alminnawi](https://discourse.slicer.org/u/ahmad_alminnawi)\
**Post date:** [July 10, 2023, 2:07pm UTC](https://discourse.slicer.org/t/change-spacing-of-segment-using-python/30499/1 "2023-07-10T14:07:50Z")

</div>

Hi all,  
I would like to specify the spacing of a segment i am creating using python.

Manually, I can do so using “specify geometry” as seen here [Segmentations — 3D Slicer documentation](https://slicer.readthedocs.io/en/latest/user_guide/modules/segmentations.html)  
under " Editing a segmentation imported from model "

Can anyone let me know how to do it in python? here is my code to create a segment with default (1x1x1) voxel size but I want to change it to 0.004x0.004x0.004

# Create segmentation

segmentationNode = slicer.mrmlScene.AddNewNodeByClass(“vtkMRMLSegmentationNode”)  
segmentationNode.CreateDefaultDisplayNodes() # only needed for display  
segmentationNode.SetName(‘Segmentation’)

# Import the model into the segmentation node

slicer.modules.segmentations.logic().ImportModelToSegmentationNode(cylinderNode, segmentationNode)

---

<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:** [July 10, 2023, 2:15pm UTC](https://discourse.slicer.org/t/change-spacing-of-segment-using-python/30499/2 "2023-07-10T14:15:34Z")

</div>

You can set the geometry (origin, spacing, axis directions, and extens) of the binary labelmap segmentation as shown in [this example in the script repository](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#rasterize-a-model-and-save-it-to-a-series-of-image-files).

---

<div class="post-metadata">

**Author:** ![ahmad\_alminnawi](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/ahmad_alminnawi/32/66700_2.png) [@ahmad\_alminnawi](https://discourse.slicer.org/u/ahmad_alminnawi)\
**Post date:** [July 10, 2023, 2:39pm UTC](https://discourse.slicer.org/t/change-spacing-of-segment-using-python/30499/3 "2023-07-10T14:39:35Z")

</div>

thank you for the reply but in that case I will have to allocate memory to each element which is huge in my case so it gives the error “Unable to allocate 242857881500 elements of size 1 bytes.”

is there a way to create the segmentation like I did before in the simple code and then modify its spacing?

---

<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:** [July 10, 2023, 2:44pm UTC](https://discourse.slicer.org/t/change-spacing-of-segment-using-python/30499/4 "2023-07-10T14:44:17Z")

</div>

> [@ahmad\_alminnawi](#):
>
> 242857881500

This means that your reference image size would be 226GB. In general, I recommend to have 10x more RAM than your reference image size, which would mean that you would need 2260GB RAM and really, really fast processor to be able to do anything with this data.

These requirements are unrealistic to meet with today’s computing hardware, so I would recommend to reconsider your resolution needs or crop your data to a smaller region.

There might be also some misunderstanding about units. What is the distance unit of your model (meter, milllimeter, …)? What is the distance unit for 0.004?

---

<div class="post-metadata">

**Author:** ![ahmad\_alminnawi](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/ahmad_alminnawi/32/66700_2.png) [@ahmad\_alminnawi](https://discourse.slicer.org/u/ahmad_alminnawi)\
**Post date:** [July 10, 2023, 2:47pm UTC](https://discourse.slicer.org/t/change-spacing-of-segment-using-python/30499/5 "2023-07-10T14:47:29Z")

</div>

it it 4 mico meters (0.004 millimeters)  
on the GUI it is working fine which is why i am confused

---

<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:** [July 10, 2023, 2:49pm UTC](https://discourse.slicer.org/t/change-spacing-of-segment-using-python/30499/6 "2023-07-10T14:49:41Z")

</div>

What are the bounds (min/mix x, y, z coordinates) of the model that you are importing?

---

<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:** [July 10, 2023, 2:54pm UTC](https://discourse.slicer.org/t/change-spacing-of-segment-using-python/30499/7 "2023-07-10T14:54:34Z")

</div>

In the script example, an empty margin of 10mm is added around the model, which corresponds to about 5000 pixels along each axis. Probably you want to change it to something like `outputVolumeMarginMm = 0.012`.

---

<div class="post-metadata">

**Author:** ![ahmad\_alminnawi](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/ahmad_alminnawi/32/66700_2.png) [@ahmad\_alminnawi](https://discourse.slicer.org/u/ahmad_alminnawi)\
**Post date:** [July 10, 2023, 2:56pm UTC](https://discourse.slicer.org/t/change-spacing-of-segment-using-python/30499/8 "2023-07-10T14:56:19Z")

</div>

I am importing an stl file of a cylinder that I created on 3dslicer  
I wrote a code where i would draw a 2 points (markups) and then a cylinder of radius 3mm and height 3mm would be created and exported as an stl file but I did not specify any bounds i just specified the points on the GUI to assign the center and direction along these points. this is the code

cylinder = pyvista.Cylinder(center=center\_cylinder, direction=cylinder\_direction,  
radius=cylinder\_radius, height=cylinder\_height)  
cylinderNode = slicer.modules.models.logic().AddModel(cylinder)

slicer.util.exportNode(cylinderNode, parentpath / ‘segmented\_volumes’ / side / cylinder\_outputfile)

---

<div class="post-metadata">

**Author:** ![ahmad\_alminnawi](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/ahmad_alminnawi/32/66700_2.png) [@ahmad\_alminnawi](https://discourse.slicer.org/u/ahmad_alminnawi)\
**Post date:** [July 10, 2023, 3:05pm UTC](https://discourse.slicer.org/t/change-spacing-of-segment-using-python/30499/9 "2023-07-10T15:05:32Z")

</div>

yes, reducing the volume margin worked like a charm! thanks

---

<div class="post-metadata">

**Author:** ![ahmad\_alminnawi](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/ahmad_alminnawi/32/66700_2.png) [@ahmad\_alminnawi](https://discourse.slicer.org/u/ahmad_alminnawi)\
**Post date:** [July 11, 2023, 2:20pm UTC](https://discourse.slicer.org/t/change-spacing-of-segment-using-python/30499/10 "2023-07-11T14:20:48Z")

</div>

One more thing,  
later i am trying to combine 2 segments together using the following code:

```auto
segmentName = 'pores'

addedSegmentID = seg.GetSegmentation().AddEmptySegment(segmentName)
segmentEditorNode.SetSelectedSegmentID(addedSegmentID)

# Logical operators
segmentEditorWidget.setActiveEffectByName("Logical operators")
effect = segmentEditorWidget.activeEffect()

operation = "UNION"
segment_to_add = "void"

effect.setParameter("Operation", operation) # change the operation here
effect.setParameter("ModifierSegmentID", segment_to_add)
effect.self().onApply()

```

i am also getting the same error we discussed earlier “Unable to allocate 1711230150 elements of size 1 bytes.”

Do I have to specify the “VolumeMargin” somewhere here as well?

---

<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:** [July 23, 2023, 12:11pm UTC](https://discourse.slicer.org/t/change-spacing-of-segment-using-python/30499/11 "2023-07-23T12:11:51Z")

</div>

`VolumeMargin` is just used in your own code, it is not an effect parameter.

I suspect that the two segments are far from each other, which would make the combined segmentation too large.

I would recommend to work with lower resolution (larger spacing) during development to make everything work faster and let you see the results even if you make some small mistakes.
