extrema2attribute

MainPage

Description: This method computes the attribute parameter to be used in the attribute filter that tries to preserve n extrema in the image. If it is not possible to preserve n extrema, it will give preserve the closest value from n from below.

Signature: attr_value,new_n = extrema2attribute(n, ext)

Input:
  • n: int, number of extrema to be preserved.
  • ext: 1d-array np.int32, extinction values array.
Output:
  • attr_value: int, the attribute value.
  • new_n: int, the number of extrema that will be preserved if the max-tree is filtered with attr_value.

Python Implementation

 1 import numpy as np
 2 
 3 def extrema2attribute(n, ext):
 4 
 5     ext = ext[ext!=0] # Non-zero extinction values
 6     temp = np.unique(ext) # Returns non-repeated elements sorted
 7     max_ext = temp[-1]
 8     bins = np.zeros(len(temp)+2, dtype = np.int32)
 9     bins[1:-1] = temp
10     bins[-1] = max_ext + 1
11     hist, bins = np.histogram(ext.flatten(),bins = bins)
12     x = bins[:-1]
13     y = (hist.sum() - np.cumsum(hist))
14     index = np.where(y >= n)[0][-1]
15     attr_value =  x[index]
16     new_n = y[index]
17     return attr_value,new_n

Example:

 1 from iamxt.max_tree_alpha import MaxTreeAlpha
 2 from iamxt.extrema_attribute import extrema2attribute
 3 import numpy as np
 4 
 5 img = adreadgray('Attachments/iamxt/MainPage/cameraman.tif')
 6 Bc = np.ones((3,3),dtype = bool)
 7 adshow(img,"Original image")
 8 
 9 mxt = MaxTreeAlpha(img,Bc)
10 mxt2 = mxt.clone()
11 mxt3 = mxt.clone()
12 
13 print "Original max-tree"
14 print "Nb. of nodes:", mxt.node_array.shape[1]
15 print "Nb. of leaves:", (mxt.node_array[1,:] == 0).sum()
16 
17 
18 n = 20
19 
20 H = mxt.computeHeight()
21 Hext = mxt.computeExtinctionValues(H,"height")
22 
23 attr1,new_n1 = extrema2attribute(n, Hext)
24 
25 print
26 print "hmax"
27 print "Attribute:", attr1
28 print "New n:", new_n1
29 
30 mxt.hmax(attr1)
31 print "Nb. of nodes:", mxt.node_array.shape[1]
32 print "Nb. of leaves:", (mxt.node_array[1,:] == 0).sum()
33 
34 adshow(mxt.getImage(),"hmax = %d" %attr1)
35 
36 V = mxt2.computeVolume()
37 Vext = mxt2.computeExtinctionValues(V,"volume")
38 attr2,new_n2 = extrema2attribute(n, Vext)
39 print "vmax"
40 print "Attribute:", attr2
41 print "New n:", new_n2
42 
43 mxt2.vmax(attr2)
44 print "Nb. of nodes:", mxt2.node_array.shape[1]
45 print "Nb. of leaves:", (mxt2.node_array[1,:] == 0).sum()
46 print
47 adshow(mxt2.getImage(),"vmax = %d" %attr2)
48 
49 A = mxt3.node_array[3,:]
50 Aext = mxt3.computeExtinctionValues(A,"area")
51 attr3,new_n3 = extrema2attribute(n, Aext)
52 print "area-open:"
53 print "Attribute:", attr3
54 print "New n:", new_n3
55 
56 mxt3.areaOpen(attr3)
57 print "Nb. of nodes:", mxt3.node_array.shape[1]
58 print "Nb. of leaves:", (mxt3.node_array[1,:] == 0).sum()
59 
60 adshow(mxt3.getImage(),"area-open = %d" %attr3)
Original max-tree
Nb. of nodes: 11123
Nb. of leaves: 4241

hmax
Attribute: 126
New n: 20
Nb. of nodes: 439
Nb. of leaves: 20
vmax
Attribute: 10169
New n: 20
Nb. of nodes: 788
Nb. of leaves: 20

area-open:
Attribute: 310
New n: 20
Nb. of nodes: 804
Nb. of leaves: 20

Original image

hmax = 126

vmax = 10169

area-open = 310

[–] Comments
Roberto Souza at 2016-05-18 13:44h
 0
Olá, Para esse código funcionar no seu computador é preciso fazer algumas adaptações (substituir as funções adshow e adread) e instalar alguns pacotes. O mais importante deles é o iamxt (https://github.com/rmsouza01/iamxt). Att, Roberto

Rubén Macias at 2016-05-18 11:08h
 0
hola!!! estoy intentando ejecutar este ejemplo desde mi computadora, solo que me surge este error, me podrias ayudar a solucionarlo. tengo el codigo en el editor de texto y desde la terminal con el comando "$ python extrema_attri.py" lo intento ejecutar pero me sale ese error ruben-HP-ENVY proyectos-python # python extrema_attri.py Traceback (most recent call last): File "extrema_attri.py", line 2, in <module> from iamxt.extrema_attribute import extrema2attribute ImportError: No module named extrema_attribute

Roberto Souza at 2016-05-17 15:57h
 0
Olá, Vc está tentando rodar no adessowiki ou no seu computador local? Atenciosamente, Roberto

Rubén Macias at 2016-05-17 13:54h
 0
hola buena tarde!!! quisiera saber como compilar este codigo pasa a paso!!! tengo un error de "ImportError: No module named extrema_attribute" no se porque.... Asi me pasa con la mayoria de los codigos. Saludos Cordiales