|
1 .. Objectives |
|
2 .. ---------- |
|
3 |
|
4 .. Clearly state the objectives of the LO (along with RBT level) |
|
5 |
|
6 .. Prerequisites |
|
7 .. ------------- |
|
8 |
|
9 .. 1. Name of LO-1 |
|
10 .. 2. Name of LO-2 |
|
11 .. 3. Name of LO-3 |
|
12 |
|
13 .. Author : Puneeth |
|
14 Internal Reviewer : |
|
15 External Reviewer : |
|
16 Checklist OK? : <put date stamp here, if OK> [2010-10-05] |
|
17 |
|
18 Script |
|
19 ------ |
|
20 |
|
21 |
|
22 {{{ Screen shows welcome slide }}} |
|
23 |
|
24 Welcome to the tutorial on accessing pieces of arrays |
|
25 |
|
26 {{{ Show the outline for this tutorial }}} |
|
27 |
|
28 In this tutorial we shall learn to access individual elements of |
|
29 arrays, get rows and columns and other chunks of arrays using |
|
30 slicing and striding. |
|
31 |
|
32 {{{ switch back to the terminal }}} |
|
33 |
|
34 As usual, we start IPython, using |
|
35 :: |
|
36 |
|
37 ipython -pylab |
|
38 |
|
39 Let us have two arrays, A and C, as the sample arrays that we will |
|
40 use to work through this tutorial. |
|
41 |
|
42 :: |
|
43 |
|
44 A = array([12, 23, 34, 45, 56]) |
|
45 |
|
46 C = array([[11, 12, 13, 14, 15], |
|
47 [21, 22, 23, 24, 25], |
|
48 [31, 32, 33, 34, 35], |
|
49 [41, 42, 43, 44, 45], |
|
50 [51, 52, 53, 54, 55]]) |
|
51 |
|
52 Pause the video here and make sure you have the arrays A and C, |
|
53 typed in correctly. |
|
54 |
|
55 Let us begin with the most elementary thing, accessing individual |
|
56 elements. Also, let us first do it with the one-dimensional array |
|
57 A, and then do the same thing with the two-dimensional array. |
|
58 |
|
59 To access, the element 34 in A, we say, |
|
60 |
|
61 :: |
|
62 |
|
63 A[1] |
|
64 |
|
65 Like lists, indexing starts from 0 in arrays, too. So, 34, the |
|
66 third element has the index 2. |
|
67 |
|
68 Now, let us access the element 34 from C. To do this, we say |
|
69 :: |
|
70 |
|
71 C[2, 3] |
|
72 |
|
73 34 is in the third row and the fourth column, and since indexing |
|
74 begins from zero, the row index is 2 and column index is 3. |
|
75 |
|
76 Now, that we have accessed one element of the array, let us change |
|
77 it. We shall change the 34 to -34 in both A and C. To do this, we |
|
78 simply assign the new value after accessing the element. |
|
79 :: |
|
80 |
|
81 A[2] = -34 |
|
82 C[2, 3] = -34 |
|
83 |
|
84 Now that we have accessed and changed a single element, let us |
|
85 access and change more than one element at a time; first rows and |
|
86 then columns. |
|
87 |
|
88 Let us access one row of C, say the third row. We do it by saying, |
|
89 :: |
|
90 |
|
91 C[2] |
|
92 |
|
93 How do we access the last row of C? We could say, |
|
94 :: |
|
95 |
|
96 C[4] |
|
97 |
|
98 for the fifth row, or as with lists, use negative indexing and say |
|
99 :: |
|
100 |
|
101 C[-1] |
|
102 |
|
103 Now, we could change the last row into all zeros, using either |
|
104 :: |
|
105 |
|
106 C[-1] = [0, 0, 0, 0, 0] |
|
107 |
|
108 or |
|
109 |
|
110 :: |
|
111 |
|
112 C[-1] = 0 |
|
113 |
|
114 Now, how do we access one column of C? As with accessing |
|
115 individual elements, the column is the second parameter to be |
|
116 specified (after the comma). The first parameter, is now replaced |
|
117 with a ``:`` to say, that we want all the elements of that |
|
118 dimension, instead of one particular element. We access the third |
|
119 column by |
|
120 |
|
121 :: |
|
122 |
|
123 C[:, 2] |
|
124 |
|
125 Following is an exercise that you must do. |
|
126 |
|
127 %%1%% Change the last column of C to zeroes. |
|
128 |
|
129 Please, pause the video here. Do the exercises and then continue. |
|
130 |
|
131 :: |
|
132 |
|
133 C[:, -1] = 0 |
|
134 |
|
135 Since A is one dimensional, rows and columns of A don't make much |
|
136 sense. It has just one row and |
|
137 :: |
|
138 |
|
139 A[:] |
|
140 |
|
141 gives the whole of A. |
|
142 |
|
143 Following is an exercise that you must do. |
|
144 |
|
145 %%2%% Change ``A`` to ``[11, 12, 13, 14, 15]``. |
|
146 |
|
147 Please, pause the video here. Do the exercises and then continue. |
|
148 |
|
149 To change A, we say |
|
150 :: |
|
151 |
|
152 A[:] = [11, 12, 13, 14, 15] |
|
153 |
|
154 Now, that we know how to access, rows and columns of an array, we |
|
155 shall learn how to access other pieces of an array. For this |
|
156 purpose, we will be using image arrays. |
|
157 |
|
158 To read an image into an array, we use the ``imread`` command. We |
|
159 shall use the image ``squares.png`` present in ``/home/fossee``. We |
|
160 shall first navigate to that path in the OS and see what the image |
|
161 contains. |
|
162 |
|
163 {{{ switch to the browser and show the image }}} |
|
164 |
|
165 {{{ switch back to the ipython terminal }}} |
|
166 |
|
167 Let us now read the data in ``squares.png`` into the array ``I``. |
|
168 :: |
|
169 |
|
170 I = imread('/home/fossee/squares.png') |
|
171 |
|
172 We can see the contents of the image, using the command |
|
173 ``imshow``. We say, |
|
174 :: |
|
175 |
|
176 imshow(I) |
|
177 |
|
178 to see what has been read into ``I``. We do not see white and black |
|
179 because, ``pylab`` has mapped white and black to different |
|
180 colors. This can be changed by using a different colormap. |
|
181 |
|
182 To see that ``I`` is really, just an array, we say, |
|
183 :: |
|
184 |
|
185 I |
|
186 |
|
187 at the prompt, and see that an array is displayed. |
|
188 |
|
189 To check the dimensions of any array, we can use the method |
|
190 shape. We say |
|
191 :: |
|
192 |
|
193 I.shape |
|
194 |
|
195 to get the dimensions of the image. As we can see, ``squares.png`` |
|
196 has the dimensions of 300x300. |
|
197 |
|
198 Our goal for this part of the tutorial would be to get the |
|
199 top-left quadrant of the image. To do this, we need to access, a |
|
200 few of the rows and a few of the columns of the array. |
|
201 |
|
202 To access, the third column of C, we said, ``C[:, 2]``. Essentially, |
|
203 we are accessing all the rows in column three of C. Now, let us |
|
204 modify this to access only the first three rows, of column three |
|
205 of C. |
|
206 |
|
207 We say, |
|
208 :: |
|
209 |
|
210 C[0:3, 2] |
|
211 |
|
212 to get the elements of rows indexed from 0 to 3, 3 not included |
|
213 and column indexed 2. Note that, the index before the colon is |
|
214 included and the index after it is not included, in the slice that |
|
215 we have obtained. This is very similar to the ``range`` function, |
|
216 where ``range`` returns a list, in which the upper limit or stop |
|
217 value is not included. |
|
218 |
|
219 Now, if we wish to access the elements of row with index 2, and in |
|
220 columns indexed 0 to 2 (included), we say, |
|
221 :: |
|
222 |
|
223 C[2, 0:3] |
|
224 |
|
225 Following is an exercise that you must do. |
|
226 |
|
227 %%3%% First, obtain the elements [22, 23] from C. Then, obtain the |
|
228 elements [11, 21, 31, 41] from C. Finally, obtain the elements [21, |
|
229 31, 41, 0]. |
|
230 |
|
231 Please, pause the video here. Do the exercises and then continue. |
|
232 |
|
233 :: |
|
234 |
|
235 C[1, 1:3] |
|
236 |
|
237 gives the elements [22, 23] |
|
238 :: |
|
239 |
|
240 C[0:4, 0] |
|
241 |
|
242 gives the elements [11, 21, 31, 41] |
|
243 :: |
|
244 |
|
245 C[1:5, 0] |
|
246 |
|
247 gives the elements [21, 31, 41, 0] |
|
248 |
|
249 Note that when specifying ranges, if you are starting from or |
|
250 going up-to the end, the corresponding element may be dropped. So, |
|
251 in the previous example to obtain [11, 21, 31, 41], we could have |
|
252 simply said, |
|
253 :: |
|
254 |
|
255 C[:4, 0] |
|
256 |
|
257 and |
|
258 :: |
|
259 |
|
260 C[1:, 0] |
|
261 |
|
262 gives the elements [21, 31, 41, 0]. If we skip both the indexes, |
|
263 we get the slice from end to end, as we already know. |
|
264 |
|
265 Following is an exercise that you must do. |
|
266 |
|
267 %%4%% Obtain the elements [[23, 24], [33, -34]] from C. |
|
268 |
|
269 Please, pause the video here. Do the exercises and then continue. |
|
270 |
|
271 :: |
|
272 |
|
273 C[1:3, 2:4] |
|
274 |
|
275 gives us the elements, [[23, 24], [33, -34]]. |
|
276 |
|
277 Now, we wish to obtain the top left quarter of the image. How do |
|
278 we go about doing it? Since, we know the shape of the image to be |
|
279 300, we know that we need to get the first 150 rows and first 150 |
|
280 columns. |
|
281 :: |
|
282 |
|
283 I[:150, :150] |
|
284 |
|
285 gives us the top-left corner of the image. |
|
286 |
|
287 We use the ``imshow`` command to see the slice we obtained in the |
|
288 form of an image and confirm. |
|
289 :: |
|
290 |
|
291 imshow(I[:150, :150]) |
|
292 |
|
293 Following is an exercise that you must do. |
|
294 |
|
295 %%5%% Pause the video here, and obtain the square in the center |
|
296 of the image. |
|
297 |
|
298 Following is an exercise that you must do. |
|
299 |
|
300 :: |
|
301 |
|
302 imshow(I[75:225, 75:225]) |
|
303 |
|
304 Our next goal is to compress the image, using a very simple |
|
305 technique to reduce the space that the image takes on disk while |
|
306 not compromising too heavily on the image quality. The idea is to |
|
307 drop alternate rows and columns of the image and save it. This way |
|
308 we will be reducing the data to a fourth of the original data but |
|
309 losing only so much of visual information. |
|
310 |
|
311 We shall first learn the idea of striding using the smaller array |
|
312 C. Suppose we wish to access only the odd rows and columns (first, |
|
313 third, fifth). We do this by, |
|
314 :: |
|
315 |
|
316 C[0:5:2, 0:5:2] |
|
317 |
|
318 if we wish to be explicit, or simply, |
|
319 :: |
|
320 |
|
321 C[::2, ::2] |
|
322 |
|
323 This is very similar to the step specified to the ``range`` |
|
324 function. It specifies, the jump or step in which to move, while |
|
325 accessing the elements. If no step is specified, a default value |
|
326 of 1 is assumed. |
|
327 :: |
|
328 |
|
329 C[1::2, ::2] |
|
330 |
|
331 gives the elements, [[21, 23, 0], [41, 43, 0]] |
|
332 |
|
333 Following is an exercise that you must do. |
|
334 |
|
335 %%6%% Obtain the following. |
|
336 [[12, 0], [42, 0]] |
|
337 [[12, 13, 14], [0, 0, 0]] |
|
338 |
|
339 Please, pause the video here. Do the exercises and then continue. |
|
340 |
|
341 :: |
|
342 |
|
343 C[::3, 1::3] |
|
344 |
|
345 gives the elements [[12, 0], [42, 0]] |
|
346 :: |
|
347 |
|
348 C[::4, 1:4] |
|
349 |
|
350 gives the elements [[12, 13, 14], [0, 0, 0]] |
|
351 |
|
352 Now, that we know how to stride over an image, we can drop |
|
353 alternate rows and columns out of the image in I. |
|
354 :: |
|
355 |
|
356 I[::2, ::2] |
|
357 |
|
358 To see this image, we say, |
|
359 :: |
|
360 |
|
361 imshow(I[::2, ::2]) |
|
362 |
|
363 This does not have much data to notice any real difference, but |
|
364 notice that the scale has reduced to show that we have dropped |
|
365 alternate rows and columns. If you notice carefully, you will be |
|
366 able to observe some blurring near the edges. To notice this |
|
367 effect more clearly, increase the step to 4. |
|
368 :: |
|
369 |
|
370 imshow(I[::4, ::4]) |
|
371 |
|
372 {{{ show summary slide }}} |
|
373 |
|
374 That brings us to the end of this tutorial. In this tutorial, we |
|
375 have learnt to access parts of arrays, specifically individual |
|
376 elements, rows and columns and larger pieces of arrays. We have |
|
377 also learnt how to modify arrays, element wise or in larger |
|
378 pieces. |
|
379 |
|
380 Thank You! |