Labelmap to scaler

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.

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!')
1 Like

Hello @Sean_Hatton,

You are using the correct filter, LabelMapToLabelImageFilter, 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?

import SimpleITK as sitk

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

print(label_map)
print(label_image)
1 Like

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?

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.

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")
1 Like

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)