lecture: edge detection - university of...

46
Irene Cheng CMPUT 299 Winter 2007 Lecture: Edge Detection Irene Cheng

Upload: others

Post on 26-Jan-2021

1 views

Category:

Documents


0 download

TRANSCRIPT

  • Irene Cheng

    CMPUT 299Winter 2007

    Lecture: Edge Detection

    Irene Cheng

  • Irene Cheng

    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 ?

  • Irene Cheng

    “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

  • Irene Cheng

    “Pixel” as the basic unit

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

    (0, 0) X

    Y

    (256, 256)

  • Irene Cheng

    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

  • Irene Cheng

    Definition of Edges

    Use Photoshop “Find Edges” tool:

    a b c d e f g h i

  • Irene Cheng

    Sharpen edges in images – Photoshop DemoSharp

    Filter – sharper – sharpen edges/sharpen more

    Blur

    Increase contrast and sharpen edges

    High contrast Low contrast

  • Irene Cheng

    Sharpen edges in images – Photoshop DemoBright

    Filter – sharper – sharpen edges/sharpen more

    Low luminosity

    Discover features

    Sharpen and image-adjust-curves

  • Irene Cheng

    Region growing + Edge detection

    The Magic Wand Tool in Photoshop

  • Irene Cheng

    Can Computer do as well as human ?

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

  • Irene Cheng

    Edge detection techniques

    • Noise reduction or elimination– Filtering or masking

    • Edge enhancement• Edge localization

    – Threshold

    111111111

    91

    111.0111.0111.0111.0111.0111.0111.0111.0111.0

    or

    0 0 00 99 00 0 0

    An example of Low Pass Filtering:

    An example of High Pass Filtering:

    −−−−−−−−

    111181111

    0 0 00 2 00 0 0

    Examples ofImage pixels

    Concept of filtering

  • Irene Cheng

    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

    ?22 yx gg +

    117 4036 8785 5244 76

    Answer:

  • Irene Cheng

    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

    =+ 22 yx gg

    Detected edge

  • Irene Cheng

    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

    ygg1tanθ

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

    yx ggg +=

  • Irene Cheng

    Find edges in images – Photoshop DemoExtract R, G, or BPixels, or luminosity values

    ApplyEdgeDetection

    Stylize – find edges

  • Irene Cheng

    Find edges in images – Photoshop Demo

    increasing threshold

    T1 T2 T3

    Find Edges

    By applying different thresholds T, the results are different: -Assign one value v1 if < T, e.g. v1=0 valueAssign another value v2 if >= T, e.g. v2=255 value You can switch the resulting

    values to show a white background

  • Irene Cheng

    Find edges in images – Photoshop DemoIncrease contrast

    Apply threshold T3

    Why is there no change ?

  • Irene Cheng

    Concept

    Pixel value p1 Pixel value p2

    p1 p2

    p2 – p1 = ∆p (e.g. gradient magnitude)

    Compare with threshold T.If T > ∆p => no edge

    u1 u2

    Apply edge detection technique to generate a bigger ∆u value around edges, so that ∆u > T

    Note: the processing is not in place

  • Irene Cheng

    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

  • Irene Cheng

    Convolution operation

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

    ),(),( yxfhyxg xx ×=

    ),(),( yxfhyxg yy ×=

    where the (Prewitt) kernels are:

    −−−

    =101101101

    xh

    −−−=

    111000111

    yh

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

  • Irene Cheng

    Convolution operation (continued)

    −−−

    =101101101

    xh

    −−−=

    111000111

    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?

  • Irene Cheng

    Simple Edge Detectors

    Result from gradient Result from Prewitt kernels

    One edge but thick

    Threshold=50 Threshold=100 or 150

  • Irene Cheng

    (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

  • Irene Cheng

    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

    OOPP range

    range+×−=

  • Irene Cheng

    Advantage of a bigger grey value range before processing

    Findedges Threshold

    128

    Map from 0-100To 0-255

    Threshold50

  • Irene Cheng

    Advantage of a bigger grey value range before thresholding

    Findedges

    Map from 0-100To 0-255

    Scale 0-200To 0-255Then applyThreshold128

    Threshold128

    Threshold128

  • Irene Cheng

    Sobel kernels

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

    −−−

    =101202101

    xh

    −−−=

    121000121

    yh

    Original image convoluation with hx with hy

    Cross from grey to white

    From white to grey

    +ve value

    -ve value

  • Irene Cheng

    • 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

  • Irene Cheng

    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 blurredx

    Gre

    y va

    lues

  • Irene Cheng

    Sobel and first-order-derivatives

    • Sobel kernels give first-order-derivatives that measure the local slope of a surface in the x and y directions.

    Grey values 255

    Sharp edge Blurred edge

  • Irene Cheng

    The Laplacian

    • The second-order-derivatives ( ) measure the rate at which the slope of the grey scale surface changes in the x and y directions, and therefore can be used for edge localization.

    – Let 0-255 represent dark to bright grey. On the darker side, is decreasing (-ve) and on the brighter side, is increasing (+ve).

    f2∇

    f2∇f2∇

  • Irene Cheng

    Sobel gives first derivatives (increase when approaching the border and decrease when leaving the border.)

    Second order derivative measures the rate at which the slope of the grey level changes with distance

    It changes sign at the centre of the edge, so the edge can be localized at the zero crossing.

    Gre

    y va

    lues

    1st & 2nd order derivatives

    1 pixel at the zero crossing

    band

    Gra

    dien

    t

  • Irene Cheng

    Another example of 2nd order derivatives

    pixel1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

    grey values50 50 50 50 50 59 64 77 85 100 109 124 132 145 149 157 157 157 157 157

    gradient values0 0 0 0 9 14 18 21 23 24 24 23 21 17 12 6 0 0 0 0

    rate of slope change5 4 3 2 1 0 -1 -2 -4 -5 -6

    -1 -1 -1 -1 1 2 1 1-ve decreasing -> increasing -> +ve

    02040

    6080

    100120

    140160180

    1 3 5 7 9 11 13 15 17 19

    pixel (x-direction)

    grey

    val

    ues

    0

    5

    10

    15

    20

    25

    30

    1 3 5 7 9 11 13 15 17 19

    pixel (x-direction)

    grad

    ient

    val

    ues

    Assuming threshold is 20

  • Irene Cheng

    The Laplacian (continued)

    • The Laplacian of an image f combines the second-order-derivatives:

    2

    2

    2

    22

    y

    f

    x

    ff∂

    ∂+∂

    ∂=∇

    •The Laplacian is seldom used on its own because it is sensitive to noise. It is often used as part of the Laplacian of Gaussian (LoG) filter.

    – a Gaussian filter to blur the image– a Laplacian to enhance edges– localization is done by finding zero crossings

  • Irene Cheng

    [reference 1]

    Radially-symmetric two-dimensional Gaussian with r2 = x2 + y2:

    −=2

    2

    2exp)(

    σrrh

    The Laplacian is:

    −=∇ 2

    2

    4

    222

    2exp

    σσσ rrh

    LoG

    The standard deviation value σ acts as a threshold value

  • Irene Cheng

    The Canny Edge Detector

    • There is a trade-off between noise reduction and edge localization. Improved noise reduction is at the expense of good localization. The Canny detector provides the best compromise between the two.

    1. Smoothing step: a Gaussian low pass filter2. Enhancement step: calculate the gradient vector at

    each pixel of the smoothed image• Gaussian separability: compute x and y gradients with one-

    dimensional kernels3. …

  • Irene Cheng

    The Canny Edge Detector (continued)

    3. Localization step:• non-maximal suppression

    – Thins the wide ridges around local maxima in gradient magnitude down to only one pixel wide

    [reference 1]

  • Irene Cheng

    Algorithm: Non-maximal suppression in the Canny edge detector

    Create an output image, gs, with the same dimensions as gFor all pixel coordinates, x and y, do

    Approximate θ(x, y) by , one of the angles 0°, 45°, 90°, 135°if g(x, y)

  • Irene Cheng

    Hysteresis thresholding

    Separate into gradient and θimages. On the gradient image, apply hysteresisthresholding. Assuming Thighhas been applied, expand the neighborhood using Tlow. Some techniques use 8 neighbors, and some use neighbors along a line normal to the gradient orientation at the edge pixel only

    [reference 1]

  • Irene Cheng

    Rank or “order statistic” filtering

    • Non-linear• In image processing it is usually necessary to

    perform high degree of noise reductionin 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.

  • Irene Cheng

    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

  • Irene Cheng

    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

  • Irene Cheng

    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

  • Irene Cheng

    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

  • Irene Cheng

    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

  • Irene Cheng

    References

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