CMPUT 206 - ugweb.cs.ualberta.caugweb.cs.ualberta.ca/~c206/W08/resources/Lecture...Kernel (nxn...

34
Irene Cheng Jan 2008 CMPUT 206 Introduction to Digital Image Processing

Transcript of CMPUT 206 - ugweb.cs.ualberta.caugweb.cs.ualberta.ca/~c206/W08/resources/Lecture...Kernel (nxn...

Page 1: CMPUT 206 - ugweb.cs.ualberta.caugweb.cs.ualberta.ca/~c206/W08/resources/Lecture...Kernel (nxn pixels) 1. Suppose a nxn (n is an odd number) kernel is used and the centre position

Irene Cheng Jan 2008

CMPUT 206

Introduction to Digital Image Processing

Page 2: CMPUT 206 - ugweb.cs.ualberta.caugweb.cs.ualberta.ca/~c206/W08/resources/Lecture...Kernel (nxn pixels) 1. Suppose a nxn (n is an odd number) kernel is used and the centre position

Irene Cheng Jan 2008

Overview

1. What is a pixel in an image ?

2. How does Photoshop, + human assistance, detect an edge in a picture/photograph ?

3. Behind Photoshop - How does the computer do it ?

Page 3: CMPUT 206 - ugweb.cs.ualberta.caugweb.cs.ualberta.ca/~c206/W08/resources/Lecture...Kernel (nxn pixels) 1. Suppose a nxn (n is an odd number) kernel is used and the centre position

Irene Cheng Jan 2008

“Pixel” as the basic unit

An image is processed in memory as bytes (8bits/bytes)

• Grey scale image [0 – 255] – 1 byte pixel

• Color image [RGB of values 0-255] – 3 bytes pixel

Page 4: CMPUT 206 - ugweb.cs.ualberta.caugweb.cs.ualberta.ca/~c206/W08/resources/Lecture...Kernel (nxn pixels) 1. Suppose a nxn (n is an odd number) kernel is used and the centre position

Irene Cheng Jan 2008

“Pixel” as the basic unit

R 80 116 228 234G 84 81 87 93B 109 101 59 65

(0, 0) X

(256, 256)

Page 5: CMPUT 206 - ugweb.cs.ualberta.caugweb.cs.ualberta.ca/~c206/W08/resources/Lecture...Kernel (nxn pixels) 1. Suppose a nxn (n is an odd number) kernel is used and the centre position

Irene Cheng Jan 2008

Definition of Edges

• What are edges in an image ?– Locations where there is a sudden variation in

the grey or color scale.

• Which image does not contain edges ?

a b c d e

f g h i

Page 6: CMPUT 206 - ugweb.cs.ualberta.caugweb.cs.ualberta.ca/~c206/W08/resources/Lecture...Kernel (nxn pixels) 1. Suppose a nxn (n is an odd number) kernel is used and the centre position

Irene Cheng Jan 2008

Definition of Edges

Use Photoshop “Find Edges” tool:

a b c d e f g h i

Page 7: CMPUT 206 - ugweb.cs.ualberta.caugweb.cs.ualberta.ca/~c206/W08/resources/Lecture...Kernel (nxn pixels) 1. Suppose a nxn (n is an odd number) kernel is used and the centre position

Irene Cheng Jan 2008

Sharpen edges in images – Photoshop Demo

Sharp

Filter – sharper – sharpen edges/sharpen more

Blur

Increase contrast and sharpen edges

High contrast Low contrast

Page 8: CMPUT 206 - ugweb.cs.ualberta.caugweb.cs.ualberta.ca/~c206/W08/resources/Lecture...Kernel (nxn pixels) 1. Suppose a nxn (n is an odd number) kernel is used and the centre position

Irene Cheng Jan 2008

Sharpen edges in images – Photoshop Demo

Bright

Filter – sharper – sharpen edges/sharpen more

Low luminosity

Discover features

Sharpen and image-adjust-curves

Page 9: CMPUT 206 - ugweb.cs.ualberta.caugweb.cs.ualberta.ca/~c206/W08/resources/Lecture...Kernel (nxn pixels) 1. Suppose a nxn (n is an odd number) kernel is used and the centre position

Irene Cheng Jan 2008

Region growing + Edge detection

The Magic Wand Tool in Photoshop

Page 10: CMPUT 206 - ugweb.cs.ualberta.caugweb.cs.ualberta.ca/~c206/W08/resources/Lecture...Kernel (nxn pixels) 1. Suppose a nxn (n is an odd number) kernel is used and the centre position

Irene Cheng Jan 2008

Can Computer do as well as human ?

• Computer applies edge detection technique based on numeric computation, not based on human perception and cognitive skill

Page 11: CMPUT 206 - ugweb.cs.ualberta.caugweb.cs.ualberta.ca/~c206/W08/resources/Lecture...Kernel (nxn pixels) 1. Suppose a nxn (n is an odd number) kernel is used and the centre position

Irene Cheng Jan 2008

A Simple Edge Detector - gradient

• Based on grey scale gradient at a pixel

),1(),1(),( yxfyxfyxgx −−+≈

)1,()1,(),( −−+≈ yxfyxfyxg y

98 98 98 10610 103 110 11651 38 60 11095 68 60 29

110 116 95 65103 110 116 120

100 139 72

-35 39-15 51

-60 -38-35 -5078 3542 56

Assuming threshold T = 50, a pixel is selected if >= T

x

y What is

22yx gg +

117 4036 8785 5244 76

Answer:

Page 12: CMPUT 206 - ugweb.cs.ualberta.caugweb.cs.ualberta.ca/~c206/W08/resources/Lecture...Kernel (nxn pixels) 1. Suppose a nxn (n is an odd number) kernel is used and the centre position

Irene Cheng Jan 2008

Gradient detection (continued)

• Red squares represent selected pixels

98 98 98 10610 103 110 11651 38 60 11095 68 60 29

110 116 95 65103 110 116 120

Assuming threshold = 50

117 4036 8785 5244 76

=+ 22yx gg

Detected edge

Page 13: CMPUT 206 - ugweb.cs.ualberta.caugweb.cs.ualberta.ca/~c206/W08/resources/Lecture...Kernel (nxn pixels) 1. Suppose a nxn (n is an odd number) kernel is used and the centre position

Irene Cheng Jan 2008

Gradient detection (continued)

• The two gradients gx and gy computed at each pixel are regarded as the x and y components of a gradient vector, which has gradient magnitude and direction given by:

22yx ggg +=

= −

x

y

g

g1tanθ

where the orientation θ is measured relative to x axis.Gradient magnitude is sometimes approximated by:

yx ggg +=

Page 14: CMPUT 206 - ugweb.cs.ualberta.caugweb.cs.ualberta.ca/~c206/W08/resources/Lecture...Kernel (nxn pixels) 1. Suppose a nxn (n is an odd number) kernel is used and the centre position

Irene Cheng Jan 2008

Find edges in images – Photoshop Demo

(Try R, G, B in lab)Extract Intensity values

ApplyEdgeDetection

Stylize – find edges

Page 15: CMPUT 206 - ugweb.cs.ualberta.caugweb.cs.ualberta.ca/~c206/W08/resources/Lecture...Kernel (nxn pixels) 1. Suppose a nxn (n is an odd number) kernel is used and the centre position

Irene Cheng Jan 2008

Find edges in images

increasing threshold (Assume Gradient Approach)

T1 T2 T3

Find Edges

By applying different thresholds T, the results are different: -Assign value 255 if < TAssign value 0 if >= T

Page 16: CMPUT 206 - ugweb.cs.ualberta.caugweb.cs.ualberta.ca/~c206/W08/resources/Lecture...Kernel (nxn pixels) 1. Suppose a nxn (n is an odd number) kernel is used and the centre position

Irene Cheng Jan 2008

Apply edge detection kernelsImage (RxC pixels)

Kernel (nxn pixels)

1. Suppose a nxn (n is an odd number) kernel is used and the centre position of the kernel is denoted by kc.

2. Slide the kernel across the image, one pixel at a time.

3. Compute the new value of each pixel, which is under kc, and repeat for all pixels in the image.

4. Note that the process is not in-place (new values are stored in a new image of same dimension.

5. Computation:� For each pixel inside the kernel,

multiply the pixel value with the kernel value.

� Take the sum of the products.6. (a) The border pixel can be duplicated, or

(b) take the mirror values of the border pixels, to fill up the kernel.

R

C

6 (a) 23 23 23 20 18

6 (b) 18 20 23 20 18

Image border

Page 17: CMPUT 206 - ugweb.cs.ualberta.caugweb.cs.ualberta.ca/~c206/W08/resources/Lecture...Kernel (nxn pixels) 1. Suppose a nxn (n is an odd number) kernel is used and the centre position

Irene Cheng Jan 2008

Edge detection kernels

• Noise reduction or elimination

• Edge enhancement

• Edge localization

111

111

111

9

1

0 0 00 99 00 0 0

An example of Low Pass Filtering:

An example of High Pass Filtering:

−−−

−−

−−−

111

181

111

0 0 00 0 00 0 0

Examples ofImage pixels

Concept of filtering

Page 18: CMPUT 206 - ugweb.cs.ualberta.caugweb.cs.ualberta.ca/~c206/W08/resources/Lecture...Kernel (nxn pixels) 1. Suppose a nxn (n is an odd number) kernel is used and the centre position

Irene Cheng Jan 2008

Convolution operation

• Based on convolution operations − compute weighted averages over a 3x3 neighborhood

),(),( yxfhyxg xx ×=

),(),( yxfhyxg yy ×=

where the (Prewitt) kernels are:

=

101

101

101

xh

−−−

=

111

000

111

yh

Note: high order convolution kernels like 5x5, etc. can also be used, but they are more computational expensive

Page 19: CMPUT 206 - ugweb.cs.ualberta.caugweb.cs.ualberta.ca/~c206/W08/resources/Lecture...Kernel (nxn pixels) 1. Suppose a nxn (n is an odd number) kernel is used and the centre position

Irene Cheng Jan 2008

Convolution operation (continued)

=

101

101

101

−−−

=

111

000

111

yh

98 98 98 10610 103 110 11651 38 60 11095 68 60 29

110 116 95 65103 110 116 120

109 9374 46

-41 -18-37 -80

-145 -940 -172

172 68106 189

98 99 9799 98 9799 98 97

Assuming threshold = ±50

(1) Is 98 an edge?

(2) What are the detected pixels?

(3) What will happen if the threshold is ±100? ±150?

Page 20: CMPUT 206 - ugweb.cs.ualberta.caugweb.cs.ualberta.ca/~c206/W08/resources/Lecture...Kernel (nxn pixels) 1. Suppose a nxn (n is an odd number) kernel is used and the centre position

Irene Cheng Jan 2008

Simple Edge Detectors

Result from gradient Result from Prewitt kernels

One edge but thick

Threshold=±50 Threshold=±100 or ±150

Page 21: CMPUT 206 - ugweb.cs.ualberta.caugweb.cs.ualberta.ca/~c206/W08/resources/Lecture...Kernel (nxn pixels) 1. Suppose a nxn (n is an odd number) kernel is used and the centre position

Irene Cheng Jan 2008

(Top Left) Original (Top Right) Apply gradient magnitudes and scale to 0-255 range

(Bottom Left) Apply threshold of 50 to Top Right

(Bottom Right) Apply thresholdOf 150 to Top Right

An Example – Gradient magnitudes

[Reference 1]

Apply threshold T:If < T, value 1If >= T, value 2

Page 22: CMPUT 206 - ugweb.cs.ualberta.caugweb.cs.ualberta.ca/~c206/W08/resources/Lecture...Kernel (nxn pixels) 1. Suppose a nxn (n is an odd number) kernel is used and the centre position

Irene Cheng Jan 2008

Scaling grey values onto another range

• Let Orange, Omin and Omax be the original range, minimum value and maximum value,

i.e. Orange = Omax – Omin

• The Nrange, Nmin and Nmax be the original range, minimum value and maximum value,

i.e. Nrange = Nmax – Nmin

• To map a pixel P from the original range to P’ in the new range:

minmin' NN

O

OPP range

range+×

−=

Page 23: CMPUT 206 - ugweb.cs.ualberta.caugweb.cs.ualberta.ca/~c206/W08/resources/Lecture...Kernel (nxn pixels) 1. Suppose a nxn (n is an odd number) kernel is used and the centre position

Irene Cheng Jan 2008

Advantage of a bigger grey value range before processing

Findedges Threshold

128

Map from 0-100To 0-255

Threshold50

Page 24: CMPUT 206 - ugweb.cs.ualberta.caugweb.cs.ualberta.ca/~c206/W08/resources/Lecture...Kernel (nxn pixels) 1. Suppose a nxn (n is an odd number) kernel is used and the centre position

Irene Cheng Jan 2008

Sobel kernels

• Sobel kernels, which give more weights to on-axis pixels

=

101

202

101

xh

−−−

=

121

000

121

yh

Original image convoluation with hx with hy

Cross from grey to white

From white to grey

+ve value

-ve value

(assign pixel value 0 if <0)

Page 25: CMPUT 206 - ugweb.cs.ualberta.caugweb.cs.ualberta.ca/~c206/W08/resources/Lecture...Kernel (nxn pixels) 1. Suppose a nxn (n is an odd number) kernel is used and the centre position

Irene Cheng Jan 2008

• Problem with edge localization– High threshold may suppress meaningful edges– Low threshold may include unwanted edges– Noise may have high magnitude

Noise in an image

xWith noiseG

radi

ent m

agni

tude

Gradient magnitude is very sensitive to noiseWithout noise

Page 26: CMPUT 206 - ugweb.cs.ualberta.caugweb.cs.ualberta.ca/~c206/W08/resources/Lecture...Kernel (nxn pixels) 1. Suppose a nxn (n is an odd number) kernel is used and the centre position

Irene Cheng Jan 2008

Blurred edges

– The detected edge can be rather broad in the case of diffuse edges, resulting in a thick band of pixels instead of a single point of maximum gradient.

Slightly blurred Heavily blurred

x

Gre

y va

lues

Sharp edge

Page 27: CMPUT 206 - ugweb.cs.ualberta.caugweb.cs.ualberta.ca/~c206/W08/resources/Lecture...Kernel (nxn pixels) 1. Suppose a nxn (n is an odd number) kernel is used and the centre position

Irene Cheng Jan 2008

Sharp & Blurred Edges

Grey values 255

Sharp edge Blurred edge

Page 28: CMPUT 206 - ugweb.cs.ualberta.caugweb.cs.ualberta.ca/~c206/W08/resources/Lecture...Kernel (nxn pixels) 1. Suppose a nxn (n is an odd number) kernel is used and the centre position

Irene Cheng Jan 2008

Rank or “order statistic” filtering

• Non-linear

• In image processing it is usually necessary to perform high degree of noise reduction in an image before performing higher-level processing steps, such as edge detection. A non-linear digital filtering technique is often used to remove noise from images or other signals.

Page 29: CMPUT 206 - ugweb.cs.ualberta.caugweb.cs.ualberta.ca/~c206/W08/resources/Lecture...Kernel (nxn pixels) 1. Suppose a nxn (n is an odd number) kernel is used and the centre position

Irene Cheng Jan 2008

Rank or “order statistic” filtering (continued)

• Compile a list of grey scales in the neighborhood of a given pixel, sort them in ascending order and select a value as the new value– Median filter; any structure that occupies less than

half of the filter’s neighborhood will be eliminated– Minimum filter and Maximum filter– Range filter: output the difference between the

maximum and minimum

Page 30: CMPUT 206 - ugweb.cs.ualberta.caugweb.cs.ualberta.ca/~c206/W08/resources/Lecture...Kernel (nxn pixels) 1. Suppose a nxn (n is an odd number) kernel is used and the centre position

Irene Cheng Jan 2008

Rank or “order statistic” filtering (continued)

– Hybrids of linear and non-linear filters, e.g. α-trimmed mean filter: sorts the neighborhood into ascending order, discards a number of them and outputs the mean of the remaining

(α is the number of values removed, in the range [0, (n2 – 1)/2] from each end of the list)

∑−

+=−

α

αα

2

12 2

1n

i

ifn

• when α = 0: mean filter• when α = (n2 – 1)/2: median filter

Page 31: CMPUT 206 - ugweb.cs.ualberta.caugweb.cs.ualberta.ca/~c206/W08/resources/Lecture...Kernel (nxn pixels) 1. Suppose a nxn (n is an odd number) kernel is used and the centre position

Irene Cheng Jan 2008

Median filter

• Non-linear technique• Consider all pixels inside the filter.• Compile a list of grey values and sort them in ascending

order.• select a value as the new value

– Median filter; any structure that occupies less than half of the filter’s neighborhood will be eliminated

• Have the advantage of non kernel-based; no problem to filter a smaller neighborhood at the corners or sides of the image

• The shape of the filter applied at the corner can give different results, e.g. square vs. cross-shaped

Page 32: CMPUT 206 - ugweb.cs.ualberta.caugweb.cs.ualberta.ca/~c206/W08/resources/Lecture...Kernel (nxn pixels) 1. Suppose a nxn (n is an odd number) kernel is used and the centre position

Irene Cheng Jan 2008

Rank filtering examples

23 20 5 12 8

8

13

18

10

What is the filtered value of the centre pixel after applying a:

1. median filter 122. mean filter 133. maximum filter 234. minimum filter 55. range filter 186. 4-trim filter 127. 0-trim filter 138. 2-trim filter 12

Page 33: CMPUT 206 - ugweb.cs.ualberta.caugweb.cs.ualberta.ca/~c206/W08/resources/Lecture...Kernel (nxn pixels) 1. Suppose a nxn (n is an odd number) kernel is used and the centre position

Irene Cheng Jan 2008

Median filtering example

(a) (b)

What is the filtered value of P in (a) ?What is the filtered value of P in (b) ?

The square and the cross are both median filters

p P

Page 34: CMPUT 206 - ugweb.cs.ualberta.caugweb.cs.ualberta.ca/~c206/W08/resources/Lecture...Kernel (nxn pixels) 1. Suppose a nxn (n is an odd number) kernel is used and the centre position

Irene Cheng Jan 2008

References

1. “Digital Image Processing − a practical introduction using Java” Nick Efford, Pearson Education 2000.