# Labelmap to scaler

**URL:** <https://discourse.itk.org/t/labelmap-to-scaler/5693>\
**Category:** Algorithms\
**Created:** [February 10, 2023, 11:36pm UTC](https://discourse.itk.org/t/labelmap-to-scaler/5693 "2023-02-10T23:36:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sean\_Hatton](https://discourse.itk.org/user_avatar/discourse.itk.org/sean_hatton/32/2957_2.png) [@Sean\_Hatton](https://discourse.itk.org/u/Sean_Hatton)\
**Post date:** [February 10, 2023, 11:36pm UTC](https://discourse.itk.org/t/labelmap-to-scaler/5693/1 "2023-02-10T23:36:47Z")

</div>

How do you get a labelmap back to a scalar image? I have been trying the below, but I keep getting a labelmap with on object, not a scalar. LabelmapToBinary didn’t work. Thoughts? I am trying to run mrc2tif but it won’t read labelmaps.

```plaintext
import os, glob
from pathlib import Path
import SimpleITK as sitk

wkdir = (os.getcwd())
files = glob.glob(wkdir + '/*seg.mrc')

for file in files:
    file = Path(file)
    print(f'Processing {file}')
    df = sitk.ReadImage(file, imageIO='MRCImageIO')
    df = sitk.Cast(df, sitk.sitkLabelUInt32)
    df = sitk.AggregateLabelMap(df)
    #df = sitk.LabelMapToLabel(df)
    binaryimage = sitk.Cast(df, sitk.sitkUInt8)
    #binaryimage = df != 0
    print(f'Writing out {file}')             
    sitk.WriteImage(binaryimage, file, imageIO='MRCImageIO', useCompression=True)
print('Complete!')

```

---

<div class="post-metadata">

**Author:** ![zivy](https://discourse.itk.org/user_avatar/discourse.itk.org/zivy/32/1726_2.png) [@zivy](https://discourse.itk.org/u/zivy)\
**Post date:** [February 12, 2023, 2:30pm UTC](https://discourse.itk.org/t/labelmap-to-scaler/5693/2 "2023-02-12T14:30:50Z")

</div>

Hello @Sean_Hatton,

You are using the correct filter, [LabelMapToLabelImageFilter](https://simpleitk.org/doxygen/v2_1/html/classitk_1_1simple_1_1LabelMapToLabelImageFilter.html), so not sure what is causing the issue. The following code works as you expected. What is the minimal change of the code below that causes the issue you are experiencing?

```auto
import SimpleITK as sitk

label_map = sitk.Image([128]*3, sitk.sitkLabelUInt32)
label_image = sitk.LabelMapToLabel(label_map)

print(label_map)
print(label_image)

```

---

<div class="post-metadata">

**Author:** ![Sean\_Hatton](https://discourse.itk.org/user_avatar/discourse.itk.org/sean_hatton/32/2957_2.png) [@Sean\_Hatton](https://discourse.itk.org/u/Sean_Hatton)\
**Post date:** [February 13, 2023, 4:27pm UTC](https://discourse.itk.org/t/labelmap-to-scaler/5693/3 "2023-02-13T16:27:42Z")

</div>

sitk.LabelMapToLabel(label\_map) is still a Label Image, not a scalar. I used AggregateLabelMap to get one label, but it is when I change the file type to UInt8 I loose all the information and its just black. This happens casting or indirectly through labelmaptobinary. Thoughts?

---

<div class="post-metadata">

**Author:** ![zivy](https://discourse.itk.org/user_avatar/discourse.itk.org/zivy/32/1726_2.png) [@zivy](https://discourse.itk.org/u/zivy)\
**Post date:** [February 13, 2023, 8:55pm UTC](https://discourse.itk.org/t/labelmap-to-scaler/5693/4 "2023-02-13T20:55:14Z")

</div>

Hello @Sean_Hatton,

Some nomenclature, in ITK/SimpleITK a `LabelMap` is a specific object type, different from an Image. Internally it stores a collection of pixel/voxel-objects and a background value. A label image is an image which has an integer pixel type.

I suspect what you are experiencing is a visualization issue, not a data/bug issue. When visualizing an image with small values, such as 1, 2, 3, using Fiji the image looks black even though the data is not all zeros. Code below illustrates that the label aggregation works as expected and you can see the tiny images (zoom in) are as expected. Change the label values to 1, 2, 3 and you get an image that looks all black, though the intensity values are correct, all 1 except the single background pixel.

```auto
import SimpleITK as sitk

# four pixel label map, pixel 0,0 is zero and is the only background pixel
label_map = sitk.Image([4,1], sitk.sitkLabelUInt32)
label_map[1,0] = 100
label_map[2,0] = 200
label_map[3,0] = 300

label_image = sitk.LabelMapToLabel(label_map)
sitk.Show(label_image, "label image")

# all pixels except the 0,0 will be 100
agg_label_map = sitk.AggregateLabelMap(label_map)
sitk.Show(sitk.Cast(agg_label_map, sitk.sitkUInt8), "converted to UInt8")

```

---

<div class="post-metadata">

**Author:** ![Sean\_Hatton](https://discourse.itk.org/user_avatar/discourse.itk.org/sean_hatton/32/2957_2.png) [@Sean\_Hatton](https://discourse.itk.org/u/Sean_Hatton)\
**Post date:** [February 15, 2023, 5:46pm UTC](https://discourse.itk.org/t/labelmap-to-scaler/5693/5 "2023-02-15T17:46:26Z")

</div>

Hi Ziv, thanks for this great information! I ended up rescaling to resolve it in line with your code.

```
df = sitk.ReadImage(file, imageIO='MRCImageIO')
df = sitk.Cast(df, sitk.sitkLabelUInt32)
df = sitk.AggregateLabelMap(df)
df = sitk.LabelMapToLabel(df)
df = sitk.Cast(sitk.RescaleIntensity(df), sitk.sitkUInt8)

```
