-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathimg_restore.py
211 lines (138 loc) · 5.08 KB
/
img_restore.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
import numpy as np
import cv2
photo = cv2.imread("noise.jpg",0)
photo = cv2.resize(photo,(1200,800))
#global methods Can't be classified
def g_mean(x):
a = np.log(x)
return np.exp(np.mean(a))
def h_mean(x):
sum = 0
for i in x :
sum += 1/i
return np.mean(sum)
#---------------------------------------------
class img_restore(object):
def __init__(self,photo,M,N) -> None:
self.photo = photo
self.M = M
self.N = N
return None
def median(self):
photo = self.photo
M = self.M
N = self.N
new = np.zeros((M,N),dtype=np.float32)
new[0,:] = photo[0,:]
new[M-1,:] = photo[M-1,:]
new[:,0] = photo[:,0]
new[:,N-1] = photo[:,N-1]
for i in range(1,M-1):
for j in range(1,N-1):
kernel = np.array([[photo[i-1,j-1],photo[i,j-1],photo[i+1,j-1]],
[photo[i-1,j],photo[i,j],photo[i+1,j]],
[photo[i-1,j+1],photo[i,j+1],photo[i+1,j+1]]])
new[i,j] = np.median(kernel)
return new
def mean(self):
photo = self.photo
M = self.M
N = self.N
new = np.zeros((M,N),dtype=np.float32)
new[0,:] = photo[0,:]
new[M-1,:] = photo[M-1,:]
new[:,0] = photo[:,0]
new[:,N-1] = photo[:,N-1]
for i in range(1,M-1):
for j in range(1,N-1):
kernel = np.array([[photo[i-1,j-1],photo[i,j-1],photo[i+1,j-1]],
[photo[i-1,j],photo[i,j],photo[i+1,j]],
[photo[i-1,j+1],photo[i,j+1],photo[i+1,j+1]]])
new[i,j] = np.mean(kernel)
return new
def geo_mean(self):
photo = self.photo
M = self.M
N = self.N
new = np.zeros((M,N),dtype=np.float32)
new[0,:] = photo[0,:]
new[M-1,:] = photo[M-1,:]
new[:,0] = photo[:,0]
new[:,N-1] = photo[:,N-1]
for i in range(1,M-1):
for j in range(1,N-1):
kernel = np.array([[photo[i-1,j-1],photo[i,j-1],photo[i+1,j-1]],
[photo[i-1,j],photo[i,j],photo[i+1,j]],
[photo[i-1,j+1],photo[i,j+1],photo[i+1,j+1]]])
new[i,j] = g_mean(kernel)
return new
def contra_har_mean(self,Q):
photo = self.photo
M = self.M
N = self.N
new = np.zeros((M,N),dtype=np.float32)
new[0,:] = photo[0,:]
new[M-1,:] = photo[M-1,:]
new[:,0] = photo[:,0]
new[:,N-1] = photo[:,N-1]
for i in range(1,M-1):
for j in range(1,N-1):
kernel = np.array([[photo[i-1,j-1],photo[i,j-1],photo[i+1,j-1]],
[photo[i-1,j],photo[i,j],photo[i+1,j]],
[photo[i-1,j+1],photo[i,j+1],photo[i+1,j+1]]])
new[i,j] = (np.mean(kernel)**(Q+1))/(np.mean(kernel)**Q)
return new
def min(self):
photo = self.photo
M = self.M
N = self.N
new = np.zeros((M,N),dtype=np.float32)
new[0,:] = photo[0,:]
new[M-1,:] = photo[M-1,:]
new[:,0] = photo[:,0]
new[:,N-1] = photo[:,N-1]
for i in range(1,M-1):
for j in range(1,N-1):
kernel = np.array([[photo[i-1,j-1],photo[i,j-1],photo[i+1,j-1]],
[photo[i-1,j],photo[i,j],photo[i+1,j]],
[photo[i-1,j+1],photo[i,j+1],photo[i+1,j+1]]])
new[i,j] = np.min(kernel)
return new
def max(self):
photo = self.photo
M = self.M
N = self.N
new = np.zeros((M,N),dtype=np.float32)
new[0,:] = photo[0,:]
new[M-1,:] = photo[M-1,:]
new[:,0] = photo[:,0]
new[:,N-1] = photo[:,N-1]
for i in range(1,M-1):
for j in range(1,N-1):
kernel = np.array([[photo[i-1,j-1],photo[i,j-1],photo[i+1,j-1]],
[photo[i-1,j],photo[i,j],photo[i+1,j]],
[photo[i-1,j+1],photo[i,j+1],photo[i+1,j+1]]])
new[i,j] = np.max(kernel)
return new
def mid_point(self):
photo = self.photo
M = self.M
N = self.N
new = np.zeros((M,N),dtype=np.float64)
new[0,:] = photo[0,:]
new[M-1,:] = photo[M-1,:]
new[:,0] = photo[:,0]
new[:,N-1] = photo[:,N-1]
for i in range(1,M-1):
for j in range(1,N-1):
kernel = np.array([[photo[i-1,j-1],photo[i,j-1],photo[i+1,j-1]],
[photo[i-1,j],photo[i,j],photo[i+1,j]],
[photo[i-1,j+1],photo[i,j+1],photo[i+1,j+1]]])
new[i,j] = (np.max(kernel) + np.min(kernel))//2
return new
new = img_restore(photo,800,1200).har_mean()
cv2.imwrite("new.jpg",new)
disp = cv2.imread("new.jpg")
cv2.imshow('frame',disp)
cv2.waitKey(0)
cv2.destroyAllWindows()