From 5c03f2aff5d131d85914d49732b187fc707e57df Mon Sep 17 00:00:00 2001 From: Juan Nunez-Iglesias Date: Tue, 15 Dec 2015 10:56:04 +1100 Subject: [PATCH] Bug: not all edges found by asymmetric footprint If we have a label image: [[1, 2], [3, 4]] Then the footprint: [[0, 0, 0], [0, 1, 1], [0, 1, 1]] will not discover the edge (2, 3), even though that edge should be present when `connectivity=2`. This commit fixes the bug by using a complete footprint, without the top/left surfaces zeroed out. See also: https://github.com/scikit-image/scikit-image/pull/1826#issuecomment-164597442 --- skimage/future/graph/rag.py | 27 ++++----------------------- 1 file changed, 4 insertions(+), 23 deletions(-) diff --git a/skimage/future/graph/rag.py b/skimage/future/graph/rag.py index 8710f9bd..8790281c 100644 --- a/skimage/future/graph/rag.py +++ b/skimage/future/graph/rag.py @@ -62,10 +62,10 @@ def _add_edge_filter(values, graph): can put it in the output array, but it is ignored by this filter. """ values = values.astype(int) - current = values[0] - for value in values[1:]: - if value != current: - graph.add_edge(current, value) + center = values[len(values) // 2] + for value in values: + if value != center and not graph.has_edge(center, value): + graph.add_edge(center, value) return 0. @@ -104,26 +104,7 @@ class RAG(nx.Graph): self.max_id = max(self.nodes_iter()) if label_image is not None: - # The footprint is constructed such that the first - # element in the array being passed to _add_edge_filter is - # the central value. - # - # For example - # if labels.ndim = 2 and connectivity = 1, then - # fp = [[0,0,0], - # [0,1,1], - # [0,1,0]] - # - # if labels.ndim = 2 and connectivity = 2, then - # fp = [[0,0,0], - # [0,1,1], - # [0,1,1]] fp = ndi.generate_binary_structure(label_image.ndim, connectivity) - for d in range(fp.ndim): - fp = fp.swapaxes(0, d) - fp[0, ...] = 0 - fp = fp.swapaxes(0, d) - ndi.generic_filter( label_image, function=_add_edge_filter,