Relabel Integer image with large integer labels

python
scikit-image

#1

Hi,

Sometimes the labelled images created by my python programs have background value of 0 but the integer labels start from lets say 618 and go on till lets say 724 and it is different for different images in the stack. I was wondering if there was a function to relabel such integer images so that the label 618 becomes label 1 in this relabelling scheme and 619 becomes 2 and so on.

It is just for visual purposes in the end as the tiff files I finally get out of them tend to be more visually appealing if I can apply a FIJI lut and show them in color, in the current scenario I have to adjust contrast for each slice of the image separately, so such a re-labelling function in python/scikit-image would really help.


#2

Yes, indeed scikit-image does have such a function, segmentation.relabel_sequential:

http://scikit-image.org/docs/dev/api/skimage.segmentation.html#relabel-sequential

Hope it helps!

Juan


#3

Hi Thanks, This is the function I am looking for but I do not understand the argument I am supposed to pass here. I want to pass in a numpy array of 2D and get a numpy array of same dimension as input. But does not seem to work like that. Is there an example where this is applied to a 2D input image to get a re-labelled image out?


#4

This worked for me:

import numpy as np
from skimage import segmentation
image = np.array([[10, 10, 10, 10, 10, 10, 10],
                  [10, 10, 10, 10, 10, 10, 10],
                  [10, 10, 11, 11, 11, 10, 10],
                  [10, 10, 11, 11, 11, 10, 10],
                  [10, 10, 11, 11, 11, 10, 10],
                  [10, 10, 10, 10, 10, 10, 10],
                  [10, 10, 10, 10, 10, 10, 10]], dtype=np.uint8)

relab, forward_map, inverse_map = segmentation.relabel_sequential(image)

print(image)
print(relab)

Resulting in:

[[10 10 10 10 10 10 10]
 [10 10 10 10 10 10 10]
 [10 10 11 11 11 10 10]
 [10 10 11 11 11 10 10]
 [10 10 11 11 11 10 10]
 [10 10 10 10 10 10 10]
 [10 10 10 10 10 10 10]]
[[1 1 1 1 1 1 1]
 [1 1 1 1 1 1 1]
 [1 1 2 2 2 1 1]
 [1 1 2 2 2 1 1]
 [1 1 2 2 2 1 1]
 [1 1 1 1 1 1 1]
 [1 1 1 1 1 1 1]]

#5

Yes, sorry @kapoorlab: it looks like our documentation could be clearer that the input array and the first output array can be any dimension (1D, as in the example, but also 2D, 3D, 4D…). You can also “discard” the forward and inverse map (which are only useful if you want to apply the same mapping to other images):

relabelled = relabel_sequential(original)[0]

Again, this will work for original of any dimension.


#6

Yes thanks that worked, I didn’t see that the output was three tuples rather than just the output image.