lecture 7: more math + image filtering€¦ · justin johnson eecs 442 wi 2020: lecture 7 - january...

Post on 30-May-2020

4 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Justin Johnson January 30, 2020EECS 442 WI 2020: Lecture 7 -

Lecture 7:More Math +

Image Filtering

1

Justin Johnson January 30, 2020EECS 442 WI 2020: Lecture 7 -

Administrative

2

HW0 was due yesterday!

HW1 due a week from yesterday

Justin Johnson January 30, 2020EECS 442 WI 2020: Lecture 7 -

Cool Talk Today:

3

https://cse.engin.umich.edu/event/numpy-a-look-at-the-past-present-and-future-of-array-computation

Justin Johnson January 30, 2020EECS 442 WI 2020: Lecture 7 -

Last Time:Matrices, Vectorization,

Linear Algebra

4

Justin Johnson January 30, 2020EECS 442 WI 2020: Lecture 7 -

Eigensystems

5

• An eigenvector 𝒗𝒊 and eigenvalue 𝜆$ of a matrix 𝑨satisfy 𝑨𝒗𝒊 = 𝜆$𝒗𝒊 (𝑨𝒗𝒊 is scaled by 𝜆$)

• Vectors and values are always paired and typically you assume 𝒗𝒊 ' = 1

• Biggest eigenvalue of A gives bounds on how much 𝑓 𝒙 = 𝑨𝒙 stretches a vector x.

• Hints of what people really mean:• “Largest eigenvector” = vector w/ largest value• “Spectral” just means there’s eigenvectors somewhere

Justin Johnson January 30, 2020EECS 442 WI 2020: Lecture 7 - 6

Suppose I have points in a grid

Justin Johnson January 30, 2020EECS 442 WI 2020: Lecture 7 - 7

Now I apply f(x) = Ax to these pointsPointy-end: Ax . Non-Pointy-End: x

Justin Johnson January 30, 2020EECS 442 WI 2020: Lecture 7 - 8

Red box – unit square, Blue box – after f(x) = Ax. What are the yellow lines and why?

𝑨 =1.1 00 1.1

Justin Johnson January 30, 2020EECS 442 WI 2020: Lecture 7 - 9

𝑨 =0.8 00 1.25

Now I apply f(x) = Ax to these pointsPointy-end: Ax . Non-Pointy-End: x

Justin Johnson January 30, 2020EECS 442 WI 2020: Lecture 7 - 10

Red box – unit square, Blue box – after f(x) = Ax. What are the yellow lines and why?

𝑨 =0.8 00 1.25

Justin Johnson January 30, 2020EECS 442 WI 2020: Lecture 7 - 11

Red box – unit square, Blue box – after f(x) = Ax. Can we draw any yellow lines?

𝑨 =cos(𝑡) −sin(𝑡)sin(𝑡) cos(𝑡)

Justin Johnson January 30, 2020EECS 442 WI 2020: Lecture 7 -

Eigenvectors of Symmetric Matrices

12

• Always n mutually orthogonal eigenvectors with n (not necessarily) distinct eigenvalues

• For symmetric 𝑨, the eigenvector with the largest eigenvalue maximizes 𝒙

𝑻𝑨𝒙𝒙𝑻𝒙

(smallest/min)

• So for unit vectors (where 𝒙𝑻𝒙 = 1), that eigenvector maximizes 𝒙𝑻𝑨𝒙

• A surprisingly large number of optimization problems rely on (max/min)imizing this

Justin Johnson January 30, 2020EECS 442 WI 2020: Lecture 7 -

Singular Value Decomposition

13

UA =

Rotation

Can always write a mxn matrix A as: 𝑨 = 𝑼𝚺𝑽𝑻

Eigenvectors of AAT

Scale

Sqrt of Eigenvalues

of ATA

σ1

σ2

σ3

00

Justin Johnson January 30, 2020EECS 442 WI 2020: Lecture 7 -

Singular Value Decomposition

14

UA =

Rotation

Can always write a mxn matrix A as: 𝑨 = 𝑼𝚺𝑽𝑻

Eigenvectors of AAT

Scale

Sqrt of Eigenvalues

of ATA

VT

Rotation

Eigenvectors of ATA

Justin Johnson January 30, 2020EECS 442 WI 2020: Lecture 7 -

Singular Value Decomposition

15

• Every matrix is a rotation, scaling, and rotation• Number of non-zero singular values = rank /

number of linearly independent vectors• “Closest” matrix to A with a lower rank

UA =σ1

σ2σ3

0

0VT

Justin Johnson January 30, 2020EECS 442 WI 2020: Lecture 7 -

Singular Value Decomposition

16

• Every matrix is a rotation, scaling, and rotation• Number of non-zero singular values = rank /

number of linearly independent vectors• “Closest” matrix to A with a lower rank

UÂ =σ1

σ2

0

0VT

0

Justin Johnson January 30, 2020EECS 442 WI 2020: Lecture 7 -

Singular Value Decomposition

17

• Every matrix is a rotation, scaling, and rotation• Number of non-zero singular values = rank /

number of linearly independent vectors• “Closest” matrix to A with a lower rank• Secretly behind basically many things you do with

matrices

Justin Johnson January 30, 2020EECS 442 WI 2020: Lecture 7 -

Solving Least-Squares

18

Start with two points (xi,yi)

𝑦@𝑦' = 𝑥@ 1

𝑥' 1𝑚𝑏

𝒚 = 𝑨𝒗

𝑦@𝑦' = 𝑚𝑥@ + 𝑏

𝑚𝑥' + 𝑏

We know how to solve this –invert A and find v (i.e., (m,b) that

fits points)

(x1,y1)

(x2,y2)

Justin Johnson January 30, 2020EECS 442 WI 2020: Lecture 7 -

Solving Least-Squares

19

Start with two points (xi,yi)

𝑦@𝑦' = 𝑥@ 1

𝑥' 1𝑚𝑏

𝒚 = 𝑨𝒗

𝑦@𝑦' − 𝑚𝑥@ + 𝑏

𝑚𝑥' + 𝑏

'

𝒚 − 𝑨𝒗 ' =

= 𝑦@ − 𝑚𝑥@ + 𝑏'+ 𝑦' − 𝑚𝑥' + 𝑏

'

(x1,y1)

(x2,y2)

The sum of squared differences between the actual value of y and what the model says y should be.

Justin Johnson January 30, 2020EECS 442 WI 2020: Lecture 7 -

Solving Least-Squares

20

Suppose there are n > 2 points

𝑦@⋮𝑦G

=𝑥@ 1⋮ ⋮𝑥G 1

𝑚𝑏

𝒚 = 𝑨𝒗

Compute 𝑦 − 𝐴𝑥 ' again

𝒚 − 𝑨𝒗 ' =I$J@

K

𝑦$ − (𝑚𝑥$ + 𝑏) '

Justin Johnson January 30, 2020EECS 442 WI 2020: Lecture 7 -

Solving Least-Squares

21

Given y, A, and v with y = Av overdetermined (A tall / more equations than unknowns) We want to minimize 𝒚 − 𝑨𝒗 𝟐, or find:

arg min𝒗 𝒚 − 𝑨𝒗 '

(The value of x that makes the expression smallest)

Solution satisfies 𝑨𝑻𝑨 𝒗∗ = 𝑨𝑻𝒚or

𝒗∗ = 𝑨𝑻𝑨R@𝑨𝑻𝒚

(Don’t actually compute the inverse!)

Justin Johnson January 30, 2020EECS 442 WI 2020: Lecture 7 -

When is Least-Squares Possible?

22

Given y, A, and v. Want y = Av

Ay = vWant n outputs, have n knobs to fiddle with, every knob is useful if A is full rank.

Ay

=v

A: rows (outputs) > columns (knobs). Thus can’t get precise output you want (not enough knobs). So settle for “closest” knob setting.

Justin Johnson January 30, 2020EECS 442 WI 2020: Lecture 7 -

When is Least-Squares Possible?

23

Given y, A, and v. Want y = Av

Ay = vWant n outputs, have n knobs to fiddle with, every knob is useful if A is full rank.

Ay =v

A: columns (knobs) > rows (outputs). Thus, any output can be expressed in infinite ways.

Justin Johnson January 30, 2020EECS 442 WI 2020: Lecture 7 -

Homogeneous Least-Squares

24

Given a set of unit vectors (aka directions) 𝒙𝟏, … , 𝒙𝒏 and I want vector 𝒗 that is as orthogonal to all the 𝒙𝒊 as possible (for some definition of orthogonal)

𝑨𝒗 =− 𝒙𝟏𝑻 −

⋮− 𝒙𝒏𝑻 −

𝒗

Stack 𝒙𝒊 into A, compute Av

=𝒙𝟏𝑻𝒗⋮

𝒙𝒏𝑻𝒗

𝒙𝟏𝒙𝟐

𝒙𝒏…

𝒗𝑨𝒗 𝟐 =I

𝒊

𝒏

𝒙𝒊𝑻𝒗𝟐Compute

0 if orthog

Sum of how orthog. v is to each x

Justin Johnson January 30, 2020EECS 442 WI 2020: Lecture 7 -

Homogenous Least-Squares

25

• A lot of times, given a matrix A we want to find the v that minimizes 𝑨𝒗 ' .

• I.e., want 𝐯∗ = argmin𝒗

𝑨𝒗 ''

• What’s a trivial solution? • Set v = 0 → Av = 0• Exclude this by forcing v to have unit norm

Justin Johnson January 30, 2020EECS 442 WI 2020: Lecture 7 -

Homogenous Least-Squares

26

Let’s look at 𝑨𝒗 ''

𝑨𝒗 '' = Rewrite as dot product

𝑨𝒗 '' = 𝒗𝑻𝑨𝑻𝐀𝐯 = 𝐯𝐓 𝐀𝐓𝐀 𝐯

𝑨𝒗 '' = 𝐀𝐯 Z(𝐀𝐯) Distribute transpose

We want the vector minimizing this quadratic formWhere have we seen this?

Justin Johnson January 30, 2020EECS 442 WI 2020: Lecture 7 -

Homogenous Least-Squares

27

arg min𝒗 [J@

𝑨𝒗 '

*Note: 𝑨𝑻𝑨 is positive semi-definite so it has all non-negative eigenvalues

(1) “Smallest”* eigenvector of 𝑨𝑻𝑨(2) “Smallest” right singular vector of 𝑨

Ubiquitious tool in vision:

For min → max, switch smallest → largest

Justin Johnson January 30, 2020EECS 442 WI 2020: Lecture 7 -

Derivatives

28

Justin Johnson January 30, 2020EECS 442 WI 2020: Lecture 7 -

Derivatives

29

Remember derivatives?

Derivative: rate at which a function f(x) changes at a point as well as the direction that increases the function

Justin Johnson January 30, 2020EECS 442 WI 2020: Lecture 7 - 30

Given quadratic function f(x)

𝑓 𝑥 is function

𝑔 𝑥 = 𝑓] 𝑥

aka

𝑔 𝑥 =𝑑𝑑𝑥𝑓(𝑥)

𝑓 𝑥, 𝑦 = 𝑥 − 2 ' + 5

Justin Johnson January 30, 2020EECS 442 WI 2020: Lecture 7 - 31

Given quadratic function f(x)

What’s special about x=2?

𝑓 𝑥 minim. at 2𝑔 𝑥 = 0 at 2

a = minimum of f →𝑔 𝑎 = 0

Reverse is not true

𝑓 𝑥, 𝑦 = 𝑥 − 2 ' + 5

Justin Johnson January 30, 2020EECS 442 WI 2020: Lecture 7 - 32

Rates of change

Suppose I want to increase f(x) by

changing x:

Blue area: move leftRed area: move right

Derivative tells you direction of ascent and

rate

𝑓 𝑥, 𝑦 = 𝑥 − 2 ' + 5

Justin Johnson January 30, 2020EECS 442 WI 2020: Lecture 7 -

Calculus to Know

33

• Really need intuition• Need chain rule• Rest you should look up / use a computer algebra

system / use a cookbook • Partial derivatives (and that’s it from multivariable

calculus)

Justin Johnson January 30, 2020EECS 442 WI 2020: Lecture 7 -

Partial Derivatives

34

• Pretend other variables are constant, take a derivative. That’s it.

• Make our function a function of two variables

𝑓' 𝑥, 𝑦 = 𝑥 − 2 ' + 5 + 𝑦 + 1 '

𝑓 𝑥 = 𝑥 − 2 ' + 5𝜕𝜕𝑥𝑓 𝑥 = 2 𝑥 − 2 ∗ 1 = 2(𝑥 − 2)

𝜕𝜕𝑥𝑓' 𝑥 = 2(𝑥 − 2)

Pretend it’s constant → derivative = 0

Justin Johnson January 30, 2020EECS 442 WI 2020: Lecture 7 - 35

Zooming Out𝑓' 𝑥, 𝑦 = 𝑥 − 2 ' + 5 + 𝑦 + 1 '

Dark = f(x,y) lowBright = f(x,y) high

Justin Johnson January 30, 2020EECS 442 WI 2020: Lecture 7 - 36

Taking a slice of𝑓' 𝑥, 𝑦 = 𝑥 − 2 ' + 5 + 𝑦 + 1 '

Slice of y=0 is the function from before:𝑓 𝑥 = 𝑥 − 2 ' + 5𝑓] 𝑥 = 2(𝑥 − 2)

Justin Johnson January 30, 2020EECS 442 WI 2020: Lecture 7 - 37

Taking a slice of𝑓' 𝑥, 𝑦 = 𝑥 − 2 ' + 5 + 𝑦 + 1 '

aab𝑓' 𝑥, 𝑦 is rate of

change & direction in x dimension

Justin Johnson January 30, 2020EECS 442 WI 2020: Lecture 7 - 38

Zooming Out𝑓' 𝑥, 𝑦 = 𝑥 − 2 ' + 5 + 𝑦 + 1 '

Gradient/Jacobian:Making a vector of

∇d=𝜕𝑓𝜕𝑥

,𝜕𝑓𝜕𝑦

gives rate and direction of change.

Arrows point OUT of minimum / basin.

Justin Johnson January 30, 2020EECS 442 WI 2020: Lecture 7 -

What Should I Know?

39

• Gradients are simply partial derivatives per-dimension: if 𝒙 in 𝑓(𝒙) has n dimensions, ∇d(𝑥)has n dimensions

• Gradients point in direction of ascent and tell the rate of ascent

• If a is minimum of 𝑓(𝒙)→ ∇e a = 𝟎• Reverse is not true, especially in high-dimensional

spaces

Justin Johnson January 30, 2020EECS 442 WI 2020: Lecture 7 -

Image Filtering

40

Justin Johnson January 30, 2020EECS 442 WI 2020: Lecture 7 -

A Noisy Image

41

Justin Johnson January 30, 2020EECS 442 WI 2020: Lecture 7 -

Cleaning it up

42

Slide Credit: D. Lowe

• We have noise in our image• Let’s replace each pixel with a weighted

average of its neighborhood• Weights are filter kernel

1/9 1/9 1/9

1/9 1/9 1/9

1/9 1/9 1/9

Out

Justin Johnson January 30, 2020EECS 442 WI 2020: Lecture 7 -

1D Case

43

1/3 1/3 1/3Filter

Signal 10 12 9 11 10 11 12

Output 10.33

Justin Johnson January 30, 2020EECS 442 WI 2020: Lecture 7 -

1D Case

44

1/3 1/3 1/3Filter

Signal 10 12 9 11 10 11 12

Output 10.33 10.66

Justin Johnson January 30, 2020EECS 442 WI 2020: Lecture 7 -

1D Case

45

1/3 1/3 1/3Filter

Signal 10 12 9 11 10 11 12

Output 10.33 10.66 10

Justin Johnson January 30, 2020EECS 442 WI 2020: Lecture 7 -

1D Case

46

1/3 1/3 1/3Filter

Signal 10 12 9 11 10 11 12

Output 10.33 10.66 10 10.66

Justin Johnson January 30, 2020EECS 442 WI 2020: Lecture 7 -

1D Case

47

1/3 1/3 1/3Filter

Signal 10 12 9 11 10 11 12

Output 10.33 10.66 10 10.66 11

Justin Johnson January 30, 2020EECS 442 WI 2020: Lecture 7 -

Applying a 2D Filter

48

I11 I12 I13

I21 I22 I23

I31 I32 I33

I14 I15 I16

I24 I25 I26

I34 I35 I36

I41 I42 I43

I51 I52 I53

I44 I45 I46

I54 I55 I56

Input

F11 F12 F13

F21 F22 F23

F31 F32 F33

Filter

O11 O12 O13

O21 O22 O23

O31 O32 O33

O14

O24

O34

Output

Justin Johnson January 30, 2020EECS 442 WI 2020: Lecture 7 -

Applying a 2D Filter

49

I11 I12 I13

I21 I22 I23

I31 I32 I33

I14 I15 I16

I24 I25 I26

I34 I35 I36

I41 I42 I43

I51 I52 I53

I44 I45 I46

I54 I55 I56

Input & FilterF11 F12 F13

F21 F22 F23

F31 F32 F33

Output

O11

O11 = I11*F11 + I12*F12 + … + I33*F33

Justin Johnson January 30, 2020EECS 442 WI 2020: Lecture 7 -

Applying a 2D Filter

50

I11 I12 I13

I21 I22 I23

I31 I32 I33

I14 I15 I16

I24 I25 I26

I34 I35 I36

I41 I42 I43

I51 I52 I53

I44 I45 I46

I54 I55 I56

Input & FilterF11 F12 F13

F21 F22 F23

F31 F32 F33

Output

O11

O12 = I12*F11 + I13*F12 + … + I34*F33

O12

Justin Johnson January 30, 2020EECS 442 WI 2020: Lecture 7 -

Applying a 2D Filter

51

I11 I12 I13

I21 I22 I23

I31 I32 I33

I14 I15 I16

I24 I25 I26

I34 I35 I36

I41 I42 I43

I51 I52 I53

I44 I45 I46

I54 I55 I56

Input

F11 F12 F13

F21 F22 F23

F31 F32 F33

Filter Output

How many times can we apply a 3x3 filter to a 5x6 image?

Justin Johnson January 30, 2020EECS 442 WI 2020: Lecture 7 -

Applying a 2D Filter

52

I11 I12 I13

I21 I22 I23

I31 I32 I33

I14 I15 I16

I24 I25 I26

I34 I35 I36

I41 I42 I43

I51 I52 I53

I44 I45 I46

I54 I55 I56

Input Output

Oij = Iij*F11 + Ii(j+1)*F12 + … + I(i+2)(j+2)*F33

O11 O12 O13

O21 O22 O23

O31 O32 O33

O14

O24

O34

F11 F12 F13

F21 F22 F23

F31 F32 F33

Filter

Justin Johnson January 30, 2020EECS 442 WI 2020: Lecture 7 -

Edge Cases

53

f

gg

gg

f

gg

gg

f

gg

gg

Convolution doesn’t keep the whole image. Suppose f is the image and g the filter.

f/g Diagram Credit: D. Lowe

Full: Any part of g touches f.

Same: Output is same size as f

Valid: Filter doesn’t fall off edge

Justin Johnson January 30, 2020EECS 442 WI 2020: Lecture 7 -

Edge Cases

54

What to about the “?” region?

Symm: fold sides over

pad/fill: add value, often 0

f

gg

gg

? ? ? ?

Circular/Wrap: wrap around

f/g Diagram Credit: D. Lowe

Justin Johnson January 30, 2020EECS 442 WI 2020: Lecture 7 -

Edge Cases: Does It Matter?

55

InputImage

Box Filtered???

Box Filtered???

(I’ve applied the filter per-color channel)Which padding did I use and why?

Note – this is a zoom of the filtered, not a filter of the zoomed

Justin Johnson January 30, 2020EECS 442 WI 2020: Lecture 7 -

Edge Cases: Does It Matter?

56

InputImage

Box FilteredSymm Pad

Box FilteredZero Pad

(I’ve applied the filter per-color channel)

Note – this is a zoom of the filtered, not a filter of the zoomed

Justin Johnson January 30, 2020EECS 442 WI 2020: Lecture 7 -

Practice with Linear Filters

57

Slide Credit: D. Lowe

Original

?0 0 0

0 1 0

0 0 0

Justin Johnson January 30, 2020EECS 442 WI 2020: Lecture 7 -

Practice with Linear Filters

58

Slide Credit: D. Lowe

Original

0 0 0

0 1 0

0 0 0

The Same!

Justin Johnson January 30, 2020EECS 442 WI 2020: Lecture 7 -

Practice with Linear Filters

59

Slide Credit: D. Lowe

Original

?0 0 0

0 0 1

0 0 0

Justin Johnson January 30, 2020EECS 442 WI 2020: Lecture 7 -

Practice with Linear Filters

60

Slide Credit: D. Lowe

Original

0 0 0

0 0 1

0 0 0

Shifted LEFT

1 pixel

Justin Johnson January 30, 2020EECS 442 WI 2020: Lecture 7 -

Practice with Linear Filters

61

Slide Credit: D. Lowe

Original

?0 1 0

0 0 0

0 0 0

Justin Johnson January 30, 2020EECS 442 WI 2020: Lecture 7 -

Practice with Linear Filters

62

Slide Credit: D. Lowe

Original

0 1 0

0 0 0

0 0 0

Shifted DOWN1 pixel

Justin Johnson January 30, 2020EECS 442 WI 2020: Lecture 7 -

Practice with Linear Filters

63

?

Slide Credit: D. Lowe

Original

1/9 1/9 1/9

1/9 1/9 1/9

1/9 1/9 1/9

Justin Johnson January 30, 2020EECS 442 WI 2020: Lecture 7 -

Practice with Linear Filters

64

Slide Credit: D. Lowe

Original

1/9 1/9 1/9

1/9 1/9 1/9

1/9 1/9 1/9

Blur(Box Filter)

Justin Johnson January 30, 2020EECS 442 WI 2020: Lecture 7 -

Practice with Linear Filters

65

?

Slide Credit: D. Lowe

Original 1/9 1/9 1/9

1/9 1/9 1/9

1/9 1/9 1/9

0 0 0

0 2 0

0 0 0

-

Justin Johnson January 30, 2020EECS 442 WI 2020: Lecture 7 -

Practice with Linear Filters

66

Slide Credit: D. Lowe

Original 1/9 1/9 1/9

1/9 1/9 1/9

1/9 1/9 1/9

0 0 0

0 2 0

0 0 0

-

Sharpened(Acccentuates

difference from local average)

Justin Johnson January 30, 2020EECS 442 WI 2020: Lecture 7 -

Sharpening

67

Slide Credit: D. Lowe

Justin Johnson January 30, 2020EECS 442 WI 2020: Lecture 7 -

Properties: Linear

68

Assume: I image f1, f2 filters Linear: apply(I,f1+f2) = apply(I,f1) + apply(I,f2)I is a white box on black, and f1, f2 are rectangles

Note: I am showing filters un-normalized and blown up. They’re a smaller box filter (i.e., each entry is 1/(size^2))

== +

=A( , )+A( , ) =

Justin Johnson January 30, 2020EECS 442 WI 2020: Lecture 7 -

Properties: Shift-Invariant

69

Assume: I image, f filterShift-invariant: shift(apply(I,f)) = apply(shift(I,f))Intuitively: only depends on filter neighborhood

A( , ) =

A( , ) =

Note: “Shift-Invariant” is standard terminology, but I think “Shift-Equivariant” is more correct

Justin Johnson January 30, 2020EECS 442 WI 2020: Lecture 7 -

Annoying Terminology

70

Often called “convolution”. Actually cross-correlation.

Cross-Correlation(Slide filter over image)

Convolution(Flip filter, then slide it)

Justin Johnson January 30, 2020EECS 442 WI 2020: Lecture 7 -

Properties of Convolution

71

• Any shift-invariant, linear operation is a convolution (⁎)• Commutative: f ⁎ g = g ⁎ f• Associative: (f ⁎ g) ⁎ h = f ⁎ (g ⁎ h)• Distributes over +: f ⁎ (g + h) = f ⁎ g + f ⁎ h• Scalars factor out: kf ⁎ g = f ⁎ kg = k (f ⁎ g)• Identity (a single one with all zeros):

Property List: K. Grauman

=*

Justin Johnson January 30, 2020EECS 442 WI 2020: Lecture 7 -

Next Time:More Image Filtering,

Edge Detection

72

top related