37
Sliding Window Filters and Edge Detection Longin Jan Latecki Computer Graphics and Image Processing CIS 601 – Fall 2004

Sliding Window Filters and Edge Detection Longin Jan Latecki Computer Graphics and Image Processing CIS 601 – Fall 2004

Embed Size (px)

Citation preview

Page 1: Sliding Window Filters and Edge Detection Longin Jan Latecki Computer Graphics and Image Processing CIS 601 – Fall 2004

Sliding Window Filters and Edge Detection

Longin Jan LateckiComputer Graphics and Image Processing

CIS 601 – Fall 2004

Page 2: Sliding Window Filters and Edge Detection Longin Jan Latecki Computer Graphics and Image Processing CIS 601 – Fall 2004

Linear Image Filters 

Linear operations calculate the resulting value in the output image pixel f(i,j) as a linear combination of brightness in a local neighborhood of the pixel h(i,j) in the input image. This equation is called to discrete convolution:

a

am

b

bn

njmihnmwhwjif ),(),(*),(

Function w is called a convolution kernel or a filter mask. In our case it is a rectangle of size (2a+1)x(2b+1).

Page 3: Sliding Window Filters and Edge Detection Longin Jan Latecki Computer Graphics and Image Processing CIS 601 – Fall 2004
Page 4: Sliding Window Filters and Edge Detection Longin Jan Latecki Computer Graphics and Image Processing CIS 601 – Fall 2004

Exercise: Compute the 2-D linear convolution of the following two signal X with mask w. Extend the signal X with 0’s where needed.

101,

143

654

321

wX

Page 5: Sliding Window Filters and Edge Detection Longin Jan Latecki Computer Graphics and Image Processing CIS 601 – Fall 2004

Averaging of brightness values is a special case of discrete convolution. For a 3 x 3 neighborhood the convolution mask w is

111

111

111

9

1w

Applying this mask to an image results in smoothing.

Matlab example program is filterEx1.m

•Local image smoothing can effectively eliminate impulsive noise or degradations appearing as thin stripes, but does not work if degradations are large blobs or thick stripes.

Image smoothing = image blurring

Page 6: Sliding Window Filters and Edge Detection Longin Jan Latecki Computer Graphics and Image Processing CIS 601 – Fall 2004

The significance of the central pixel may be increased to better reflect properties of Gaussian noise:

111

121

111

10

1w

121

242

121

16

1w

Page 7: Sliding Window Filters and Edge Detection Longin Jan Latecki Computer Graphics and Image Processing CIS 601 – Fall 2004

Nonlinear Image Filters

Median is an order filter, it uses order statistics.Given an NxN window W(x,y) with pixel (x,y) being themidpoint of W, the pixel intensity values of pixels in Ware ordered from smallest to the largest, as follow:

2...321 NIIII

Median filter selects the middle value as the value of (x,y).

104)),((

858895

104114100

104110110

),(

yxWMedianyxW

Page 8: Sliding Window Filters and Edge Detection Longin Jan Latecki Computer Graphics and Image Processing CIS 601 – Fall 2004

Morphological Filters

110)),((

858895

104104100

104110110

),(

yxWdilationyxW

dilationerosiongclo

erosiondilationopening

yxWyxWerosion

yxWyxWdilation

sin

)),(min()),((

)),(max()),((

Page 9: Sliding Window Filters and Edge Detection Longin Jan Latecki Computer Graphics and Image Processing CIS 601 – Fall 2004

Binary Case

Black pixels have value 0 White (background pixels) have value 1

Page 10: Sliding Window Filters and Edge Detection Longin Jan Latecki Computer Graphics and Image Processing CIS 601 – Fall 2004

For comparison see Order Filters on

www.ee.siue.edu/~cvip/CVIPtools_demos/mainframe.shtml

Homework 3

Implement in Matlab a linear filter for image smoothing (blurring)using convolution method (filter2 function).Implement also nonlinear filters: median, opening, and closing.Apply them to images noise_1.gif, noise_2.gif inwww.cis.temple.edu/~latecki/CIS601-04\Lectures\Images

Compare the results.

Page 11: Sliding Window Filters and Edge Detection Longin Jan Latecki Computer Graphics and Image Processing CIS 601 – Fall 2004

Edge Detection

• What are edges in an image?

• Edge Detection

• Edge Detection Methods

• Edge Operators

• Matlab Program

• Performance

Page 12: Sliding Window Filters and Edge Detection Longin Jan Latecki Computer Graphics and Image Processing CIS 601 – Fall 2004

What are edges in an image?

Edges are those places in an image that correspond to object boundaries.

Edges are pixels where image brightness changes abruptly.

 

Brightness vs. Spatial Coordinates

Page 13: Sliding Window Filters and Edge Detection Longin Jan Latecki Computer Graphics and Image Processing CIS 601 – Fall 2004

More About Edges

An edge is a property attached to an individual pixel and is calculated from the image function behavior in a neighborhood of the pixel.

It is a vector variable (magnitude of the gradient, direction of an edge) .

Page 14: Sliding Window Filters and Edge Detection Longin Jan Latecki Computer Graphics and Image Processing CIS 601 – Fall 2004

Image To Edge Map

Page 15: Sliding Window Filters and Edge Detection Longin Jan Latecki Computer Graphics and Image Processing CIS 601 – Fall 2004

Edge Detection

Edge information in an image is found by looking at the relationship a pixel has with its neighborhoods.

If a pixel’s gray-level value is similar to those around it, there is probably not an edge at that point.

If a pixel’s has neighbors with widely varying gray levels, it may present an edge point.

Page 16: Sliding Window Filters and Edge Detection Longin Jan Latecki Computer Graphics and Image Processing CIS 601 – Fall 2004

Edge Detection Methods

Many are implemented with convolution mask and based on discrete approximations to differential operators.

Differential operations measure the rate of change in the image brightness function.

Some operators return orientation information. Other only return information about the existence of an edge at each point.

Page 17: Sliding Window Filters and Edge Detection Longin Jan Latecki Computer Graphics and Image Processing CIS 601 – Fall 2004

A 2D grayvalue - image is a 2D -> 1D function

v = f(x,y)

Page 18: Sliding Window Filters and Edge Detection Longin Jan Latecki Computer Graphics and Image Processing CIS 601 – Fall 2004

Edge detectors •locate sharp changes in the intensity function •edges are pixels where brightness changes abruptly.

•Calculus describes changes of continuous functions using derivatives; an image function depends on two variables - partial derivatives. •A change of the image function can be described by a gradient that points in the direction of the largest growth of the image function.•An edge is a property attached to an individual pixel and is calculated from the image function behavior in a neighborhood of the pixel. •It is a vector variable:magnitude of the gradient and direction

Page 19: Sliding Window Filters and Edge Detection Longin Jan Latecki Computer Graphics and Image Processing CIS 601 – Fall 2004

•The gradient direction gives the direction of maximal growth of the function, e.g., from black (f(i,j)=0) to white (f(i,j)=255).

•This is illustrated below; closed lines are lines of the same brightness. •Boundary and its parts (edges) are perpendicular to the direction of the gradient.

Page 20: Sliding Window Filters and Edge Detection Longin Jan Latecki Computer Graphics and Image Processing CIS 601 – Fall 2004
Page 21: Sliding Window Filters and Edge Detection Longin Jan Latecki Computer Graphics and Image Processing CIS 601 – Fall 2004

•The gradient magnitude and gradient direction are continuous image functions, where arg(x,y) is the angle (in radians) from the x-axis to the point (x,y).

Page 22: Sliding Window Filters and Edge Detection Longin Jan Latecki Computer Graphics and Image Processing CIS 601 – Fall 2004

•A digital image is discrete in nature, derivatives must be approximated by differences. •The first differences of the image g in the vertical direction (for fixed i) and in the horizontal direction (for fixed j) •n is a small integer, usually 1.

The value n should be chosen small enough to provide a good approximation to the derivative, but large enough to neglect unimportant changes in the image function.

Page 23: Sliding Window Filters and Edge Detection Longin Jan Latecki Computer Graphics and Image Processing CIS 601 – Fall 2004

Roberts Operator

Mark edge point only No information about edge orientation Work best with binary images Primary disadvantage:

High sensitivity to noise Few pixels are used to approximate the gradient

Page 24: Sliding Window Filters and Edge Detection Longin Jan Latecki Computer Graphics and Image Processing CIS 601 – Fall 2004

Roberts Operator (Cont.)

First form of Roberts Operator

Second form of Roberts Operator

22 ),1()1,()1,1(),( crIcrIcrIcrI

|),1()1,(||)1,1(),(| crIcrIcrIcrI

10

011h

01

102h

Page 25: Sliding Window Filters and Edge Detection Longin Jan Latecki Computer Graphics and Image Processing CIS 601 – Fall 2004

Prewitt Operator

Looks for edges in both horizontal and vertical directions, then combine the information into a single metric.

Edge Magnitude = Edge Direction =

111

000

111

y

101

101

101

x

22 yx

x

y1tan

Page 26: Sliding Window Filters and Edge Detection Longin Jan Latecki Computer Graphics and Image Processing CIS 601 – Fall 2004

Sobel Operator

Similar to the Prewitt, with different mask coefficients:

Edge Magnitude = Edge Direction =

121

000

121

y

101

202

101

x

22 yx

x

y1tan

Page 27: Sliding Window Filters and Edge Detection Longin Jan Latecki Computer Graphics and Image Processing CIS 601 – Fall 2004

Kirsch Compass Masks

Taking a single mask and rotating it to 8 major compass orientations: N, NW, W, SW, S, SE, E, and NE.

The edge magnitude = The maximum value found by the convolution of each mask with the image.

The edge direction is defined by the mask that produces the maximum magnitude.

Page 28: Sliding Window Filters and Edge Detection Longin Jan Latecki Computer Graphics and Image Processing CIS 601 – Fall 2004

Kirsch Compass Masks (Cont.)

The Kirsch masks are defined as follows:

EX: If NE produces the maximum value, then the edge direction is Northeast

533

503

533

E

333

503

553

NE

333

303

555

N

333

305

355

NW

335

305

335

W

355

305

333

SW

555

303

333

S

553

503

533

SE

Page 29: Sliding Window Filters and Edge Detection Longin Jan Latecki Computer Graphics and Image Processing CIS 601 – Fall 2004

Robinson Compass Masks

Similar to the Kirsch masks, with mask coefficients of 0, 1, and 2:

101

202

101

E

012

101

210

NE

121

000

121

N

210

101

012

NW

101

202

101

W

012

101

210

SW

121

000

121

S

210

101

012

SE

Page 30: Sliding Window Filters and Edge Detection Longin Jan Latecki Computer Graphics and Image Processing CIS 601 – Fall 2004

•Sometimes we are interested only in edge magnitudes without regard to their orientations. •The Laplacian may be used.

•The Laplacian has the same properties in all directions and is therefore invariant to rotation in the image.

•The Laplace operator is a very popular operator approximating the second derivative which gives the gradient magnitude only.

Page 31: Sliding Window Filters and Edge Detection Longin Jan Latecki Computer Graphics and Image Processing CIS 601 – Fall 2004

Laplacian Operators

Edge magnitude is approximated in digital images by a convolution sum.

The sign of the result (+ or -) from two adjacent pixels provide edge orientation and tells us which side of edge brighter

Page 32: Sliding Window Filters and Edge Detection Longin Jan Latecki Computer Graphics and Image Processing CIS 601 – Fall 2004

Laplacian Operators (Cont.)

Masks for 4 and 8 neighborhoods

Mask with stressed significance of the central pixel or its neighborhood

010

141

010

111

181

111

121

242

121

212

141

212

Page 33: Sliding Window Filters and Edge Detection Longin Jan Latecki Computer Graphics and Image Processing CIS 601 – Fall 2004

Performance

Please try the following link Matlab demo. To run type EDgui

Sobel and Prewitt methods are very effectively providing good edge maps.

Kirsch and Robinson methods require more time for calculation and their results are not better than the ones produced by Sobel and Prewitt methods.

Roberts and Laplacian methods are not very good as expected.

Page 34: Sliding Window Filters and Edge Detection Longin Jan Latecki Computer Graphics and Image Processing CIS 601 – Fall 2004

•Gradient operators can be divided into three categories

I. Operators approximating derivatives of the image function using differences. •rotationally invariant (e.g., Laplacian) need one convolution mask only. Individual gradient operators that examine small local neighborhoods are in fact convolutions and can be expressed by convolution masks.

•approximating first derivatives use several masks, the orientation is estimated on the basis of the best matching of several simple patterns. Operators which are able to detect edge direction. Each mask corresponds to a certain direction.

Page 35: Sliding Window Filters and Edge Detection Longin Jan Latecki Computer Graphics and Image Processing CIS 601 – Fall 2004

II. Operators based on the zero crossings of the image function second derivative (e.g., Marr-Hildreth or Canny edge detector).

III. Operators which attempt to match an image function to a parametric model of edges. Parametric models describe edges more precisely than simple edge magnitude and direction and are much more computationally intensive.

The categories II and III will not be covered here;

Page 36: Sliding Window Filters and Edge Detection Longin Jan Latecki Computer Graphics and Image Processing CIS 601 – Fall 2004

A Quick Note

Matlab’s image processing toolbox provides edge function to find edges in an image:

I = imread('rice.tif'); BW1 = edge(I,'prewitt'); BW2 = edge(I,'canny'); imshow(BW1) figure, imshow(BW2) Edge function supports six different edge-finding

methods: Sobel, Prewitt, Roberts, Laplacian of Gaussian, Zero-cross, and Canny.

Page 37: Sliding Window Filters and Edge Detection Longin Jan Latecki Computer Graphics and Image Processing CIS 601 – Fall 2004

Homework 4:Edge Map in Matlab

Select an example image. Which of the six edge detection method

provided by Matlab works best for you. Show the result of edge map