From e79bbed001309167b3df5116e75271a4479118c7 Mon Sep 17 00:00:00 2001 From: Vighnesh Birodkar Date: Wed, 28 Jan 2015 22:01:30 +0530 Subject: [PATCH] Improved test case and removed duplicate test --- doc/examples/plot_rag_merge.py | 42 ++++----------------------------- skimage/graph/tests/test_rag.py | 33 +++++++++++++------------- 2 files changed, 22 insertions(+), 53 deletions(-) diff --git a/doc/examples/plot_rag_merge.py b/doc/examples/plot_rag_merge.py index 5120d96b..0f8ca1cb 100644 --- a/doc/examples/plot_rag_merge.py +++ b/doc/examples/plot_rag_merge.py @@ -34,13 +34,12 @@ def _weight_mean_color(graph, src, dst, n): The absolute difference of the mean color between node `dst` and `n`. """ - #print 'merging diff = graph.node[dst]['mean color'] - graph.node[n]['mean color'] diff = np.linalg.norm(diff) return diff -def _pre_merge_mean_color(graph, src, dst): +def merge_mean_color(graph, src, dst): """Callback called before merging two nodes of a mean color distance graph. This method computes the mean color of `dst`. @@ -58,44 +57,13 @@ def _pre_merge_mean_color(graph, src, dst): graph.node[dst]['pixel count']) -def merge_hierarchical_mean_color(labels, rag, thresh, rag_copy=True, - in_place_merge=False): - """Perform hierarchical merging of a color distance RAG. - - Greedily merges the most similar pair of nodes until no edges lower than - `thresh` remain. - - Parameters - ---------- - labels : ndarray - The array of labels. - rag : RAG - The Region Adjacency Graph. - thresh : float - Regions connected by an edge with weight smaller than `thresh` are - merged. - rag_copy : bool, optional - If set, the RAG copied before modifying. - in_place_merge : bool, optional - If set, the nodes are merged in place. Otherwise, a new node is - created for each merge. - - Examples - -------- - >>> from skimage import data, graph, segmentation - >>> img = data.coffee() - >>> labels = segmentation.slic(img) - >>> rag = graph.rag_mean_color(img, labels) - >>> new_labels = graph.merge_hierarchical_mean_color(labels, rag, 40) - """ - return graph.merge_hierarchical(labels, rag, thresh, rag_copy, - in_place_merge, _pre_merge_mean_color, - _weight_mean_color) - img = data.coffee() labels = segmentation.slic(img, compactness=30, n_segments=400) g = graph.rag_mean_color(img, labels) -labels2 = merge_hierarchical_mean_color(labels, g, 40) + +labels2 = graph.merge_hierarchical(labels, g, 40, False, True, + merge_mean_color, _weight_mean_color) + g2 = graph.rag_mean_color(img, labels2) out = color.label2rgb(labels2, img, kind='avg') diff --git a/skimage/graph/tests/test_rag.py b/skimage/graph/tests/test_rag.py index 5887b502..df78ed7d 100644 --- a/skimage/graph/tests/test_rag.py +++ b/skimage/graph/tests/test_rag.py @@ -2,7 +2,7 @@ import numpy as np from skimage import graph from skimage._shared.version_requirements import is_installed from numpy.testing.decorators import skipif -from skimage import segmentation, io +from skimage import segmentation from numpy import testing @@ -110,17 +110,7 @@ def test_rag_error(): 2, 'non existant mode') -@skipif(not is_installed('networkx')) -def test_rag_error(): - img = np.zeros((10, 10, 3), dtype='uint8') - labels = np.zeros((10, 10), dtype='uint8') - labels[:5, :] = 0 - labels[5:, :] = 1 - testing.assert_raises(ValueError, graph.rag_mean_color, img, labels, - 2, 'non existant mode') - def _weight_mean_color(graph, src, dst, n): - #print 'merging diff = graph.node[dst]['mean color'] - graph.node[n]['mean color'] diff = np.linalg.norm(diff) return diff @@ -139,20 +129,31 @@ def merge_hierarchical_mean_color(labels, rag, thresh, rag_copy=True, in_place_merge, _pre_merge_mean_color, _weight_mean_color) + @skipif(not is_installed('networkx')) def test_rag_hierarchical(): img = np.zeros((8, 8, 3), dtype='uint8') labels = np.zeros((8, 8), dtype='uint8') - img[:, :, :] = 128 - labels[:,:] = 1 + img[:, :, :] = 30 + labels[:, :] = 1 - img[0:4,0:4,:] = 255,255,255 + img[0:4, 0:4, :] = 10, 10, 10 labels[0:4, 0:4] = 2 - img[4:, 0:4,:] = 0,0,0 + img[4:, 0:4, :] = 20, 20, 20 labels[4:, 0:4] = 3 g = graph.rag_mean_color(img, labels) - result = merge_hierarchical_mean_color(labels, g, 300) + g2 = g.copy() + thresh = 17.3206 # just above 10*sqrt(3) + + result = merge_hierarchical_mean_color(labels, g, thresh) + assert len(np.unique(result)) == 2 + + result = merge_hierarchical_mean_color(labels, g2, thresh, + in_place_merge=True) + assert len(np.unique(result)) == 2 + + result = graph.cut_threshold(labels, g, thresh) assert len(np.unique(result)) == 1