From 2b10d9817944de7cac622a00f73a96876913584d Mon Sep 17 00:00:00 2001 From: Vighnesh Birodkar Date: Mon, 16 Jun 2014 02:04:42 +0530 Subject: [PATCH] Added mean color RAG construction code --- skimage/graph/_construct.pyx | 126 +++++++++++++++++++++++++++++++++++ skimage/graph/graph.py | 23 +++++++ skimage/graph/setup.py | 5 ++ 3 files changed, 154 insertions(+) create mode 100644 skimage/graph/_construct.pyx create mode 100644 skimage/graph/graph.py diff --git a/skimage/graph/_construct.pyx b/skimage/graph/_construct.pyx new file mode 100644 index 00000000..3b91fe64 --- /dev/null +++ b/skimage/graph/_construct.pyx @@ -0,0 +1,126 @@ +import networkx as nx +cimport numpy as cnp +import numpy as np + + +def construct_rag_meancolor_3d( img, arr): + cdef Py_ssize_t l, b, h, i, j, k + cdef cnp.int32_t current, next + l = arr.shape[0] + b = arr.shape[1] + h = arr.shape[2] + + g = nx.Graph() + + i = 0 + while i < l - 1: + j = 0 + while j < b - 1: + k = 0 + while k < h - 1: + current = arr[i, j, k] + + try : + g.node[current]['pixel_count'] += 1 + g.node[current]['total_color'] += img[i,j] + except KeyError: + g.add_node(current) + g.node[current]['pixel_count'] = 1 + g.node[current]['total_color'] = img[i,j].astype(np.long) + g.node[current]['labels'] = [arr[i,j]] + + next = arr[i + 1, j, k] + if current != next: + g.add_edge(current, next) + + next = arr[i, j + 1, k] + if current != next: + g.add_edge(current, next) + + next = arr[i + 1, j + 1, k] + if current != next: + g.add_edge(current, next) + + next = arr[i + 1, j, k + 1] + if current != next: + g.add_edge(current, next) + + next = arr[i, j + 1, k + 1] + if current != next: + g.add_edge(current, next) + + next = arr[i + 1, j + 1, k + 1] + if current != next: + g.add_edge(current, next) + + next = arr[i, j, k + 1] + if current != next: + g.add_edge(current, next) + + + k += 1 + + j += 1 + + i += 1 + + + for n in g.nodes(): + g.node[n]['mean_color'] = g.node[n]['total_color']/g.node[n]['pixel_count'] + + for x,y in g.edges_iter() : + diff = g.node[x]['mean_color'] - g.node[y]['mean_color'] + g[x][y]['weight'] = np.sqrt(diff.dot(diff)) + + return g + + +def construct_rag_meancolor_2d(img, arr): + cdef Py_ssize_t l, b, h, i, j, k + cdef cnp.int32_t current, next + l = arr.shape[0] + b = arr.shape[1] + + g = nx.Graph() + + i = 0 + while i < l - 1: + j = 0 + while j < b - 1: + current = arr[i, j] + + try : + g.node[current]['pixel_count'] += 1 + g.node[current]['total_color'] += img[i,j] + except KeyError: + g.add_node(current) + g.node[current]['pixel_count'] = 1 + g.node[current]['total_color'] = img[i,j].astype(np.long) + g.node[current]['labels'] = [arr[i,j]] + + next = arr[i + 1, j] + if current != next: + g.add_edge(current, next) + + next = arr[i, j + 1] + if current != next: + g.add_edge(current, next) + + next = arr[i + 1, j + 1] + if current != next: + g.add_edge(current, next) + + j += 1 + + i += 1 + + + for n in g.nodes(): + g.node[n]['mean_color'] = g.node[n]['total_color']/g.node[n]['pixel_count'] + + for x,y in g.edges_iter() : + diff = g.node[x]['mean_color'] - g.node[y]['mean_color'] + g[x][y]['weight'] = np.sqrt(diff.dot(diff)) + + + return g diff --git a/skimage/graph/graph.py b/skimage/graph/graph.py new file mode 100644 index 00000000..2597e985 --- /dev/null +++ b/skimage/graph/graph.py @@ -0,0 +1,23 @@ +import netwrokx as nx + +class Graph(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) diff --git a/skimage/graph/setup.py b/skimage/graph/setup.py index 463d2739..252eeed2 100644 --- a/skimage/graph/setup.py +++ b/skimage/graph/setup.py @@ -17,6 +17,8 @@ def configuration(parent_package='', top_path=None): cython(['_spath.pyx'], working_path=base_path) cython(['_mcp.pyx'], working_path=base_path) cython(['heap.pyx'], working_path=base_path) + cython(['_construct.pyx'], working_path=base_path) + config.add_extension('_spath', sources=['_spath.c'], include_dirs=[get_numpy_include_dirs()]) @@ -24,6 +26,9 @@ def configuration(parent_package='', top_path=None): include_dirs=[get_numpy_include_dirs()]) config.add_extension('heap', sources=['heap.c'], include_dirs=[get_numpy_include_dirs()]) + config.add_extension('_construct', sources=['_construct.c'], + include_dirs=[get_numpy_include_dirs()]) + return config