應用各種濾波器去除各種雜訊影像

研究背景

產生雜訊影像, 濾波後效果除視覺效果比較外, 利用與原影像計算PSNR來比較結果。
至少需產生三種雜訊影像, 每種至少利用3種濾波器實作比較其結果, 測試影像使用Lenna.bmp。

程式實作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import numpy as np
import cv2
from numpy.random import uniform, normal, exponential, rayleigh
import matplotlib.pyplot as plt

def uniform_noise( f, scale ): # 均勻雜訊
g = f.copy( )
nr, nc = f.shape[:2]
for x in range( nr ):
for y in range( nc ):
value = f[x,y] + uniform( 0, 1 ) * scale
g[x,y] = np.uint8( np.clip( value, 0, 255 ) )
return g

def gaussian_noise( f, scale ): # 高斯雜訊
g = f.copy( )
nr, nc = f.shape[:2]
for x in range( nr ):
for y in range( nc ):
value = f[x,y] + normal( 0, scale )
g[x,y] = np.uint8( np.clip( value, 0, 255 ) )
return g

def exponential_noise( f, scale ): # 指數雜訊
g = f.copy( )
nr, nc = f.shape[:2]
for x in range( nr ):
for y in range( nc ):
value = f[x,y] + exponential( scale )
g[x,y] = np.uint8( np.clip( value, 0, 255 ) )
return g

def rayleigh_noise( f, scale ): # 瑞雷雜訊
g = f.copy( )
nr, nc = f.shape[:2]
for x in range( nr ):
for y in range( nc ):
value = f[x,y] + rayleigh( scale )
g[x,y] = np.uint8( np.clip( value, 0, 255 ) )
return g

def salt_pepper_noise( f, probability ): # 鹽與胡椒雜訊
g = f.copy( )
nr, nc = f.shape[:2]
for x in range( nr ):
for y in range( nc ):
value = uniform( 0, 1 )
if value > 0 and value <= probability / 2:
g[x,y] = 0
elif value > probability / 2 and value <= probability:
g[x,y] = 255
else:
g[x,y] = f[x,y]
return g

def PSNR( f, g ): # PSNR
nr, nc = f.shape[:2]
MSE = 0.0
for x in range( nr ):
for y in range( nc ):
MSE += ( float( f[x,y] ) - float( g[x,y] ) ) ** 2
MSE /= ( nr * nc )
PSNR = 10 * np.log10( ( 255 * 255 ) / MSE )
return PSNR

def histogram( f ):
if f.ndim != 3:
hist = cv2.calcHist( [f], [0], None, [256], [0,256] )
plt.plot( hist )
else:
color = ( 'b', 'g', 'r' )
for i, col in enumerate( color ):
hist = cv2.calcHist( f, [i], None, [256], [0,256] )
plt.plot( hist, color = col )
plt.xlim( [0,256] )
plt.xlabel( "Intensity" )
plt.ylabel( "#Intensities" )
plt.show( )

def main( ):
print( "Image Degradation with Noise Model" )
print( "(1) Uniform Noise" )
print( "(2) Gaussian Noise" )
print( "(3) Exponential Noise" )
print( "(4) Rayleigh Noise" )
print( "(5) Salt and Pepper Noise" )
method = eval( input( "Please enter your choice: " ) )
if method >= 1 and method <= 4:
scale = eval( input( "Please enter scale(e.g., 20): " ) )
elif method == 5:
probability = eval( input ( "Please enter probability(e.g., 0.05): " ) )
else:
print( "Noise model not supported!" )
exit( )
img1 = cv2.imread( "gaussian_noise_15.bmp", -1 )
if method == 1:
img2 = uniform_noise( img1, scale )
elif method == 2:
img2 = gaussian_noise( img1, scale )
elif method == 3:
img2 = exponential_noise( img1, scale )
elif method == 4:
img2 = rayleigh_noise( img1, scale )
else:
img2 = salt_pepper_noise( img1, probability )
print( "PSNR =", PSNR( img1, img2 ) )
cv2.imshow( 'lenna', img1 )
cv2.imshow( 'Uniform Noise_10_test', img2 )
cv2.imwrite("Uniform Noise_10_test.bmp", img2 )
histogram( img2 )


if __name__ == '__main__':
main()
cv2.waitKey( 0 )
cv2.destroyAllWindows( )

實驗比較

  1. 雜訊影像與PSNR比較
    表一為雜訊影像與PSNR比較,在Uniform Noise當中,如圖1至圖3濾波器影像的變化可知,隨著Scale的值下降,PSNR的值隨之上升,影像失真越少,保有原有的品質。Gaussian Noise亦有相同的變化,如圖4至圖6所示。但是Salt and Pepper Noise,如圖7至圖9,隨著機率值的上升,PSNR的值隨之下降,與前面兩個相比,影像品質失真較多。接著將PSNR較佳的結果,進入下一個實驗。
    雜訊影像與PSNR比較

  2. 濾波器與PSNR比較
    表2為濾波器與PSNR比較,本實驗將圖3 Uniform Noise, Scale: 10、圖6 Gaussian Noise, Scale: 10、圖7 Salt and Pepper Noise, Probability: 0.05之雜訊影像圖進行比較。從多種雜訊圖比較均值濾波器(Average filter)、高斯濾波器(Gausian filter)、雙邊濾波器(Bilateral Filter)去除雜訊影像之結果。可見雙邊濾波器具有較佳的結果,PSNR所得分數較高,比其他兩者濾波器勝出許多,次之為高斯濾波器,最小為均值濾波器。去除雜訊影像之結果圖可於圖12 uniform_noise_10_bilateralFilter、圖15 gaussian_noise_10_bilateralFilter、圖18 salt_pepper_noise_bilateralFilter所示,去除雜訊影像後可其肉眼所見相當清晰。

濾波器與PSNR比較

實驗結果圖

  • 測試影像
    Lenna
  1. 圖 1 Uniform Noise, Scale: 20
    圖1

  2. 圖 2 Uniform Noise, Scale: 15
    圖2

  3. 圖 3 Uniform Noise, Scale: 10
    圖3

  4. 圖 4 Gaussian Noise, Scale: 20
    圖4

  5. 圖 5 Gaussian Noise, Scale: 15
    圖5

  6. 圖 6 Gaussian Noise, Scale: 10
    圖6

  7. 圖 7 Salt and Pepper Noise, Probability: 0.05
    圖7

  8. 圖 8 Salt and Pepper Noise, Probability: 0.10
    圖8

  9. 圖 9 Salt and Pepper Noise, Probability: 0.15
    圖9

  10. 圖 10 uniform_noise_10_blur
    圖10

  11. 圖11 uniform_noise_10_GaussianBlur
    圖11

  12. 圖 12 uniform_noise_10_bilateralFilter
    圖12

  13. 圖 13 gaussian_noise_10_blur
    圖13

  14. 圖14 gaussian_noise_10_GaussianBlur
    圖14

  15. 圖15 gaussian_noise_10_bilateralFilter
    圖15

  16. 圖 16 salt_pepper_noise_blur
    圖16

  17. 圖 17 salt_pepper_noise_GaussianBlur
    圖17

  18. 圖 18 salt_pepper_noise_bilateralFilter
    圖18