Title: L24. More on Image File Processing
1L24. More on Image File Processing
- Filtering Noise
- Edge Detection
2 Pictures as Arrays
- A black and white picture can be encoded
- as a 2D Array
- Typical
-
- 0 lt A(i,j) lt 255
- (black)
(white) - Values in between correspond to different
- levels of grayness.
3 Just a Bunch of Numbers
1458-by-2084
150 149 152 153 152 155 151 150
153 154 153 156 153 151 155 156
155 158 154 153 156 157 156 159
156 154 158 159 158 161 157 156
159 160 159 162
4 Dirt!
1458-by-2084
150 149 152 153 152 155 151 150
153 154 153 156 153 2 3 156
155 158 154 2 1 157 156 159
156 154 158 159 158 161 157 156
159 160 159 162
Note how the dirty pixels look out of place
5Can We Filter Out the Noise?
6 Idea
1458-by-2084
150 149 152 153 152 155 151 150
153 154 153 156 153 ? ? 156
155 158 154 ? ? 157 156 159
156 154 158 159 158 161 157 156
159 160 159 162
Assign typical neighborhood gray values to
dirty pixels
7Getting Precise
- Typical neighborhood gray values
Could use Median Or Mean
radius 1
radius 3
Well look at Median Filtering first
8Median Filtering
- Visit each pixel.
- Replace its gray value by the median
- of the gray values in the neighborhood.
9Using a radius 1 Neighborhood
0 6 6 6 6 7 7 7 7
Before
After
10How to Visit Every Pixel
m 9
n 18
for i1m for j1n Compute new gray
value for pixel (i,j). end end
11Original
i 1
j 1
Filtered
Replace with the median of the values
under the window.
12Original
i 1
j 2
Filtered
Replace with the median of the values
under the window.
13Original
i 1
j 3
Filtered
Replace with the median of the values
under the window.
14Original
i 1
j n
Filtered
Replace with the median of the values
under the window.
15Original
i 2
j 1
Filtered
Replace with the median of the values
under the window.
16Original
i 2
j 2
Filtered
Replace with the median of the values
under the window.
17Original
i m
j n
Filtered
Replace with the median of the values
under the window.
18What We Need
- (1) A function that computes the median
- value in a 2-dimensional array C
- m medVal(C)
- (2) A function that builds the filtered
- image by using median values of radius r
- neighborhoods
- B medFilter(A,r)
-
19Computing Medians
x
x sort(x)
x
n length(x) n 7 m ceil(n/2) m
4 med x(m) med 36
If n is even, then use med ( x(m)
x(m1) )/2
20Median of a 2D Array
- function med medVal(C)
- p,q size(C)
- x
- for k1p
- x x C(k,)
- end
- Compute median of x and assign to med.
21Medians vs Means
- A
- 150 151 158 159 156
- 153 151 156 155 151
- 150 155 152 154 159
- 156 154 152 158 152
- 152 158 157 150 157
- Median 154 Mean 154.2
22Medians vs Means
- A
- 150 151 158 159 156
- 153 151 156 155 151
- 150 155 0 154 159
- 156 154 152 158 152
- 152 158 157 150 157
- Median 154 Mean 148.2
23Back to Filtering
m 9
n 18
for i1m for j1n Compute new gray
value for pixel (i,j). end end
24Window Inside
m 9
n 18
New gray value for pixel (7,4)
medVal( A(68,35) )
25Window Partly Outside
m 9
n 18
New gray value for pixel (7,1)
medVal( A(68,12) )
26Window Partly Outside
m 9
n 18
New gray value for pixel (9,18)
medVal( A(89,1718) )
27 function B medFilter(A,r) B from A via
median filtering with radius r neighborhoods.
m,n size(A) B uint8(zeros(m,n))
for i1m for j1n C pixel
(i,j) neighborhood B(i,j) medVal(C)
end end
28The Pixel (i,j) Neighborhood
- iMin max(1,i-r)
- iMax min(m,ir)
- jMin max(1,j-r)
- jMax min(n,jr)
- C A(iMiniMax,jMinjMax)
m
A
r 1
r 2
n
29B medFilter(A)
30Original
31What About Using the Meaninstead of the Median?
- Replace each gray value with the
- average gray value in the radius r
- neighborhood.
32Mean Filter with r 3
33Mean Filter with r 10
34Why it Fails
150 149 152 153 152 155 151 150
153 154 153 156 153 2 3 156
155 158 154 2 1 157 156 159
156 154 158 159 158 161 157 156
159 160 159 162
The mean does not capture representative values.
85 86 87 88
35And Median Filters LeaveEdges (Pretty Much) Alone
200 200 200 200 200 200 200 200
200 200 200 100 200 200 200 200
100 100 200 200 200 100 100 100
200 200 100 100 100 100 200 100
100 100 100 100
Inside the box, the 200s stay at 200 and the
100s stay at 100.
36Finding Edges
37What is an Edge?
- Near an edge, grayness values change
- abruptly.
200 200 200 200 200 200 200 200
200 200 200 100 200 200 200 200
100 100 200 200 200 100 100 100
200 200 100 100 100 100 200 100
100 100 100 100
38The Rate-of-Change-Array
- Suppose A is an image array with integer
- values between 0 and 255
- B(i,j) be the maximum difference between
- A(i,j) and any of its eight neighbors.
39Example
81
Rate-of-change at middle pixel is 30
40- function B Edges(P)
- P is a jpeg file
- B is the corresponding
- Rate-Of-Change array
-
- A rgb2gray(imread(P))
- m,n size(A)
- B uint8(zeros(m,n))
- for i2m-1
- for j 2n-1
- B(i,j) ?????
- end
- end
41Recipe for B(i,j)
- The 3-by-3 subarray that includes
- A(i,j) and its 8 neighbors
- Neighbors A(i-1i1,j-1j1)
- Subtract A(i,j) from each entry
- Diff Neighbors A(i,j))
- Take absolute value of each entry..
- posDiff abs(Diff)
- Compute largest value in each column
- colMax max(posDiff)
- Compute the max of the column maxs
- B(I,j) max(colMax)
42Rate-of-Change Array to Image
- B Edges('Tower.jpg')
- Compute 0-1 array that identifies
- those B entries bigger than 20
- importantPixels B gt 20
- Display those pixels with maximum
- brightness
- C uint8( 255importantPixels )
- imshow(C)
Bgt0 is a 0-1 array, the ones are located where
B(i,j)gt 20.
43Threshhold 40
44Threshhold 20
45Threshhold 30