Lecture 2 1 Image filtering - uio.no fileImage filtering • Image filters in the spatial domain:...

38
Image Processing Lecture 2.1 - Image filtering Idar Dyrdal

Transcript of Lecture 2 1 Image filtering - uio.no fileImage filtering • Image filters in the spatial domain:...

Page 1: Lecture 2 1 Image filtering - uio.no fileImage filtering • Image filters in the spatial domain: – Filtering is a mathematical operation on a grid of numbers – Smoothing, sharpening

Image Processing Lecture 2.1 - Image filtering

Idar Dyrdal

Page 2: Lecture 2 1 Image filtering - uio.no fileImage filtering • Image filters in the spatial domain: – Filtering is a mathematical operation on a grid of numbers – Smoothing, sharpening

Image function

2

Page 3: Lecture 2 1 Image filtering - uio.no fileImage filtering • Image filters in the spatial domain: – Filtering is a mathematical operation on a grid of numbers – Smoothing, sharpening

Digital Image

3

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 0 255 255 0 0 0 0 0

0 0 0 255 255 170 0 0 0 0

0 0 255 170 170 255 0 0 0 0

0 0 255 170 170 85 85 0 0 0

0 170 170 255 85 85 170 170 0 0

0 0 85 85 170 170 170 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

Page 4: Lecture 2 1 Image filtering - uio.no fileImage filtering • Image filters in the spatial domain: – Filtering is a mathematical operation on a grid of numbers – Smoothing, sharpening

Image Processing

• Point operators

• Image filtering in spatial domain - Linear filters - Non-linear filters

• Image filtering in frequency domain - Fourier transform

4

Page 5: Lecture 2 1 Image filtering - uio.no fileImage filtering • Image filters in the spatial domain: – Filtering is a mathematical operation on a grid of numbers – Smoothing, sharpening

Point Operators

• Pixel transforms - Brightness adjustment - Contrast adjustment - …

• Colour transforms

• Histogram equalization

• …

5

(Each pixel multiplied by 2)

5 14

1 71

5 310

10 28

2 142

10 620

(Pixel-by-pixel transformation)

Page 6: Lecture 2 1 Image filtering - uio.no fileImage filtering • Image filters in the spatial domain: – Filtering is a mathematical operation on a grid of numbers – Smoothing, sharpening

Pixel transforms - example

Original image Processed image

6

Page 7: Lecture 2 1 Image filtering - uio.no fileImage filtering • Image filters in the spatial domain: – Filtering is a mathematical operation on a grid of numbers – Smoothing, sharpening

Histogram equalization

7

Page 8: Lecture 2 1 Image filtering - uio.no fileImage filtering • Image filters in the spatial domain: – Filtering is a mathematical operation on a grid of numbers – Smoothing, sharpening

Image filtering

• Image filters in the spatial domain: – Filtering is a mathematical operation on a grid of numbers – Smoothing, sharpening (enhancing the image) – Feature extraction (measuring texture, finding edges, distinctive points and

patterns).

• Image filters in the frequency domain: – Filtering is a way to modify the (spatial) frequencies of images – Noise removal, (re)sampling, image compression.

8

Page 9: Lecture 2 1 Image filtering - uio.no fileImage filtering • Image filters in the spatial domain: – Filtering is a mathematical operation on a grid of numbers – Smoothing, sharpening

Image filtering in spatial domain

Modify the pixels in an image based on some function of a local neighborhood of each pixel:

9

5 14

1 71

5 310

10

Local image data Modified image data

Page 10: Lecture 2 1 Image filtering - uio.no fileImage filtering • Image filters in the spatial domain: – Filtering is a mathematical operation on a grid of numbers – Smoothing, sharpening

Linear filtering

Convolution or cross-correlation where each pixel in the filtered image is a linear combination of the pixels in a local neighborhood in the original image:

10

11 0020

0 00

Kernel Modified image dataLocal image data

166 141 81

5 310

The coefficients of the linear combination is contained in the “kernel” (filter mask).

Page 11: Lecture 2 1 Image filtering - uio.no fileImage filtering • Image filters in the spatial domain: – Filtering is a mathematical operation on a grid of numbers – Smoothing, sharpening

Cross-correlation

This is called a cross-correlation operation:

Let f be the image, h be the kernel (of size 2k+1 x 2k+1), and g be the output image:

11

Page 12: Lecture 2 1 Image filtering - uio.no fileImage filtering • Image filters in the spatial domain: – Filtering is a mathematical operation on a grid of numbers – Smoothing, sharpening

Linear filtering

12

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 0 90 90 0 0 0 0 0

0 0 0 90 90 90 0 0 0 0

0 0 90 90 90 90 0 0 0 0

0 0 90 90 90 90 90 0 0 0

0 90 90 90 90 90 90 90 0 0

0 0 90 90 90 90 90 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 10 20 20 10 0 0 0 0

0 0 20

111111111

Page 13: Lecture 2 1 Image filtering - uio.no fileImage filtering • Image filters in the spatial domain: – Filtering is a mathematical operation on a grid of numbers – Smoothing, sharpening

Linear filtering

13

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 0 90 90 0 0 0 0 0

0 0 0 90 90 90 0 0 0 0

0 0 90 90 90 90 0 0 0 0

0 0 90 90 90 90 90 0 0 0

0 90 90 90 90 90 90 90 0 0

0 0 90 90 90 90 90 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 10 20 20 10 0 0 0 0

0 0 20 40 50 30 10 0 0 0

0 10 40 70 80 50 20 0 0 0

0 20 50 80 90 70 40 10

111111111

Page 14: Lecture 2 1 Image filtering - uio.no fileImage filtering • Image filters in the spatial domain: – Filtering is a mathematical operation on a grid of numbers – Smoothing, sharpening

Linear filtering

14

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 0 90 90 0 0 0 0 0

0 0 0 90 90 90 0 0 0 0

0 0 90 90 90 90 0 0 0 0

0 0 90 90 90 90 90 0 0 0

0 90 90 90 90 90 90 90 0 0

0 0 90 90 90 90 90 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 10 20 20 10 0 0 0 0

0 0 20 40 50 30 10 0 0 0

0 10 40 70 80 50 20 0 0 0

0 20 50 80 90 70 40 10 0 0

0 40 70 90 90 80 60 30 10 0

10 40 70 90 90 90 70 40 10 0

0 30 50 60 60 60 50 30 10 0

0 10 20 30 30 30 20 10 0 0

0 0 0 0 0 0 0 0 0 0

111111111

Page 15: Lecture 2 1 Image filtering - uio.no fileImage filtering • Image filters in the spatial domain: – Filtering is a mathematical operation on a grid of numbers – Smoothing, sharpening

Moving average filter (box filter)

15

111111

111

9 x 9 kernel

3 x 3 kernel

Replaces each pixel with an average of its neigborhood (smoothing effect)

Page 16: Lecture 2 1 Image filtering - uio.no fileImage filtering • Image filters in the spatial domain: – Filtering is a mathematical operation on a grid of numbers – Smoothing, sharpening

Sharpening filter

16

Enhances differences with local average

111111111

000020000 -

Page 17: Lecture 2 1 Image filtering - uio.no fileImage filtering • Image filters in the spatial domain: – Filtering is a mathematical operation on a grid of numbers – Smoothing, sharpening

Convolution

• Same as cross-correlation, except that the kernel is “flipped” (horizontally and vertically):

• This is called a convolution operation:

• Convolution is commutative and associative (no difference between filter and image):

• Apply several filters, one after the other:

17

Page 18: Lecture 2 1 Image filtering - uio.no fileImage filtering • Image filters in the spatial domain: – Filtering is a mathematical operation on a grid of numbers – Smoothing, sharpening

Gaussian filter (smoothing)

18

Page 19: Lecture 2 1 Image filtering - uio.no fileImage filtering • Image filters in the spatial domain: – Filtering is a mathematical operation on a grid of numbers – Smoothing, sharpening

Gaussian filtering

Original image

19

Page 20: Lecture 2 1 Image filtering - uio.no fileImage filtering • Image filters in the spatial domain: – Filtering is a mathematical operation on a grid of numbers – Smoothing, sharpening

Edge detection

20

Page 21: Lecture 2 1 Image filtering - uio.no fileImage filtering • Image filters in the spatial domain: – Filtering is a mathematical operation on a grid of numbers – Smoothing, sharpening

Edges and image derivatives

• An edge is a place of rapid change of the image intensity function

• Corresponds to extrema of the first derivative of the image intensity function

• Discrete approximation to the image derivatives:

Image gradient:

Gradient magnitude:

Prewitt operator:

10-1

10-110-1

111000-1-1-1

21

Page 22: Lecture 2 1 Image filtering - uio.no fileImage filtering • Image filters in the spatial domain: – Filtering is a mathematical operation on a grid of numbers – Smoothing, sharpening

Image gradient

22

Page 23: Lecture 2 1 Image filtering - uio.no fileImage filtering • Image filters in the spatial domain: – Filtering is a mathematical operation on a grid of numbers – Smoothing, sharpening

Derivative of Gaussians

23

Page 24: Lecture 2 1 Image filtering - uio.no fileImage filtering • Image filters in the spatial domain: – Filtering is a mathematical operation on a grid of numbers – Smoothing, sharpening

Sobel operator

Common approximation of the derivative of a Gaussian:

10-1

20-2

10-1

121

000

-1-2-1

x-direction y-direction

24

Page 25: Lecture 2 1 Image filtering - uio.no fileImage filtering • Image filters in the spatial domain: – Filtering is a mathematical operation on a grid of numbers – Smoothing, sharpening

Sobel operator - example

25

x-direction y-direction

Gradient magnitude

Page 26: Lecture 2 1 Image filtering - uio.no fileImage filtering • Image filters in the spatial domain: – Filtering is a mathematical operation on a grid of numbers – Smoothing, sharpening

Non-linear filtering - Median filter

A median filter operates over a neighborhood in the input image by selecting the median intensity:

26

Compute median from

neighborhoodModified image dataLocal image data

46 141 81

5 310

Other non-linear filters: • Bilateral filters (outlier rejection) • Anisotropic diffusion • Morphological operations (on binary images) • …

Page 27: Lecture 2 1 Image filtering - uio.no fileImage filtering • Image filters in the spatial domain: – Filtering is a mathematical operation on a grid of numbers – Smoothing, sharpening

Median filtering - example

27 Image with Salt & Pepper noise Image after median filtering

Page 28: Lecture 2 1 Image filtering - uio.no fileImage filtering • Image filters in the spatial domain: – Filtering is a mathematical operation on a grid of numbers – Smoothing, sharpening

Filtering in frequency domain

Fourier (1807): Any univariate function can be rewritten as

a weighted sum of sines and cosines of different frequencies (true with some subtle restrictions).

This leads to: • Fourier Series • Fourier Transform (continuous and discrete) • Fast Fourier Transform (FFT)

Jean Baptiste Joseph Fourier (1768-1830)

28

Page 29: Lecture 2 1 Image filtering - uio.no fileImage filtering • Image filters in the spatial domain: – Filtering is a mathematical operation on a grid of numbers – Smoothing, sharpening

Sum of sines

The Fourier transform stores the magnitude and phase at each frequency

Amplitude: Phase:

Frequency

Amplitude

29

+ + =

Page 30: Lecture 2 1 Image filtering - uio.no fileImage filtering • Image filters in the spatial domain: – Filtering is a mathematical operation on a grid of numbers – Smoothing, sharpening

Two-dimensional Fourier transform

Continous transform:

Discrete transform:

30

Page 31: Lecture 2 1 Image filtering - uio.no fileImage filtering • Image filters in the spatial domain: – Filtering is a mathematical operation on a grid of numbers – Smoothing, sharpening

Fourier analysis in images

Intensity images

Fourier images

31

Page 32: Lecture 2 1 Image filtering - uio.no fileImage filtering • Image filters in the spatial domain: – Filtering is a mathematical operation on a grid of numbers – Smoothing, sharpening

The Convolution Theorem

The Fourier transform of the convolution of two functions is the product of their Fourier transforms:

Convolution in spatial domain is equivalent to multiplication in frequency domain:

32

Page 33: Lecture 2 1 Image filtering - uio.no fileImage filtering • Image filters in the spatial domain: – Filtering is a mathematical operation on a grid of numbers – Smoothing, sharpening

Example – Gaussian (low pass) filtering

Original colour image Gray level image

33

Page 34: Lecture 2 1 Image filtering - uio.no fileImage filtering • Image filters in the spatial domain: – Filtering is a mathematical operation on a grid of numbers – Smoothing, sharpening

Example – Gaussian filtering

FFT

34

Page 35: Lecture 2 1 Image filtering - uio.no fileImage filtering • Image filters in the spatial domain: – Filtering is a mathematical operation on a grid of numbers – Smoothing, sharpening

Example – Gaussian filtering

FFT

35

Page 36: Lecture 2 1 Image filtering - uio.no fileImage filtering • Image filters in the spatial domain: – Filtering is a mathematical operation on a grid of numbers – Smoothing, sharpening

Example – Gaussian filtering

=

36

Page 37: Lecture 2 1 Image filtering - uio.no fileImage filtering • Image filters in the spatial domain: – Filtering is a mathematical operation on a grid of numbers – Smoothing, sharpening

Example – Gaussian filtering

Inverse FFT

37

Page 38: Lecture 2 1 Image filtering - uio.no fileImage filtering • Image filters in the spatial domain: – Filtering is a mathematical operation on a grid of numbers – Smoothing, sharpening

Summary

Image Processing

• Point operators

• Image filtering in spatial domain – Linear filters – Non-linear filters

• Image filtering in frequency domain – Fourier transforms – Gaussian (low pass) filtering

More information: Szeliski 3.1 – 3.4

38