98 similarly |
98 similarly |
99 c[::2,:] returns 2x3 array with first and third row |
99 c[::2,:] returns 2x3 array with first and third row |
100 |
100 |
101 and c[::2, ::2] will give us 2x2 array with first and third row and column |
101 and c[::2, ::2] will give us 2x2 array with first and third row and column |
102 |
102 |
103 With |
103 Lets us try to use these concepts of slicing and striding for doing some basic image manipulation |
|
104 |
|
105 pylab has a function imread to read images. We will use '(in)famous' lena image for our experimentation. Its there on desktop. |
|
106 |
|
107 a = imread('lena.png') |
|
108 a is a numpy array with the 'RGB' values of each pixel |
|
109 a.shape |
|
110 |
|
111 its a 512x512x3 array. |
|
112 |
|
113 to view the image write |
|
114 imshow(a) |
|
115 |
|
116 lets try to crop the image to top left quarter. Since a is a normal array we can use slicing to get top left quarter by |
|
117 imshow(a[:255,:255]) (half of 512 is 256) |
|
118 |
|
119 But hat is not 'interesting' part of lena. Lets crop the image so that only her face is visible. for that we will need some rough estimates of pixels. |
|
120 imshow(a) |
|
121 now move your mouse cursor over the image, it will give us x, y coordinates where ever we take our cursor. We can get rough estimate of lena's face now cropping to those boundaries is simple |
|
122 imshow(a[200:400, 200:400]) |
|
123 |
|
124 Next we will try striding on this image. We will resize the image by skipping each alternate pixel. We have already seen how to skip alternate elements so, |
|
125 imshow(a[::2, ::2]) |
|
126 note now the size of image is just 256x256 and still quality of image is not much compromised. |
|
127 ------------------------- |
|
128 |
|
129 Till now we have covered initializing and accessing elements of arrays. Now we shall concentrate on functions available for arrays. We start this by creating 4x4 array by |
|
130 |
|
131 a = array([[ 1, 1, 2, -1],[ 2, 5, -1, -9], [ 2, 1, -1, 3], [ 1, -3, 2, 7]]) |
|
132 a |
|
133 |
|
134 To get transpose of this matrix write |
|
135 a.T |
|
136 sum() function returns sum of all the elements of a matrix. |
|
137 sum(a) |
|
138 |
|
139 lets create one more array for checking more operations |
|
140 b = array([[3,2,-1,5], [2,-2,4,9], [-1,0.5,-1,-7], [9,-5,7,3]]) |
|
141 |
|
142 + will take care of matrix additions |
|
143 a + b |
|
144 |
|
145 lets try multiplication now, |
|
146 a * b will return element wise product of two matrices. |
|
147 |
|
148 To get matrix product of a and b we use |
|
149 dot(a, b) |
|
150 |
|
151 and to get inverse of matrix |
|
152 |
|
153 inv(a) |
|
154 |
|
155 det(a) returns determinant of matrix a |
|
156 |
|
157 we shall create one array e |
|
158 e = array([[3,2,4],[2,0,2],[4,2,3]]) |
|
159 and then to evaluate eigenvalues of array |
|
160 eig(a) |
|
161 it returns both eigen values and eigen vector of given matrix |
|
162 to get only eigen values use |
|
163 eigvals(a) |
|
164 |
|
165 This brings us to end of this session. We have covered Matrices |
|
166 Initialization |
|
167 Slicing |
|
168 Striding |
|
169 A bit of image processing |
|
170 Functions available for arrays |
|
171 Thank you |
104 |
172 |
105 ---------------- |
173 ---------------- |
106 We have seen |
174 We have seen |
107 Welcome to the Tutorial on arrays. |
175 Welcome to the Tutorial on arrays. |
108 |
176 |