spatial filtering and convolution...spatial filtering apply a filter (also sometimes called a...

40
Spatial Filtering and Convolution

Upload: others

Post on 01-Nov-2020

14 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Spatial Filtering and Convolution...Spatial Filtering apply a filter (also sometimes called a kernel or mask) to an image a new pixel value is calculated, one pixel at a time the

Spatial Filtering and Convolution

Page 2: Spatial Filtering and Convolution...Spatial Filtering apply a filter (also sometimes called a kernel or mask) to an image a new pixel value is calculated, one pixel at a time the

Spatial Filtering

apply a filter (also sometimes called a kernel or mask) to an image

Page 3: Spatial Filtering and Convolution...Spatial Filtering apply a filter (also sometimes called a kernel or mask) to an image a new pixel value is calculated, one pixel at a time the

Spatial Filtering

apply a filter (also sometimes called a kernel or mask) to an image

a new pixel value is calculated, one pixel at a time

Page 4: Spatial Filtering and Convolution...Spatial Filtering apply a filter (also sometimes called a kernel or mask) to an image a new pixel value is calculated, one pixel at a time the

Spatial Filtering

apply a filter (also sometimes called a kernel or mask) to an image

a new pixel value is calculated, one pixel at a time

the neighbouring pixels influence the result

Page 5: Spatial Filtering and Convolution...Spatial Filtering apply a filter (also sometimes called a kernel or mask) to an image a new pixel value is calculated, one pixel at a time the
Page 6: Spatial Filtering and Convolution...Spatial Filtering apply a filter (also sometimes called a kernel or mask) to an image a new pixel value is calculated, one pixel at a time the
Page 7: Spatial Filtering and Convolution...Spatial Filtering apply a filter (also sometimes called a kernel or mask) to an image a new pixel value is calculated, one pixel at a time the

New Pixel Value

the pixel at location (x, y) in the new image will have a value:

Page 8: Spatial Filtering and Convolution...Spatial Filtering apply a filter (also sometimes called a kernel or mask) to an image a new pixel value is calculated, one pixel at a time the

New Pixel Value

the pixel at location (x, y) in the new image will have a value:

s(x, y) = w1 f(x-1, y-1) + w2 f(x-1, y) + w3 f(x-1, y+1) + w4 f(x, y-1) + w5 f(x, y) + w6 f(x, y+1) + w7 f(x+1, y-1) + w8 f(x+1, y) + w9 f(x+1, y+1)

Page 9: Spatial Filtering and Convolution...Spatial Filtering apply a filter (also sometimes called a kernel or mask) to an image a new pixel value is calculated, one pixel at a time the

New Image

slide the mask over by one pixel and repeat

NOTE: original image requires a 1-pixel border be added or else the new image will be smaller than the original

Page 10: Spatial Filtering and Convolution...Spatial Filtering apply a filter (also sometimes called a kernel or mask) to an image a new pixel value is calculated, one pixel at a time the

ExampleFind the new pixel value...

1 1 1

1 1 1

1 1 1

135 146 133 134 141

140 159 148 156 160

137 147 133 123 120

137 138 129 122 120

139 130 125 119 113

1/9

The mask

The original image

Multiply the values in the mask with the corresponding values in the image and add them together.

Page 11: Spatial Filtering and Convolution...Spatial Filtering apply a filter (also sometimes called a kernel or mask) to an image a new pixel value is calculated, one pixel at a time the

ExampleFind the new pixel value...

1 1 1

1 1 1

1 1 1

135 146 133 134 141

140 159 148 156 160

137 147 133 123 120

137 138 129 122 120

139 130 125 119 113

1/9

The mask

The original imageNormalization factor. Equal to 1 over the sum of the elements in the mask.

Multiply the values in the mask with the corresponding values in the image and add them together.

Page 12: Spatial Filtering and Convolution...Spatial Filtering apply a filter (also sometimes called a kernel or mask) to an image a new pixel value is calculated, one pixel at a time the

ExampleFind the new pixel value...

1 1 1

1 1 1

1 1 1

135 146 133 134 141

140 159 148 156 160

137 147 133 123 120

137 138 129 122 120

139 130 125 119 113

1/9

The mask

The original image

Multiply the values in the mask with the corresponding values in the image and add them together.

Page 13: Spatial Filtering and Convolution...Spatial Filtering apply a filter (also sometimes called a kernel or mask) to an image a new pixel value is calculated, one pixel at a time the

ExampleFind the new pixel value...

1 1 1

1 1 1

1 1 1

135 146 133 134 141

140 159 148 156 160

137 147 133 123 120

137 138 129 122 120

139 130 125 119 113

1/9

The mask

The original image

Multiply the values in the mask with the corresponding values in the image and add them together.

Page 14: Spatial Filtering and Convolution...Spatial Filtering apply a filter (also sometimes called a kernel or mask) to an image a new pixel value is calculated, one pixel at a time the

ExampleFind the new pixel value...

1 1 1

1 1 1

1 1 1

135 146 133 134 141

140 159 148 156 160

137 147 133 123 120

137 138 129 122 120

139 130 125 119 113

1/9

The mask

The original image

Multiply the values in the mask with the corresponding values in the image and add them together.

Page 15: Spatial Filtering and Convolution...Spatial Filtering apply a filter (also sometimes called a kernel or mask) to an image a new pixel value is calculated, one pixel at a time the

ExampleFind the new pixel value...

1 1 1

1 1 1

1 1 1

135 146 133 134 141

140 159 148 156 160

137 147 133 123 120

137 138 129 122 120

139 130 125 119 113

1/9

The mask

The original image

Multiply the values in the mask with the corresponding values in the image and add them together.

Page 16: Spatial Filtering and Convolution...Spatial Filtering apply a filter (also sometimes called a kernel or mask) to an image a new pixel value is calculated, one pixel at a time the

ExampleFind the new pixel value...

1 1 1

1 1 1

1 1 1

135 146 133 134 141

140 159 148 156 160

137 147 133 123 120

137 138 129 122 120

139 130 125 119 113

1/9

The mask

The original image

Multiply the values in the mask with the corresponding values in the image and add them together.

Page 17: Spatial Filtering and Convolution...Spatial Filtering apply a filter (also sometimes called a kernel or mask) to an image a new pixel value is calculated, one pixel at a time the

ExampleFind the new pixel value...

1 1 1

1 1 1

1 1 1

135 146 133 134 141

140 159 148 156 160

137 147 133 123 120

137 138 129 122 120

139 130 125 119 113

1/9

The mask

The original image

Multiply the values in the mask with the corresponding values in the image and add them together.

Page 18: Spatial Filtering and Convolution...Spatial Filtering apply a filter (also sometimes called a kernel or mask) to an image a new pixel value is calculated, one pixel at a time the

ExampleFind the new pixel value...

1 1 1

1 1 1

1 1 1

135 146 133 134 141

140 159 148 156 160

137 147 133 123 120

137 138 129 122 120

139 130 125 119 113

1/9

The mask

The original image

Multiply the values in the mask with the corresponding values in the image and add them together.

Page 19: Spatial Filtering and Convolution...Spatial Filtering apply a filter (also sometimes called a kernel or mask) to an image a new pixel value is calculated, one pixel at a time the

ExampleFind the new pixel value...

1 1 1

1 1 1

1 1 1

135 146 133 134 141

140 159 148 156 160

137 147 133 123 120

137 138 129 122 120

139 130 125 119 113

1/9

The mask

The original image

Multiply the values in the mask with the corresponding values in the image and add them together.

Page 20: Spatial Filtering and Convolution...Spatial Filtering apply a filter (also sometimes called a kernel or mask) to an image a new pixel value is calculated, one pixel at a time the

ExampleFind the new pixel value...

1 1 1

1 1 1

1 1 1

135 146 133 134 141

140 159 148 156 160

137 147 133 123 120

137 138 129 122 120

139 130 125 119 113

1/9

The mask

The original image

Multiply the values in the mask with the corresponding values in the image and add them together.

Page 21: Spatial Filtering and Convolution...Spatial Filtering apply a filter (also sometimes called a kernel or mask) to an image a new pixel value is calculated, one pixel at a time the

Example

142 142 139

141 139 135

135 130 123

The result

Page 22: Spatial Filtering and Convolution...Spatial Filtering apply a filter (also sometimes called a kernel or mask) to an image a new pixel value is calculated, one pixel at a time the

What did you just do?

1 1 1

1 1 1

1 1 1

1/9

Page 23: Spatial Filtering and Convolution...Spatial Filtering apply a filter (also sometimes called a kernel or mask) to an image a new pixel value is calculated, one pixel at a time the

What did you just do?

this is the averaging mask!

1 1 1

1 1 1

1 1 1

1/9

Page 24: Spatial Filtering and Convolution...Spatial Filtering apply a filter (also sometimes called a kernel or mask) to an image a new pixel value is calculated, one pixel at a time the

What did you just do?

this is the averaging mask!

the result is to smooth out the image

1 1 1

1 1 1

1 1 1

1/9

Page 25: Spatial Filtering and Convolution...Spatial Filtering apply a filter (also sometimes called a kernel or mask) to an image a new pixel value is calculated, one pixel at a time the

Original image

Page 26: Spatial Filtering and Convolution...Spatial Filtering apply a filter (also sometimes called a kernel or mask) to an image a new pixel value is calculated, one pixel at a time the

Averaged image

Page 27: Spatial Filtering and Convolution...Spatial Filtering apply a filter (also sometimes called a kernel or mask) to an image a new pixel value is calculated, one pixel at a time the
Page 28: Spatial Filtering and Convolution...Spatial Filtering apply a filter (also sometimes called a kernel or mask) to an image a new pixel value is calculated, one pixel at a time the

this operator is known as convolution

Page 29: Spatial Filtering and Convolution...Spatial Filtering apply a filter (also sometimes called a kernel or mask) to an image a new pixel value is calculated, one pixel at a time the

this operator is known as convolution

one convolves an image with a filter

Page 30: Spatial Filtering and Convolution...Spatial Filtering apply a filter (also sometimes called a kernel or mask) to an image a new pixel value is calculated, one pixel at a time the

More complicated filters

Page 31: Spatial Filtering and Convolution...Spatial Filtering apply a filter (also sometimes called a kernel or mask) to an image a new pixel value is calculated, one pixel at a time the

Gaussian Filter

Page 32: Spatial Filtering and Convolution...Spatial Filtering apply a filter (also sometimes called a kernel or mask) to an image a new pixel value is calculated, one pixel at a time the

Gaussian Filter

Page 33: Spatial Filtering and Convolution...Spatial Filtering apply a filter (also sometimes called a kernel or mask) to an image a new pixel value is calculated, one pixel at a time the

What would a Gaussian function look like expressed as a 3x3 mask?

Page 34: Spatial Filtering and Convolution...Spatial Filtering apply a filter (also sometimes called a kernel or mask) to an image a new pixel value is calculated, one pixel at a time the

3x3 Gaussian

a Gaussian function expressed as a 3x3 mask

Page 35: Spatial Filtering and Convolution...Spatial Filtering apply a filter (also sometimes called a kernel or mask) to an image a new pixel value is calculated, one pixel at a time the

3x3 Gaussian

1 2 1

2 4 2

1 2 1

1/16a Gaussian function expressed as a 3x3 mask

Page 36: Spatial Filtering and Convolution...Spatial Filtering apply a filter (also sometimes called a kernel or mask) to an image a new pixel value is calculated, one pixel at a time the

Not all masks are 3x3

Page 37: Spatial Filtering and Convolution...Spatial Filtering apply a filter (also sometimes called a kernel or mask) to an image a new pixel value is calculated, one pixel at a time the

0.01 0.03 0.05 0.08 0.1 0.11 0.1 0.08 0.05 0.03 0.01

0.03 0.06 0.11 0.18 0.23 0.25 0.23 0.18 0.11 0.06 0.03

0.05 0.11 0.21 0.32 0.42 0.46 0.42 0.32 0.21 0.11 0.05

0.08 0.18 0.32 0.5 0.65 0.71 0.65 0.5 0.32 0.18 0.08

0.1 0.23 0.42 0.65 0.84 0.92 0.84 0.65 0.42 0.23 0.1

0.11 0.25 0.46 0.71 0.92 1 0.92 0.71 0.46 0.25 0.11

0.1 0.23 0.42 0.65 0.84 0.92 0.84 0.65 0.42 0.23 0.1

0.08 0.18 0.32 0.5 0.65 0.71 0.65 0.5 0.32 0.18 0.08

0.05 0.11 0.21 0.32 0.42 0.46 0.42 0.32 0.21 0.11 0.05

0.03 0.06 0.11 0.18 0.23 0.25 0.23 0.18 0.11 0.06 0.03

0.01 0.03 0.05 0.08 0.1 0.11 0.1 0.08 0.05 0.03 0.01

Page 38: Spatial Filtering and Convolution...Spatial Filtering apply a filter (also sometimes called a kernel or mask) to an image a new pixel value is calculated, one pixel at a time the

0.01 0.03 0.05 0.08 0.1 0.11 0.1 0.08 0.05 0.03 0.01

0.03 0.06 0.11 0.18 0.23 0.25 0.23 0.18 0.11 0.06 0.03

0.05 0.11 0.21 0.32 0.42 0.46 0.42 0.32 0.21 0.11 0.05

0.08 0.18 0.32 0.5 0.65 0.71 0.65 0.5 0.32 0.18 0.08

0.1 0.23 0.42 0.65 0.84 0.92 0.84 0.65 0.42 0.23 0.1

0.11 0.25 0.46 0.71 0.92 1 0.92 0.71 0.46 0.25 0.11

0.1 0.23 0.42 0.65 0.84 0.92 0.84 0.65 0.42 0.23 0.1

0.08 0.18 0.32 0.5 0.65 0.71 0.65 0.5 0.32 0.18 0.08

0.05 0.11 0.21 0.32 0.42 0.46 0.42 0.32 0.21 0.11 0.05

0.03 0.06 0.11 0.18 0.23 0.25 0.23 0.18 0.11 0.06 0.03

0.01 0.03 0.05 0.08 0.1 0.11 0.1 0.08 0.05 0.03 0.01

11 x 11 mask

Page 39: Spatial Filtering and Convolution...Spatial Filtering apply a filter (also sometimes called a kernel or mask) to an image a new pixel value is calculated, one pixel at a time the

the larger the mask, the greater the effect

Mask Sizes

Page 40: Spatial Filtering and Convolution...Spatial Filtering apply a filter (also sometimes called a kernel or mask) to an image a new pixel value is calculated, one pixel at a time the

averaging filter

mask sizes:

no mask 3x3

5x5 9x9

15x15 35x35