As a reminder, we can use the point
method to apply an arbitrary function to each pixel.
A filter applies a convolution kernel to an image.
Although this sounds fancy, this is just a generalization of the point
method where the function takes as input the neighborhood of the pixels.
The kernel is represented by an $n$x$n$ matrix where the target pixel is in the center.
The output of the filter is the sum of the products of the matrix elements with the corresponding pixels.
Examples, from Wikipedia):
![]() |
![]() |
![]() |
Identity | Blur | Edge Detection |
The PIL ImageFilter module includes a number of built-in convolution kernels that can be applied using the image filter
method.
((3, 3), 13, 0, (1, 1, 1, 1, 5, 1, 1, 1, 1))
[[1 1 1] [1 5 1] [1 1 1]] 13
The output is the sum of the product of the coefficients and the corresponding pixels divided by the denominator and incremented by the offset.
[[1 1 1 1 1] [1 0 0 0 1] [1 0 0 0 1] [1 0 0 0 1] [1 1 1 1 1]] 16
[[1 1 1] [1 5 1] [1 1 1]] 13
[[-1 -1 -1] [-1 8 -1] [-1 -1 -1]] 1
[[-2 -2 -2] [-2 32 -2] [-2 -2 -2]] 16
There are also non-linear filters that don't compute sums of products. Instead, they compare the values of the neighboring pixels (e.g., max value, min value, median value).
Recursion is when a function calls itself on a small version of the problem to compute the answer.
It is a very useful way to think about complex, but decomposable, problems.
8
A recursive function must have a base case, an input that is eventually reached that does not require any further calls to the function ($n \le 1$ above).
What happens if you forget the base case?
--------------------------------------------------------------------------- RecursionError Traceback (most recent call last) <ipython-input-16-aba404ac2c91> in <module> 2 return brokenfib(n-1)+brokenfib(n-2) 3 ----> 4 brokenfib(5) <ipython-input-16-aba404ac2c91> in brokenfib(n) 1 def brokenfib(n): ----> 2 return brokenfib(n-1)+brokenfib(n-2) 3 4 brokenfib(5) ... last 1 frames repeated, from the frame below ... <ipython-input-16-aba404ac2c91> in brokenfib(n) 1 def brokenfib(n): ----> 2 return brokenfib(n-1)+brokenfib(n-2) 3 4 brokenfib(5) RecursionError: maximum recursion depth exceeded
Every time you call a function, the function and its arguments are placed on the call stack.
The call stack will eventually run out of memory.
Python limits how many calls can be on the call stack.
3000
if n <= 1: return 1
)n-1
, n-2
)fib(n-1)
, fib(n-2)
)fib(n-1)+fib(n-2)
)10
accumulator: 5, n: 5 accumulator: 20, n: 4 accumulator: 60, n: 3 accumulator: 120, n: 2 accumulator: 120, n: 1 factorial 5 = 120
accumulator: 1, n: 5 accumulator: 5, n: 4 accumulator: 20, n: 3 accumulator: 60, n: 2 accumulator: 120, n: 1 factorial 5 = 120
Click on a pixel, fill that pixel and all touching pixels of the same color with the fill color.
Flood-fill (pixel, target-color, replacement-color)
What is/are the recursive step(s)?
What is the base case?
Flood-fill (pixel, target-color, replacement-color):
The idea is to threshold the image and then count the number of white blobs.
load