Improved test case and removed duplicate test

This commit is contained in:
Vighnesh Birodkar
2015-01-28 22:01:30 +05:30
parent 237b11fefd
commit e79bbed001
2 changed files with 22 additions and 53 deletions
+5 -37
View File
@@ -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')
+17 -16
View File
@@ -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