-
Notifications
You must be signed in to change notification settings - Fork 0
/
filters.py
182 lines (161 loc) · 5.86 KB
/
filters.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
import numpy as np
from scipy.signal import gaussian,wiener
import cv2
from numpy.fft import fft2, ifft2
import math
import numpy
from itertools import product
def average_filter(img):
mask = np.ones([3, 3], dtype = int)
mask = mask / 9
m,n = img.shape
result = img
for row in range(m):
for col in range(n):
currentElement=0; left=0; right=0; top=0; bottom=0; topLeft=0;
topRight=0; bottomLeft=0; bottomRight=0;
counter = 1
currentElement = img[row][col]
if not col-1 < 0:
left = img[row][col-1]
counter +=1
if not col+1 > n-1:
right = img[row][col+1]
counter +=1
if not row-1 < 0:
top = img[row-1][col]
counter +=1
if not row+1 > m-1:
bottom = img[row+1][col]
counter +=1
if not row-1 < 0 and not col-1 < 0:
topLeft = img[row-1][col-1]
counter +=1
if not row-1 < 0 and not col+1 > n-1:
topRight = img[row-1][col+1]
counter +=1
if not row+1 > m-1 and not col-1 < 0:
bottomLeft = img[row+1][col-1]
counter +=1
if not row+1 > m-1 and not col+1 > n-1:
bottomRight = img[row+1][col+1]
counter +=1
total = int(currentElement)+int(left)+int(right)+int(top)+int(bottom)+int(topLeft)+int(topRight)+int(bottomLeft)+int(bottomRight)
avg = total//counter
result[row][col] = avg
return result
def median_filter(img, s=3):
#s = filter_size
temp = []
indexer = s // 2
out = []
out = np.zeros((len(img),len(img[0])))
for i in range(len(img)):
for j in range(len(img[0])):
for z in range(s):
if i + z - indexer < 0 or i + z - indexer > len(img) - 1:
for c in range(s):
temp.append(0)
else:
if j + z - indexer < 0 or j + indexer > len(img[0]) - 1:
temp.append(0)
else:
for k in range(s):
temp.append(img[i + z - indexer][j + k - indexer])
temp.sort()
out[i][j] = temp[len(temp) // 2]
temp = []
out = out.astype(np.uint8)
return out
def weighted_median_filter(img):
mask = np.ones([3, 3], dtype = int)
temp = []
out = np.zeros((len(img),len(img[0])))
row,col = img.shape
temp_filter = [[4,4,4],
[4,1,4],
[4,4,4]]
for i in range(1,row-1):
for j in range(1,col-1):
for a in range(-1,2,1):
for b in range(-1,2,1):
temp.append((img[i+a][j+b]*temp_filter[a+1][b+1],img[i+a][j+b]))
temp.sort()
out[i][j] = temp[len(temp) // 2][1]
temp = []
out = out.astype(np.uint8)
return out
def gen_gaussian_kernel(k_size, sigma):
center = k_size // 2
x, y = np.mgrid[0 - center : k_size - center, 0 - center : k_size - center]
g = 1 / (2 * np.pi * sigma) * np.exp(-(np.square(x) + np.square(y)) / (2 * np.square(sigma)))
return g
def gaussian_filter(img, k_size=3, sigma=1):
img = np.pad(img, 1)
height, width = img.shape[0], img.shape[1]
dst_height = height - k_size + 1
dst_width = width - k_size + 1
#print(dst_height,dst_width)
img_array = np.zeros((dst_height * dst_width, k_size * k_size))
row = 0
for i, j in product(range(dst_height), range(dst_width)):
window = np.ravel(img[i : i + k_size, j : j + k_size])
img_array[row, :] = window
row += 1
gaussian_kernel = gen_gaussian_kernel(k_size, sigma)
filter_array = np.ravel(gaussian_kernel)
dst = np.dot(img_array, filter_array).reshape(dst_height, dst_width).astype(np.uint8)
#516*516
return dst
def gaussian_filter2(img, k_size=3, sigma=1):
height, width = img.shape[0], img.shape[1]
dst_height = height
dst_width = width
#print(dst_height,dst_width)
img_array = np.zeros((dst_height * dst_width, k_size * k_size))
row = 0
for i, j in product(range(0,dst_height-2), range(0,dst_width-2)):
#print(i,j)
window = np.ravel(img[i : i + k_size, j : j + k_size])
img_array[row, :] = window
row += 1
#print("here")
gaussian_kernel = gen_gaussian_kernel(k_size, sigma)
filter_array = np.ravel(gaussian_kernel)
dst = np.dot(img_array, filter_array).reshape(dst_height, dst_width).astype(np.uint8)
return dst
def gaussian_kernel(kernel_size = 3):
h = gaussian(kernel_size, kernel_size / 3).reshape(kernel_size, 1)
h = np.dot(h, h.transpose())
h /= np.sum(h)
return h
def wiener_filter(img, kernel=None, K=300):
if kernel==None:
kernel = gaussian_kernel(3)
kernel /= np.sum(kernel)
dummy = np.copy(img)
dummy = fft2(dummy)
kernel = fft2(kernel, s = img.shape)
kernel = np.conj(kernel) / (np.abs(kernel) ** 2 + K)
dummy = dummy * kernel
dummy = np.abs(ifft2(dummy))
norm_image = cv2.normalize(dummy, None, alpha = 0, beta = 255, norm_type = cv2.NORM_MINMAX, dtype = cv2.CV_32F)
norm_image = norm_image.astype(np.uint8)
return norm_image
def show(img,name=None):
if name==None:
name="temp"
cv2.imshow(name,img)
cv2.waitKey(0)
cv2.destroyAllWindows()
#
# img = cv2.imread('small.jpg')
# b = img[:,:,0]
# g = img[:,:,1]
# r = img[:,:,2]
# b_d = wiener_filter(b)
# g_d = wiener_filter(g)
# r_d = wiener_filter(r)
# out = cv2.merge((b_d,g_d,r_d))
# show(out)
# print(out)