From a6c9a5a2a7e1b6f858134a1079f8e38d3a968f11 Mon Sep 17 00:00:00 2001 From: Vighnesh Birodkar Date: Mon, 16 Jun 2014 17:55:40 +0530 Subject: [PATCH] Renamed graph.py to rag.py --- skimage/graph/_construct.pyx | 6 +++--- skimage/graph/rag.py | 35 +++++++++++++++++++++++++++++++++ skimage/graph/tests/test_rag.py | 10 ++++++++++ 3 files changed, 48 insertions(+), 3 deletions(-) create mode 100644 skimage/graph/rag.py create mode 100644 skimage/graph/tests/test_rag.py diff --git a/skimage/graph/_construct.pyx b/skimage/graph/_construct.pyx index 3b91fe64..ece98143 100644 --- a/skimage/graph/_construct.pyx +++ b/skimage/graph/_construct.pyx @@ -1,4 +1,4 @@ -import networkx as nx +import rag cimport numpy as cnp import numpy as np @@ -10,7 +10,7 @@ def construct_rag_meancolor_3d( img, arr): b = arr.shape[1] h = arr.shape[2] - g = nx.Graph() + g = rag.RAG() i = 0 while i < l - 1: @@ -81,7 +81,7 @@ def construct_rag_meancolor_2d(img, arr): l = arr.shape[0] b = arr.shape[1] - g = nx.Graph() + g = rag.RAG() i = 0 while i < l - 1: diff --git a/skimage/graph/rag.py b/skimage/graph/rag.py new file mode 100644 index 00000000..9795787b --- /dev/null +++ b/skimage/graph/rag.py @@ -0,0 +1,35 @@ +import networkx as nx +import _construct +from skimage import util + +class RAG(nx.Graph): + + def merge_nodes(i,j): + if not self.has_edge(i, j): + raise ValueError('Cant merge non adjacent nodes') + + # print "before ",self.order() + for x in self.neighbors(i): + if x == j: + continue + w1 = self.get_edge_data(x, i)['weight'] + w2 = -1 + if self.has_edge(x, j): + w2 = self.get_edge_data(x, j)['weight'] + + w = max(w1, w2) + + self.add_edge(x, j, weight=w) + + self.node[j]['labels'] += self.node[i]['labels'] + self.remove_node(i) + +def rag_meancolor(img,labels): + + img = util.img_as_ubyte(img) + if img.ndim == 3 : + return _construct.construct_rag_meancolor_3d(img,labels) + elif img.ndim == 2 : + return _construct.construct_rag_meancolor_2d(img,labels) + else : + raise ValueError("Image dimension not supported") diff --git a/skimage/graph/tests/test_rag.py b/skimage/graph/tests/test_rag.py new file mode 100644 index 00000000..08d1a1cf --- /dev/null +++ b/skimage/graph/tests/test_rag.py @@ -0,0 +1,10 @@ +import numpy as np + +def test_threshold_cut(): + arr = np.array((100,100,3),dtype='uint8') + arr[:50,:50] = 0 + arr[:50,50:] = 1 + arr[50:,50:] = 2 + arr[50:,50:] = 3 + +