imageprocessing - nus computingcs4243/lecture/imageproc.pdf · outline 1 basics of image processing...

29
Image Processing CS4243 Computer Vision and Pattern Recognition Leow Wee Kheng Department of Computer Science School of Computing National University of Singapore Leow Wee Kheng (CS4243) Image Processing 1 / 29

Upload: trinhkien

Post on 06-Mar-2018

234 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: ImageProcessing - NUS Computingcs4243/lecture/imageproc.pdf · Outline 1 Basics of Image Processing 2 Convolution & Cross Correlation 3 Applications Box Filter 1D Gaussian Filter

Image Processing

CS4243 Computer Vision and Pattern Recognition

Leow Wee Kheng

Department of Computer Science

School of Computing

National University of Singapore

Leow Wee Kheng (CS4243) Image Processing 1 / 29

Page 2: ImageProcessing - NUS Computingcs4243/lecture/imageproc.pdf · Outline 1 Basics of Image Processing 2 Convolution & Cross Correlation 3 Applications Box Filter 1D Gaussian Filter

Outline

1 Basics of Image Processing

2 Convolution & Cross Correlation

3 ApplicationsBox Filter1D Gaussian Filter2D Gaussian Filter

4 Self Study

5 Exercises

6 Further Reading

Leow Wee Kheng (CS4243) Image Processing 2 / 29

Page 3: ImageProcessing - NUS Computingcs4243/lecture/imageproc.pdf · Outline 1 Basics of Image Processing 2 Convolution & Cross Correlation 3 Applications Box Filter 1D Gaussian Filter

Basics of Image Processing

Basics of Image Processing

Suppose you have an image f(x, y).

What happens if you multiply a constant c to the intensity value ofeach pixel?

r(x, y) = c f(x, y)

Leow Wee Kheng (CS4243) Image Processing 3 / 29

Page 4: ImageProcessing - NUS Computingcs4243/lecture/imageproc.pdf · Outline 1 Basics of Image Processing 2 Convolution & Cross Correlation 3 Applications Box Filter 1D Gaussian Filter

Basics of Image Processing

c = 1.5 c = 0.6

c > 1: image becomes brighter.

c < 1: image becomes darker.

What if you multiply different values to different pixels?

Leow Wee Kheng (CS4243) Image Processing 4 / 29

Page 5: ImageProcessing - NUS Computingcs4243/lecture/imageproc.pdf · Outline 1 Basics of Image Processing 2 Convolution & Cross Correlation 3 Applications Box Filter 1D Gaussian Filter

Basics of Image Processing

(a) Contrast enhancement (b) Contrast reduction

Contrast enhancement:make bright pixels brighter, dark pixels darker.

Contrast reduction:make bright pixels darker, dark pixels brighter.

This type of image manipulation is called point processing.

Leow Wee Kheng (CS4243) Image Processing 5 / 29

Page 6: ImageProcessing - NUS Computingcs4243/lecture/imageproc.pdf · Outline 1 Basics of Image Processing 2 Convolution & Cross Correlation 3 Applications Box Filter 1D Gaussian Filter

Convolution & Cross Correlation

Convolution & Cross Correlation

Now, try something special:

place a grid, called mask, over a part of an image

multiply pixels under red dot by 1

multiply pixels under empty grid by 0

add up the products

Leow Wee Kheng (CS4243) Image Processing 6 / 29

Page 7: ImageProcessing - NUS Computingcs4243/lecture/imageproc.pdf · Outline 1 Basics of Image Processing 2 Convolution & Cross Correlation 3 Applications Box Filter 1D Gaussian Filter

Convolution & Cross Correlation

For the image, take dark pixel value = 1, light pixel value = 0.

0

0

3

1

0

Leow Wee Kheng (CS4243) Image Processing 7 / 29

Page 8: ImageProcessing - NUS Computingcs4243/lecture/imageproc.pdf · Outline 1 Basics of Image Processing 2 Convolution & Cross Correlation 3 Applications Box Filter 1D Gaussian Filter

Convolution & Cross Correlation

The final result is

0

0 0 0 00

0 0 1 01

0 1 3 2 0

0

0

1

1

3

1 0

1 0

This type of image manipulation is called neighbourhood processing.

In particular, the above process is called template matching.It finds the locations at which the template best matches the image.

Template matching is a kind of cross correlation.

Leow Wee Kheng (CS4243) Image Processing 8 / 29

Page 9: ImageProcessing - NUS Computingcs4243/lecture/imageproc.pdf · Outline 1 Basics of Image Processing 2 Convolution & Cross Correlation 3 Applications Box Filter 1D Gaussian Filter

Convolution & Cross Correlation Convolution

Convolution

Convolution between image f(x, y) and kernel k(x, y) is

f(x, y) ∗ k(x, y) =

−∞

−∞

f(u, v) k(x− u, y − v) du dv (1)

In discrete form,

f(x, y) ∗ k(x, y) =W−1∑

i=0

H−1∑

j=0

f(i, j) k(x− i, y − j) (2)

where W and H are the the width and height of the image.

Convolution is commutative (Exercise):

f(x, y) ∗ k(x, y) = k(x, y) ∗ f(x, y). (3)

Leow Wee Kheng (CS4243) Image Processing 9 / 29

Page 10: ImageProcessing - NUS Computingcs4243/lecture/imageproc.pdf · Outline 1 Basics of Image Processing 2 Convolution & Cross Correlation 3 Applications Box Filter 1D Gaussian Filter

Convolution & Cross Correlation Convolution

1D Example

1

( )

1

α

f u

1

k u

α

1/2

( )

−1

( )−

α

1/2

1x

k x u

f u k x u( )−

α

1/2

x0 <= <= 1

1

( )

x

( ) ( )−

α

1/2

1

x1 <= <= 2

f u k x u

x

1/2

f x ( )g x∗

α21

( )

Demo

Leow Wee Kheng (CS4243) Image Processing 10 / 29

Page 11: ImageProcessing - NUS Computingcs4243/lecture/imageproc.pdf · Outline 1 Basics of Image Processing 2 Convolution & Cross Correlation 3 Applications Box Filter 1D Gaussian Filter

Convolution & Cross Correlation Cross Correlation

Cross Correlation

Cross correlation between image f(x, y) and kernel k(x, y) is

f(x, y) ◦ k(x, y) =

−∞

−∞

f(u, v) k(x+ u, y + v) du dv (4)

In discrete form,

f(x, y) ◦ k(x, y) =W−1∑

i=0

H−1∑

j=0

f(i, j) k(x+ i, y + j) (5)

where W and H are the the width and height of the image.

If f = k, then it is called auto-correlation.

Cross correlation and convolution are related by (Exercise):

f(x, y) ◦ k(x, y) = f(−x,−y) ∗ k(x, y). (6)

Leow Wee Kheng (CS4243) Image Processing 11 / 29

Page 12: ImageProcessing - NUS Computingcs4243/lecture/imageproc.pdf · Outline 1 Basics of Image Processing 2 Convolution & Cross Correlation 3 Applications Box Filter 1D Gaussian Filter

Convolution & Cross Correlation Cross Correlation

1D Example

1

( )

1

α

f u

1

k u

α

1/2

( )

x

( )

α

1/2

1

k x+u

( )f u k x+u

α

1/2

x

x0 <= <= 1

( )

1

( )f u k x+u

α

1/2

1

x

x

( )

−1 <= <= 0

−1

f x ( )g xO

α

1/2

1

( )

Notes:

+x slides kernel k to the left (−x direction).

−x slides kernel k to the right (+x direction).

Leow Wee Kheng (CS4243) Image Processing 12 / 29

Page 13: ImageProcessing - NUS Computingcs4243/lecture/imageproc.pdf · Outline 1 Basics of Image Processing 2 Convolution & Cross Correlation 3 Applications Box Filter 1D Gaussian Filter

Convolution & Cross Correlation Cross Correlation

More convenient way to implement cross correlation:

f(x, y) ◦ k(x, y) =

w/2∑

i=−w/2

h/2∑

j=−h/2

f(x+ i, y + j) k(i, j) (7)

where w and h are the width and height of template k.

(0, 0) x

y

f

k

+i

+j

f has origin at the top-left (or bottom-left) corner.

k has origin in the middle; need odd-sized mask.

+x,+y slide template towards +x,+y directions.

Leow Wee Kheng (CS4243) Image Processing 13 / 29

Page 14: ImageProcessing - NUS Computingcs4243/lecture/imageproc.pdf · Outline 1 Basics of Image Processing 2 Convolution & Cross Correlation 3 Applications Box Filter 1D Gaussian Filter

Convolution & Cross Correlation Cross Correlation

Symmetric Kernel

Convolution is commutative. So,

f(x, y) ∗ k(x, y) = k(x, y) ∗ f(x, y) (8)

=

−∞

−∞

k(u, v) f(x− u, y − v) du dv (9)

Substituting µ = −u and ν = −v into Eq. 9 gives

f(x, y) ∗ k(x, y) =

−∞

−∞

f(x+ µ, y + ν) k(−µ,−ν) dµ dν (10)

Since k is symmetric, i.e., k(x, y) = k(−x,−y), we obtain

f(x, y) ∗ k(x, y) =

−∞

−∞

f(x+ µ, y + ν) k(µ, ν) dµ dν (11)

Thus, convolution is equal to cross correlation if kernel is symmetric.

Leow Wee Kheng (CS4243) Image Processing 14 / 29

Page 15: ImageProcessing - NUS Computingcs4243/lecture/imageproc.pdf · Outline 1 Basics of Image Processing 2 Convolution & Cross Correlation 3 Applications Box Filter 1D Gaussian Filter

Applications

Applications

Convolve (or correlate) image with different kernels produces differentresults:

uniform mask: box filter, averaging, smoothing and remove noise

Gaussian: smoothing and remove noise

difference mask: edge detection

difference of Gaussian: edge detection

Leow Wee Kheng (CS4243) Image Processing 15 / 29

Page 16: ImageProcessing - NUS Computingcs4243/lecture/imageproc.pdf · Outline 1 Basics of Image Processing 2 Convolution & Cross Correlation 3 Applications Box Filter 1D Gaussian Filter

Applications Box Filter

Box Filter

Noise can be reduced by applying a box filter:

1 1 1

1 1 1

1 1 1

f(x, y) ∗ k(x, y) =

w/2∑

i=−w/2

w/2∑

j=−w/2

f(x+ i, y + j) (12)

Usually, we normalize the mask values so that

w/2∑

i=−w/2

w/2∑

j=−w/2

k(i, j) = 1 (13)

Leow Wee Kheng (CS4243) Image Processing 16 / 29

Page 17: ImageProcessing - NUS Computingcs4243/lecture/imageproc.pdf · Outline 1 Basics of Image Processing 2 Convolution & Cross Correlation 3 Applications Box Filter 1D Gaussian Filter

Applications Box Filter

3×3 normalized box filter:

1/9 1/9 1/9

1/9 1/9 1/9

1/9 1/9 1/9

That is,

f(x, y) ∗ k(x, y) =1

w2

w/2∑

i=−w/2

w/2∑

j=−w/2

f(x+ i, y + j) (14)

For w = 3, we have

f(x, y) ∗ k(x, y) =1

9

1∑

i=−1

1∑

j=−1

f(x+ i, y + j) (15)

Leow Wee Kheng (CS4243) Image Processing 17 / 29

Page 18: ImageProcessing - NUS Computingcs4243/lecture/imageproc.pdf · Outline 1 Basics of Image Processing 2 Convolution & Cross Correlation 3 Applications Box Filter 1D Gaussian Filter

Applications Box Filter

(a) (b) (c) (d)

(e) (f) (g)

(a) original image

(b) salt-and-pepper noise, isolated pixels of wrong gray value

(c) uniform noise, noise levels follow a uniform distribution

(d) Gaussian noise, noise levels follow a Gaussian distribution

(e)–(g) filtered by a box filter

Leow Wee Kheng (CS4243) Image Processing 18 / 29

Page 19: ImageProcessing - NUS Computingcs4243/lecture/imageproc.pdf · Outline 1 Basics of Image Processing 2 Convolution & Cross Correlation 3 Applications Box Filter 1D Gaussian Filter

Applications Box Filter

Note that removing noise can also blur edges:

(a) (b) (c) (d)

(a) corrupted with salt-and-pepper noise

(b) filtered by 3×3 box filter

(c) filtered by 5×5 box filter

(d) filtered by 7×7 box filter

Leow Wee Kheng (CS4243) Image Processing 19 / 29

Page 20: ImageProcessing - NUS Computingcs4243/lecture/imageproc.pdf · Outline 1 Basics of Image Processing 2 Convolution & Cross Correlation 3 Applications Box Filter 1D Gaussian Filter

Applications Box Filter

Why? Because box filtering = averaging neighbour pixels (Eq. 14)!

See the following “zoomed-in view”:

0 0 0 0 0

0 0 0 0 0

1 1 1 1 1

1 1 1 1 1

1 1 1 1 1

∗1 1 1

1 1 1

1 1 1

=

0 0 0 0 0

2 3 3 3 2

4 6 6 6 4

6 9 9 9 6

4 6 6 6 4

Averaging causes a gradual change of pixel values.

Sharp edge is blurred.

Filtered values at image boundaries are smaller:boundary effect.

To reduce edge blurring, use median filter [GW92, SS01] oranisotropic filter [PM90].

Leow Wee Kheng (CS4243) Image Processing 20 / 29

Page 21: ImageProcessing - NUS Computingcs4243/lecture/imageproc.pdf · Outline 1 Basics of Image Processing 2 Convolution & Cross Correlation 3 Applications Box Filter 1D Gaussian Filter

Applications 1D Gaussian Filter

Gaussian Filter

1D (normalized) Gaussian

g(x) =1

√2πσ

exp

(

−x2

2σ2

)

(16)

-3 -2 -1 0 1 2 3 0

0.1

0.2

0.3

0.4

0.5

x

g ( x )

Leow Wee Kheng (CS4243) Image Processing 21 / 29

Page 22: ImageProcessing - NUS Computingcs4243/lecture/imageproc.pdf · Outline 1 Basics of Image Processing 2 Convolution & Cross Correlation 3 Applications Box Filter 1D Gaussian Filter

Applications 2D Gaussian Filter

2D (normalized) Gaussian

g(x, y) =1

2πσ2exp

(

−x2 + y2

2σ2

)

(17)

σ = 15 pixels, top view around the peak

Leow Wee Kheng (CS4243) Image Processing 22 / 29

Page 23: ImageProcessing - NUS Computingcs4243/lecture/imageproc.pdf · Outline 1 Basics of Image Processing 2 Convolution & Cross Correlation 3 Applications Box Filter 1D Gaussian Filter

Applications 2D Gaussian Filter

Like box filters, 2D Gaussian can be used as a smoothing filter:

f(x, y) ∗ g(x, y) =∑

i

j

f(x+ i, y + j) g(i, j) (18)

That is, Gaussian filtering is weighted averaging.

2D Gaussian is a separable kernel:

f(x, y) ∗ g(x, y) = (f(x, y) ∗ g(x)) ∗ g(y) (19)

First convolve f by horizontal 1-D Gaussian g(x).Then, convolve result by vertical 1-D Gaussian g(y).This method is more efficient.

Complexity of original Gaussian smoothing is O(WHwh).Complexity of efficient Gaussian smoothing is O(WH(w + h)).

Leow Wee Kheng (CS4243) Image Processing 23 / 29

Page 24: ImageProcessing - NUS Computingcs4243/lecture/imageproc.pdf · Outline 1 Basics of Image Processing 2 Convolution & Cross Correlation 3 Applications Box Filter 1D Gaussian Filter

Applications 2D Gaussian Filter

Notes:

To use Gaussian, need to discretize the function.

Size of Gaussian mask must be large enough.

The larger the Gaussian’s σ, the larger is the mask.

Cut off the mask at a sufficiently small mask value.

-3 -2 -1 0 1 2 3 0

0.1

0.2

0.3

0.4

0.5

x

g ( x )

extent of Gaussian mask

Leow Wee Kheng (CS4243) Image Processing 24 / 29

Page 25: ImageProcessing - NUS Computingcs4243/lecture/imageproc.pdf · Outline 1 Basics of Image Processing 2 Convolution & Cross Correlation 3 Applications Box Filter 1D Gaussian Filter

Applications 2D Gaussian Filter

Example: Gaussian smoothing.

(a) (b) (c)

(a) original image

(b) filtered by Gaussian with σ = 1.

(c) filtered by Gaussian with σ = 2.

Like box filters, Gaussian filters remove noise and blur edges.

Leow Wee Kheng (CS4243) Image Processing 25 / 29

Page 26: ImageProcessing - NUS Computingcs4243/lecture/imageproc.pdf · Outline 1 Basics of Image Processing 2 Convolution & Cross Correlation 3 Applications Box Filter 1D Gaussian Filter

Self Study

Self Study

Edge detection ([SS01] Section 5.6–5.8):

Difference of Gaussian (DoG): large difference indicates edge.

Laplacian of Gaussian (LoG): zero-crossing indicates edge.

Canny edge detector: more immune to noise than LoG.

Leow Wee Kheng (CS4243) Image Processing 26 / 29

Page 27: ImageProcessing - NUS Computingcs4243/lecture/imageproc.pdf · Outline 1 Basics of Image Processing 2 Convolution & Cross Correlation 3 Applications Box Filter 1D Gaussian Filter

Exercises

Exercises

(1) Show that convolution is commutative, i.e.,f(x, y) ∗ k(x, y) = k(x, y) ∗ f(x, y).

(2) Show that cross correlation of the form given in Eq. 4 is related toconvolution by

f(x, y) ◦ k(x, y) = f(−x,−y) ∗ k(x, y). (20)

(3) Show that cross correlation of the form given in Eq. 7 is related toconvolution by

f(x, y) ◦ k(x, y) = f(x, y) ∗ k(−x,−y). (21)

Leow Wee Kheng (CS4243) Image Processing 27 / 29

Page 28: ImageProcessing - NUS Computingcs4243/lecture/imageproc.pdf · Outline 1 Basics of Image Processing 2 Convolution & Cross Correlation 3 Applications Box Filter 1D Gaussian Filter

Further Reading

Further Reading

Convolution and correlation: [GW92] Section 3.3.8, [SS01] Section5.10.

Image filtering: [SS01] Chapter 5.

Median filtering: [SS01] Section 5.5.

Anisotropic filtering: [PM90].

Nonlinear filtering (including median filtering, ansiotropicfiltering): [Sze10] Section 3.3.1.

Edge detection: [SS01] Section 5.6–5.8, [Sze10] Section 4.2.

OpenCV supports many image processing functions:

Convolution

Image filtering and smoothing

Edge detection, corner detection

Leow Wee Kheng (CS4243) Image Processing 28 / 29

Page 29: ImageProcessing - NUS Computingcs4243/lecture/imageproc.pdf · Outline 1 Basics of Image Processing 2 Convolution & Cross Correlation 3 Applications Box Filter 1D Gaussian Filter

Reference

Reference

R. C. Gonzalez and R. E. Woods.Digital Image Processing.Addison-Wesley, 1992.

P. Perona and J. Malik.Scale-space and edge detection using anisotropic diffusion.IEEE Trans. on Pattern Analysis and Machine Intelligence,12(7):629–639, 1990.

L. Shapiro and Stockman.Computer Vision.Prentice-Hall, 2001.

R. Szeliski.Computer Vision: Algorithms and Applications.Springer, 2010.

Leow Wee Kheng (CS4243) Image Processing 29 / 29