Foundations of Computing

CS 450 Lecture 8
Spatial Filters
Processing Digital Images
• digital images are often processed using “spatial filters”
Motion
Filter
• digital filters are based on mathematical functions that
operate on the pixels of the image
• Grayscale Transformation Functions
transform each pixel uniformly and
independently according to the function
regardless of its location in the image
• Spatial filters transform a pixel depending
upon its relation to surrounding ones
Local Filters
Blurring
• Sharpening
• Unsharp Masking
• Edge and line detection
• Noise filters
Blurring Algorithm
For (every pixel in column x)
{ For (every pixel in row y of column x)
{
average image[x,y] with it’s neighbors;
}
}
Blurring
• Average each pixel with its neighbors by :
multiplying each pixel
and its neighbors
1
1
1
1
1
1
1
1
1
=
- add multiplied pixels and divide by 9
- store result in a new array
• The new array contains a blurred image.
by 1:
Gaussian Blur
Multiply neighbors by less than middle
Blur Demo
•Photoshop
–Filter->Blur->Blur
–Filter->Blur->Gaussian Blur
Local Filters
• Blurring
Sharpening
Unsharp Masking
• Edge and line detection
• Noise filters
Sharpening
• Subtract neighboring pixels by :
multiplying each pixel
and its neighbors
0 -1
0
-1 +5 -1
0 -1
=
0
- add multiplied pixels
- store result in a new array
• The new array contains a sharpened image.
by:
Unsharp Masking
• Unsharp masking also sharpens an image
• The Algorithm:
1. call the original (unblurred) image A
2. blur the image (call it B)
3. subtract B from the unblurred image (let C = A-B)
4. Multiply C by some number, k (k > 1)
5. Sharpened Image = A + kC
Sharpen/Unsharp mask
• Photoshop
– Filter->Sharpen->Sharpen
– Filter->Sharpen->Unsharp mask
Local Filters
• Blurring
• Sharpening
• Unsharp Masking
Edge and line detection
• Noise filters
Edge and Line Detection
• Multiply each pixel
and its neighbors
the following corresponding patterns (numbers)
-1
0 +1
-1 -1 -1
-1
0 +1
0
-1
0 +1 +1 +1 +1
Gets
Vertical Edges
0
0
Gets
Horizontal Edges
by
- add results
- clip to 255
- result =
“Edge” image
Edge Detection
• edge detection is often used to recognized objects of
interest in the image
Edge Detection
• Photoshop
– Filter->Stylize->Find Edges
Local Filters
• Blurring
• Sharpening
• Unsharp Masking
• Edge and line detection
Noise filters
Noise Filters
• A median filter takes away “salt & pepper” noise
(speckles)
Noise Filter
• Photoshop
– Filter->Noise->Median Filter
Kernel Filtering
Blur
Edges
Sharpen
1
1
1
0 -1
0
1
1
1
-1 +5 -1
1
1
1
0 -1
-1
0 +1
-1
0 +1
-1
0 +1
0
Apply same algorithm at every
pixel using different kernel
values
-1 -1 -1
0
0
0
+1 +1 +1