1 of 220

2 of 220

3 of 220

Say we want affine transformation

  • How many knowns do we get with one match? mA = n
    • nx = a00*mx + a01*my + a02*1
    • ny = a10*mx + a11*my + a12*1
    • Solve M a = b
      • MT M a = MT b
      • a = (MT M)-1 MT b
    • Still works if overdetermined
      • WHY???

4 of 220

Linear least squares

Want to minimize squared error: || b - M a ||2 =

bTb - 2aTMTb + aTMTMa

This is convex and minimized when gradient = 0. So we take the derivative wrt a and set = 0.

-MTb + (MTM)a = 0

(MTM)a = MTb

a = (MTM)-1MTb yay!!!!

5 of 220

So how does least squares do?

6 of 220

So how does least squares do?

Error based on squared residual

Very scared of being wrong, even for just one point

Very bad at handling outliers in data

7 of 220

RANSAC: RANdom SAmple Consensus

8 of 220

RANSAC: RANdom SAmple Consensus

  • Parameters: data, model, n points to fit model,�k iterations, t threshold, d “good” fit cutoff

bestmodel = None�bestfit = INF�While i < k:� sample = draw n random points from data� Fit model to sample� inliers = data within t of model� if inliers > bestfit:� Fit model to all inliers� bestfit = fit� bestmodel = model� if inliers > d:� return model�return bestmodel

9 of 220

RANSAC: RANdom SAmple Consensus

  • Works well even with extreme noise.

10 of 220

We want projective (homography)

  • New matrix equations:

  • Same procedure, Solve M a = b
    • Exact if #rows of M = 8
    • Least squares if #rows of M > 8

11 of 220

In practice:

12 of 220

What’s happening?

13 of 220

Very bad for big panoramas!

14 of 220

How do we fix it? Cylinders!

15 of 220

How do we fix it? Cylinders!

16 of 220

Does it work?

17 of 220

Does it work? Yay!

18 of 220

THIS IS AS GOOD AS IT GETS!

  • Not so fast…
  • Our descriptor has some issues:
    • Not invariant to lighting changes!
  • Want features invariant to lighting

19 of 220

Histogram of Oriented Gradients (HOG)

Binning from http://aishack.in/tutorials/sift-scale-invariant-feature-transform-features/

  • Dalal and Triggs 2005
  • Better image descriptor
    • Compute gradients
    • Bin gradients
    • Aggregate blocks (4x4, 16x16 cells)
    • Normalize gradient magnitudes
  • Not reliant on magnitude, just direction
    • Invariant to some lighting changes
  • Train SVM to recognize people

20 of 220

Histogram of Oriented Gradients (HOG)

21 of 220

THIS IS AS GOOD AS IT GETS!

  • Not so fast…
  • Harris and has some issues:
    • Corners detection is rotation invariant
    • Harris not invariant to scale
  • Descriptors are also hard
    • Just looking at pixels is not rotation invariant!
    • HOG also not rotation invariant

22 of 220

Want features invariant to scaling, rotation, etc.

  • Scale Invariant Feature Transform (SIFT)
    • Lowe et al. 2004, many images from that paper
  • Get scale-invariant response map
  • Find keypoints
  • Extract rotation-invariant descriptors
    • Normalize based on orientation
    • Normalize based on lighting

23 of 220

Extract DoG features at multiple scales

24 of 220

Find local-maxima in location and scale

25 of 220

Throw out weak responses and edges

  • Estimate gradients
    • Similar to before, look at nearby responses
    • Not whole image, only a few points! Faster!
    • Throw out weak responses
  • Find cornery things
    • Same deal, structure matrix, use det and trace information

26 of 220

Find main orientation of patches

  • Look at weighted histogram of nearby gradients
    • Any gradient within 80% of peak gets its own descriptor
      • Multiple keypoints per pixel
    • Descriptors are normalized based on main orientation

27 of 220

Keypoints are normalized gradient histograms

  • Divide into subwindows (2x2, 4x4)
  • Bin gradients within subwindow, get histogram
    • Normalize to unit length
    • Clamp at maximum .2
    • Normalize again
    • Helps with lighting changes!

28 of 220

SIFT is great!

  • Find good keypoints, describe them
  • Finding objects, recognition, panoramas, etc.

29 of 220

SIFT is great!

30 of 220

31 of 220

Topics:

  • Sparse* Optical Flow (Lucas-Kanade, 1981)
  • Dense Optical Flow (Farneback, 2003)

*LK can technically be dense.

32 of 220

What is Optical Flow?

33 of 220

What is Optical Flow?

Movement

34 of 220

What is Optical Flow?

Movement

35 of 220

What is Optical Flow?

Movement

36 of 220

What is Optical Flow?

Movement

37 of 220

What is Optical Flow?

Movement

Object

38 of 220

What is Optical Flow?

Movement

Pan

39 of 220

What is Optical Flow?

Movement

Forward

40 of 220

What is Optical Flow?

Movement

41 of 220

Why do we want Optical Flow?

42 of 220

Why do we want Optical Flow?

Motion Estimation

43 of 220

Why do we want Optical Flow?

Motion Estimation

Object Tracking

44 of 220

Why do we want Optical Flow?

Motion Estimation

Object Tracking

Visual Odometry

45 of 220

How do we find the flow in an image?

46 of 220

Feature Matching

47 of 220

Previously: Features!

  • Highly descriptive local regions
  • Ways to describe those regions
  • Useful for:
    • Matching
    • Recognition
    • Detection

48 of 220

Feature Matching

49 of 220

Feature Matching

50 of 220

Feature Matching

SOLVED!

51 of 220

Feature Matching

Disadvantages:

52 of 220

Feature Matching

Disadvantages:

-Sparse!

53 of 220

Feature Matching

Disadvantages:

-Sparse!

-Feature alignment not exact

54 of 220

Feature Matching

55 of 220

Feature Matching

Disadvantages:

-Sparse!

-Feature alignment not exact

-Low accuracy

56 of 220

Feature Matching

Advantages:

Disadvantages:

-Sparse!

-Feature alignment not exact

-Low accuracy

57 of 220

Feature Matching

Advantages:

-Scale/rotation invariant

-*kinda* lighting invariant

-Can handle large movements

Disadvantages:

-Sparse!

-Feature alignment not exact

-Low accuracy

58 of 220

Feature Matching

Advantages:

-Scale/rotation invariant

-*kinda* lighting invariant

-Can handle large movements

Disadvantages:

-Sparse!

-Feature alignment not exact

-Low accuracy

Overall: Doesn’t work very well for Optical Flow

59 of 220

What do we do instead?

60 of 220

An observation...

61 of 220

An observation...

62 of 220

An observation...

63 of 220

An observation...

64 of 220

An observation...

f(t)

65 of 220

An observation...

f(t)

66 of 220

An observation...

f(t)

67 of 220

An observation...

f(t)

68 of 220

An observation...

f(t)

69 of 220

An observation...

f(t)

70 of 220

An observation...

f(t)

71 of 220

An observation...

f(t)

72 of 220

An observation...

f(t)

73 of 220

An observation...

f(t)

f(-p)

74 of 220

An observation...

f(t)

f(-p)

SAME!

75 of 220

An observation...

f(t)

f(-p)

Insight: Moving the object to the right is the same as moving the observed pixel to the left

76 of 220

Lucas-Kanade Optical Flow

Can we use this relationship to compute optical flow?

77 of 220

Lucas-Kanade Optical Flow

Can we use this relationship to compute optical flow?

B.D. Lucas, T. Kanade, “An Image Registration Technique with an Application to Stereo Vision”, in Proceedings of Image Understanding Workshop, 1981, pp. 121-130.

78 of 220

Lucas-Kanade Optical Flow

Can we use this relationship to compute optical flow?

B.D. Lucas, T. Kanade, “An Image Registration Technique with an Application to Stereo Vision”, in Proceedings of Image Understanding Workshop, 1981, pp. 121-130.

You will be implementing the LK optical flow algorithm for the homework

79 of 220

Lucas-Kanade Optical Flow

Assuming the object moves by 𝚫p over 𝚫t

f(p - 𝚫p, t) = f(p, t + 𝚫t)

80 of 220

Lucas-Kanade Optical Flow

Assuming the object moves by 𝚫p over 𝚫t

f(p - 𝚫p, t) = f(p, t + 𝚫t)

Observation point

81 of 220

Lucas-Kanade Optical Flow

Assuming the object moves by 𝚫p over 𝚫t

f(p - 𝚫p, t) = f(p, t + 𝚫t)

The movement @p

82 of 220

Lucas-Kanade Optical Flow

Assuming the object moves by 𝚫p over 𝚫t

f(p - 𝚫p, t) = f(p, t + 𝚫t)

First image

Second image

83 of 220

Lucas-Kanade Optical Flow

Assuming the object moves by 𝚫p over 𝚫t

f(p - 𝚫p, t) = f(p, t + 𝚫t)

Brightness

84 of 220

Lucas-Kanade Optical Flow

Assuming the object moves by 𝚫p over 𝚫t

f(p - 𝚫p, t) = f(p, t + 𝚫t)

Approximate with a first-order Taylor expansion

85 of 220

Lucas-Kanade Optical Flow

Assuming the object moves by 𝚫p over 𝚫t

f(p - 𝚫p, t) = f(p, t + 𝚫t)

Approximate with a first-order Taylor expansion

What in the doodely is a first-order Taylor expansion?

86 of 220

Quick & Dirty: Taylor Expansion

Approximate complex function with nth-order polynomial

87 of 220

Quick & Dirty: Taylor Expansion

Approximate complex function with nth-order polynomial

f(x) ≈ 𝚺i aixi

88 of 220

Quick & Dirty: Taylor Expansion

Approximate complex function with nth-order polynomial

f(x) ≈ 𝚺i aixi

For first-order i=1

≈ a1x1 + a0x0

≈ mx + b

89 of 220

Quick & Dirty: Taylor Expansion

Approximate complex function with nth-order polynomial

f(x) ≈ 𝚺i aixi

For first-order i=1

≈ a1x1 + a0x0

≈ mx + b

x

f(x)

mx + b

90 of 220

Quick & Dirty: Taylor Expansion

Approximate complex function with nth-order polynomial

f(x) ≈ 𝚺i aixi

For first-order i=1

≈ a1x1 + a0x0

≈ mx + b

x

f(x)

mx + b

Equal at x

91 of 220

Lucas-Kanade Optical Flow

Assuming the object moves by 𝚫p over 𝚫t

f(p - 𝚫p, t) = f(p, t + 𝚫t)

Approximate with a first-order Taylor expansion

m(p - 𝚫p) + b ≈ f(p, t + 𝚫t)

92 of 220

Lucas-Kanade Optical Flow

Assuming the object moves by 𝚫p over 𝚫t

f(p - 𝚫p, t) = f(p, t + 𝚫t)

Approximate with a first-order Taylor expansion

m(p - 𝚫p) + b ≈ f(p, t + 𝚫t)

mp - m𝚫p + b ≈ f(p, t + 𝚫t)

93 of 220

Lucas-Kanade Optical Flow

mp - m𝚫p + b ≈ f(p, t + 𝚫t)

Out of space

94 of 220

Lucas-Kanade Optical Flow

mp - m𝚫p + b ≈ f(p, t + 𝚫t)

mp - m𝚫p + b - f(p, t) ≈ f(p, t + 𝚫t) - f(p, t)

Subtract f(p, t) from both sides

95 of 220

Lucas-Kanade Optical Flow

mp - m𝚫p + b ≈ f(p, t + 𝚫t)

mp - m𝚫p + b - f(p, t) ≈ f(p, t + 𝚫t) - f(p, t)

Recall f(x, t) = mx + b

96 of 220

Lucas-Kanade Optical Flow

mp - m𝚫p + b ≈ f(p, t + 𝚫t)

mp - m𝚫p + b - f(p, t) ≈ f(p, t + 𝚫t) - f(p, t)

Recall f(x, t) = mx + b

Plugging mp + b in for f(p, t) cancels out!

97 of 220

Lucas-Kanade Optical Flow

mp - m𝚫p + b ≈ f(p, t + 𝚫t)

mp - m𝚫p + b - f(p, t) ≈ f(p, t + 𝚫t) - f(p, t)

Recall f(x, t) = mx + b

Plugging mp + b in for f(p, t) cancels out!

98 of 220

Lucas-Kanade Optical Flow

mp - m𝚫p + b ≈ f(p, t + 𝚫t)

mp - m𝚫p + b - f(p, t) ≈ f(p, t + 𝚫t) - f(p, t)

Recall f(x, t) = mx + b

-m𝚫p ≈ f(p, t + 𝚫t) - f(p, t)

99 of 220

Lucas-Kanade Optical Flow

mp - m𝚫p + b ≈ f(p, t + 𝚫t)

mp - m𝚫p + b - f(p, t) ≈ f(p, t + 𝚫t) - f(p, t)

Recall f(x, t) = mx + b

-m𝚫p ≈ f(p, t + 𝚫t) - f(p, t)

These we know

100 of 220

Lucas-Kanade Optical Flow

mp - m𝚫p + b ≈ f(p, t + 𝚫t)

mp - m𝚫p + b - f(p, t) ≈ f(p, t + 𝚫t) - f(p, t)

Recall f(x, t) = mx + b

-m𝚫p ≈ f(p, t + 𝚫t) - f(p, t)

These we know

This is what we want to know

101 of 220

Lucas-Kanade Optical Flow

mp - m𝚫p + b ≈ f(p, t + 𝚫t)

mp - m𝚫p + b - f(p, t) ≈ f(p, t + 𝚫t) - f(p, t)

Recall f(x, t) = mx + b

-m𝚫p ≈ f(p, t + 𝚫t) - f(p, t)

These we know

This is what we want to know

What is this?!

102 of 220

Lucas-Kanade Optical Flow

mp - m𝚫p + b ≈ f(p, t + 𝚫t)

mp - m𝚫p + b - f(p, t) ≈ f(p, t + 𝚫t) - f(p, t)

Recall f(x, t) = mx + b

-m𝚫p ≈ f(p, t + 𝚫t) - f(p, t)

Slope

x

f(x)

mx + b

103 of 220

Lucas-Kanade Optical Flow

mp - m𝚫p + b ≈ f(p, t + 𝚫t)

mp - m𝚫p + b - f(p, t) ≈ f(p, t + 𝚫t) - f(p, t)

Recall f(x, t) = mx + b

-m𝚫p ≈ f(p, t + 𝚫t) - f(p, t)

Slope = derivative

x

f(x)

mx + b

104 of 220

Lucas-Kanade Optical Flow

mp - m𝚫p + b ≈ f(p, t + 𝚫t)

mp - m𝚫p + b - f(p, t) ≈ f(p, t + 𝚫t) - f(p, t)

Recall f(x, t) = mx + b

-m𝚫p ≈ f(p, t + 𝚫t) - f(p, t)

Slope = derivative, aka gradient,

We know how to do gradients!

x

f(x)

mx + b

105 of 220

Previously: Image derivatives

  • Recall:
  • We don’t have an “actual”�Function, must estimate
  • Possibility: set h = 2
  • What will that look like?

106 of 220

Lucas-Kanade Optical Flow

mp - m𝚫p + b ≈ f(p, t + 𝚫t)

mp - m𝚫p + b - f(p, t) ≈ f(p, t + 𝚫t) - f(p, t)

Recall f(x, t) = mx + b

-m𝚫p ≈ f(p, t + 𝚫t) - f(p, t)

m = dp = dx 0

0 dy

x

f(x)

mx + b

107 of 220

Lucas-Kanade Optical Flow

mp - m𝚫p + b ≈ f(p, t + 𝚫t)

mp - m𝚫p + b - f(p, t) ≈ f(p, t + 𝚫t) - f(p, t)

Recall f(x, t) = mx + b

-m𝚫p ≈ f(p, t + 𝚫t) - f(p, t)

-dp𝚫p ≈ f(p, t + 𝚫t) - f(p, t)

108 of 220

Lucas-Kanade Optical Flow

mp - m𝚫p + b ≈ f(p, t + 𝚫t)

mp - m𝚫p + b - f(p, t) ≈ f(p, t + 𝚫t) - f(p, t)

Recall f(x, t) = mx + b

-m𝚫p ≈ f(p, t + 𝚫t) - f(p, t)

-dp𝚫p ≈ f(p, t + 𝚫t) - f(p, t)

Let’s drop the vector notation

109 of 220

Lucas-Kanade Optical Flow

mp - m𝚫p + b ≈ f(p, t + 𝚫t)

mp - m𝚫p + b - f(p, t) ≈ f(p, t + 𝚫t) - f(p, t)

Recall f(x, t) = mx + b

-m𝚫p ≈ f(p, t + 𝚫t) - f(p, t)

-dp𝚫p ≈ f(p, t + 𝚫t) - f(p, t)

-dx𝚫x + -dy𝚫y ≈ f((x,y), t + 𝚫t) - f((x,y), t)

110 of 220

Lucas-Kanade Optical Flow

-dx𝚫x + -dy𝚫y ≈ f((x,y), t + 𝚫t) - f((x,y), t)

111 of 220

Lucas-Kanade Optical Flow

-dx𝚫x + -dy𝚫y ≈ f((x,y), t + 𝚫t) - f((x,y), t)

Let’s clean up the notation a bit

dx*u + dy*v = It[x,y] - It+𝚫t[x,y]

112 of 220

Lucas-Kanade Optical Flow

-dx𝚫x + -dy𝚫y ≈ f((x,y), t + 𝚫t) - f((x,y), t)

Let’s clean up the notation a bit

dx*u + dy*v = It[x,y] - It+𝚫t[x,y]

113 of 220

Lucas-Kanade Optical Flow

-dx𝚫x + -dy𝚫y ≈ f((x,y), t + 𝚫t) - f((x,y), t)

Let’s clean up the notation a bit

dx*u + dy*v = It[x,y] - It+𝚫t[x,y]

114 of 220

Lucas-Kanade Optical Flow

-dx𝚫x + -dy𝚫y ≈ f((x,y), t + 𝚫t) - f((x,y), t)

Let’s clean up the notation a bit

dx*u + dy*v = It[x,y] - It+𝚫t[x,y]

115 of 220

Lucas-Kanade Optical Flow

-dx𝚫x + -dy𝚫y ≈ f((x,y), t + 𝚫t) - f((x,y), t)

Let’s clean up the notation a bit

dx*u + dy*v = It[x,y] - It+𝚫t[x,y]

116 of 220

Lucas-Kanade Optical Flow

-dx𝚫x + -dy𝚫y ≈ f((x,y), t + 𝚫t) - f((x,y), t)

Let’s clean up the notation a bit

dx*u + dy*v = It[x,y] - It+𝚫t[x,y]

117 of 220

Lucas-Kanade Optical Flow

-dx𝚫x + -dy𝚫y ≈ f((x,y), t + 𝚫t) - f((x,y), t)

Let’s clean up the notation a bit

dx*u + dy*v = It[x,y] - It+𝚫t[x,y]

Solve for u,v

118 of 220

Lucas-Kanade Optical Flow

-dx𝚫x + -dy𝚫y ≈ f((x,y), t + 𝚫t) - f((x,y), t)

Let’s clean up the notation a bit

dx*u + dy*v = It[x,y] - It+𝚫t[x,y]

Solve for u,v

1 equation, 2 unknowns

119 of 220

Lucas-Kanade Optical Flow

-dx𝚫x + -dy𝚫y ≈ f((x,y), t + 𝚫t) - f((x,y), t)

Let’s clean up the notation a bit

dx*u + dy*v = It[x,y] - It+𝚫t[x,y]

Solve for u,v

1 equation, 2 unknowns IMPOSSIBLE!

120 of 220

Lucas-Kanade Optical Flow

Let’s back up

dx*u + dy*v = It[x,y] - It+𝚫t[x,y]

121 of 220

Lucas-Kanade Optical Flow

Let’s back up

dx*u + dy*v = It[x,y] - It+𝚫t[x,y]

What does this mean?

122 of 220

Lucas-Kanade Optical Flow

dx*u + dy*v = It[x,y] - It+𝚫t[x,y]

123 of 220

Lucas-Kanade Optical Flow

dx*u + dy*v = It[x,y] - It+𝚫t[x,y]

124 of 220

Lucas-Kanade Optical Flow

dx*u + dy*v = It[x,y] - It+𝚫t[x,y]

dx

dy

125 of 220

Lucas-Kanade Optical Flow

dx*u + dy*v = It[x,y] - It+𝚫t[x,y]

dx

dy

Brightness/x-pixel

Brightness/y-pixel

126 of 220

Lucas-Kanade Optical Flow

dx*u + dy*v = It[x,y] - It+𝚫t[x,y]

dx

dy

u

v

127 of 220

Lucas-Kanade Optical Flow

dx*u + dy*v = It[x,y] - It+𝚫t[x,y]

dx

dy

u

v

128 of 220

Lucas-Kanade Optical Flow

dx*u + dy*v = It[x,y] - It+𝚫t[x,y]

dx

dy

u

v

Underspecified: Lots of u and v solve this equation.

129 of 220

Lucas-Kanade Optical Flow

dx*u + dy*v = It[x,y] - It+𝚫t[x,y]

dx

dy

u

v

Idea: What if we assume neighbors move together? I.e., they have the same u,v.

130 of 220

Lucas-Kanade Optical Flow

dx*u + dy*v = It[x,y] - It+𝚫t[x,y]

dx

dy

u

v

131 of 220

Lucas-Kanade Optical Flow

dx1*u + dy1*v = It[x1,y1] - It+𝚫t[x1,y1]

dx2*u + dy2*v = It[x2,y2] - It+𝚫t[x2,y2]

...

dx9*u + dy9*v = It[x9,y9] - It+𝚫t[x9,y9]

9 equations, 2 unknowns

132 of 220

Lucas-Kanade Optical Flow

dx1*u + dy1*v = It[x1,y1] - It+𝚫t[x1,y1]

dx2*u + dy2*v = It[x2,y2] - It+𝚫t[x2,y2]

...

dx9*u + dy9*v = It[x9,y9] - It+𝚫t[x9,y9]

9 equations, 2 unknowns

OVERSPECIFIED!

133 of 220

Lucas-Kanade Optical Flow

S𝚫p = T

S = dx1 dy1

dx2 dy2

...

dx9 dy9

𝚫p = u

v

T = It[x1,y1] - It+𝚫t[x1,y1]

It[x2,y2] - It+𝚫t[x2,y2]

...

It[x9,y9] - It+𝚫t[x9,y9]

134 of 220

Lucas-Kanade Optical Flow

S𝚫p = T

||S𝚫p - T||2 = 0

𝚫p = (STS)-1STT

S = dx1 dy1

dx2 dy2

...

dx9 dy9

𝚫p = u

v

T = It[x1,y1] - It+𝚫t[x1,y1]

It[x2,y2] - It+𝚫t[x2,y2]

...

It[x9,y9] - It+𝚫t[x9,y9]

Least-squares solution

135 of 220

Lucas-Kanade Optical Flow

S𝚫p = T

||S𝚫p - T||2 = 0

𝚫p = (STS)-1STT

S = dx1 dy1

dx2 dy2

...

dx9 dy9

𝚫p = u

v

T = It[x1,y1] - It+𝚫t[x1,y1]

It[x2,y2] - It+𝚫t[x2,y2]

...

It[x9,y9] - It+𝚫t[x9,y9]

What if this isn’t invertible?

136 of 220

Lucas-Kanade Optical Flow

S𝚫p = T

||S𝚫p - T||2 = 0

𝚫p = (STS)-1STT

S = dx1 dy1

dx2 dy2

...

dx9 dy9

𝚫p = u

v

T = It[x1,y1] - It+𝚫t[x1,y1]

It[x2,y2] - It+𝚫t[x2,y2]

...

It[x9,y9] - It+𝚫t[x9,y9]

What if this isn’t invertible?

E.G., no structure around (x,y)

137 of 220

Lucas-Kanade Optical Flow

Testing for invertibility: 𝚫p = (STS)-1STT

138 of 220

Lucas-Kanade Optical Flow

Testing for invertibility: 𝚫p = (STS)-1STT

STS is symmetric so it can be decomposed as

STS = U UT

𝛌1 0

0 𝛌2

139 of 220

Lucas-Kanade Optical Flow

Testing for invertibility: 𝚫p = (STS)-1STT

STS is symmetric so it can be decomposed as

STS = U UT

det(STS - 𝛌I) = 0

𝛌1 0

0 𝛌2

140 of 220

Lucas-Kanade Optical Flow

Testing for invertibility: 𝚫p = (STS)-1STT

STS is symmetric so it can be decomposed as

STS = U UT

det(STS - 𝛌I) = 0

= 0

𝛌1 0

0 𝛌2

𝚺i (dxi)2 - 𝛌 𝚺i (dxi)(dyi)

𝚺i (dxi)(dyi) 𝚺i (dyi)2 - 𝛌

141 of 220

Lucas-Kanade Optical Flow

Testing for invertibility: 𝚫p = (STS)-1STT

STS is symmetric so it can be decomposed as

STS = U UT

det(STS - 𝛌I) = 0

= 0

If either 𝛌1 or 𝛌2 are ≤0, S is not invertible and there is no solution!

𝛌1 0

0 𝛌2

𝚺i (dxi)2 - 𝛌 𝚺i (dxi)(dyi)

𝚺i (dxi)(dyi) 𝚺i (dyi)2 - 𝛌

142 of 220

Lucas-Kanade Optical Flow

How to pick (x,y)? What makes a good feature?

143 of 220

Lucas-Kanade Optical Flow

How to pick (x,y)? What makes a good feature?

144 of 220

Lucas-Kanade Optical Flow

How to pick (x,y)? What makes a good feature?

Bad

No gradient

145 of 220

Lucas-Kanade Optical Flow

How to pick (x,y)? What makes a good feature?

Bad

No gradient

146 of 220

Lucas-Kanade Optical Flow

How to pick (x,y)? What makes a good feature?

Bad

No gradient

Slightly better

Can’t detect vertical motion

147 of 220

Lucas-Kanade Optical Flow

How to pick (x,y)? What makes a good feature?

Bad

No gradient

Slightly better

Can’t detect vertical motion

148 of 220

Lucas-Kanade Optical Flow

How to pick (x,y)? What makes a good feature?

Bad

No gradient

Slightly better

Can’t detect vertical motion

Best

Vertical and horizontal motion

149 of 220

Lucas-Kanade Optical Flow

How to pick (x,y)? What makes a good feature?

CORNERS!

150 of 220

Lucas-Kanade Optical Flow

Demo

151 of 220

Lucas-Kanade Optical Flow

When does LK fail?

152 of 220

Lucas-Kanade Optical Flow

When does LK fail?

-Lighting changes

-Large movement

-Specularities

-No good features

-Aperture Problem

153 of 220

Lucas-Kanade Optical Flow

When does LK fail?

-Lighting changes

-Large movement

-Specularities

-No good features

-Aperture Problem

154 of 220

Lucas-Kanade Optical Flow

Aperture Problem

155 of 220

Lucas-Kanade Optical Flow

Aperture Problem

Line moving up and to the right

156 of 220

Lucas-Kanade Optical Flow

Aperture Problem

Line moving up and to the right

157 of 220

Lucas-Kanade Optical Flow

Aperture Problem

Line moving up and to the right

Line moving only to the right

158 of 220

Lucas-Kanade Optical Flow

Aperture Problem - Barber pole illusion

159 of 220

Lucas-Kanade Optical Flow

Aperture Problem - Barber pole illusion

160 of 220

Lucas-Kanade Optical Flow

Aperture Problem - Barber pole illusion

Actual movement to the right

161 of 220

Lucas-Kanade Optical Flow

Aperture Problem - Barber pole illusion

Apparent movement to the up and right

162 of 220

Homework 3

Implement Lucas-Kanade.

Come up with some *minor* way to improve it and test it.

163 of 220

Improving on LK: Iterative LK

dx1*u + dy1*v = It[x,y] - It+𝚫t[x,y]

164 of 220

Improving on LK: Iterative LK

dx1*u + dy1*v = It[x,y] - It+𝚫t[x,y]

dx2*u + dy2*v = It[x,y] - It+𝚫t[x + dx1,y + dy1]

Propagate dx1, dy1 forward and re-estimate

165 of 220

Improving on LK: Iterative LK

dx1*u + dy1*v = It[x,y] - It+𝚫t[x,y]

dx2*u + dy2*v = It[x,y] - It+𝚫t[x + dx1,y + dy1]

...

dxn*u + dyn*v = It[x,y] - It+𝚫t[x + dxn-1,y + dyn-1]

Propagate dx1, dy1 forward and re-estimate

Propagate dx2, dy2 forward and re-estimate

166 of 220

Improving on LK: Image Pyramids

167 of 220

Improving on LK: Image Pyramids

Image 1

Image 2

168 of 220

Improving on LK: Image Pyramids

Image 1

Image 2

169 of 220

Improving on LK: Image Pyramids

Image 1

Image 2

Run LK

Run LK

Run LK

Upsample

Upsample

170 of 220

Optical Flow

171 of 220

Optical Flow

Sparse vs Dense

172 of 220

Optical Flow

Sparse vs Dense

Compute flow only for specific features

Compute flow for all pixels

173 of 220

Optical Flow

Sparse vs Dense

Compute flow only for specific features

Compute flow for all pixels

Lucas-Kanade is technically a dense algorithm, but in practice only works on good feature points, i.e., it’s sparse.

174 of 220

Optical Flow

Is there a way to do dense optical flow?

175 of 220

Farnebäck Optical Flow

Farnebäck, Gunnar. "Two-frame motion estimation based on polynomial expansion." Scandinavian conference on Image analysis. Springer, Berlin, Heidelberg, 2003.

176 of 220

Farnebäck Optical Flow

Recall: We assumed the object moves by 𝚫p over 𝚫t

f(p - 𝚫p, t) = f(p, t + 𝚫t)

177 of 220

Farnebäck Optical Flow

Recall: We assumed the object moves by 𝚫p over 𝚫t

f(p - 𝚫p, t) = f(p, t + 𝚫t)

In LK we approximate this with a first-order Taylor Expansion

178 of 220

Farnebäck Optical Flow

Recall: We assumed the object moves by 𝚫p over 𝚫t

f(p - 𝚫p, t) = f(p, t + 𝚫t)

Instead, let’s approximate with a second-order Taylor Expansion

179 of 220

Quick & Dirty: Taylor Expansion

Approximate complex function with nth-order polynomial

f(x) ≈ 𝚺i aixi

For first-order i=1

≈ a1x1 + a0x0

≈ mx + b

x

f(x)

mx + b

180 of 220

Quick & Dirty: Taylor Expansion

Approximate complex function with nth-order polynomial

f(x) ≈ 𝚺i aixi

For second-order i=2

≈ a1x2 + a1x1 + a0x0

≈ ax2 + bx + c

x

f(x)

ax2 + bx + c

181 of 220

Quick & Dirty: Taylor Expansion

Approximate complex function with nth-order polynomial

f(x) ≈ 𝚺i aixi

For second-order i=2

≈ a1x2 + a1x1 + a0x0

≈ ax2 + bx + c

xTAx + bx + c

x

f(x)

ax2 + bx + c

Vector notation

182 of 220

Farnebäck Optical Flow

Recall: We assumed the object moves by 𝚫p over 𝚫t

f(p - 𝚫p, t) = f(p, t + 𝚫t)

Instead, let’s approximate with a second-order Taylor Expansion

183 of 220

Farnebäck Optical Flow

Recall: We assumed the object moves by 𝚫p over 𝚫t

f(p - 𝚫p, t) = f(p, t + 𝚫t)

Instead, let’s approximate with a second-order Taylor Expansion

(p-𝚫p)TAt(p-𝚫p) + bt(p-𝚫p) + ct = pTAt+𝚫tp + bt+𝚫tp + ct+𝚫t

184 of 220

Farnebäck Optical Flow

Recall: We assumed the object moves by 𝚫p over 𝚫t

f(p - 𝚫p, t) = f(p, t + 𝚫t)

Instead, let’s approximate with a second-order Taylor Expansion

(p-𝚫p)TAt(p-𝚫p) + bt(p-𝚫p) + ct = pTAt+𝚫tp + bt+𝚫tp + ct+𝚫t

Poly parameters from first image

Poly parameters from second image

185 of 220

Farnebäck Optical Flow

Recall: We assumed the object moves by 𝚫p over 𝚫t

f(p - 𝚫p, t) = f(p, t + 𝚫t)

Instead, let’s approximate with a second-order Taylor Expansion

(p-𝚫p)TAt(p-𝚫p) + bt(p-𝚫p) + ct = pTAt+𝚫tp + bt+𝚫tp + ct+𝚫t

pTAtp + (bt - 2At𝚫p)Tp + 𝚫pTAt𝚫p - bt𝚫p + ct = pTAt+𝚫tp + pbt+𝚫t + ct+𝚫t

186 of 220

Farnebäck Optical Flow

Recall: We assumed the object moves by 𝚫p over 𝚫t

f(p - 𝚫p, t) = f(p, t + 𝚫t)

Instead, let’s approximate with a second-order Taylor Expansion

(p-𝚫p)TAt(p-𝚫p) + bt(p-𝚫p) + ct = pTAt+𝚫tp + bt+𝚫tp + ct+𝚫t

pTAtp + (bt - 2At𝚫p)Tp + 𝚫pTAt𝚫p - bt𝚫p + ct = pTAt+𝚫tp + pbt+𝚫t + ct+𝚫t

187 of 220

Farnebäck Optical Flow

Recall: We assumed the object moves by 𝚫p over 𝚫t

f(p - 𝚫p, t) = f(p, t + 𝚫t)

Instead, let’s approximate with a second-order Taylor Expansion

(p-𝚫p)TAt(p-𝚫p) + bt(p-𝚫p) + ct = pTAt+𝚫tp + bt+𝚫tp + ct+𝚫t

pTAtp + (bt - 2At𝚫p)Tp + 𝚫pTAt𝚫p - bt𝚫p + ct = pTAt+𝚫tp + pbt+𝚫t + ct+𝚫t

At = At+𝚫t

188 of 220

Farnebäck Optical Flow

Recall: We assumed the object moves by 𝚫p over 𝚫t

f(p - 𝚫p, t) = f(p, t + 𝚫t)

Instead, let’s approximate with a second-order Taylor Expansion

(p-𝚫p)TAt(p-𝚫p) + bt(p-𝚫p) + ct = pTAt+𝚫tp + bt+𝚫tp + ct+𝚫t

pTAtp + (bt - 2At𝚫p)Tp + 𝚫pTAt𝚫p - bt𝚫p + ct = pTAt+𝚫tp + pbt+𝚫t + ct+𝚫t

At = At+𝚫t

189 of 220

Farnebäck Optical Flow

Recall: We assumed the object moves by 𝚫p over 𝚫t

f(p - 𝚫p, t) = f(p, t + 𝚫t)

Instead, let’s approximate with a second-order Taylor Expansion

(p-𝚫p)TAt(p-𝚫p) + bt(p-𝚫p) + ct = pTAt+𝚫tp + bt+𝚫tp + ct+𝚫t

pTAtp + (bt - 2At𝚫p)Tp + 𝚫pTAt𝚫p - bt𝚫p + ct = pTAt+𝚫tp + pbt+𝚫t + ct+𝚫t

At = At+𝚫t

bt+𝚫t = bt - 2At𝚫p

190 of 220

Farnebäck Optical Flow

Recall: We assumed the object moves by 𝚫p over 𝚫t

f(p - 𝚫p, t) = f(p, t + 𝚫t)

Instead, let’s approximate with a second-order Taylor Expansion

(p-𝚫p)TAt(p-𝚫p) + bt(p-𝚫p) + ct = pTAt+𝚫tp + bt+𝚫tp + ct+𝚫t

pTAtp + (bt - 2At𝚫p)Tp + 𝚫pTAt𝚫p - bt𝚫p + ct = pTAt+𝚫tp + pbt+𝚫t + ct+𝚫t

At = At+𝚫t

bt+𝚫t = bt - 2At𝚫p

191 of 220

Farnebäck Optical Flow

Recall: We assumed the object moves by 𝚫p over 𝚫t

f(p - 𝚫p, t) = f(p, t + 𝚫t)

Instead, let’s approximate with a second-order Taylor Expansion

(p-𝚫p)TAt(p-𝚫p) + bt(p-𝚫p) + ct = pTAt+𝚫tp + bt+𝚫tp + ct+𝚫t

pTAtp + (bt - 2At𝚫p)Tp + 𝚫pTAt𝚫p - bt𝚫p + ct = pTAt+𝚫tp + pbt+𝚫t + ct+𝚫t

At = At+𝚫t

bt+𝚫t = bt - 2At𝚫p

ct+𝚫t = 𝚫pTAt𝚫p - bt𝚫p + ct

192 of 220

Farnebäck Optical Flow

bt+𝚫t = bt - 2At𝚫p

193 of 220

Farnebäck Optical Flow

bt+𝚫t = bt - 2At𝚫p

Solve for 𝚫p

𝚫p = -½(At)-1(bt+𝚫t - bt)

194 of 220

Farnebäck Optical Flow

bt+𝚫t = bt - 2At𝚫p

Solve for 𝚫p

𝚫p = -½(At)-1(bt+𝚫t - bt)

A, b, and c can be solved pointwise using least squares over a small neighborhood like before

195 of 220

Farnebäck Optical Flow

bt+𝚫t = bt - 2At𝚫p

Solve for 𝚫p

𝚫p = -½(At)-1(bt+𝚫t - bt)

A, b, and c can be solved pointwise using least squares over a small neighborhood like before

196 of 220

Farnebäck Optical Flow

bt+𝚫t = bt - 2At𝚫p

Solve for 𝚫p

𝚫p = -½(At)-1(bt+𝚫t - bt)

A, b, and c can be solved pointwise using least squares over a small neighborhood like before

S = x1 y1

x2 y2

...

xk yk

T = I[x1,y1]

I[x2,y2]

...

I[xk,yk]

197 of 220

Farnebäck Optical Flow

bt+𝚫t = bt - 2At𝚫p

Solve for 𝚫p

𝚫p = -½(At)-1(bt+𝚫t - bt)

A, b, and c can be solved pointwise using least squares over a small neighborhood like before

S = x1 y1

x2 y2

...

xk yk

T = I[x1,y1]

I[x2,y2]

...

I[xk,yk]

SAST + bST + c = T

||SAST + bST + c - T||2 = 0

198 of 220

Farnebäck Optical Flow

bt+𝚫t = bt - 2At𝚫p

Solve for 𝚫p

𝚫p = -½(At)-1(bt+𝚫t - bt)

A, b, and c can be solved pointwise using least squares over a small neighborhood like before

S = x1 y1

x2 y2

...

xk yk

T = I[x1,y1]

I[x2,y2]

...

I[xk,yk]

SAST + bST + c = T

||SAST + bST + c - T||2 = 0

Quadratic Regression

199 of 220

Farnebäck Optical Flow

bt+𝚫t = bt - 2At𝚫p

Solve for 𝚫p

𝚫p = -½(At)-1(bt+𝚫t - bt)

Same problem as before:

What if it’s not invertible?

200 of 220

Farnebäck Optical Flow

bt+𝚫t = bt - 2At𝚫p

Let’s try a different approach:

201 of 220

Farnebäck Optical Flow

bt+𝚫t = bt - 2At𝚫p

Let’s try a different approach:

At𝚫p = -½(bt+𝚫t - bt)

202 of 220

Farnebäck Optical Flow

bt+𝚫t = bt - 2At𝚫p

Let’s try a different approach:

At𝚫p = -½(bt+𝚫t - bt)

At = At+𝚫t

203 of 220

Farnebäck Optical Flow

bt+𝚫t = bt - 2At𝚫p

Let’s try a different approach:

At𝚫p = -½(bt+𝚫t - bt)

At = At+𝚫t

At ≠ At+𝚫t

In reality not equal

204 of 220

Farnebäck Optical Flow

bt+𝚫t = bt - 2At𝚫p

Let’s try a different approach:

At𝚫p = -½(bt+𝚫t - bt)

At = At+𝚫t

At ≠ At+𝚫t

In reality not equal

A = ½(At + At+𝚫t)

Average together

205 of 220

Farnebäck Optical Flow

bt+𝚫t = bt - 2At𝚫p

Let’s try a different approach:

A𝚫p = -½(bt+𝚫t - bt)

206 of 220

Farnebäck Optical Flow

bt+𝚫t = bt - 2At𝚫p

Let’s try a different approach:

A𝚫p = -½(bt+𝚫t - bt)

𝚫b = -½(bt+𝚫t - bt)

207 of 220

Farnebäck Optical Flow

bt+𝚫t = bt - 2At𝚫p

Let’s try a different approach:

A𝚫p = 𝚫b

208 of 220

Farnebäck Optical Flow

bt+𝚫t = bt - 2At𝚫p

Let’s try a different approach:

A𝚫p = 𝚫b

Assume neighbors move together (just like LK)

209 of 220

Farnebäck Optical Flow

bt+𝚫t = bt - 2At𝚫p

Let’s try a different approach:

A𝚫p = 𝚫b

Assume neighbors move together (just like LK)

Ai𝚫p = 𝚫bi

210 of 220

Farnebäck Optical Flow

bt+𝚫t = bt - 2At𝚫p

Let’s try a different approach:

A𝚫p = 𝚫b

Assume neighbors move together (just like LK)

Ai𝚫p = 𝚫bi

Apply least squares

𝚺i wi||Ai𝚫p - 𝚫bi||2

211 of 220

Farnebäck Optical Flow

bt+𝚫t = bt - 2At𝚫p

Let’s try a different approach:

A𝚫p = 𝚫b

Assume neighbors move together (just like LK)

Ai𝚫p = 𝚫bi

Apply least squares

𝚺i wi||Ai𝚫p - 𝚫bi||2

Gaussian weight (so we care more about the center of the neighborhood than the edges)

212 of 220

Farnebäck Optical Flow

Still assuming 𝚫p is constant over the neighborhood, but not really true…

213 of 220

Farnebäck Optical Flow

Still assuming 𝚫p is constant over the neighborhood, but not really true…

Relax and assume instead 𝚫p is an affine function in the neighborhood.

214 of 220

Farnebäck Optical Flow

Still assuming 𝚫p is constant over the neighborhood, but not really true…

Relax and assume instead 𝚫p is an affine function in the neighborhood.

𝚫p =

a1 + a2x + a3y + a7x2 + a8xy

a4 + a5x + a6y + a7xy + a8y2

215 of 220

Farnebäck Optical Flow

Still assuming 𝚫p is constant over the neighborhood, but not really true…

Relax and assume instead 𝚫p is an affine function in the neighborhood.

𝚫p =

𝚫p = Sd

a1 + a2x + a3y + a7x2 + a8xy

a4 + a5x + a6y + a7xy + a8y2

S =

1 x y 0 0 0 x2 xy

0 0 0 1 x y xy y2

d =

a1

a2

a3

a4

a5

a6

a7

a8

216 of 220

Farnebäck Optical Flow

𝚺i wi||Ai𝚫p - 𝚫bi||2

217 of 220

Farnebäck Optical Flow

𝚺i wi||Ai𝚫p - 𝚫bi||2

𝚺i wi||AiSd - 𝚫bi||2

Plug in Sd for 𝚫p.

218 of 220

Farnebäck Optical Flow

𝚺i wi||Ai𝚫p - 𝚫bi||2

𝚺i wi||AiSd - 𝚫bi||2

d = 𝚺iwi(Si)T(Ai)TSi 𝚺iwi(Si)T(Ai)T𝚫bi

Plug in Sd for 𝚫p.

Least squares solve for d

-1

219 of 220

Farnebäck Optical Flow

𝚺i wi||Ai𝚫p - 𝚫bi||2

𝚺i wi||AiSd - 𝚫bi||2

d = 𝚺iwi(Si)T(Ai)TSi 𝚺iwi(Si)T(Ai)T𝚫bi

Plug in Sd for 𝚫p.

Least squares solve for d

-1

Same inverse problem, but in practice less of an issue

220 of 220

Farnebäck Optical Flow

Demo