Morph-M Python - Neighborhood Operator » History » Version 3
Serge Koudoro, 10/26/2009 02:46 PM
1 | 1 | Serge Koudoro | h1. Morph-M Python - Neighborhood Operator |
---|---|---|---|
2 | |||
3 | Example 1: Erosion. This generic function, create a list with all neighborhord values. These values are send to its operator and rthe return value will be put on center. |
||
4 | |||
5 | <pre><code class="ruby"> |
||
6 | def MyErode(imIn, nl, imOut): |
||
7 | # version lambda: |
||
8 | morphee.ImNeighborhoodUnaryOperation(imIn, nl, lambda l:min(l), imOut) |
||
9 | |||
10 | # version that directly use the python function 'min': |
||
11 | morphee.ImNeighborhoodUnaryOperation(imIn, nl, min, imOut) |
||
12 | |||
13 | if __name__=='__main__': |
||
14 | im8=morphee.fileRead(os.path.join(images_dir_gray,"foreman.png")) |
||
15 | imOut=morphee.getSame(im8) |
||
16 | imOut_2=morphee.getSame(im8) |
||
17 | nl=morphee.NeighborList.neighborsSquare2D |
||
18 | |||
19 | # version Python: |
||
20 | MyErode(im8,nl,imOut) |
||
21 | # version C++: |
||
22 | morphee.ImErode(im8,nl,imOut_2) |
||
23 | |||
24 | # vérification: |
||
25 | morphee.pngFileWrite(imOut,os.path.join(temp_dir,"ero1.png")) |
||
26 | morphee.pngFileWrite(imOut_2,os.path.join(temp_dir,"ero2.png")) |
||
27 | for p1,p2 in zip(imOut.imageData(), imOut_2.imageData()): |
||
28 | assert(p1==p2) |
||
29 | </code></pre> |
||
30 | |||
31 | 3 | Serge Koudoro | *Rank Filter* |
32 | 1 | Serge Koudoro | |
33 | <pre><code class="ruby"> |
||
34 | class RankOrder: |
||
35 | def __init__(self, quantile): |
||
36 | # on stocke la valeur du quantile: |
||
37 | self.quantile=quantile |
||
38 | |||
39 | def __call__(self, pyt): |
||
40 | l=list(pyt) |
||
41 | |||
42 | # l est la liste des valeurs des voisins |
||
43 | l.sort() |
||
44 | # conversion quantile/index: |
||
45 | index=int(len(l)*self.quantile) |
||
46 | if index >= len(l):# si on dépasse, on prend le dernier |
||
47 | index=-1# -1 est le dernier élément de la liste |
||
48 | return l[index] |
||
49 | |||
50 | def ImRankOrder(imIn, nl, quantile, imOut): |
||
51 | op=RankOrder(quantile) |
||
52 | |||
53 | morphee.ImNeighborhoodUnaryOperation(imIn,nl,op,imOut) |
||
54 | 2 | Serge Koudoro | |
55 | # min (erosion) |
||
56 | ImRankOrder(im8,nl,0,imOut) |
||
57 | # check results: |
||
58 | morphee.ImErode(im8,nl,imOut_2) |
||
59 | for p1,p2 in zip(imOut.imageData(), imOut_2.imageData()): |
||
60 | assert(p1==p2) |
||
61 | |||
62 | # max (dilatation) |
||
63 | ImRankOrder(im8,nl,1,imOut) |
||
64 | # check results: |
||
65 | morphee.ImDilate(im8,nl,imOut_2) |
||
66 | for p1,p2 in zip(imOut.imageData(), imOut_2.imageData()): |
||
67 | assert(p1==p2) |
||
68 | |||
69 | # quantile à 0.25: |
||
70 | ImRankOrder(im8,nl,0.25,imOut) |
||
71 | # check results: |
||
72 | for pOut,pIn in zip(imOut.imageData(), im8.imageData()): |
||
73 | try: |
||
74 | assert(pOut<=pIn) |
||
75 | except: |
||
76 | pass |
||
77 | #print "Opérateur non anti-extensif: ",pOut, pIn |
||
78 | |||
79 | 1 | Serge Koudoro | </code></pre> |
80 | |||
81 | Filtre de rang, avec [anti]extensivité garantie |
||
82 | |||
83 | <pre><code class="ruby"> |
||
84 | class RankOrderWithExtensivity: |
||
85 | def __init__(self, quantile): |
||
86 | self.quantile=quantile |
||
87 | if quantile > 0.5: |
||
88 | self.f=max |
||
89 | elif quantile < 0.5: |
||
90 | self.f=min |
||
91 | else: |
||
92 | self.f=None |
||
93 | |||
94 | def __call__(self, pyt,center): |
||
95 | l=list(pyt) |
||
96 | l.sort() |
||
97 | index=int(len(l)*self.quantile) |
||
98 | if index == len(l): |
||
99 | index=-1 |
||
100 | |||
101 | if self.f: |
||
102 | # selon que l'on a choisi un quantile |
||
103 | # dans la moitié supérieure ou inférieure, |
||
104 | # on prend le max (resp. min) avec le centre, |
||
105 | # pour garantir imOut>=imIn (resp <=) |
||
106 | return self.f(center, l[index]) |
||
107 | else: |
||
108 | return l[index] |
||
109 | |||
110 | def ImRankOrderWithExtensivity(imIn, nl, quantile, imOut): |
||
111 | op=RankOrderWithExtensivity(quantile) |
||
112 | |||
113 | morphee.ImNeighborhoodUnaryOperationWithCenter(imIn,nl,op,imOut) |
||
114 | |||
115 | # quantile à 0.25 sauf si on est pas en-dessous |
||
116 | ImRankOrderWithExtensivity(im8,nl,0.25,imOut) |
||
117 | 2 | Serge Koudoro | # Check results |
118 | 1 | Serge Koudoro | for pOut,pIn in zip(imOut.imageData(), im8.imageData()): |
119 | try: |
||
120 | assert(pOut<=pIn) |
||
121 | except: |
||
122 | print "Opérateur complexe non anti-extensif: ",pOut, pIn |
||
123 | </code></pre> |
||
124 | |||
125 | 2 | Serge Koudoro | Minkowski Addition (dual dilatation) |
126 | 1 | Serge Koudoro | |
127 | <pre><code class="ruby"> |
||
128 | def MinkowskiAdditionOperator(pyt,center): |
||
129 | while pyt.isNotFinished(): |
||
130 | if pyt.getPixel() < center: |
||
131 | pyt.setPixel(center) |
||
132 | pyt.next() |
||
133 | |||
134 | def MyMinkowskiAddition(imIn, nl, imOut): |
||
135 | morphee.ImUnaryOperationOnNeighborhoodWithCenter(imIn,nl,MinkowskiAdditionOperator,imOut) |
||
136 | |||
137 | # Addition de minkowski (dilatation duale) |
||
138 | # version Python: |
||
139 | MyMinkowskiAddition(im8,nl,imOut) |
||
140 | # version C++: |
||
141 | morphee.ImMinkowskiAddition(im8,nl,imOut_2) |
||
142 | morphee.pngFileWrite(imOut,os.path.join(temp_dir,"dil1.png")) |
||
143 | morphee.pngFileWrite(imOut_2,os.path.join(temp_dir,"dil2.png")) |
||
144 | for p1,p2 in zip(imOut.imageData(), imOut_2.imageData()): |
||
145 | assert(p1==p2) |
||
146 | </code></pre> |