Renamed _construct.pyx to _build_rag.pyx and some code corrections

This commit is contained in:
Vighnesh Birodkar
2014-06-18 00:46:25 +05:30
parent 53318c44df
commit 2a7ec3afd4
5 changed files with 41 additions and 41 deletions
+2 -2
View File
@@ -3,9 +3,9 @@
RAG Thresholding
================
This examples constructs a Region Adjacency Graph and merges region which are
This examples constructs a Region Adjacency Graph (RAG) and merges regions which are
similar in color. We construct a RAG and define edges as the difference in
mean color. We the join regions with similar mean color.
mean color. We then join regions with similar mean color.
"""
@@ -8,9 +8,9 @@ def construct_rag_meancolor_3d(img, arr):
difference in mean color of regions as edge weights.
Given an image and its segmentation, this method constructs the
corresponsing Region Adjacency Graph (RAG).Each node in the RAG
represents a contiguous pixels with in `img` the same label in
`arr`
corresponsing Region Adjacency Graph (RAG). Each node in the RAG
represents contiguous pixels with in `img` with the same label in
`arr`. There is an edge between each pair of adjacent regions.
Parameters
----------
@@ -25,29 +25,29 @@ def construct_rag_meancolor_3d(img, arr):
The region adjacency graph.
"""
cdef Py_ssize_t l, b, h, i, j, k
cdef Py_ssize_t depth,width,height, i, j, k
cdef cnp.int32_t current, next
l = arr.shape[0]
b = arr.shape[1]
h = arr.shape[2]
width = arr.shape[0]
height = arr.shape[1]
depth = arr.shape[2]
g = rag.RAG()
i = 0
while i < l - 1:
for i in range(width-1):
j = 0
while j < b - 1:
for j in range(height-1):
k = 0
while k < h - 1:
for k in range(depth-1):
current = arr[i, j, k]
try:
g.node[current]['pixel_count'] += 1
g.node[current]['total_color'] += img[i, j]
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]['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]
@@ -85,12 +85,12 @@ def construct_rag_meancolor_3d(img, arr):
i += 1
for n in g.nodes():
g.node[n]['mean_color'] = g.node[n][
'total_color'] / g.node[n]['pixel_count']
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))
diff = g.node[x]['mean color'] - g.node[y]['mean color']
g[x][y]['weight'] = np.linalg.norm(diff)
return g
@@ -100,9 +100,9 @@ def construct_rag_meancolor_2d(img, arr):
difference in mean color of regions as edge weights.
Given an image and its segmentation, this method constructs the
corresponsing Region Adjacency Graph (RAG).Each node in the RAG
represents a contiguous pixels with in `img` the same label in
`arr`
corresponsing Region Adjacency Graph (RAG). Each node in the RAG
represents contiguous pixels with in `img` with the same label in
`arr`. There is an edge between each pair of adjacent regions.
Parameters
----------
@@ -117,26 +117,26 @@ def construct_rag_meancolor_2d(img, arr):
The region adjacency graph.
"""
cdef Py_ssize_t l, b, h, i, j, k
cdef Py_ssize_t width, height, h, i, j, k
cdef cnp.int32_t current, next
l = arr.shape[0]
b = arr.shape[1]
width = arr.shape[0]
height = arr.shape[1]
g = rag.RAG()
i = 0
while i < l - 1:
for i in range(width-1):
j = 0
while j < b - 1:
for j in range(height-1):
current = arr[i, j]
try:
g.node[current]['pixel_count'] += 1
g.node[current]['total_color'] += img[i, j]
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]['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]
@@ -156,11 +156,11 @@ def construct_rag_meancolor_2d(img, arr):
i += 1
for n in g.nodes():
g.node[n]['mean_color'] = g.node[n][
'total_color'] / g.node[n]['pixel_count']
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))
diff = g.node[x]['mean color'] - g.node[y]['mean color']
g[x][y]['weight'] = np.linalg.norm(diff)
return g
+1 -1
View File
@@ -11,7 +11,7 @@ def threshold_cut(label, rag, thresh):
Parameters
----------
label : (width, height, 3) or (width, height, depth, 3) ndarray
label : (width, height) or (width, height, 3) ndarray
The array of labels.
rag : RAG
The region adjacency graph.
+2 -2
View File
@@ -1,7 +1,7 @@
import networkx as nx
from skimage import util
from ._construct import construct_rag_meancolor_2d
from ._construct import construct_rag_meancolor_3d
from ._build_rag import construct_rag_meancolor_2d
from ._build_rag import construct_rag_meancolor_3d
class RAG(nx.Graph):
+2 -2
View File
@@ -17,7 +17,7 @@ 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)
cython(['_build_rag.pyx'], working_path=base_path)
config.add_extension('_spath', sources=['_spath.c'],
@@ -26,7 +26,7 @@ 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'],
config.add_extension('_build_rag', sources=['_build_rag.c'],
include_dirs=[get_numpy_include_dirs()])