Skip to content

Consider returning segments starting from 1 from SLIC #3722

Closed

Description

@jni

We are getting close to being fully consistent in considering 0 to be the background in a label image. However, SLIC returns segments starting from 0, resulting in problems like this SO question:

https://stackoverflow.com/questions/54506079/slic-segmentation-in-skimage/54526084

where SLIC returns a full segmentation of the image, but segment 0 is ignored by regionprops. (Note: maybe the background segment should be measured by regionprops anyway!)

Way to reproduce

Input image:

example-image

import imageio
import numpy as np
import matplotlib.image as mpimg
import matplotlib.pyplot as plt
from skimage.measure import regionprops
from skimage.segmentation import slic
from skimage.segmentation import mark_boundaries

def rgb2gray(rgb):
    return np.dot(rgb[..., :3], [0.2126, 0.7152, 0.0722])

img_file_path = 'example-image.jpg'
image = imageio.imread(img_file_path)
segments_slic = slic(image, n_segments=250, compactness=100)
regions = regionprops(segments_slic, intensity_image=rgb2gray(image))
for props in regions:
    cy, cx = props.centroid
    plt.plot(cx, cy, 'ro')

plt.imshow(mark_boundaries(image, segments_slic))
plt.show()

Output:

figure_1

Note the missing segment annotation on the top left. Adding 1 + to the SLIC call fixes this.

>>> import sys; print(sys.version)
3.6.3 |Anaconda custom (x86_64)| (default, Nov  3 2017, 12:34:11)
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)]
>>> import platform; print(platform.platform())
Darwin-18.2.0-x86_64-i386-64bit

Activity

  1. manasbedmutha98 commented on Feb 5, 2019

    @manasbedmutha98

    Hi @jni. I am a beginner and need some help undertanding the fix.
    Like you suggested here, since we need to start from 1 instead of a 0, how about returning labels +1 as a temporary fix?

    Or else, any guidelines to go about?

  2. jni commented on Feb 5, 2019

    @jni
    MemberAuthor

    @manasbedmutha98 thanks for chiming in!

    Firstly, there's probably a more efficient way of doing this within SLIC so that we don't have to duplicate a potentially huge labels array.

    But, secondly, we have to worry about backwards compatibility, so the path to take is a bit tricky. We can't just change the output, but will have to instead add a flag to get the new behaviour, and a warning that the behaviour will change someday.

  3. added
    ⏩ type: Enhancement
    on Feb 15, 2019
  4. added this to the 0.16 milestone on Feb 15, 2019
  5. rfezzani commented on Apr 16, 2019

    @rfezzani
    Member

    Can't we consider that the problem comes from regionprops and not from slic?
    I mean that making regionpropos not consider 0 as background by default solves the problem.
    So, adding an argument background to regionprops (like in the function skimage.measure.label) may do the trick.

  6. jni commented on Apr 16, 2019

    @jni
    MemberAuthor

    @rfezzani well, that probably should happen anyway, yes (see #2755 and #3180), but I still think SLIC should return labels starting from 1, because it is a convention seen in many places, within the library (e.g. watershed returns labels from 1) but also in SciPy, which considers 0 as background for label and for find_objects (which is actually the source of this in regionprops).

  7. stefanv commented on Apr 16, 2019

    @stefanv
    Member
  8. jni commented on Apr 17, 2019

    @jni
    MemberAuthor

    Yes, an FAQ is certainly needed. We should ensure we distinguish issues that are "settled" from those that we are actively exploring solutions to.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    ⏩ type: Enhancement

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions