Spatial Filtering: Image Processing Fourth Year Class ECE Department Semester Two, 2017

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 23

Image processing

Fourth year class


ECE Department
Semester two, 2017
Lecture 5

Spatial Filtering

https://sites.google.com/site/elce4011imageprocessing

Prof Ahmed Khorsheed Al-Sulaifanie


ECE Department
College of Engineering
Sunday, April 16, 2017
Spatial Filtering
The two principal terms used to identify this operation are neighborhood processing and spatial
filtering, with the second term being more common.
If the computations performed on the pixels of the neighborhoods are linear, the operation is called
linear spatial filtering (the term spatial convolution also used); otherwise it is called nonlinear
spatial filtering.

Subimage pixels

Mask has the


same dimensions
of the
neighborhood
subimage pixel
The neighborhood
dimension of subimage
pixels
Smoothing Linear Filters/ Averaging Filters
Smoothing Linear Filters/ Averaging Filters
 The response of a smoothing linear spatial filter is simply the average of the
pixels contained in the neighborhood of the filter mask.
 These kind of filters are called averaging filters or lowpass filters.

The 3x3 averaging method is one example of the mask operation or Spatial filtering.
The mask operation has the corresponding mask (sometimes called window or template).
w The mask contains coefficients to be multiplied with pixel values.

Example : moving averaging


w(1,1) w(2,1) w(3,1)
1 1 1
w(1,2) w(2,2) w(3,2)
1
1 1 1
9
w(3,1) w(3,2) w(3,3) 1 1 1

Mask coefficients The mask of the 3x3 moving average


filter has all coefficients = 1/9
General action of Mask operation
The mask operation at each point is performed by:
1. Move the reference point (center) of mask to the location to be computed
2. Compute sum of products between mask coefficients and pixels in subimage under the mask.

… Mask frame

p(1,1) p(2,1) p(3,1) w(1,1) w(2,1) w(3,1)

… …
p(2,1) p(2,2) p(3,2) w(1,2) w(2,2) w(3,2)

p(1,3) p(2,3) p(3,3) w(3,1) w(3,2) w(3,3)


Mask coefficients
Subimage
N M
The reference point y   w(i, j )  p (i, j )
of the mask i 1 j 1
Illustration: Computing average value within 3X3 window

Step 1: Move the window to the first location where we want to compute the average value
and then select only pixels inside the window.

Step 2: Compute the average


2 4 1 2 6 2 2 4 1
value
9 2 3 4 4 4 9 2 3 3 3
1
7 2 9 7 6 7 7 2 9 y    p(i, j )
i 1 j 1 9
5 2 3 6 1 5 Sub image p

7 4 2 5 1 2 Step 3: Place the


result at the pixel
Original image
in the output image

Step 4: Move the 4.3


window to the next
location and go to Step 2
Output image
Smoothing Linear Filter : Moving Average
Application : noise reduction
and image smoothing

Disadvantage: lose sharp details

(Images from Rafael C. Gonzalez and Richard E.


Wood, Digital Image Processing, 2nd Edition.
Smoothing Linear Filter (cont.)

(Images from Rafael C. Gonzalez and Richard E.


Wood, Digital Image Processing, 2nd Edition.
Steps of spatial filtering over the whole image

The spatial filtering on the whole image is given by:

1. Move the mask over the image at each location.


2. Compute sum of products between the mask coefficients and pixels inside subimage
under the mask.
3. Store the results at the corresponding pixels of the output image.
4. Move the mask to the next location and go to step 2 until all pixel locations have been
used.
Examples of Spatial Filtering Masks
Examples of the masks
y
Sobel operators 3x3 moving average filter

x -1 0 1 -1 -2 -1 1 1 1
1
-2 0 2 0 0 0 1 1 1
9
-1 0 1 1 2 1 1 1 1
P P
to compute to compute
x y
3x3 sharpening filter

-1 -1 -1
1
-1 8 -1
9
-1 -1 -1
Order-Statistic Filters
Original image

subimage

Statistic parameters
Mean, Median, Mode,
Min, Max, Etc.

Moving
window

Output image
Median Filter
A median filter is good for removing impulse, isolated noise

highest gray level


lowest gray level
Median

Sorted
Moving array
image
window
Salt noise Filter output

Pepper noise
Normally, impulse noise has high magnitude
and is isolated. When we sort pixels in the
moving window, noise pixels are usually
at the ends of the array.

Therefore, it’s rare that the noise pixel will be a median value.
Order-Statistic Filters: Median Filter

(Images from Rafael C. Gonzalez and Richard E.


Wood, Digital Image Processing, 2nd Edition.
1D Convolution

• For any given n, how to obtain

Step 1: time reversal of signal g(k)g(-k) )


Step 2: shift g(-k) by n samples to obtain g(n-k)
Step 3: multiply f(k) and g(n-k) for each k and then take the summation over k.
Note
change variable n to get the whole sequence.

A A

13
2D Convolution
2D Convolution is a common image processing technique that changes the intensities of a
pixel to reflect the intensities of the surrounding pixels. A common use of convolution is to
create image filters. Convolution is used to get the popular image effects like blur, sharpen,
and edge detection.
In a similar manner of 1D, the 2D convolution of Kernel w(x,y) and image f(x,y), is given by
the expression:

Interchange between w and f in shifting give


the same output
w*f = f*w
2D convolution operation Example

2D convolution involves calculating the


weighted sum of a neighborhood of
pixels. The weights are taken from a
convolution kernel.
Each value from the neighborhood of w(x,y)
f(x,y)
pixels is multiplied with its opposite on
the matrix.
For example, the top-left of the
neighbor is multiplied by the bottom-
right of the kernel. All these values are
summed up to calculate the result of the
convolution.
Consider a 3x3 neighborhood. Given a
convolution kernel (mask) ω, you need
to rotate the mask with 180o as shown.
2D convolution operation Example
Add 0 padding to image f(x,y) and start moving the 180o rotated kernel

0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 00 0 0 0 0
0 0 0 0 00 0 0 0 0
0 0 0 0 00 0 0 0 0

0 0 0
0
0
2D convolution operation Example
2D convolution operation Example

Continue on the operation of moving, multiplying and sum the final result of
Convolution will be as shown
Correlation Similarity to Convolution
No folding (time-reversal)

rxy  l   x  l   y  l  ryx  l   y  l   x  l 

In Matlab:
rxy = Conv(x,fliplr(y))

Autocorrelation is Correlation of a signal with itself


rxx  l    x  n  x  n  l   r  l 
n 
xx l  0, 1, 2,
Convolution, Correlation and Autocorrelation
2D Correlation
Correlation is nearly identical to convolution with only a minor difference,
where instead of multiplying the pixel by the opposite in the kernel, you multiply
it by the equivalent (top-left multiplied by top-left).

Consider a 3x3 neighborhood. Given a correlation kernel (mask) ω, and input


image f,
Home work
of image matrix f(x, y) with kernel w(x,y)
(a) Compute the 2D convolution.
(b) Compute the 2D correlation.
(c) Write matlab codes for part a and b.

2 4 1 2 6
-1 0 1
9 2 3 4 4
-2 0 2
7 2 9 7 6
-1 0 1
5 2 3 6 1

kernel w(x,y) image matrix f(x, y)

You might also like