-
Notifications
You must be signed in to change notification settings - Fork 0
/
implementation.py
238 lines (211 loc) · 6.44 KB
/
implementation.py
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
import cv2
from matplotlib import pyplot as plt
import numpy as np
from filters import *
from efficiency import *
#from google.colab.patches import cv2_imshow
def goShow(img, str1):
#cv2.imshow("here",img)
cv2.imwrite('out/'+str1+'.png',img)
def show(img,name=None):
if name==None:
name="temp"
cv2.imshow(name,img)
cv2.waitKey(0)
cv2.destroyAllWindows()
def histogram(img):
plt.hist(img.ravel(),256,[0,256])
plt.show()
def org_prc(img):
b = img[:,:,0]
g = img[:,:,1]
r = img[:,:,2]
clahe = cv2.createCLAHE(clipLimit = 5)
b_c = clahe.apply(b)
g_c = clahe.apply(g)
r_c = clahe.apply(r)
out = cv2.merge((b_c,g_c,r_c))
return out
def noise_removal(img,type):
#splitting image into 3 parts
b = img[:,:,0]
g = img[:,:,1]
r = img[:,:,2]
b_d = globals()[type](**{'img':b})
g_d = globals()[type](**{'img':g})
r_d = globals()[type](**{'img':r})
clahe = cv2.createCLAHE(clipLimit = 5)
b_c = clahe.apply(b_d)
g_c = clahe.apply(g_d)
r_c = clahe.apply(r_d)
out = cv2.merge((b_c,g_c,r_c))
return out
def process(img):
#applying filter for noise removal on r,g,b separetely
out_average = noise_removal(img, "average_filter")
out_median = noise_removal(img, "median_filter")
out_weiner = noise_removal(img, "wiener_filter")
out_gaussian = noise_removal(img, "gaussian_filter")
out_weighted_median = noise_removal(img, "weighted_median_filter")
show(out_average)
show(out_median)
show(out_weiner)
show(out_gaussian)
show(out_weighted_median)
def summoning(x,y):
out = []
out.append(psnr(x,y))
out.append(ssim(x,y))
out.append(correlationCoefficient(x,y))
out.append(epi(x,y))
return out
def printIt(temp):
print("\t\t",temp[0],"\t\t",temp[1],"\t\t",temp[2])
def go_psnr(F,ss1,gg1,img):
print("PSNR")
out = []
Var = [0.001,0.01,0.1,0.2]
for j in range(5):
print("\t"+F[j])
print("\t\tVariance\t\tSalt and Pepper\t\tGaussian")
for q in range(4):
#print("yo")
temp = []
temp.append(Var[q])
img1 = ss1[j][q]
temp.append(psnr(img,img1))
goShow(img1,F[j]+" SNP " + str(Var[q]))
img1 = gg1[j][q]
temp.append(psnr(img,img1))
goShow(img1,F[j]+"Gauss "+ str(Var[q]))
out.append(temp)
printIt(temp)
return out
def go_ssim(F,ss1,gg1,img):
print("SSIM")
print("\t\tVariance\t\tSalt and Pepper\t\tGaussian")
out = []
Var = [0.001,0.01,0.1,0.2]
for j in range(5):
print("\t"+F[j])
for q in range(4):
temp = []
temp.append(Var[q])
img1 = ss1[j][q]
temp.append(ssim(img,img1))
goShow(img1,F[j]+" SNP " + str(Var[q]))
img1 = gg1[j][q]
temp.append(ssim(img,img1))
goShow(img1,F[j]+"Gauss "+ str(Var[q]))
out.append(temp)
printIt(temp)
return out
def go_coc(F,ss1,gg1,img):
print("CoC")
print("\t\tVariance\t\tSalt and Pepper\t\tGaussian")
out = []
Var = [0.001,0.01,0.1,0.2]
for j in range(5):
print("\t"+F[j])
for q in range(4):
temp = []
#print("\t"+F[j])
temp.append(Var[q])
img1 = ss1[j][q]
temp.append(correlationCoefficient(img,img1))
goShow(img1,F[j]+" SNP " + str(Var[q]))
img1 = gg1[j][q]
temp.append(correlationCoefficient(img,img1))
goShow(img1,F[j]+"Gauss "+ str(Var[q]))
out.append(temp)
printIt(temp)
return out
def go_epi(F,ss1,gg1,img):
print("EPI")
print("\t\tVariance\t\tSalt and Pepper\t\tGaussian")
out = []
Var = [0.001,0.01,0.1,0.2]
for j in range(5):
print("\t"+F[j])
for q in range(4):
temp = []
#print("\t"+F[j])
temp.append(Var[q])
img1 = ss1[j][q]
temp.append(epi(img,img1))
goShow(img1,F[j]+" SNP " + str(Var[q]))
img1 = gg1[j][q]
temp.append(epi(img,img1))
goShow(img1,F[j]+"Gauss "+ str(Var[q]))
out.append(temp)
printIt(temp)
return out
# def preprocess():
img = cv2.imread('img/example.jpg')
#process(img)
g1 = []
g1.append(cv2.imread('img/gauss_0.001.jpg'))
#process(g1);
g1.append(cv2.imread('img/gauss_0.01.jpg'))
#process(g2)
g1.append(cv2.imread('img/gauss_0.1.jpg'))
#process(g3)
g1.append(cv2.imread('img/gauss_0.2.jpg'))
#process(g4)
s1 = []
s1.append(cv2.imread('img/snp_0.001.jpg'))
#process(s1)
s1.append(cv2.imread('img/snp_0.01.jpg'))
#process(s2)
s1.append(cv2.imread('img/snp_0.1.jpg'))
#process(s3)
s1.append(cv2.imread('img/snp_0.2.jpg'))
F = ["average_filter","median_filter","wiener_filter","gaussian_filter","weighted_median_filter"]
outt = []
ss1 = []
gg1 = []
org = org_prc(img)
print(org.shape)
Var = [0.001,0.01,0.1,0.2]
for p in range(5):
tmp1 = []
tmp2 = []
for q in range(4):
print("applying " + str(F[p]) + " on variance " + str(Var[q]) + " of Salt and Pepper Noise")
tmp1.append(noise_removal(s1[q], F[p]))
print("applying " + str(F[p]) + " on variance " + str(Var[q]) + " of Gaussian Noise")
tmp2.append(noise_removal(g1[q], F[p]))
ss1.append(tmp1)
gg1.append(tmp2)
outt.append(go_psnr(F,ss1,gg1,org))
outt.append(go_ssim(F,ss1,gg1,org))
outt.append(go_epi(F,ss1,gg1,org))
outt.append(go_coc(F,ss1,gg1,org))
efficacy = ["PSNR","SSIM","EPI","CoC"]
for eff in range(4):
print(efficacy[eff])
for p in range(4):
print("For ",Var[p])
r = 0
tmp1 = []
tmp2 = []
for q in range(5):
z = r + p
r = r + 4
gmp = []
tmp1.append(outt[eff][z][1])
tmp2.append(outt[eff][z][2])
# tmp.append(gmp)
#do here for tmp1 and tmp2
x= ['Average','Median','Wiener','Gaussian','Weighted median']
x1=np.arange(5)
y=tmp1
y1=tmp2
plt.bar(x,y,color=['r'],width=[0.3])
plt.bar(x1+0.33,y1,color='b',width=[0.3])
plt.title("Red: Salt and Pepper\nBlue: Gaussian")
plt.xlabel('filters')
plt.ylabel('y-axis')
plt.show()
#process(s4)
preprocess()