76 \title[Exercises]{Exercises} |
76 \title[Exercises]{Exercises} |
77 |
77 |
78 \author[FOSSEE] {FOSSEE} |
78 \author[FOSSEE] {FOSSEE} |
79 |
79 |
80 \institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay} |
80 \institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay} |
81 \date[] {30 April, 2010\\Day 1, Session 5} |
81 \date[] {SciPy 2010, Introductory tutorials\\Day 1, Session 5} |
82 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
82 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
83 |
83 |
84 %\pgfdeclareimage[height=0.75cm]{iitmlogo}{iitmlogo} |
84 %\pgfdeclareimage[height=0.75cm]{iitmlogo}{iitmlogo} |
85 %\logo{\pgfuseimage{iitmlogo}} |
85 %\logo{\pgfuseimage{iitmlogo}} |
86 |
86 |
178 \includegraphics[height=2in, interpolate=true]{data/damp} |
178 \includegraphics[height=2in, interpolate=true]{data/damp} |
179 \column{0.45\textwidth} |
179 \column{0.45\textwidth} |
180 \begin{block}{Damped Oscillation} |
180 \begin{block}{Damped Oscillation} |
181 \tiny |
181 \tiny |
182 \begin{lstlisting} |
182 \begin{lstlisting} |
183 In []: x = linspace(0, 4*pi) |
183 In []: t = linspace(0, 4*pi) |
184 In []: plot(x, exp(x/10)*sin(x)) |
184 In []: plot(t, exp(t/10)*sin(t)) |
185 \end{lstlisting} |
185 \end{lstlisting} |
186 \end{block} |
186 \end{block} |
187 \end{columns} |
187 \end{columns} |
188 \end{frame} |
188 \end{frame} |
189 |
189 |
190 \begin{frame}[fragile] |
190 \begin{frame}[fragile] |
191 \frametitle{Problem 3 cont...} |
191 \frametitle{Problem 3 cont...} |
192 Create a sequence of images in which the damped oscillator($e^{-x/10}sin(x)$) slowly evolves over time. |
192 Create a sequence of images in which the damped |
|
193 oscillator($e^{-t/10}sin(t)$) slowly evolves over time $t$. |
193 \begin{columns} |
194 \begin{columns} |
194 \column{0.35\textwidth} |
195 \column{0.35\textwidth} |
195 \includegraphics[width=1.5in,height=1.5in, interpolate=true]{data/plot2} |
196 \includegraphics[width=1.5in,height=1.5in, interpolate=true]{data/plot2} |
196 \column{0.35\textwidth} |
197 \column{0.35\textwidth} |
197 \includegraphics[width=1.5in,height=1.5in, interpolate=true]{data/plot4} |
198 \includegraphics[width=1.5in,height=1.5in, interpolate=true]{data/plot4} |
199 \includegraphics[width=1.5in,height=1.5in, interpolate=true]{data/plot6} |
200 \includegraphics[width=1.5in,height=1.5in, interpolate=true]{data/plot6} |
200 \end{columns} |
201 \end{columns} |
201 \begin{block}{Hint} |
202 \begin{block}{Hint} |
202 \small |
203 \small |
203 \begin{lstlisting} |
204 \begin{lstlisting} |
204 savefig('plot'+str(i)+'.png') #i is int variable |
205 savefig('plot'+str(i)+'.png') #i is some int |
205 \end{lstlisting} |
206 \end{lstlisting} |
206 \end{block} |
207 \end{block} |
207 \end{frame} |
208 \end{frame} |
208 |
209 |
209 \begin{frame}[fragile] |
210 \begin{frame}[fragile] |
210 \frametitle{Problem 4} |
211 \frametitle{Problem 4} |
211 \begin{lstlisting} |
212 \begin{lstlisting} |
212 In []: x = imread('smoothing.png') |
213 In []: x = imread('smoothing.gif') |
213 In []: x.shape |
214 In []: x.shape |
214 Out[]: (256, 256) |
215 Out[]: (256, 256) |
215 In []: imshow(x,cmap=cm.gray) |
216 In []: imshow(x,cmap=cm.gray) |
216 \end{lstlisting} |
217 In []: colorbar() |
|
218 \end{lstlisting} |
217 \emphbar{Replace each pixel with mean of neighboring pixels} |
219 \emphbar{Replace each pixel with mean of neighboring pixels} |
218 \begin{center} |
220 \begin{center} |
219 \includegraphics[height=1in, interpolate=true]{data/neighbour} |
221 \includegraphics[height=1in, interpolate=true]{data/neighbour} |
220 \end{center} |
222 \end{center} |
221 \end{frame} |
223 \end{frame} |
248 |
250 |
249 \begin{frame}[fragile] |
251 \begin{frame}[fragile] |
250 \frametitle{Solution} |
252 \frametitle{Solution} |
251 \begin{lstlisting} |
253 \begin{lstlisting} |
252 In []: y = zeros_like(x) |
254 In []: y = zeros_like(x) |
253 In []: y[1:-1,1:-1] = x[:-2,1:-1]/4+ |
255 In []: y[1:-1,1:-1] = x[:-2,1:-1]/4 + |
254 x[2:,1:-1]/4+ |
256 x[2:,1:-1]/4 + |
255 x[1:-1,2:]/4+ |
257 x[1:-1,2:]/4 + |
256 x[1:-1,:-2]/4 |
258 x[1:-1,:-2]/4 |
257 In []: imshow(y,cmap=cm.gray) |
259 In []: imshow(y,cmap=cm.gray) |
258 \end{lstlisting} |
260 \end{lstlisting} |
|
261 \end{frame} |
|
262 |
|
263 \begin{frame}[fragile] |
|
264 \frametitle{Problem 4 cont\ldots} |
|
265 \begin{itemize} |
|
266 \item Apply the smoothing operation repeatedly to the original |
|
267 image |
|
268 |
|
269 \item Subtract the smoothed image from the original to obtain |
|
270 the edges |
|
271 \end{itemize} |
|
272 \end{frame} |
|
273 |
|
274 \begin{frame}[fragile] |
|
275 \frametitle{Problem 5} |
|
276 |
|
277 What if you did the following in problem 4? |
|
278 \begin{lstlisting} |
|
279 In []: y1[1:-1,1:-1] = (x[:-2,1:-1] + |
|
280 x[2:,1:-1] + |
|
281 x[1:-1,2:] + |
|
282 x[1:-1,:-2])/4 |
|
283 \end{lstlisting} |
|
284 |
|
285 Are the answers different? |
|
286 |
|
287 \end{frame} |
|
288 |
|
289 \begin{frame}[fragile] |
|
290 \frametitle{Problem 5 cont\ldots} |
|
291 Why? The answer lies in the following: |
|
292 \begin{lstlisting} |
|
293 In []: x.dtype |
|
294 Out[]: dtype('uint8') |
|
295 |
|
296 In []: print x.itemsize |
|
297 1 |
|
298 |
|
299 In []: z = x/4.0 |
|
300 |
|
301 In []: print z.dtype |
|
302 float64 |
|
303 |
|
304 \end{lstlisting} |
|
305 \end{frame} |
|
306 |
|
307 \begin{frame}[fragile] |
|
308 \frametitle{Problem 5 cont\ldots} |
|
309 What if you did this? |
|
310 |
|
311 \begin{lstlisting} |
|
312 x = imread('smoothing.gif') |
|
313 y2 = zeros_like(x) |
|
314 y2[1:-1,1:-1] = x[:-2,1:-1]/4. + |
|
315 x[2:,1:-1]/4. + |
|
316 x[1:-1,2:]/4. + |
|
317 x[1:-1,:-2]/4. |
|
318 \end{lstlisting} |
|
319 \begin{itemize} |
|
320 \item Will the answer be any different from \typ{y}? |
|
321 \item What will the dtype of \typ{y2} be? |
|
322 \item Discuss what is going on! |
|
323 \end{itemize} |
|
324 |
|
325 \end{frame} |
|
326 |
|
327 \begin{frame}[fragile] |
|
328 \frametitle{Problem 5 cont\ldots} |
|
329 Did you do the right thing to find the edges earlier in problem 4? Fix it! |
|
330 |
|
331 Note that: |
|
332 \begin{lstlisting} |
|
333 In []: print x.dtype |
|
334 uint8 |
|
335 In []: x1 = x.astype('float64') |
|
336 In []: print x1.dtype |
|
337 float64 |
|
338 In []: print x.dtype.char |
|
339 d |
|
340 In []: x.dtype.<TAB> # Explore! |
|
341 \end{lstlisting} |
|
342 \end{frame} |
|
343 |
|
344 \begin{frame}[fragile] |
|
345 \frametitle{Problem 6} |
|
346 |
|
347 Edge detection looks much nicer with \typ{lena.png}, try it! The |
|
348 gotcha is that it is a 4 component RGBA image with elements in the |
|
349 range $[0.0, 1.0]$. |
|
350 |
|
351 \begin{lstlisting} |
|
352 In []: x = imread('lena.png') |
|
353 |
|
354 In []: print x.shape |
|
355 (512, 512, 4) |
|
356 |
|
357 In []: print x.min(), x.max() |
|
358 0.0 1.0 |
|
359 \end{lstlisting} |
|
360 |
|
361 Repeat the edge detection with this image. |
|
362 |
259 \end{frame} |
363 \end{frame} |
260 |
364 |
261 |
365 |
262 \end{document} |
366 \end{document} |
263 |
367 |