Transcript Document

การกรองและปรับปรุงคุณภาพของภาพ
(Image Filtering and Enhancement)
เนือ้ หา
1 สั ญญาณรบกวน ความสว่ าง และความคมชัด
2 การกรองสั ญญาณรบกวนโดยกระทาในระดับจุดภาพ
3 การกรองสั ญญาณรบกวนโดยการกรองทางตาแหน่ ง
4 การกรองสั ญญาณรบกวนโดยการกรองทางความถี่
5 การปรับปรุงคุณภาพของภาพโดยการใช้ ฮิสโตแกรม
• การปรับปรุงภาพ (Image Enhancement) เป็ น
กระบวนการทีท่ าให้ คุณภาพของภาพดีขนึ้ เพือ่ จุดมุ่งหมายเฉพาะ
• โดยการกาจัดสิ่ งรบกวน (Noise) ออกจากภาพและ/หรือ
• การกระทาทีท่ าให้ คุณลักษณะของภาพ (Feature) เช่ น ความ
สว่ าง ความคมชัด ขอบของวัตถุในภาพให้ ปรากฏชัดเจนขึน้
• การกระทานีอ้ าจทาให้ บางส่ วนของภาพมีคุณภาพด้ อยกว่ าเดิม
อย่ างไรก็ตามหากบริเวณนีไ้ ม่ มีความสาคัญมากนักเราจะถือว่ าการ
กระทาครั้งนีป้ ระสบความสาเร็จและยอมรับได้
การปรับปรุงภาพแบ่ งออกตามประเภทของ
การประมวลผลได้ เป็ น 4 แบบ คือ
1. Pixel-based Image Enhancement
2. Spatial-filtering-based Image
Enhancement
3. Frequency-based Image Enhancement
4. Histogram-based Image Enhancement
1. สาเหตุ
1.1 สั ญญาณรบกวน
•
Gaussian (Normal) Noise

1
p( z ) 
e
2 
•
( z  )2
2 2
Salt-and-pepper Noise
Pa for z  a

p ( z )  Pb for z  b
0 otherwise

1. Gaussian (Normal) Noise
2. Salt-and-pepper Noise
•
Uniform Noise
 1
for a  z  b

p(z )   b - a
0 otherwise
•
ab

2
2
(
b

a
)
2 
12
Exponential Noise
ae
p( z )  
0
-az
for z  0
for z  0
1

a
1
  2
a
2
•
Erlang (Gamma) Noise
 a b z b1 -az
e
for z  0

p( z )   (b  1)!
0
for z  0

•
b

a
b
  2
a
2
Rayleigh Noise
2
 (z - a)e
p( z )   b
0

(z -a) 2
b
for z  0
for z  0
 a
b
4
b(4   )
 
4
2
1.2 ความสว่ างของภาพ (Brightness)
1 M 1N 1
brightness  B 
i (x , y )


MN y  0 x  0
1.3 ความคมชัดของภาพ (Contrast)
contrast  C 
1
MN
M 1 N 1
  [i (x , y )  B ]
y 0 x 0
2
B = 204.81
B = 61.14
C = 40.50
C = 36.82
B = 129
B = 126.14
C = 77.48
C = 12.59
2. การกรองสั ญญาณรบกวนโดยกระทาในระดับ
จุดภาพ (Pixel-based Image Filtering)
• การบวกภาพทีต่ ้ องการจะปรับปรุ งเข้ าด้ วยกันแล้ วหาค่ าเฉลีย่ (Image
Averaging)
• ถ้ า i(x,y) เป็ นภาพต้ นฉบับก่อนการปนเปื้ อนด้ วยสั ญญาณรบกวน
• สั ญญาณรบกวน n(x,y)
• ภาพผลลัพธ์ คอื g ( x , y )  i ( x , y )  n ( x , y )
• สามารถกาจัดสั ญญาณรบกวนนีด้ ้ วยการหาค่ าเฉลีย่
• gˆ ( x, y)  1 k g ( x, y)
 i
k
i 1
•
, y) ค่าเข้ าใกล้ ค่า i(x,y) และค่ า
ซึ่งพบว่ าเมือ่ จานวนภาพ k เพิม่ ขึน้ ค่ าของ gˆ ( xจะมี
ความแปรปรวนของภาพจะมีค่าลดลง
3. การกรองสั ญญาณรบกวนโดยการกรองทาง
ตาแหน่ ง (Spatial Image Filtering)
การกรองสิ่ งรบกวนออกจากภาพโดยการทา Spatial
Convolution ซึ่งใช้ ตัวทาบ (Kernel) ที่มีขนาดและ
ค่ านา้ หนักต่ างกันเพือ่ กาจัดสั ญญาณรบกวนเหล่านีซ้ ึ่งมีลกั ษณะ
ต่ างๆ กัน โดยมีรายละเอียดดังนี้
3.1 การกรองแบบเฉลีย่ (Mean Moving Average)
1
9
1
9
1
9
1
9
1
9
1
9
1
9
1
9
1
9
3.1 การกรองแบบเฉลีย่ (Mean Moving Average)
• Low-pass filter
• Linear operator
• ทาให้ ภาพเบลอ
• เหมาะกับ noise แบบ Gaussian
3.2 การกรองแบบ Gaussian Filtering
3
25
6
25
6
25
14
25
8
25
19
25
6
25
14
25
3
25
6
25
8
25
6
25
3
25
19
25
14
25
6
25
25
25
19
25
8
25
19
25
14
25
6
25
8
25
6
25
3
25
3.2 การกรองแบบ Gaussian
•สมมาตรในเชิงวงกลม h ( x , y )  e
• Low-pass filter
 ( x 2 y 2 )

a2
• Linear operator
• ทาให้ ภาพเบลอ
• เหมาะกับ noise แบบ Gaussian
3.3 การกรองแบบ Median
3.3.1 Running median smoother
3.3 การกรองแบบ Median
3.3.2 Weighted median smoother
1.1
4
0.2
0.8
6
0.1
0.6
14
8
9
2
6
8
0.1
0.5
17
25
15
9
0.1
0.4
14
0.1
0.3
15
0.1
0.2
17
0.1
0.1
25
0.0
0.0
0.2
0.1
0.1
0.1
0.3
0.1
0.1
0.0
0.1
W
0.3
4
ผลลัพธ์
ภาพต้ นฉบับ
2
T0=0. 55
ผลลัพธ์ สุดท้ าย
T0=0. 55
3.3 การกรองแบบ Median
• Low-pass filter
• Non-linear operator
• ทาให้ ภาพเบลอ
• เหมาะกับ noise แบบ Salt-andpepper
3.4 การกรองแบบ High-pass filtering
0
-1
0
-1
5
-1
0
-1
1
-2
1
-2
5
-2
-1
-1
-1
1
-2
1
-1
9
-1
-1
-1
-1
0
3.4 การกรองแบบ High-pass filtering
• High-pass filter
• Linear operator
• ทาให้ ภาพคมชัด ดังนั้นจึงมีส่วนขยายสั ญ
ยาณรบกวนให้ เด่ นชัดขึน้
• เหมาะในกรณีกรอง noise แล้วกระทา
high-pass filter อีกครั้งเพือ่ ทาให้ ภาพ
ไม่ เบลอนัก
4. การกรองโดยการกรองทางความถี่
ในกรณีทภี่ าพซึ่งต้ องการปรับปรุงคุณภาพมีขนาด
ใหญ่ พบว่ าถ้ าหากใช้ การกรองทางตาแหน่ งจะต้ อง
เสี ยเวลาในการประมวลผลมาก ดังนั้นจึงควร
ประยุกต์ ใช้ การแปลงและการแปลงกลับของฟูเรียร์
ตลอดจนคุณสมบัตขิ องการแปลงนีเ้ พือ่ ทาการกรอง
โดยกระทาบน Frequency Domain
ถ้ าภาพทีต่ ้ องการจะปรับปรุงเป็ น f(x,y)
และตัวกรองเป็ น h(x,y) ดังนั้น ภาพที่ผ่าน
การกรองแล้ว (Filtered Image) จะ
เป็ น g(x,y)
g (x , y ) 
  i ( x  i , y  j ) * h (i , j )
i , j H
ซึ่งก็คอื การทาการกรองทางตาแหน่ งโดยใช้
convolution ดังทีไ่ ด้ กล่ าวมาแล้ วในหัวข้ อก่ อน
สาหรับการกระทาอีกแบบหนึ่งซึ่งอาศัยคุณสมบัติที่สาคัญ
ของการแปลงฟูเรียร์ ซึ่งกล่ าวว่ าการกระทา
convolution ใน spatial domain จะสมนัย
กับการคูณใน frequency domain
G(u,v) = F(u,v)H(u,v)……..
โดยที่ G(u,v), F(u,v) และ H(u,v) คือผลของการ
แปลงฟูเรียร์ ของ g(x,y), f(x,y) และ h(x,y)
ตามลาดับ
จากนั้นจะทาการกรองความถี่ที่สมนัยกับสั ญญาณ
รบกวนออก และขั้นตอนสุ ดท้ ายก็คอื ทาการแปลงกลับ
ฟูเรียร์ จะได้ ภาพผลลัพธ์ ซึ่งผ่ านการกรองแล้ ว
g(x,y) = -1 {G(u,v)}
ซึ่งสามารถสรุปได้ ดงั แผนผัง
Image
f(x,y)
F(u,v)
H(u,v)
Fourier
Transform
G(u,v) = F(u,v)H(u,v)
Inverse Fourier
transform
g(x,y)
ข้ อดี
• ประหยัดเวลาสาหรับการประมวลผลภาพขนาดใหญ่ เมื่อ
เทียบกับเวลาสาหรับการประมวลผลใน Spatial
Domain โดยใช้ Spatial Convolution
• ในบางกรณีการออกแบบ Filter Frequency
Response (H(u,v)) ใน Frequency
Domain สะดวกกว่ าการออกแบบตัวทาบเพือ่ กระทาการ
กรองใน Spatial Domain (h(x,y))
5. Image Enhancement: Histogram
Based Methods
The histogram of a digital image with gray values r0 , r1 ,, rL1
is the discrete function
nk
p ( rk ) 
n
nk: Number of pixels with gray value rk
n: total Number of pixels in the image
The function p(rk) represents the fraction of the total number
of pixels with gray value rk.
Histogram provides a global description of the appearance of
the image.
If we consider the gray values in the image as realizations of a
random variable R, with some probability density, histogram
provides an approximation to this probability density. In other
words,
Pr(R  rk )  p(rk )
Some Typical Histograms
The shape of a histogram provides useful information for
contrast enhancement.
Dark image
Bright image
Low contrast image
High contrast image
5.1 Negation
Output Gray-level, s
L-1
s = (L-1) - r
Input Gray-level, r
L-1
5.2 Piecewise Contrast Stretching
r ,

s   ( r  r1 )  s1
 (r  r )  s
1
2

for 0  r  r1
for r1  r  r2
for r2  r  L  1
Output Gray-level, s
L-1
(r2 ,s2 )
(r1 ,s1 )
Input Gray-level, r
L-1
5.3 Piecewise Gray-scale Slicing
5.4 Log-Exponential Transform
s  a log(1  r )
5.5 Power-Law Transform

s  ar )
ภาพต้นฉบับ และภาพหลังการแปลงแบบ Power-Law Transform
(s = arγ ) โดยที่ a = 1 และ γ เท่ากับ 3, 4 และ 5 ตามลาดับ
5.6 Histogram Equalization
he histogram equalization is an approach to enhance a given
image. The approach is to design a transformation T(.) such that
the gray values in the output is uniformly distributed in [0, 1].
Let us assume for the moment that the input image to be
enhanced has continuous gray values, with r = 0 representing
black and r = 255 representing white.
We need to design a gray value transformation s = T(r), based
on the histogram of the input image, which will enhance the
image.
As before, we assume that:
(1) T(r) is a monotonically increasing function for
0 r 1 (preserves order from black to white).
(2) T(r) maps [0,1] into [0,1] (preserves the range of allowed
Gray values).
Let us denote the inverse transformation by r T -1(s) . We
assume that the inverse transformation also satisfies the above
two conditions.
We consider the gray values in the input image and output
image as random variables in the interval [0, 1].
Let pin(r) and pout(s) denote the probability density of the
Gray values in the input and output images.
If pin(r) and T(r) are known, and r T -1(s) satisfies
condition 1, we can write (result from probability theory):
dr 

pout ( s)   pin (r ) 
ds  r T 1 ( s )

One way to enhance the image is to design a transformation
T(.) such that the gray values in the output is uniformly
distributed in [0, 1], i.e. pout (s)  1,
0  s 1
In terms of histograms, the output image will have all
gray values in “equal proportion” .
This technique is called histogram equalization.
Next we derive the gray values in the output is uniformly
distributed in [0, 1].
Consider the transformation
r
s  T (r )   pin (w)dw,
0
0  r 1
Note that this is the cumulative distribution function (CDF)
of pin (r) and satisfies the previous two conditions.
From the previous equation and using the fundamental
theorem of calculus,
ds
 pin (r )
dr
Therefore, the output histogram is given by

1 
pout ( s)   pin (r ) 
 1r T 1 ( s )  1,

pin (r )  r T 1 ( s )

0  s 1
The output probability density function is uniform,
regardless of the input.
Thus, using a transformation function equal to the CDF of
input gray values r, we can obtain an image with uniform gray
values.
This usually results in an enhanced image, with an increase
in the dynamic range of pixel values.
How to implement histogram equalization?
Step 1:For images with discrete gray values, compute:
nk
pin ( rk ) 
n
0  rk  1
0  k  L 1
L: Total number of gray levels
nk: Number of pixels with gray value rk
n: Total number of pixels in the image
Step 2: Based on CDF, compute the discrete version of the
previous transformation :
k
sk  T (rk )   pin (rj )
j 0
0  k  L 1
Example:
Consider an 8-level image with gray values (0, 1, …,
7). The normalized gray values are (0, 1/7, 2/7, …, 1). The
normalized histogram is given below:
NB: The gray values in output are also (0, 1/7, 2/7, …, 1).
# pixels
Fraction
of # pixels
Gray value
Normalized gray value
k
 Applying the transformation, sk  T (rk )   pin (rj ) we have
j 0
Notice that there are only five distinct gray levels --- (1/7, 3/7,
5/7, 6/7, 1) in the output image. We will relabel them as (s0,
s1, …, s4 ).
With this transformation, the output image will have
histogram
Histogram of output image
# pixels
Gray values
Note that the histogram of output image is only approximately,
and not exactly, uniform. This should not be surprising, since there
is no result that claims uniformity in the discrete case.
Example
Original image and its histogram
Histogram equalized image and its histogram
 Comments:
Histogram equalization may not always produce desirable
results, particularly if the given histogram is very narrow. It
can produce false edges and regions. It can also increase
image “graininess” and “patchiness.”
5.7 Histogram Specification
(Histogram Matching)
Histogram equalization yields an image whose pixels are (in
theory) uniformly distributed among all gray levels.
Sometimes, this may not be desirable. Instead, we may want a
transformation that yields an output image with a pre-specified
histogram. This technique is called histogram specification.
Given Information
(1) Input image from which we can compute its histogram .
(2) Desired histogram.
Goal
Derive a point operation, H(r), that maps the input image into
an output image that has the user-specified histogram.
Again, we will assume, for the moment, continuous-gray
values.
Approach of derivation
z=H(r) = G-1(v=s=T(r))
Input
image
s=T(r)
Uniform
image
v=G(z)
Output
image
Suppose, the input image has probability density in p(r) . We
want to find a transformation z H (r) , such that the probability
density of the new image obtained by this transformation is pout(z) ,
which is not necessarily uniform.
First apply the transformation
r
s  T (r )   pin (w)dw,
0
0  r 1
(*)
This gives an image with a uniform probability density.
If the desired output image were available, then the following
transformation would generate an image with uniform density:
z
V  G( z )   pout (w)dw,
0
0  z 1
(**)
From the gray values nwe can obtain the gray values z by
using the inverse transformation, z  G-1(v)
If instead of using the gray values nobtained from (**), we
use the gray values s obtained from (*) above (both are
uniformly distributed ! ), then the point transformation
Z=H(r)= G-1[ v=s =T(r)]
will generate an image with the specified density out p(z) ,
from an input image with density in p(r) !
For discrete gray levels, we have
k
sk  T (rk )   pin (rj )
j 0
k
0  k  L 1
vk  G( zk )   pout ( z j )  sk
0  k  L 1
j 0
 If the transformation zk G(zk) is one-to-one, the inverse
transformation sk G-1 (sk) , can be easily determined, since
we are dealing with a small set of discrete gray values.
In practice, this is not usually the case (i.e., ) zk G(zk) is not
one-to-one) and we assign gray values to match the given
histogram, as closely as possible.
Algorithm for histogram specification:
(1) Equalize input image to get an image with uniform
gray values, using the discrete equation:
k
sk  T (rk )   pin (rj )
j 0
0  k  L 1
(2) Based on desired histogram to get an image with
uniform gray values, using the discrete equation:
k
vk  G( zk )   pout ( z j )  sk
0  k  L 1
j 0
(3)
1
1
z  G ( v=s )  z  G [T ( r )]
Example:
Consider an 8-level 64 x 64 previous image.
# pixels
Gray value
It is desired to transform this image into a new image, using a
transformation Z=H(r)= G-1[T(r)], with histogram as specified
below:
# pixels
Gray values
The transformation T(r) was obtained earlier (reproduced
below):
Now we compute the transformation G as before.
Computer z=G-1 (s),Notice that G is not invertible.
G-1(0) = ?
G-1(1/7) = 3/7
G-1(2/7) = 4/7
G-1(3/7) = ?
G-1(4/7) = ?
G-1(5/7) = 5/7
G-1(6/7) = 6/7
G-1(1) = 1
Combining the two transformation T and G-1 , compute
z=H(r)= G-1[v=s=T(r)]
Applying the transformation H to the original image yields an
image with histogram as below:
Again, the actual histogram of the output image does not exactly
but only approximately matches with the specified histogram. This
is because we are dealing with discrete histograms.
Original image and its histogram
Histogram specified image and its histogram
Desired histogram