You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Random Walker fails if a seed is surrounded by masked pixels #1875
This is due to the sparse representation of the graph. Here is the code to reproduce the error:
fromskimage.segmentationimportrandom_walkerimportnumpyasnpdata=np.random.random((3, 3))
labels=-np.ones((3, 3))
# Make a seedlabels[0, 1] =1# Point next to the seedlabels[0, 0] =0# Set a seed in the middle, it is surrounded by masked pixelslabels[2, 2] =2random_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.
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.
@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?
@jnirandom_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.
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.
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.
@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.
@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. =)
@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...).
>>>fromskimage.segmentationimportrandom_walker>>>importnumpyasnp>>>>>>>>>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.]])
Activity
emmanuelle commentedon Jan 24, 2016
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.labeland 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 ?
jni commentedon Jan 25, 2016
@emmanuelle I must admit I've never used
random_walkerbefore 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?JDWarner commentedon Jan 25, 2016
@jni
random_walkerin 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.
jni commentedon Jan 25, 2016
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.
AlexandreAbraham commentedon Jan 26, 2016
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.
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.
emmanuelle commentedon Jan 26, 2016
@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.
jni commentedon Jan 26, 2016
@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. =)
AlexandreAbraham commentedon Jan 27, 2016
@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...).
emmanuelle commentedon Jan 30, 2016
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_weightsrfezzani commentedon Oct 19, 2021
I can't reproduce this issue. Should we close it?
rfezzani commentedon Oct 20, 2021
AlexandreAbraham commentedon Oct 20, 2021
If it works now, yes, it is 5 year old!
rfezzani commentedon Oct 20, 2021
Thank you @AlexandreAbraham 🎉