diff --git a/doc/examples/plot_rag_meancolor.py b/doc/examples/plot_rag_meancolor.py index 988572b5..aebfe6fc 100644 --- a/doc/examples/plot_rag_meancolor.py +++ b/doc/examples/plot_rag_meancolor.py @@ -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. """ diff --git a/skimage/graph/_construct.pyx b/skimage/graph/_build_rag.pyx similarity index 64% rename from skimage/graph/_construct.pyx rename to skimage/graph/_build_rag.pyx index c3cde3b9..e6825c9d 100644 --- a/skimage/graph/_construct.pyx +++ b/skimage/graph/_build_rag.pyx @@ -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 diff --git a/skimage/graph/graph_cut.py b/skimage/graph/graph_cut.py index 0e7ea25c..40adcdf8 100644 --- a/skimage/graph/graph_cut.py +++ b/skimage/graph/graph_cut.py @@ -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. diff --git a/skimage/graph/rag.py b/skimage/graph/rag.py index 1129d4a9..72befba9 100644 --- a/skimage/graph/rag.py +++ b/skimage/graph/rag.py @@ -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): diff --git a/skimage/graph/setup.py b/skimage/graph/setup.py index 252eeed2..436029da 100644 --- a/skimage/graph/setup.py +++ b/skimage/graph/setup.py @@ -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()])