Morph-M Python - Python Active Windows » History » Version 3
Jean Felder, 03/16/2010 06:28 PM
1 | 1 | Serge Koudoro | h1. Morph-M Python - Python Active Windows |
---|---|---|---|
2 | |||
3 | h2{color:#8B0000;background:#ddd}. Introduction |
||
4 | |||
5 | *What Active windows does ?* |
||
6 | |||
7 | Sometimes, you do not need to work on the whole image. Active window is here to help you ! Define 1,2,3 or more active window on your image, then get all these pixel and work with them. |
||
8 | |||
9 | 2 | Serge Koudoro | Example images are better than words, so try all example. |
10 | 1 | Serge Koudoro | |
11 | 2 | Serge Koudoro | h2{color:#8B0000;background:#ddd}. Example: Construct 3D volume with image stack |
12 | 1 | Serge Koudoro | |
13 | 2 | Serge Koudoro | h2{color:#8B0000;background:#ddd}. Example : Build 1 image with some part of 2 different images |
14 | |||
15 | 1 | Serge Koudoro | | !{width:400px}http://morphm.ensmp.fr/attachments/84/active_windows_1.PNG! |<pre><code class="ruby"> |
16 | import morphee as mp |
||
17 | |||
18 | def combineImage(ImIn1,ImIn2): |
||
19 | #we activate the same window on each image (Red windows) |
||
20 | ImIn1.setActiveWindow(201,14,1,30,30,1) |
||
21 | ImIn2.setActiveWindow(201,14,1,30,30,1) |
||
22 | |||
23 | #copy only active window |
||
24 | mp.ImCopy(ImIn1,ImIn2) |
||
25 | |||
26 | # remove active window so all image is actived |
||
27 | ImIn1.resetActiveWindow() |
||
28 | ImIn2.resetActiveWindow() |
||
29 | |||
30 | #Now, we activate the same window on each image(Blue windows) |
||
31 | ImIn1.setActiveWindow(45,73,0,50,40,1) |
||
32 | ImIn2.setActiveWindow(45,73,0,50,40,1) |
||
33 | |||
34 | #copy only active window |
||
35 | mp.ImCopy(ImIn1,ImIn2) |
||
36 | |||
37 | #remove active window so all image is actived |
||
38 | ImIn1.resetActiveWindow() |
||
39 | ImIn2.resetActiveWindow() |
||
40 | |||
41 | return ImIn2 |
||
42 | |||
43 | if __name__=='__main__': |
||
44 | #Read im1 and im2. |
||
45 | im1=mp.fileRead(images_dir+ "\\Gray\\tools.png") |
||
46 | im2=mp.fileRead(images_dir+ "\\Gray\\tw.png") |
||
47 | |||
48 | final_image = combineImage(im1,im2) |
||
49 | |||
50 | #write result |
||
51 | mp.fileWrite(final_image,"D:\\final_image.png") |
||
52 | </code></pre>| |
||
53 | |||
54 | 2 | Serge Koudoro | h2{color:#8B0000;background:#ddd}. Example : Mathematical Morphology operation on Active window |
55 | 1 | Serge Koudoro | |
56 | | !{width:400px}http://morphm.ensmp.fr/attachments/85/active_windows_2.PNG! |<pre><code class="ruby"> |
||
57 | import morphee as mp |
||
58 | |||
59 | def ErodeImagePart(im): |
||
60 | imEro = mp.getSame(im) |
||
61 | |||
62 | #create structuring element |
||
63 | nl = mp.NeighborList.neighborsSquare2D |
||
64 | |||
65 | #we activate the same window on each image (Red box) |
||
66 | im.setActiveWindow(1,1,1,66,66,1) |
||
67 | imEro.setActiveWindow(1,1,1,66,66,1) |
||
68 | |||
69 | #Erode active window |
||
70 | mp.ImErode( im, mp.HomotheticSE(nl,10), imEro) |
||
71 | |||
72 | # remove active window so all image is actived |
||
73 | im.resetActiveWindow() |
||
74 | imEro.resetActiveWindow() |
||
75 | |||
76 | return imEro |
||
77 | |||
78 | if __name__=='__main__': |
||
79 | #Read image |
||
80 | im=mp.fileRead(images_dir+ "\\Bin\\balls.png") |
||
81 | |||
82 | final_image = ErodeImagePart(im) |
||
83 | |||
84 | #write result |
||
85 | mp.fileWrite(final_image,"D:\\final_image.png") |
||
86 | </code></pre>| |
||
87 | |||
88 | 2 | Serge Koudoro | h2{color:#8B0000;background:#ddd}. Example : Set image Boundary with Active window |
89 | 1 | Serge Koudoro | |
90 | | !{width:400px}http://morphm.ensmp.fr/attachments/86/active_windows_3.png! |<pre><code class="ruby"> |
||
91 | import morphee as mp |
||
92 | |||
93 | def set_boundary(im, depth, value = 255): |
||
94 | |||
95 | for i in [(0,0,0),(im.wxSize-depth, 0, 0)]: |
||
96 | im.setActiveWindow(i[0], i[1], i[2], depth, |
||
97 | im.wySize,im.wzSize) |
||
98 | mp.ImSetConstant(im, value) |
||
99 | im.resetActiveWindow() |
||
100 | for i in [(0, 0, 0),(0, im.wySize-depth, 0)]: |
||
101 | im.setActiveWindow(i[0], i[1], i[2], im.wxSize, |
||
102 | depth,im.wzSize) |
||
103 | mp.ImSetConstant(im, value) |
||
104 | im.resetActiveWindow() |
||
105 | if im.getZSize() == 3: |
||
106 | for i in [(0, 0, 0),(0, 0, im3d.wzSize-depth)]: |
||
107 | im.setActiveWindow(i[0], i[1], i[2], im.wxSize, |
||
108 | im.wySize,depth) |
||
109 | mp.ImSetConstant(im, value) |
||
110 | im.resetActiveWindow() |
||
111 | return im |
||
112 | |||
113 | if __name__=='__main__': |
||
114 | #Read image |
||
115 | im=mp.fileRead(images_dir+ "\\Gray\\tw.png") |
||
116 | |||
117 | final_image = set_boundary(im, 50, 0): |
||
118 | |||
119 | #write result |
||
120 | mp.fileWrite(final_image,"D:\\final_image.png") |
||
121 | </code></pre>| |
||
122 | 3 | Jean Felder | |
123 | h2{color:#8B0000;background:#ddd}. Of active window, coordinates and offsets |
||
124 | |||
125 | We have already seen how to create and use an Active Window. |
||
126 | However, we can notice that an active Window defines a new system of coordinates. |
||
127 | That's why, it could be interesting to use offsets and Coordinates in this new system. |
||
128 | Two operations can be defined: |
||
129 | * coordinates to offset transformation |
||
130 | * offset to coordinates transformation |
||
131 | |||
132 | h3{color:black}. +coordinates to offset+ |
||
133 | |||
134 | Let's consider the following example: |
||
135 | |||
136 | <pre><code class="ruby"> |
||
137 | import morphee as mp |
||
138 | |||
139 | # I create a new image, size: 200x200x1 |
||
140 | imIn = mp.createImage(mp.dataCategory.dtScalar, mp.scalarDataType.sdtUINT8) |
||
141 | imIn.setSize(200, 200) |
||
142 | imIn.allocateImage() |
||
143 | |||
144 | # Let's get an offset from a pixel inside the image: |
||
145 | print "offset at (50, 23):", mp.GetOffsetFromCoords( imIn, (50, 23, 0) ) |
||
146 | |||
147 | # Let's define an active Window now ! |
||
148 | # It begins at (50, 20, 0), length : (10, 10, 1) |
||
149 | imIn.setActiveWindow(50, 20, 0, 10, 10, 1) |
||
150 | |||
151 | # Let's get an offset inside the active Window |
||
152 | print "Active Window, offset at (0, 3):", mp.GetOffsetFromWindowCoords( imIn, (0, 3, 0) ) |
||
153 | </code></pre> |
||
154 | |||
155 | If we try to launch the previous example, we'll get as expected : |
||
156 | <pre> |
||
157 | offset at (50, 23): 4650 |
||
158 | Active Window, offset at (0, 3): 4650 |
||
159 | </pre> |
||
160 | |||
161 | From the previous example, we can notice that an offset is a global measure ! It means that your offset doesn't change when an active window is set. |
||
162 | You just need to remember that your coordinates change with an active Window but not your offset |
||
163 | |||
164 | h3{color:black}. +offset to coordinates+ |
||
165 | |||
166 | We can now see the inverse function wich transforms an active window into coords. |
||
167 | The following code is quite simple: |
||
168 | |||
169 | <pre><code class="ruby"> |
||
170 | import morphee as mp |
||
171 | |||
172 | # I create a new image, size: 200x200x1 |
||
173 | imIn = mp.createImage(mp.dataCategory.dtScalar, mp.scalarDataType.sdtUINT8) |
||
174 | imIn.setSize(200, 200) |
||
175 | imIn.allocateImage() |
||
176 | |||
177 | # Let's get coords from an offset inside the image: |
||
178 | print "coords without active window:", mp.GetCoordsFromOffset( imIn, 4258 ) |
||
179 | |||
180 | # Let's define an active Window now ! |
||
181 | # It begins at (50, 20, 0), length : (10, 10, 1) |
||
182 | imIn.setActiveWindow(50, 20, 0, 10, 10, 1) |
||
183 | |||
184 | # Let's get coords inside the active Window |
||
185 | print "coords with active window:", mp.GetRelativeCoordsFromOffset( imIn, 4258 ) |
||
186 | </code></pre> |
||
187 | |||
188 | It will produce the following output: |
||
189 | |||
190 | <pre> |
||
191 | coords without active window: (58, 21, 0) |
||
192 | coords with active window: (8, 1, 0) |
||
193 | </pre> |
||
194 | |||
195 | Unlike the first operation (coordinates to offset transformation), the offset to coordinates operation will check if |
||
196 | your offset is inside the image. As far as active window are concerned, it means that if you have set an active window, |
||
197 | you can't get coords out of your active window. |
||
198 | For example, this code will return an error: |
||
199 | |||
200 | <pre><code class="ruby"> |
||
201 | import morphee as mp |
||
202 | |||
203 | # I create a new image, size: 200x200x1 |
||
204 | imIn = mp.createImage(mp.dataCategory.dtScalar, mp.scalarDataType.sdtUINT8) |
||
205 | imIn.setSize(200, 200) |
||
206 | imIn.allocateImage() |
||
207 | |||
208 | # Let's get coords from a pixel inside the image: |
||
209 | # 4258 = 21 * 200 + 58 -> (58, 21, 0) |
||
210 | print "coords without active window:", mp.GetCoordsFromOffset( imIn, 4258 ) |
||
211 | |||
212 | # Let's define an active Window now ! |
||
213 | # It begins at (50, 10, 0), length : (10, 10, 1) |
||
214 | imIn.setActiveWindow(50, 10, 0, 10, 10, 1) |
||
215 | |||
216 | # Let's get try to get coords outside of the active Window, |
||
217 | # it will return an error ! |
||
218 | print "coords with active window, error:", mp.GetRelativeCoordsFromOffset( imIn, 4258 ) |
||
219 | </code></pre> |
||
220 | |||
221 | The error: |
||
222 | |||
223 | <pre> |
||
224 | coords without active window: (58, 21, 0) |
||
225 | coords with active window, error: |
||
226 | Traceback (most recent call last): |
||
227 | File "./test.py", line 19, in <module> |
||
228 | print "coords with active window, error:", mp.GetRelativeCoordsFromOffset( imIn, 4258 ) |
||
229 | RuntimeError: t_GetRelativeCoordsFromOffset : result outside the window |
||
230 | |||
231 | -- Morphee backtrace: |
||
232 | </pre> |