Docs formatting

This commit is contained in:
Vighnesh Birodkar
2014-06-19 02:50:24 +05:30
parent 10f91fe8b0
commit 850f835114
2 changed files with 19 additions and 17 deletions
+9 -9
View File
@@ -1,11 +1,12 @@
import networkx as nx
import numpy as np
def threshold_cut(label, rag, thresh):
"""Combines regions seperated by weight less than threshold.
Given an image's labels and its RAG, outputs new labels by
combining regions whose nodes are seperated by a weight less
combining regions whose nodes are seperated by a weight less
than the given threshold.
Parameters
@@ -14,7 +15,7 @@ def threshold_cut(label, rag, thresh):
The array of labels.
rag : RAG
The region adjacency graph.
thresh : float
thresh : float
The threshold, regions with edge weights less than this
are combined.
@@ -23,19 +24,18 @@ def threshold_cut(label, rag, thresh):
out : (width, height, 3) or (width, height, depth, 3) ndarray
The new labelled array.
"""
to_remove = [(x,y) for x,y,d in rag.edges_iter(data = True) if d['weight'] >= thresh]
to_remove = [(x, y)
for x, y, d in rag.edges_iter(data=True) if d['weight'] >= thresh]
rag.remove_edges_from(to_remove)
comps = nx.connected_components(rag)
out = np.copy(label)
for i, nodes in enumerate(comps) :
for node in nodes :
for l in rag.node[node]['labels'] :
for i, nodes in enumerate(comps):
for node in nodes:
for l in rag.node[node]['labels']:
out[label == l] = i
return out
+10 -8
View File
@@ -2,16 +2,18 @@ import networkx as nx
import _construct
from skimage import util
class RAG(nx.Graph):
"""
The class for holding the Region Adjacency Graph (RAG).
Each region is a contiguous set of pixels in an image, usuall
sharing some common property.Adjacent regions have an edge
sharing some common property.Adjacent regions have an edge
between their corresponding nodes.
"""
def merge_nodes(self,i,j):
def merge_nodes(self, i, j):
"""Merges nodes `i` and `j`.
The new combined node is adjacent to all the neighbors of `i`
@@ -43,7 +45,7 @@ class RAG(nx.Graph):
self.add_edge(x, j, weight=w)
self.node[j]['labels'] += self.node[i]['labels']
self.node[j]['labels'] += self.node[i]['labels']
self.remove_node(i)
@@ -70,9 +72,9 @@ def rag_meancolor(img, labels):
"""
img = util.img_as_ubyte(img)
if img.ndim == 4 :
return _construct.construct_rag_meancolor_3d(img,labels)
elif img.ndim == 3 :
return _construct.construct_rag_meancolor_2d(img,labels)
else :
if img.ndim == 4:
return _construct.construct_rag_meancolor_3d(img, labels)
elif img.ndim == 3:
return _construct.construct_rag_meancolor_2d(img, labels)
else:
raise ValueError("Image dimension not supported")