Skip to content

Random Walker fails if a seed is surrounded by masked pixels #1875

Closed

Description

@AlexandreAbraham

This is due to the sparse representation of the graph. Here is the code to reproduce the error:

from skimage.segmentation import random_walker
import numpy as np


data = np.random.random((3, 3))
labels = -np.ones((3, 3))

# Make a seed
labels[0, 1] = 1
# Point next to the seed
labels[0, 0] = 0

# Set a seed in the middle, it is surrounded by masked pixels
labels[2, 2] = 2

random_walker(data, labels)

The error:

Traceback (most recent call last):
  File "test_rw.py", line 15, in <module>
    random_walker(data, labels)
  File "/home/aa013911/.local/lib/python3.4/site-packages/skimage/segmentation/random_walker_segmentation.py", line 430, in random_walker
    lap_sparse, B = _buildAB(lap_sparse, labels)
  File "/home/aa013911/.local/lib/python3.4/site-packages/skimage/segmentation/random_walker_segmentation.py", line 145, in _buildAB
    B = lap_sparse[unlabeled_indices][:, seeds_indices]
  File "/usr/lib/python3/dist-packages/scipy/sparse/csr.py", line 250, in __getitem__
    P = extractor(col,self.shape[1]).T        # [1:2,[1,2]]
  File "/usr/lib/python3/dist-packages/scipy/sparse/csr.py", line 214, in extractor
    (min_indx,max_indx) = check_bounds(indices,N)
  File "/usr/lib/python3/dist-packages/scipy/sparse/csr.py", line 200, in check_bounds
    raise IndexError('index (%d) out of range' % max_indx)
IndexError: index (2) out of range

Another error is triggered if the 8th line labels[0, 1] = 1 is commented.

Activity

  1. emmanuelle commented on Jan 24, 2016

    @emmanuelle
    Member

    Thank you @AlexandreAbraham Indeed, it is implicitly assumed that every pixel with 0-label is in the same connected component as a seed, and conversely every seed is in the same connected component as a 0-label pixel.

    We could check this by calling measure.label and checking that each connected components has label values both 0 and positive. We could discard isolated seeds, but isolated 0-label pixels should trigger a (more meaningful) error, I think. These verifications would have a performance cost, but we might add a keyword argument to bypass them for a performance-eager user.

    What do you think @AlexandreAbraham @jni @ahojnnes @JDWarner ?

  2. jni commented on Jan 25, 2016

    @jni
    Member

    @emmanuelle I must admit I've never used random_walker before and I'm a bit mystified by the interface, which appears to be quite different from watershed. In watershed, seeds are any nonzero point, and the segmentation proceeds from the seeds. What's happening here?

  3. JDWarner commented on Jan 25, 2016

    @JDWarner
    Contributor

    @jni random_walker in effect finds the shortest stochastic path - with penalties paid based on some weighting function - from each point in the image back to the nearest element of each seed region. I think of it more like a graph theory problem, though it can be expressed in multiple frameworks.

    The problem is that there is an inherent assumption: a path exists from each seed region to every non-masked point on the image.

    We've added the ability to mask regions in this algorithm. There are good use cases for this. However, when you fence off or enclose a seed region the algorithm is going to have a problem. This is actually expected. There's no way to get there!

    @emmanuelle delineated our options well. We basically can either check and discard such regions or allow them to fail with a more graceful error message. I'd prefer a little of both: do the check, but instead of discarding data provide the user with a very informative error message including the problematic label.

  4. jni commented on Jan 25, 2016

    @jni
    Member

    Why does anything have to fail? Why not solve separately for each connected component? Even if the result is a single labeled voxel, it sounds to me like this would be a correct interpretation for many inputs.

  5. AlexandreAbraham commented on Jan 26, 2016

    @AlexandreAbraham
    Author

    Why does anything have to fail? Why not solve separately for each connected component? Even if the result is a single labeled voxel, it sounds to me like this would be a correct interpretation for many inputs.

    If a 0-label pixel is isolated, that means that the RW is unable to label it. An error must be triggered because not all pixels have been labelled. And if the user makes the assumption that all pixels are labelled after the random walker, he is going to have a bad surprise.

    We could check this by calling measure.label and checking that each connected components has label values both 0 and positive.

    label would be a bit overkilled, as you mentioned it. A simple way to fix the first bug (error when a seed is isolated) is to add one a reflexive edge for each vertex. For the problem of isolated unlabeled voxels, I don't know if a simple solution exists.

  6. emmanuelle commented on Jan 26, 2016

    @emmanuelle
    Member

    @AlexandreAbraham I'm not sure I understand what you propose with the reflexive edge, could you please elaborate? We also have to make sure that the size of the graph / sparse matrix does not increase too much, since the random walker algorithm has a quite high memory cost.

  7. jni commented on Jan 26, 2016

    @jni
    Member

    @AlexandreAbraham it seems to me the problem is not isolated seeds but isolated unlabeled points. That's what we should check for. Other than that I think my proposal remains valid... Which I just saw is basically the same proposal as @emmanuelle's original one. =)

  8. AlexandreAbraham commented on Jan 27, 2016

    @AlexandreAbraham
    Author

    @jnl there are 2 errors: when a seed is isolated (see my script above), which is not legitimate, and one when unlabeled points are isolated, which is legitimate but raises an unhelpful error message (see my script, just comment the 9th line).

    @emmanuelle the isolated seeds and points "disappear" of the laplacian matrix because they are not present in any edge. By doing a quick and dirty patch where I add edges going from the seeds to themselves with 0 cost, it seemed to solve the isolated seed problem (I had no time to check if it impact the rest of the algorithm though...).

  9. emmanuelle commented on Jan 30, 2016

    @emmanuelle
    Member

    The issue comes from here: https://github.com/scikit-image/scikit-image/blob/master/skimage/segmentation/random_walker_segmentation.py#L170
    since pixels corresponding to isolated seeds are removed. We could indeed add edges from pixels seeds to themselves, but this would require to pass additional arguments to the function _mask_edges_weights

  10. rfezzani commented on Oct 19, 2021

    @rfezzani
    Member

    I can't reproduce this issue. Should we close it?

  11. rfezzani commented on Oct 20, 2021

    @rfezzani
    Member
    >>> from skimage.segmentation import random_walker
    >>> import numpy as np
    >>> 
    >>> 
    >>> data = np.random.random((3, 3))
    >>> labels = -np.ones((3, 3))
    >>> 
    >>> # Make a seed
    ... labels[0, 1] = 1
    >>> # Point next to the seed
    ... labels[0, 0] = 0
    >>> 
    >>> # Set a seed in the middle, it is surrounded by masked pixels
    ... labels[2, 2] = 2
    >>> 
    >>> random_walker(data, labels)
    array([[ 1.,  1., -1.],
           [-1., -1., -1.],
           [-1., -1.,  2.]])
  12. AlexandreAbraham commented on Oct 20, 2021

    @AlexandreAbraham
    Author

    If it works now, yes, it is 5 year old!

  13. rfezzani commented on Oct 20, 2021

    @rfezzani
    Member

    Thank you @AlexandreAbraham 🎉

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

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions