Drafts 02

 1 import iamxt
 2 import numpy as np
 3 
 4 def eccentricity(mxt):
 5     n = mxt.node_array.shape[-1]
 6     m11 = np.zeros(n)
 7     m20 = np.zeros(n)
 8     m02 = np.zeros(n)
 9     ecc = np.zeros((n,), np.float)
10     for i in xrange(n):
11         cc = mxt.recConnectedComponent(i)
12         xind,yind = np.nonzero(cc)
13         area = mxt.node_array[3, i]
14         sumx, sumy = mxt.node_array[5,i],mxt.node_array[8,i]
15         centroid = np.array([1.0*sumx/area, 1.0*sumy/area])
16         m11[i] = ((1.*xind-centroid[0])*(yind-centroid[1])).sum()/area
17         m20[i] = ((1.*xind-centroid[0])**2).sum()/area
18         m02[i] = ((1.*yind-centroid[1])**2).sum()/area
19         ecc[i] = np.sqrt((m20[i]-m02[i])**2+4*m11[i]**2)/(m20[i]+m02[i])
20     return ecc
21 
22 
23 img = adreadgray('MVBook/cards.png')
24 img = adreadgray(find_attachment_file('iamxt/morph_tree_generate_cc_graph/synthetic01.png'))
25 Bc = np.ones((3,3), dtype = bool)
26 
27 
28 mxt = iamxt.MaxTreeAlpha(img,Bc)
29 ecc1 =  eccentricity(mxt)
30 L1,L2,ecc2 = mxt.computeEccentricity()
31 
32 print ecc1
33 print
34 print ecc2
[ 0.38660037  0.50648398  0.00415806  0.          0.04078182  0.11335942
  0.06001602  0.03639065  0.04866548]

[ 0.44237665  0.3275946   0.99171832  1.          0.92163233  0.79636509
  0.88676393  0.92977426  0.90718589]