mirror of
https://github.com/wassname/scikit-image.git
synced 2026-08-05 13:21:12 +08:00
Added mean color RAG construction code
This commit is contained in:
@@ -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
|
||||
@@ -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)
|
||||
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user