# Adjusting and saving binary labelmaps

**URL:** <https://discourse.slicer.org/t/adjusting-and-saving-binary-labelmaps/1126>\
**Category:** Support\
**Tags:** registration, binarylabelmap, file-export\
**Created:** [September 26, 2017, 7:22pm UTC](https://discourse.slicer.org/t/adjusting-and-saving-binary-labelmaps/1126 "2017-09-26T19:22:38Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![stevenagl12](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/stevenagl12/32/3391_2.png) [@stevenagl12](https://discourse.slicer.org/u/stevenagl12)\
**Post date:** [September 26, 2017, 7:22pm UTC](https://discourse.slicer.org/t/adjusting-and-saving-binary-labelmaps/1126/1 "2017-09-26T19:22:38Z")

</div>

I have cropped ROI volumes that were registered and their segmentations transforms and exported to binary labelmaps. I am trying to save those labelmaps to files that have the same dimensions so that I can create a probabilistic atlas from them. I assumed that the registration and transformation of those segmentations would have constrained the labelmap images to the same dimensions, but I guess I was wrong. Is there a way to shift all of the segmentation labelmaps to have the same number of binary voxels and save those images to be called up in MATLAB to perform the euclidean distance transform and such?

---

<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 26, 2017, 7:40pm UTC](https://discourse.slicer.org/t/adjusting-and-saving-binary-labelmaps/1126/2 "2017-09-26T19:40:19Z")

</div>

When you export to Segmentation to Labelmap in Segmentations module’s Export-import section, in the Advanced section you can set a Reference volume. Segments will be padded/resampled to have the same geometry as the Reference volume.

Note that with numpy, SimpleITK, VTK, etc. libraries readily available in Slicer in Python, it’s hard to find anything that is worth sent to be computed in Matlab.

For example, you have several distance map computation algorithms in SimpleITK that you can call directly from Slicer. Just load MRHead sample volume and copy-paste this into the Python console:

```
import SimpleITK as sitk
import sitkUtils

intputVolumeNode = getNode('MRHead')
inputImage = sitkUtils.PullVolumeFromSlicer(inputVolumeNode)

dtFilter = sitk.SignedMaurerDistanceMapImageFilter()
dtFilter.SetSquaredDistance(False)
outputImage = dtFilter.Execute(inputImage)
    
outputVolumeNode = sitkUtils.PushVolumeToSlicer(outputImage, name='DistanceMap')
slicer.util.setSliceViewerLayers(background = outputVolumeNode)
```

---

<div class="post-metadata">

**Author:** ![stevenagl12](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/stevenagl12/32/3391_2.png) [@stevenagl12](https://discourse.slicer.org/u/stevenagl12)\
**Post date:** [September 26, 2017, 8:16pm UTC](https://discourse.slicer.org/t/adjusting-and-saving-binary-labelmaps/1126/3 "2017-09-26T20:16:33Z")

</div>

Is there a way to add all of the distance maps generated by the filter and then normalize that output by using the max and minimum distances to get a probabilistic distance atlas in slicer?

---

<div class="post-metadata">

**Author:** ![stevenagl12](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/stevenagl12/32/3391_2.png) [@stevenagl12](https://discourse.slicer.org/u/stevenagl12)\
**Post date:** [September 26, 2017, 8:17pm UTC](https://discourse.slicer.org/t/adjusting-and-saving-binary-labelmaps/1126/4 "2017-09-26T20:17:06Z")

</div>

Also, can slicer perform efficient belief propagation or markov random fields?

---

<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 26, 2017, 8:20pm UTC](https://discourse.slicer.org/t/adjusting-and-saving-binary-labelmaps/1126/5 "2017-09-26T20:20:33Z")

</div>

> [@stevenagl12](#):
>
> Is there a way to add all of the distance maps generated by the filter and then normalize that output by using the max and minimum distances to get a probabilistic distance atlas in slicer?

These seem to be basic numpy operations. Typically there is a direct mapping of these simple Matlab functions to numpy - see [https://docs.scipy.org/doc/numpy-dev/user/numpy-for-matlab-users.html](https://docs.scipy.org/doc/numpy-dev/user/numpy-for-matlab-users.html).

---

<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 26, 2017, 8:28pm UTC](https://discourse.slicer.org/t/adjusting-and-saving-binary-labelmaps/1126/6 "2017-09-26T20:28:35Z")

</div>

> [@stevenagl12](#):
>
> can slicer perform efficient belief propagation or markov random fields

ITK has some [MRF filters](https://itk.org/Doxygen/html/group__MRFFilters.html), you should be able to access them in Python via SimpleITK. There should be tons of other Python-based tools, which you may be able to import into Slicer directly (if they are Python-only). If you need larger, binary packages then for now you need to launch a separate Python interpreter to perform those computations (but this limitation should go away when Slicer is upgraded to use Python3).

---

<div class="post-metadata">

**Author:** ![stevenagl12](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/stevenagl12/32/3391_2.png) [@stevenagl12](https://discourse.slicer.org/u/stevenagl12)\
**Post date:** [June 13, 2018, 5:03pm UTC](https://discourse.slicer.org/t/adjusting-and-saving-binary-labelmaps/1126/7 "2018-06-13T17:03:30Z")

</div>

Is there a way, that when saving a binary file, you can save the pixel values for a particular object as a specific number that you want? Say you have the object of a target artery among other things in the binary labelmap. Can you save the labelmap with those pixels as 7200, and others as specific codes? Or altering a saved binary labelmap to adhere to those codes?

---

<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 13, 2018, 7:05pm UTC](https://discourse.slicer.org/t/adjusting-and-saving-binary-labelmaps/1126/8 "2018-06-13T19:05:36Z")

</div>

Probably the easiest is to export the segmentation to a labelmap node and then update voxel values with a numpy: `voxelArray[voxelArray > 0] = 7200`. See examples in the [script repository](https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Modify_voxels_in_a_volume).
