-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathFilter_GUI.py
209 lines (178 loc) · 8.4 KB
/
Filter_GUI.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
import streamlit as st
import cv2
from PIL import Image
import time
from io import BytesIO
import numpy as np
class smoothingFilters():
def __init__(self,image,K = 3, P= None):
self.k = K
self.image = image
self.P = P
self.outputimage = None
def trimmedMean(self,array):
trimGrayscale = 1
flattenarray = array.flatten()
sortedArray = sorted(flattenarray)
arrayLength = len(array)
# print(sortedArray)
trimmedArray = sortedArray[trimGrayscale:arrayLength-trimGrayscale]
trimmedMeanValue = np.mean(trimmedArray)/(arrayLength-2*trimGrayscale)
return trimmedMeanValue
def inverseGradient(self,array,P):
array=np.array(array, dtype=float)
final_image=[]
filter_array = array.flatten()
flattenarray = array.flatten()
#print(flattenarray)
value_of_p=P
center_pos=0
if(len(flattenarray)%2==0):
center_pos=int(len(flattenarray)/2)
else:
center_pos=int((len(flattenarray)-1)/2)
#print(flattenarray.shape)
#if(flattenarray[center_pos]==0):
#flattenarray=flattenarray+1
flattenarray[center_pos]=flattenarray[center_pos]+1
for i in range(0,len(flattenarray)):
if(i==4):
continue
else:
#print("---------"+str(i)+"------"+str(flattenarray[i]))
#print(abs(flattenarray[4]-flattenarray[i]))
value=abs(flattenarray[i]-flattenarray[4])
if(value==0):
value=value+1
else:
continue
flattenarray[i]=1/value
#print(flattenarray)
flattenarray[center_pos]=0
#print(flattenarray)
sum_total=sum(flattenarray)
#print("SUm total is:"+str(sum_total))
for i in range(0,len(flattenarray)):
if(i==4):
flattenarray[i]=value_of_p
else:
flattenarray[i]=((1-value_of_p)*(flattenarray[i]/sum_total))
#print(flattenarray)
Convolve_array=np.multiply(filter_array,flattenarray)
Gradient_Inverse_value=sum(Convolve_array)
#print(filter_array)
# print(Gradient_Inverse_value)
return Gradient_Inverse_value
def imagePadding(self,band,paddingValue):
##vertical Top padding
band = np.insert(band,0,band[0]*np.ones(paddingValue)[:,None],axis = 0)
##vertical bottom padding
band = np.insert(band,len(band),band[-1]*np.ones(paddingValue)[:,None],axis = 0)
##Horizontal left padding
band = np.insert(band, 0, band[:,0]*np.ones(paddingValue)[:,None], axis=1)
##Horizontal right padding
band = np.insert(band, len(band[0]), band[:,-1]*np.ones(paddingValue)[:,None], axis=1)
return band
def convolution(self,band,algo):
if (self.k % 2 == 0):
raise Exception("K should be odd number.")
paddingValue = int((self.k - 1)/2)
imageBand = self.imagePadding(band,paddingValue)
arrayshape = imageBand.shape
outputband = []
for rowNo in range(arrayshape[0] - paddingValue*2):
outputbandCol = []
for colNo in range(arrayshape[1] - paddingValue*2):
neighbourhoodMatrix = imageBand[rowNo:rowNo + self.k,colNo:colNo + self.k]
# print(neighbourhoodMatrix)
if algo == 'Trimmed Mean Filter':
avgValue = self.trimmedMean(neighbourhoodMatrix)
if algo == 'Inverse Gradient Filter':
avgValue = self.inverseGradient(neighbourhoodMatrix,self.P)
outputbandCol.append(avgValue)
outputband.append(outputbandCol)
return np.array(outputband)
def globalmeanstd(self,imagearray):
meanarray = np.array([np.mean(array[~np.isnan(array)].flatten()) for array in imagearray ])
stdarray = np.array([np.std(array[~np.isnan(array)].flatten()) for array in imagearray ])
print(meanarray,stdarray)
return meanarray,stdarray
def display(self,outputimage):
lengtharray = len(outputimage)
bandsequence = [outputimage[lengtharray - i] for i in range(1,lengtharray+1)]
if lengtharray!=3:
emptyarray = np.zeros(bandsequence[0].shape)
for _ in range(3-lengtharray):
bandsequence.append(emptyarray)
final_image=cv2.merge(tuple(bandsequence))
cv2.imwrite('outimage.jpg',final_image)
st.image('./outimage.jpg', caption=f"Output image", use_column_width=True)
def trimmedSmoothing(self,algo=None):
outputimage = []
img = self.image
if len(img.shape) != 3:
bands = 1
else:bands = img.shape[2]
inputmean, inputstd = [],[]
for bandNumber in range(bands):
if bands == 1:
bandarray = np.array(img)
else: bandarray = np.array([array[:,bandNumber] for array in img])
inputmean.append(np.mean(bandarray[~np.isnan(bandarray)].flatten()))
inputstd.append(np.std(bandarray[~np.isnan(bandarray)].flatten()))
outputband = self.convolution(bandarray,algo)
outputimage.append(outputband)
self.display(outputimage)
mean, std = self.globalmeanstd(outputimage)
st.markdown(f"The **input image** global mean to standard deviation ratio(RGB):**{tuple(np.round(np.array(inputmean)/np.array(inputstd),2))}**")
st.markdown(f"The **output image** global mean to standard deviation ratio(RGB):**{tuple(np.round(np.array(mean)/np.array(std),2))}**")
return None
def inverseGradientSmoothing(self,algo = None):
outputimage = []
img = self.image
if len(img.shape) != 3:
bands = 1
else:bands = img.shape[2]
inputmean, inputstd = [],[]
for bandNumber in range(bands):
if bands == 1:
bandarray = np.array(img)
else: bandarray = np.array([array[:,bandNumber] for array in img])
inputmean.append(np.mean(bandarray[~np.isnan(bandarray)].flatten()))
inputstd.append(np.std(bandarray[~np.isnan(bandarray)].flatten()))
outputband = self.convolution(bandarray,algo)
outputimage.append(outputband)
self.display(outputimage)
mean, std = self.globalmeanstd(outputimage)
st.markdown(f"The **input image** global mean to standard deviation ratio(RGB):**{tuple(np.round(np.array(inputmean)/np.array(inputstd),2))}**")
st.markdown(f"The **output image** global mean to standard deviation ratio(RGB):**{tuple(np.round(np.array(mean)/np.array(std),2))}**")
return None
Algorithm = st.sidebar.selectbox('Which algorithm do you want to implement?', ["Select algorithm", 'Trimmed Mean Filter','Inverse Gradient Filter'])
st.markdown("# Trimmed mean & Inverse gradient filter GUI")
inputimagefile = st.file_uploader('Upload the image',type = ['png','jpeg','jpg'])
show_file = st.empty()
if not inputimagefile:
show_file.info('Please upload the image')
if isinstance(inputimagefile,BytesIO):
imagefile = inputimagefile.read()
st.image(imagefile, caption=f"Input image", use_column_width=True)
pil_image = Image.open(inputimagefile)
img_array = np.array(pil_image)
print(img_array.shape)
if Algorithm == 'Select algorithm':
pass
if Algorithm == 'Trimmed Mean Filter':
K_trim = st.sidebar.slider('Select size of convolution matrix: n x n',3, 9, step=2)
start = time.time()
ourfilter = smoothingFilters(img_array,K=K_trim)
outputimage = ourfilter.trimmedSmoothing(algo = 'Trimmed Mean Filter' )
end = time.time()
st.markdown(f"**Total runtime: {round(end-start,2)} sec**")
if Algorithm == 'Inverse Gradient Filter':
K_inverse = st.sidebar.slider('Select size of convolution matrix: n x n ',3, 9,step=2)
P = st.sidebar.slider('Select weight assigned to center pixel (p)',0.0, 1.0,value=0.5)
start = time.time()
ourfilter = smoothingFilters(img_array,K=K_inverse, P = P)
outputimage = ourfilter.inverseGradientSmoothing(algo = 'Inverse Gradient Filter')
end = time.time()
st.markdown(f"**Total runtime: {round(end-start,2)} sec**")