mirror of
https://github.com/wassname/scikit-image.git
synced 2026-07-02 19:05:57 +08:00
cleaned up _ncut.py
This commit is contained in:
+14
-22
@@ -2,37 +2,29 @@ import networkx as nx
|
||||
import numpy as np
|
||||
from scipy import sparse
|
||||
|
||||
|
||||
def DW_matrix(graph):
|
||||
#print graph[4][4]['weight']
|
||||
|
||||
W = nx.to_scipy_sparse_matrix(graph, format='csc')
|
||||
entries = W.sum(0)
|
||||
D = sparse.dia_matrix( (entries,0),shape = W.shape).tocsc()
|
||||
#print W[4,4]
|
||||
|
||||
m,n = W.shape
|
||||
#for i in range(n):
|
||||
# W[i,i] = 1.0
|
||||
return D,W
|
||||
D = sparse.dia_matrix((entries, 0), shape=W.shape).tocsc()
|
||||
return D, W
|
||||
|
||||
def ncut_cost(mask,D,W):
|
||||
|
||||
def ncut_cost(mask, D, W):
|
||||
|
||||
mask = np.array(mask)
|
||||
mask_list = [ np.logical_xor(mask[i], mask) for i in range(mask.shape[0])]
|
||||
mask_list = [np.logical_xor(mask[i], mask) for i in range(mask.shape[0])]
|
||||
mask_array = np.array(mask_list)
|
||||
|
||||
cut = float(W[mask_array].sum()/2.0)
|
||||
#print W.todense()
|
||||
#print mask_array.astype(int)
|
||||
#print "cut=",cut
|
||||
|
||||
|
||||
cut = float(W[mask_array].sum() / 2.0)
|
||||
assoc_a = D.data[mask].sum()
|
||||
assoc_b = D.data[np.logical_not(mask)].sum()
|
||||
|
||||
#print cut
|
||||
#print assoc_a,assoc_b
|
||||
return (cut/assoc_a) + (cut/assoc_b)
|
||||
|
||||
def norml(a):
|
||||
return (cut / assoc_a) + (cut / assoc_b)
|
||||
|
||||
|
||||
def normalize(a):
|
||||
mi = a.min()
|
||||
mx = a.max()
|
||||
return (a-mi)/(mx-mi)
|
||||
return (a - mi) / (mx - mi)
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
cimport numpy as cnp
|
||||
import numpy as np
|
||||
|
||||
|
||||
def argmin2(cnp.float64_t[:] array):
|
||||
cdef cnp.float64_t min1 = np.inf
|
||||
cdef cnp.float64_t min2 = np.inf
|
||||
@@ -14,14 +15,14 @@ def argmin2(cnp.float64_t[:] array):
|
||||
|
||||
while i < array.shape[0]:
|
||||
x = array[i]
|
||||
if x < min1 :
|
||||
if x < min1:
|
||||
min2 = min1
|
||||
i2 = i1
|
||||
min1 = x
|
||||
i1 = i
|
||||
elif x > min1 and x < min2 :
|
||||
elif x > min1 and x < min2:
|
||||
min2 = x
|
||||
i2 = i
|
||||
i += 1
|
||||
|
||||
|
||||
return i2
|
||||
|
||||
Reference in New Issue
Block a user