Fig. 7.18 -

 1 import ia870 as ia
 2 from iamxt.iamax_tree_alpha import MaxTreeAlpha
 3 import ia636
 4 import numpy as np
 5 
 6 f=adreadgray('MVBook/jangada.png')
 7 f=f[99:-50,:]
 8 fgrad = mmareaclose(ia.iagradm(f),10)
 9 
10 mxt = MaxTreeAlpha(ia.ianeg(fgrad))
11 H = mxt.computeHeight()
12 m1= mxt.getExtinctionValues(H,"height")
13 h = ia636.iahistogram(m1)
14 print 'h[:10]',h[:10]
15 adshow(ia636.iaplot(h[1:]))
16 h[0]=0
17 H = h.sum() - cumsum(h)
18 adshow(ia636.iaplot(H))
19 n = H.max()
20 print 'n:',n
21 print 'H[:10]',H[:10]
22 Hi = np.zeros(n+1)
23 for i in np.arange(len(H)):
24     #print 'i,H[i]:',i,H[i]
25     Hi[H[i]] = i
26 adshow(ia636.iaplot(Hi))
27 # interpolate
28 m = Hi[-1]
29 for i in np.arange(n-2,-1,-1):
30     if Hi[i]:
31         m = Hi[i]
32     else:
33         Hi[i] = m
34 adshow(ia636.iaplot(Hi))
35 
36 rm = mmregmin(fgrad)
37 print 'minregs', mmlabel(rm).max()
38 print '260 regiões', Hi[260]
39 
40 g_grad = mmhmin(fgrad,8) #Hi[260])
41 cb=mmwatershed(g_grad,mmsecross(),'regions')
42 print 'max reg', cb.max()
43 g=mmgrain(cb,f,'mean')
44 adshow(f,'original')
45 adshow(g_grad,'gradient')
46 adshow(g)
h[:10] [4176  446  242  123   73   54   33   30   36   22]
n: 1224
H[:10] [1224  778  536  413  340  286  253  223  187  165]
minregs 1738
260 regiões 5.0
max reg 260

original

gradient