-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScript.py
More file actions
174 lines (137 loc) · 5.03 KB
/
Script.py
File metadata and controls
174 lines (137 loc) · 5.03 KB
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
import random
import numpy as np
import matplotlib.pyplot as plt
import cv2
def cropOutput(out2D, imageHeight, imageWidth, outputHeight, outputWidth):
A = np.zeros((imageHeight, imageWidth), dtype=int)
for i in range(imageHeight):
for j in range(imageWidth):
A[i][j] = out2D[i + (outputHeight - imageHeight)][j + outputWidth - imageWidth]
return A
def sizeAfterConv(height_width, kernelHeight_Width, padTop, padBot, stride):
return ((height_width + padTop + padBot - kernelHeight_Width) // stride) + 1
def paddImage(A, WindowHeight, WindowWidth):
imageHeight = len(A)
imageWidth = len(A[0])
paddingHeight = WindowHeight - 1
paddingWidth = WindowWidth - 1
# padding for convolution
paddedImage = np.zeros((imageHeight + 2 * paddingHeight, imageWidth + 2 * paddingWidth), dtype=int)
im = 0
for i in range(paddingHeight, imageHeight + paddingHeight):
jm = 0
for j in range(paddingWidth, imageWidth + paddingWidth):
paddedImage[i][j] = A[im][jm]
jm += 1
im += 1
return paddedImage
def gaussian(A):
noise = []
height = len(A)
width = len(A[0])
for i in range(height):
noise.append([])
for j in range(width):
noise[i].append(random.randint(-150, 150))
noise = np.array(noise)
A = A + noise
return A
def saltAndPaper(A):
for i in range(len(A)):
for j in range(len(A[0])):
if random.uniform(0.0, 1.0) < 0.5: # random probability
if random.uniform(0.0, 1.0) < 0.5:
A[i][j] = 0
else:
A[i][j] = 254
return A
def myImNoise(A, param):
if param == "gaussian":
return gaussian(A)
else:
return saltAndPaper(A)
def findMedian(W):
# w1D = np.reshape(W, (height * width, -1))
w1Dsorted = np.sort(W)
w1Dunique = np.unique(w1Dsorted)
# print(w1Dunique)
median = w1Dunique[len(w1Dunique) // 2]
return median
def median(A):
windowHeight = 5
windowWidth = 5
paddedIm = paddImage(A, windowHeight, windowWidth)
outputHeight = len(paddedIm) - windowHeight
outputWidth = len(paddedIm[0]) - windowWidth
# medianImage = np.zeros(outputHeight * outputWidth, dtype=int) # 1D **** THE OUTPUT
medianImage = []
for i in range(len(paddedIm) - windowHeight):
for j in range(len(paddedIm[0]) - windowWidth):
temp = []
for m in range(windowHeight):
for n in range(windowWidth):
temp.append(paddedIm[m + i][n + j])
temp = np.array(temp)
medianImage.append(findMedian(temp))
medianImage = np.array(medianImage)
out2D = np.reshape(medianImage, (outputHeight, -1)) # maybe output height second parameter
#return out2D
return cropOutput(out2D, len(A), len(A[0]), outputHeight, outputWidth)
def myConv2(A, k):
windowHeight = len(k)
windowWidth = len(k[0])
paddedIm = paddImage(A, windowHeight, windowWidth)
outputHeight = len(paddedIm) - windowHeight
outputWidth = len(paddedIm[0]) - windowWidth
# medianImage = np.zeros(outputHeight * outputWidth, dtype=int) # 1D **** THE OUTPUT
medianImage = []
k1D = np.reshape(k, (1, -1))
for i in range(len(paddedIm) - windowHeight):
for j in range(len(paddedIm[0]) - windowWidth):
temp = []
for m in range(windowHeight):
for n in range(windowWidth):
temp.append(paddedIm[m + i][n + j])
temp = np.array(temp)
mul = temp * k1D
medianImage.append(mul.sum())
medianImage = np.array(medianImage)
out2D = np.reshape(medianImage, (outputHeight, -1)) # maybe output height second parameter
#return out2D
return cropOutput(out2D, len(A), len(A[0]), outputHeight, outputWidth)
def myImFilter(A, param):
if param == "mean":
height = 16
width = 16
kernelF = np.full((height, width), (1/(height*width)))
kernelF = np.reshape(kernelF, (height, -1))
#kernelF = np.array([[1.0 / 9, 1.0 / 9, 1.0 / 9],
# [1.0 / 9, 1.0 / 9, 1.0 / 9],
# [1.0 / 9, 1.0 / 9, 1.0 / 9]])
outMean = myConv2(A, kernelF)
return outMean
else:
return median(A)
matrix = cv2.imread('test.jpg', 0) # read image - black and white
plt.subplot(2, 3, 1)
plt.imshow(matrix, cmap='gray')
kernel = np.array([[-1, 0, 1],
[-1, 0, 1],
[-1, 0, 1]])
plt.subplot(2, 3, 3)
plt.imshow(kernel, cmap='gray')
#out = myConv2(matrix, kernel, 'same')
#plt.subplot(2, 3, 5)
#plt.imshow(out, cmap='gray')
#out = medianTOconv(matrix, kernel)
#plt.subplot(2, 3, 5)
#plt.imshow(out, cmap='gray')
# out1 = signal.convolve2d(matrix, kernel, boundary='symm', mode='same')
# plt.subplot(2, 3, 4)
# plt.imshow(out1, cmap='gray')
matrix = myImNoise(matrix, "gaussian")
plt.subplot(2, 3, 2)
plt.imshow(matrix, cmap='gray')
plt.subplot(2, 3, 6)
plt.imshow(myImFilter(matrix, "mean"), cmap='gray')
plt.show()