EE409 – Digital Image Processing 2017-2018 Spring
4. Image Enhancement in Spatial Domain b) Filtering
by: Dr. Gökhan Koray GÜLTEKİN
Neighborhood of a Pixel Neighborhood of a pixel p at position x,y is a set N(p) of pixels defined relative to p. Example 1: (8 neighbor of p) N(p) = {(x,y): |x-xP|=1, |y-yP| = 1}
P
Q 2
1
Neighborhood of a Pixel More examples of neighborhoods: P
P
P
P
P
P
3
Neighborhood of a Pixel  Usually neighborhoods are used which are close to discs, since properties of the eucledian metric are often useful
 The most prominent neighborhoods are the 4-Neighborhood and the 8-Neighborhood
P
P
4
2
Neighborhood of a Pixel We will define spatial filters on the 8-Neighborhood and their bigger relatives
P P P N8
N24
N48
5
Neighborhood of a Pixel Index system for N8:
n1
n2
n3
n4
n5
n6
n7
n8
n9
6
3
Generating Uniform Random Noise g ( x , y ) f ( x, y ) n ( x, y ) Corrupted image
Original image
Noise
im1=imread('cameraman.tif'); im1d=im2double(im1); row = 256; col = 256; noise = rand(row, col)-0.5; imshow(im1d+noise); 7
Generating Different Noise Types To add noise to an image:
We can use ‘imnoise’ function of Matlab
imnoise(I,’type’,coef);
‘type’ can be ‘salt n pepper’, ‘speckle’, ‘gaussian’ etc.
8
4
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 g(i,j) in the input image. This equation is called discrete convolution:
a
b
w(m, n) g (i m, j n)
f (i, j ) g * w
m a nb
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). 9
Linear Image Filters What happens to P if we apply this formula:
P = i wi ni with wi given by:
w1=1
w2=1
w3=1
w4=1
w5=4
w6=1
w7=1
w8=1
w9=1
5
Mask Representation
11
Mask on Image
12
6
13
Exercise: Compute the 2-D linear convolution of signal X with mask w. Extend the signal X with 0’s if needed (padding).
1 2 3 X 4 5 6, w 1 0 1 3 4 1 14
7
Averaging Filter Blurring / Smoothing (Sometimes also referred to lowpass-filtering) Average the values of the center pixel and its neighbors: Purpose:
• •
Reduction of ‘irrelevant’ details Noise reduction 15
Image smoothing = image blurring Averaging of brightness values is a special case of discrete convolution. For a 3 x 3 neighborhood the convolution mask w is
1 1 1 1 w 1 1 1 9 1 1 1 Applying this mask to an image results in smoothing.
•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. 16
8
Averaging Filter Blurring / smoothing the image 1
1
1
1
1
1
1
1
1
* 1/9
Apply this scheme to every single pixel !
Averaging Filter
18
9
Averaging Filter, Application
19
Averaging Filter, Application
20
10
Weighted Averaging Filter Basic idea: Weigh the center point the highest, decrease weight by distance to center. The general formula for weighted average:
P = i wi ni / i wi Constant value, depending on mask, not on image !
21
Weighted Averaging Filter Masks
The significance of the central pixel may be increased to better reflect properties of Gaussian noise:
1 1 1 1 w 1 2 1 10 1 1 1
1 2 1 1 w 2 4 2 16 1 2 1
22
11
Weighted Averaging Filter Masks
23
Weighted Averaging Filter 1
2
1
2
4
2
1
2
1
* 1/16
24
12
Image Filtering in Matlab ď Ž
The borders are usually padded with zeros for the computations of the edges neighborhoods.
ď Ž
Linear filtering can be done with convolution - conv2(Img, h) or correlation - filter2(Img, h) - imfilter(Img,h) % We mostly use
25
Image Filtering Options in Matlab
26
13
Random Filters Lets have a look at different values of wi and their effects ! % Description: Given an image 'im', create 12 filtered versions using randomly designed filters for i=1:12 w=rand(7,7); % create a random 7x7 filter-matrix w=w*2 - 1; % range: -1 to 1 w=w/sum(w(:)); % normalize im1=conv2(im,w);% Filter subplot(4,3,i); imshow(im1/max(im1(:))); end 27
28
14
Non – Linear Filtering:
Order-Statistics Filters
Median Min Max 29
Median The median M of a set of values is such that half the values in the set are less than (or equal to) M, and half are greater (or equal to) M.
1
2
3
3
4
5
6
6
6
7
8
9
9
30
15
Median Filter Median filter is an order filter, it uses order statistics. Given an NxN window W(x,y), the pixel intensity values of pixels in W are ordered from smallest to the largest, as follow:
I1 I 2 I 3 ... I N 2 Median filter selects the middle value:
110 110 104 W ( x, y ) 100 114 104 Median(W ( x, y )) 104 95 88 85 31
Median Filter
Median of a 3x3 patch:
10 20 10 25 10 75 90 85 100
Sort: (10 10 10 20 25 75 85 90 100)
Example: Original signal: Noisy signal:
100 100 100 100 10 10 10 10 10 100 103 100 100 10
9 10 11 10
Filter by 1x3 averaging [ 1 1 1]/3:
101 101 70
Filter by 1x3 median filter:
100 100 100 10 10 10 10
40 10 10 10
32
16
Median Filter
Median filters are nonlinear.
Median filtering reduces noise without blurring edges and other sharp details.
Median filtering is particularly effective when the noise pattern consists of strong, spike-like components. (Salt-and-pepper noise.)
33
Median Filter Most important properties of the median: Less sensible to noise than mean Selects an element from the original set of values Can be computed in O(n)
34
17
Median Filter vs Averaging Filter Original
3x3 averaging filter
Salt&Pepper noise added
3x3 median filter
35
Median Filter vs Averaging Filter
36
18
Min & Max Filter 2
5
7
3
3
4
2
3
3
4
8
3
3
3
3
3
MIN
2
5
7
3
3
2
2
3
3
4
8
3
3
3
3
3
2
5
7
3
3
8
2
3
3
4
8
3
3
3
3
3
MAX
B=ordfilt2(A,1,ones(3,3)) % implements a 3-by-3 minimum filter; B=ordfilt2(A,9,ones(3,3)) % implements a 3-by-3 maximum filter.
37
Questions
? 38
19