mirror of
https://github.com/wassname/scikit-image.git
synced 2026-07-13 03:52:54 +08:00
Added docs
This commit is contained in:
@@ -3,7 +3,28 @@ cimport numpy as cnp
|
||||
import numpy as np
|
||||
|
||||
|
||||
def construct_rag_meancolor_3d( img, arr):
|
||||
def construct_rag_meancolor_3d(img, arr):
|
||||
"""Computes the Region Adjacency Graph of a 3D color image using
|
||||
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`
|
||||
|
||||
Parameters
|
||||
----------
|
||||
img : (width, height, depth, 3) ndarray
|
||||
Input image.
|
||||
arr : (width, height, depth) ndarray
|
||||
The array with labels.
|
||||
|
||||
Returns
|
||||
-------
|
||||
out : RAG
|
||||
The region adjacency graph.
|
||||
"""
|
||||
|
||||
cdef Py_ssize_t l, b, h, i, j, k
|
||||
cdef cnp.int32_t current, next
|
||||
l = arr.shape[0]
|
||||
@@ -19,15 +40,15 @@ def construct_rag_meancolor_3d( img, arr):
|
||||
k = 0
|
||||
while k < h - 1:
|
||||
current = arr[i, j, k]
|
||||
|
||||
try :
|
||||
|
||||
try:
|
||||
g.node[current]['pixel_count'] += 1
|
||||
g.node[current]['total_color'] += img[i,j]
|
||||
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]]
|
||||
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:
|
||||
@@ -57,18 +78,17 @@ def construct_rag_meancolor_3d( img, arr):
|
||||
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']
|
||||
g.node[n]['mean_color'] = g.node[n][
|
||||
'total_color'] / g.node[n]['pixel_count']
|
||||
|
||||
for x,y in g.edges_iter() :
|
||||
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))
|
||||
|
||||
@@ -76,6 +96,27 @@ def construct_rag_meancolor_3d( img, arr):
|
||||
|
||||
|
||||
def construct_rag_meancolor_2d(img, arr):
|
||||
"""Computes the Region Adjacency Graph of a 2D color image using
|
||||
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`
|
||||
|
||||
Parameters
|
||||
----------
|
||||
img : (width, height, 3) ndarray
|
||||
Input image.
|
||||
arr : (width, height) ndarray
|
||||
The array with labels.
|
||||
|
||||
Returns
|
||||
-------
|
||||
out : RAG
|
||||
The region adjacency graph.
|
||||
"""
|
||||
|
||||
cdef Py_ssize_t l, b, h, i, j, k
|
||||
cdef cnp.int32_t current, next
|
||||
l = arr.shape[0]
|
||||
@@ -89,14 +130,14 @@ def construct_rag_meancolor_2d(img, arr):
|
||||
while j < b - 1:
|
||||
current = arr[i, j]
|
||||
|
||||
try :
|
||||
try:
|
||||
g.node[current]['pixel_count'] += 1
|
||||
g.node[current]['total_color'] += img[i,j]
|
||||
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]]
|
||||
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:
|
||||
@@ -114,13 +155,12 @@ 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() :
|
||||
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
|
||||
|
||||
@@ -2,18 +2,34 @@ import networkx as nx
|
||||
import numpy as np
|
||||
|
||||
def threshold_cut(label, rag, thresh):
|
||||
"""Combines regions seperated by weight less than threshold.
|
||||
|
||||
#print [rag.edges_iter(data = True)]
|
||||
Given an image's labels and its RAG, outputs new labels by
|
||||
combining regions whose nodes are seperated by a weight less
|
||||
than the given threshold.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
label : (width, height, 3) or (width, height, depth, 3) ndarray
|
||||
The array of labels.
|
||||
rag : RAG
|
||||
The region adjacency graph.
|
||||
thresh : float
|
||||
The threshold, regions with edge weights less than this
|
||||
are combined.
|
||||
|
||||
Returns
|
||||
-------
|
||||
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]
|
||||
#print "edges to remove",len(to_remove)
|
||||
|
||||
rag.remove_edges_from(to_remove)
|
||||
|
||||
#print "to remove", to_remove
|
||||
|
||||
comps = nx.connected_components(rag)
|
||||
out = np.copy(label)
|
||||
#print "comps",len(comps)
|
||||
|
||||
for i, nodes in enumerate(comps) :
|
||||
|
||||
@@ -21,6 +37,5 @@ def threshold_cut(label, rag, thresh):
|
||||
for l in rag.node[node]['labels'] :
|
||||
out[label == l] = i
|
||||
|
||||
#print out
|
||||
#print label
|
||||
|
||||
return out
|
||||
|
||||
+45
-2
@@ -3,8 +3,30 @@ 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
|
||||
between their corresponding nodes.
|
||||
"""
|
||||
|
||||
def merge_nodes(self,i,j):
|
||||
"""Merges nodes `i` and `j`.
|
||||
|
||||
The new combined node is adjacent to all the neighbors of `i`
|
||||
and `j`. In case of conflicting edges, edge with higher weight
|
||||
is chosen.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
i : int
|
||||
Node to be merged.
|
||||
j : int
|
||||
Node to be merged.
|
||||
|
||||
"""
|
||||
|
||||
def merge_nodes(i,j):
|
||||
if not self.has_edge(i, j):
|
||||
raise ValueError('Cant merge non adjacent nodes')
|
||||
|
||||
@@ -24,7 +46,28 @@ class RAG(nx.Graph):
|
||||
self.node[j]['labels'] += self.node[i]['labels']
|
||||
self.remove_node(i)
|
||||
|
||||
def rag_meancolor(img,labels):
|
||||
|
||||
def rag_meancolor(img, labels):
|
||||
"""Computes the Region Adjacency Graph of a color image using
|
||||
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`
|
||||
|
||||
Parameters
|
||||
----------
|
||||
img : (width, height, 3) or (width, height, depth, 3) ndarray
|
||||
Input image.
|
||||
arr : (width, height) or (width, height, depth) ndarray
|
||||
The array with labels.
|
||||
|
||||
Returns
|
||||
-------
|
||||
out : RAG
|
||||
The region adjacency graph.
|
||||
"""
|
||||
|
||||
img = util.img_as_ubyte(img)
|
||||
if img.ndim == 4 :
|
||||
|
||||
Reference in New Issue
Block a user