Foundations of Data Analysis April 29, 2021

88
Convolution Foundations of Data Analysis April 29, 2021

Transcript of Foundations of Data Analysis April 29, 2021

Page 1: Foundations of Data Analysis April 29, 2021

Convolution

Foundations of Data Analysis

April 29, 2021

Page 2: Foundations of Data Analysis April 29, 2021

Spatial Filters

DefinitionA spatial filter is an image operation where each pixelvalue I(u, v) is changed by a function of the intensitiesof pixels in a neighborhood of (u, v).

Page 3: Foundations of Data Analysis April 29, 2021

What Spatial Filters Can Do

Blurring/Smoothing

Page 4: Foundations of Data Analysis April 29, 2021

What Spatial Filters Can Do

Sharpening

Page 5: Foundations of Data Analysis April 29, 2021

What Spatial Filters Can Do

Weird Stuff

Page 6: Foundations of Data Analysis April 29, 2021

Example: The Mean of a NeighborhoodConsider taking the mean in a 3 × 3 neighborhood:

uu-1 u+1

v-1

v

v+1

I′(u, v) =19

1∑i=−1

1∑j=−1

I(u + i, v + j)

Page 7: Foundations of Data Analysis April 29, 2021

Example: The Mean of a NeighborhoodConsider taking the mean in a 3 × 3 neighborhood:

uu-1 u+1

v-1

v

v+1

I′(u, v) =19

1∑i=−1

1∑j=−1

I(u + i, v + j)

Page 8: Foundations of Data Analysis April 29, 2021

How a Linear Spatial Filter Works

H is the filter “kernel” or “matrix”

For the neighborhood mean: H(i, j) = 19

1 1 11 1 11 1 1

Page 9: Foundations of Data Analysis April 29, 2021

How a Linear Spatial Filter Works

H is the filter “kernel” or “matrix”

For the neighborhood mean: H(i, j) = 19

1 1 11 1 11 1 1

Page 10: Foundations of Data Analysis April 29, 2021

General Filter Equation

Notice that the kernel H is just a small image!

Let H : RH → [0,K − 1]

I′(u, v) =∑

(i,j)∈RH

I(u + i, v + j) · H(i, j)

This is known as a correlation of I and H

Page 11: Foundations of Data Analysis April 29, 2021

What Does This Filter Do?

100

00 0 0

00

Identity function (leaves image alone)

Page 12: Foundations of Data Analysis April 29, 2021

What Does This Filter Do?

100

00 0 0

00

Identity function (leaves image alone)

Page 13: Foundations of Data Analysis April 29, 2021

What Does This Filter Do?

111

11 1 1

11

19

Mean (averages neighborhood)

Page 14: Foundations of Data Analysis April 29, 2021

What Does This Filter Do?

111

11 1 1

11

19

Mean (averages neighborhood)

Page 15: Foundations of Data Analysis April 29, 2021

What Does This Filter Do?

000

00 0 0

10

Shift left by one pixel

Page 16: Foundations of Data Analysis April 29, 2021

What Does This Filter Do?

000

00 0 0

10

Shift left by one pixel

Page 17: Foundations of Data Analysis April 29, 2021

What Does This Filter Do?

17-1-1

-1-1 -1 -1

-1-1

19

Sharpen (identity minus mean filter)

Page 18: Foundations of Data Analysis April 29, 2021

What Does This Filter Do?

17-1-1

-1-1 -1 -1

-1-1

19

Sharpen (identity minus mean filter)

Page 19: Foundations of Data Analysis April 29, 2021

Filter Normalization

I Notice that all of our filter examples sum up to one

I Multiplying all entries in H by a constant will causethe image to be multiplied by that constant

I To keep the overall brightness constant, we need Hto sum to one

I′(u, v) =∑i, j

I(u + i, v + j) · (cH(i, j))

= c∑i, j

I(u + i, v + j) · H(i, j)

Page 20: Foundations of Data Analysis April 29, 2021

Filter Normalization

I Notice that all of our filter examples sum up to oneI Multiplying all entries in H by a constant will cause

the image to be multiplied by that constant

I To keep the overall brightness constant, we need Hto sum to one

I′(u, v) =∑i, j

I(u + i, v + j) · (cH(i, j))

= c∑i, j

I(u + i, v + j) · H(i, j)

Page 21: Foundations of Data Analysis April 29, 2021

Filter Normalization

I Notice that all of our filter examples sum up to oneI Multiplying all entries in H by a constant will cause

the image to be multiplied by that constantI To keep the overall brightness constant, we need H

to sum to one

I′(u, v) =∑i, j

I(u + i, v + j) · (cH(i, j))

= c∑i, j

I(u + i, v + j) · H(i, j)

Page 22: Foundations of Data Analysis April 29, 2021

Effect of Filter Size

Mean Filters:

Original 7 × 7 15 × 15 41 × 41

Page 23: Foundations of Data Analysis April 29, 2021

What To Do At The Boundary?

Page 24: Foundations of Data Analysis April 29, 2021

What To Do At The Boundary?

I Crop

I PadI ExtendI Wrap

Page 25: Foundations of Data Analysis April 29, 2021

What To Do At The Boundary?

I CropI Pad

I ExtendI Wrap

Page 26: Foundations of Data Analysis April 29, 2021

What To Do At The Boundary?

I CropI PadI Extend

I Wrap

Page 27: Foundations of Data Analysis April 29, 2021

What To Do At The Boundary?

I CropI PadI ExtendI Wrap

Page 28: Foundations of Data Analysis April 29, 2021

ConvolutionDefinitionConvolution of an image I by a kernel H is given by

I′(u, v) =∑

(i,j)∈RH

I(u− i, v− j) · H(i, j)

This is denoted: I′ = I ∗ H

I Notice this is the same as correlation with H, butwith negative signs on the I indices

I Equivalent to vertical and horizontal flipping of H:

I′(u, v) =∑

(−i,−j)∈RH

I(u + i, v + j) · H(−i,−j)

Page 29: Foundations of Data Analysis April 29, 2021

ConvolutionDefinitionConvolution of an image I by a kernel H is given by

I′(u, v) =∑

(i,j)∈RH

I(u− i, v− j) · H(i, j)

This is denoted: I′ = I ∗ H

I Notice this is the same as correlation with H, butwith negative signs on the I indices

I Equivalent to vertical and horizontal flipping of H:

I′(u, v) =∑

(−i,−j)∈RH

I(u + i, v + j) · H(−i,−j)

Page 30: Foundations of Data Analysis April 29, 2021

ConvolutionDefinitionConvolution of an image I by a kernel H is given by

I′(u, v) =∑

(i,j)∈RH

I(u− i, v− j) · H(i, j)

This is denoted: I′ = I ∗ H

I Notice this is the same as correlation with H, butwith negative signs on the I indices

I Equivalent to vertical and horizontal flipping of H:

I′(u, v) =∑

(−i,−j)∈RH

I(u + i, v + j) · H(−i,−j)

Page 31: Foundations of Data Analysis April 29, 2021

Linear Operators

DefinitionA linear operator F on an image is a mapping from oneimage to another, I′ = F(I), that satisfies:

1. F(cI) = cF(I),2. F(I1 + I2) = F(I1) + F(I2),

where I, I1, I2 are images, and c is a constant.

Both correlation and convolution are linear operators

Page 32: Foundations of Data Analysis April 29, 2021

Infinite Image Domains

Let’s define our image and kernel domains to be infinite:

Ω = Z× Z

Remember Z = . . . ,−2,−1, 0, 1, 2, . . .

Now convolution is an infinite sum:

I′(u, v) =∞∑

i=−∞

∞∑i=−∞

I(u− i, v− j) · H(i, j)

This is denoted I′ = I ∗ H.

Page 33: Foundations of Data Analysis April 29, 2021

Infinite Image Domains

Let’s define our image and kernel domains to be infinite:

Ω = Z× Z

Remember Z = . . . ,−2,−1, 0, 1, 2, . . .

Now convolution is an infinite sum:

I′(u, v) =∞∑

i=−∞

∞∑i=−∞

I(u− i, v− j) · H(i, j)

This is denoted I′ = I ∗ H.

Page 34: Foundations of Data Analysis April 29, 2021

Infinite Image Domains

The infinite image domain Ω = Z× Z is just a trick tomake the theory of convolution work out.

We can still imagine that the image is defined on abounded (finite) domain, [0,w]× [0, h], and is set tozero outside of this.

Page 35: Foundations of Data Analysis April 29, 2021

Infinite Image Domains

The infinite image domain Ω = Z× Z is just a trick tomake the theory of convolution work out.

We can still imagine that the image is defined on abounded (finite) domain, [0,w]× [0, h], and is set tozero outside of this.

Page 36: Foundations of Data Analysis April 29, 2021

Properties of Convolution

Commutativity:

I ∗ H = H ∗ I

This means that we can think of the image as the kerneland the kernel as the image and get the same result.

In other words, we can leave the image fixed and slidethe kernel or leave the kernel fixed and slide the image.

Page 37: Foundations of Data Analysis April 29, 2021

Properties of Convolution

Commutativity:

I ∗ H = H ∗ I

This means that we can think of the image as the kerneland the kernel as the image and get the same result.

In other words, we can leave the image fixed and slidethe kernel or leave the kernel fixed and slide the image.

Page 38: Foundations of Data Analysis April 29, 2021

Properties of Convolution

Commutativity:

I ∗ H = H ∗ I

This means that we can think of the image as the kerneland the kernel as the image and get the same result.

In other words, we can leave the image fixed and slidethe kernel or leave the kernel fixed and slide the image.

Page 39: Foundations of Data Analysis April 29, 2021

Properties of Convolution

Associativity:

(I ∗ H1) ∗ H2 = I ∗ (H1 ∗ H2)

This means that we can apply H1 to I followed by H2, orwe can convolve the kernels H2 ∗ H1 and then apply theresulting kernel to I.

Page 40: Foundations of Data Analysis April 29, 2021

Properties of Convolution

Associativity:

(I ∗ H1) ∗ H2 = I ∗ (H1 ∗ H2)

This means that we can apply H1 to I followed by H2, orwe can convolve the kernels H2 ∗ H1 and then apply theresulting kernel to I.

Page 41: Foundations of Data Analysis April 29, 2021

Properties of Convolution

Linearity:(a · I) ∗ H = a · (I ∗ H)

(I1 + I2) ∗ H = (I1 ∗ H) + (I2 ∗ H)

This means that we can multiply an image by a constantbefore or after convolution, and we can add two imagesbefore or after convolution and get the same results.

Page 42: Foundations of Data Analysis April 29, 2021

Properties of Convolution

Linearity:(a · I) ∗ H = a · (I ∗ H)

(I1 + I2) ∗ H = (I1 ∗ H) + (I2 ∗ H)

This means that we can multiply an image by a constantbefore or after convolution, and we can add two imagesbefore or after convolution and get the same results.

Page 43: Foundations of Data Analysis April 29, 2021

Properties of Convolution

Shift-Invariance:Let S be the operator that shifts an image I:

S(I)(u, v) = I(u + a, v + b)

ThenS(I ∗ H) = S(I) ∗ H

This means that we can convolve I and H and then shiftthe result, or we can shift I and then convolve it with H.

Page 44: Foundations of Data Analysis April 29, 2021

Properties of Convolution

Shift-Invariance:Let S be the operator that shifts an image I:

S(I)(u, v) = I(u + a, v + b)

ThenS(I ∗ H) = S(I) ∗ H

This means that we can convolve I and H and then shiftthe result, or we can shift I and then convolve it with H.

Page 45: Foundations of Data Analysis April 29, 2021

Properties of Convolution

Theorem: The only shift-invariant, linear operators onimages are convolutions.

Page 46: Foundations of Data Analysis April 29, 2021

Computational Complexity of Convolution

If my image I has size M × N and my kernel H has size(2R + 1)× (2R + 1), then what is the complexity ofconvolution?

I′(u, v) =R∑

i=−R

R∑j=−R

I(u− i, v− j) · H(i, j)

Answer: O(MN(2R + 1)(2R + 1)) = O(MNR2).Or, if we consider the image size fixed, O(R2).

Page 47: Foundations of Data Analysis April 29, 2021

Computational Complexity of Convolution

If my image I has size M × N and my kernel H has size(2R + 1)× (2R + 1), then what is the complexity ofconvolution?

I′(u, v) =R∑

i=−R

R∑j=−R

I(u− i, v− j) · H(i, j)

Answer: O(MN(2R + 1)(2R + 1)) = O(MNR2).

Or, if we consider the image size fixed, O(R2).

Page 48: Foundations of Data Analysis April 29, 2021

Computational Complexity of Convolution

If my image I has size M × N and my kernel H has size(2R + 1)× (2R + 1), then what is the complexity ofconvolution?

I′(u, v) =R∑

i=−R

R∑j=−R

I(u− i, v− j) · H(i, j)

Answer: O(MN(2R + 1)(2R + 1)) = O(MNR2).Or, if we consider the image size fixed, O(R2).

Page 49: Foundations of Data Analysis April 29, 2021

Which is More Expensive?

The following both shift the image 10 pixels to the left:

1. Convolve with a 21× 21 shift operator (all zeroswith a 1 on the right edge)

2. Repeatedly convolve with a 3 × 3 shift operator 10times

The first method requires 212 · wh = 441 · wh.The second method requires (9 · wh) · 10 = 90 · wh.

Page 50: Foundations of Data Analysis April 29, 2021

Which is More Expensive?

The following both shift the image 10 pixels to the left:

1. Convolve with a 21× 21 shift operator (all zeroswith a 1 on the right edge)

2. Repeatedly convolve with a 3 × 3 shift operator 10times

The first method requires 212 · wh = 441 · wh.

The second method requires (9 · wh) · 10 = 90 · wh.

Page 51: Foundations of Data Analysis April 29, 2021

Which is More Expensive?

The following both shift the image 10 pixels to the left:

1. Convolve with a 21× 21 shift operator (all zeroswith a 1 on the right edge)

2. Repeatedly convolve with a 3 × 3 shift operator 10times

The first method requires 212 · wh = 441 · wh.The second method requires (9 · wh) · 10 = 90 · wh.

Page 52: Foundations of Data Analysis April 29, 2021

Some More Filters

Box Gaussian Laplace

Page 53: Foundations of Data Analysis April 29, 2021

Edge Detection

Page 54: Foundations of Data Analysis April 29, 2021

What is an Edge?

Image Value vs X-Position

0

20

40

60

80

100

120

140

160

0 20 40 60 80 100 120

An abrupt transition in intensity between two regions

Page 55: Foundations of Data Analysis April 29, 2021

What is an Edge?

Image Value vs X-Position

0

20

40

60

80

100

120

140

160

0 20 40 60 80 100 120

An abrupt transition in intensity between two regions

Page 56: Foundations of Data Analysis April 29, 2021

What is an Edge?

Image X-Derivative vs X-Position

-80

-60

-40

-20

0

20

40

0 20 40 60 80 100 120

Image derivatives are high (or low) at edges

Page 57: Foundations of Data Analysis April 29, 2021

Review: Derivative of a Function

Given a function f : R→ R, its derivative is defined as

dfdx

(x) = limε→0

f (x + ε)− f (x)

ε

Derivative of f is the slope ofthe tangent to the graph of f

Page 58: Foundations of Data Analysis April 29, 2021

Review: Derivative of a Function

Given a function f : R→ R, its derivative is defined as

dfdx

(x) = limε→0

f (x + ε)− f (x)

ε

Derivative of f is the slope ofthe tangent to the graph of f

Page 59: Foundations of Data Analysis April 29, 2021

Derivatives of Discrete Functions

x

f(x)

Discrete function defined on integer values of x

Page 60: Foundations of Data Analysis April 29, 2021

Derivatives of Discrete Functions

x

f(x)

Slopes (derivatives) don’t match on left and right

Page 61: Foundations of Data Analysis April 29, 2021

Derivatives of Discrete Functions

x

f(x)

Instead take the average of the two (or secant)

Page 62: Foundations of Data Analysis April 29, 2021

Derivatives of Discrete Functions

x

f(x)

Instead take the average of the two (or secant)

Page 63: Foundations of Data Analysis April 29, 2021

Finite Differences

Forward Difference

∆+ f (x) = f (x + 1)− f (x) right slope

Backward Difference

∆− f (x) = f (x)− f (x− 1) left slope

Central Difference

∆ f (x) =12

( f (x + 1)− f (x− 1)) average slope

Page 64: Foundations of Data Analysis April 29, 2021

Finite Differences

Forward Difference

∆+ f (x) = f (x + 1)− f (x) right slope

Backward Difference

∆− f (x) = f (x)− f (x− 1) left slope

Central Difference

∆ f (x) =12

( f (x + 1)− f (x− 1)) average slope

Page 65: Foundations of Data Analysis April 29, 2021

Finite Differences

Forward Difference

∆+ f (x) = f (x + 1)− f (x) right slope

Backward Difference

∆− f (x) = f (x)− f (x− 1) left slope

Central Difference

∆ f (x) =12

( f (x + 1)− f (x− 1)) average slope

Page 66: Foundations of Data Analysis April 29, 2021

Finite Differences as Convolutions

Forward Difference

∆+ f (x) = f (x + 1)− f (x)

Take a convolution kernel: H = [1 −1 0]

∆+ f = f ∗ H

(Remember that the kernel H is flipped in convolution)

Page 67: Foundations of Data Analysis April 29, 2021

Finite Differences as Convolutions

Forward Difference

∆+ f (x) = f (x + 1)− f (x)

Take a convolution kernel: H = [1 −1 0]

∆+ f = f ∗ H

(Remember that the kernel H is flipped in convolution)

Page 68: Foundations of Data Analysis April 29, 2021

Finite Differences as Convolutions

Forward Difference

∆+ f (x) = f (x + 1)− f (x)

Take a convolution kernel: H = [1 −1 0]

∆+ f = f ∗ H

(Remember that the kernel H is flipped in convolution)

Page 69: Foundations of Data Analysis April 29, 2021

Finite Differences as Convolutions

Central Difference

∆ f (x) =12

( f (x + 1)− f (x− 1))

Convolution kernel here is: H =[ 1

2 0 − 12

]∆ f (x) = f ∗ H

Notice: Derivative kernels sum to zero!

Page 70: Foundations of Data Analysis April 29, 2021

Finite Differences as Convolutions

Central Difference

∆ f (x) =12

( f (x + 1)− f (x− 1))

Convolution kernel here is: H =[ 1

2 0 − 12

]∆ f (x) = f ∗ H

Notice: Derivative kernels sum to zero!

Page 71: Foundations of Data Analysis April 29, 2021

Finite Differences as Convolutions

Central Difference

∆ f (x) =12

( f (x + 1)− f (x− 1))

Convolution kernel here is: H =[ 1

2 0 − 12

]∆ f (x) = f ∗ H

Notice: Derivative kernels sum to zero!

Page 72: Foundations of Data Analysis April 29, 2021

Derivatives of Images

I Images have two parameters: I(x, y)

I We can take derivatives with respect to x or yI Central differences:

∆x I = I ∗ Hx, and ∆y I = I ∗ Hy,

where Hx = [0.5 0 −0.5] and Hy =

−0.50

0.5

Page 73: Foundations of Data Analysis April 29, 2021

Derivatives of Images

I Images have two parameters: I(x, y)

I We can take derivatives with respect to x or y

I Central differences:

∆x I = I ∗ Hx, and ∆y I = I ∗ Hy,

where Hx = [0.5 0 −0.5] and Hy =

−0.50

0.5

Page 74: Foundations of Data Analysis April 29, 2021

Derivatives of Images

I Images have two parameters: I(x, y)

I We can take derivatives with respect to x or yI Central differences:

∆x I = I ∗ Hx, and ∆y I = I ∗ Hy,

where Hx = [0.5 0 −0.5] and Hy =

−0.50

0.5

Page 75: Foundations of Data Analysis April 29, 2021

Derivatives of Images

x-derivative using central difference:

∗[ 1

2 0 − 12

]=

Page 76: Foundations of Data Analysis April 29, 2021

Derivatives of Images

x-derivative using central difference:

∗[ 1

2 0 − 12

]=

Page 77: Foundations of Data Analysis April 29, 2021

Derivatives of Images

x-derivative using central difference:

∗[ 1

2 0 − 12

]=

Page 78: Foundations of Data Analysis April 29, 2021

Derivatives of Images

y-derivative using central difference:

0.50−0.5

=

Page 79: Foundations of Data Analysis April 29, 2021

Derivatives of Images

y-derivative using central difference:

0.50−0.5

=

Page 80: Foundations of Data Analysis April 29, 2021

Derivatives of Images

y-derivative using central difference:

0.50−0.5

=

Page 81: Foundations of Data Analysis April 29, 2021

Combining x and y Derivatives

The discrete gradient of I(x, y) is the 2D vector:

∇I(x, y) =

[∆x I(x, y)∆y I(x, y)

]

The gradient magnitude is

‖∇I(x, y)‖ =√

(∆x I(x, y))2 + (∆y I(x, y))2

Page 82: Foundations of Data Analysis April 29, 2021

Combining x and y Derivatives

The discrete gradient of I(x, y) is the 2D vector:

∇I(x, y) =

[∆x I(x, y)∆y I(x, y)

]

The gradient magnitude is

‖∇I(x, y)‖ =√

(∆x I(x, y))2 + (∆y I(x, y))2

Page 83: Foundations of Data Analysis April 29, 2021

Image Gradient

I Gradient points in direction ofmaximal increasing intensity

I Length (magnitude) ofgradient equals amount ofchange in that direction

I Gradient is perpendicular (90degrees) to edge contour

Page 84: Foundations of Data Analysis April 29, 2021

Image Gradient

I Gradient points in direction ofmaximal increasing intensity

I Length (magnitude) ofgradient equals amount ofchange in that direction

I Gradient is perpendicular (90degrees) to edge contour

Page 85: Foundations of Data Analysis April 29, 2021

Image Gradient

I Gradient points in direction ofmaximal increasing intensity

I Length (magnitude) ofgradient equals amount ofchange in that direction

I Gradient is perpendicular (90degrees) to edge contour

Page 86: Foundations of Data Analysis April 29, 2021

Convolutional Neural Networks(CNNs)

Page 87: Foundations of Data Analysis April 29, 2021

Learning a Filter

w1 w2 w3

w4 w5 w6

w7 w8 w9

?

Filter consists of weights that need to be learned.

Page 88: Foundations of Data Analysis April 29, 2021

Convolutional Neural Networks

http://towardsdatascience.com/