Sie sind auf Seite 1von 41

Spatial Filtering (Chapter 3)

CS474/674 - Prof. Bebis

Spatial Filtering Methods (or Mask Processing Methods)

output image

Spatial Filtering
The word filtering has been borrowed from the frequency domain. Filters can be classified as:
Low-pass (i.e., preserve low frequencies) High-pass (i.e., preserve high frequencies) Band-pass (i.e., preserve frequencies within a band) Band-reject (i.e., preserve frequencies outside a band)

Spatial Filtering (contd)


Spatial filtering consists of:
(1) A neighborhood (2) A predefined operation that is performed on the pixels inside the neighborhood
output image

Spatial Filtering (contd)


Typically, the neighborhood is rectangular and its size is much smaller than that of f(x,y) - e.g., 3x3 or 5x5

Spatial filtering (contd)


Example: apply a mask of weights

output image

Spatial filtering (contd)


Assume the origin of the mask is the center of the mask.
g ( x, y )

s 1 t 1

w(s, t ) f ( x s, y t )

for a K x K mask:
g ( x, y )
s K /2 t K /2

K /2

K /2

w( s, t ) f ( x s, y t )

Spatial Filtering (contd)


A filtered image is generated as the center of the mask visits every pixel in the input image.

output image

Mask Elements
The sum of weights in a mask affect the overall intensity of the resulting image. Typically, a mask is normalized such that the sum of weights is equal to one. When a mask contains negative values, it is normalized such that the sum of weights is equal to zero.

1/9

1/16

1/4

Handling Pixels Close to Boundaries


pad with zeroes
0 0 0 .0 0 0 0 .0

or

Handling Pixels Close to Boundaries (contd)

Remove extra rows/columns from the result

Linear vs Non-Linear Spatial Filtering Methods


A filtering method is linear when the output is a weighted sum of the input pixels.

Methods that do not satisfy this property are called non-linear.


e.g.,

Linear Spatial Filtering Methods


Two main linear spatial filtering methods:
Correlation Convolution

Correlation
g
Output Image

g ( x, y ) w( x, y ) f ( x, y )

s K /2 t K /2

K /2

K /2

w( s, t ) f ( x s, y t )

Correlation (contd)
It is often used in applications where it is necessary to measure the similarity between images or parts of images.

Convolution
Same as correlation except that the mask is flipped both horizontally and vertically.
g ( x, y ) w( x, y ) f ( x, y )

s K /2 t K /2

K /2

K /2

w( s, t ) f ( x s, y t )

Note: if w(x,y) is symmetric, that is w(x,y)=w(-x,-y), then convolution is equivalent to correlation!

Example

Correlation:

Convolution:

How do we choose the elements of a mask?


Samples from functions having certain properties. Normalize mask elements.

Gaussian

1st derivative of Gaussian

2nd derivative of Gaussian

Filters
Smoothing (i.e., low-pass filters)
The elements of the mask must be positive

Sharpening (i.e., high-pass filters)


The elements of the mask contain both positive and negative weights.
1st derivative of Gaussian
2nd derivative of Gaussian

Gaussian

Smoothing Filters: Averaging (Low-pass filtering)

Smoothing Filters: Averaging (contd)


The size of the mask determines the degree of smoothing and loss of detail.
original 3x3 5x5 7x7

15x15

25x25

Smoothing Filters: Averaging (contd)


Example: extract, largest, brightest objects
15 x 15 averaging image thresholding

Smoothing filters: Gaussian


The weights are samples from a Gaussian function

The value of determines the degree of smoothing.

Smoothing filters: Gaussian (contd)


As increases, more samples must be obtained to represent the Gaussian function accurately.

Smoothing Filters: Median Filtering (non-linear)


Very effective for removing salt and pepper noise (i.e., random occurrences of black and white pixels).

averaging

median filtering

Smoothing Filters: Median Filtering (contd)


Replace each pixel by the median in a neighborhood around the pixel.

Sharpening Filters (High Pass filtering)


Useful for emphasizing transitions in intensity (e.g., edges). Compute intensity differences in local image regions.

Sharpening Filters (contd)


Note that the response of high-pass filtering might be negative. Values must be re-mapped to [0, 255]
original image sharpened images

Sharpening Filters: Unsharp Masking


Obtain a sharp image by subtracting a lowpass filtered image from the original image:

Sharpening Filters: High Boost


Image sharpening emphasizes edges but details (i.e., low frequency components) might be lost. High boost filter: amplify input image

(A-1)

Sharpening Filters: Unsharp Masking (contd)


If A=1, we get a high pass filter If A>1, part of the original image is added back to the high pass filtered image.
A high boost image looks like the original image with a relative degree of edge enhancement.

Sharpening Filters: Unsharp Masking (contd)

A=1.4

A=1.9

Sharpening Filters: Derivatives


Taking the derivative of an image results in sharpening the image. The derivative of am image can be computed using the gradient.

Sharpening Filters: Derivatives (contd)


The gradient is a vector which has magnitude and direction:

f f || | or | x y
(cheaper approximation)

Magnitude: provides information about edge strength. Direction: perpendicular to the direction of the edge.

Sharpening Filters: Gradient Computation


Approximate gradient using finite differences.

sensitive to vertical edges x sensitive to horizontal edges

Sharpening Filters: Gradient Computation (contd)

Sharpening Filters: Gradient Computation (contd)


We can implement and using the following masks:
(x+1/2,y)

good approximation at (x+1/2,y) good approximation at (x,y+1/2)

(x,y+1/2) *

Example: approximate gradient at z5

Sharpening Filters: Gradient Computation (contd)


A different approximation of the gradient:
good approximation
(x+1/2,y+1/2)

We can implement

and

using the following masks:

Sharpening Filters: Gradient Computation (contd)


Example: approximate gradient at z5

Other approximations

Sobel

Sharpening Filters: Gradient Computation (contd)


Prewitt

Combine with thresholding to better visualize edges

If mag(grad)>25, set to white otherwise, set to black

Sharpening Filters: Gradient Computation (contd)


Detect imperfections in contact lenses using Sobel masks.

Das könnte Ihnen auch gefallen