-
Notifications
You must be signed in to change notification settings - Fork 2
/
spatial.py
279 lines (238 loc) · 8.59 KB
/
spatial.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import matplotlib.pyplot as plt
import numpy as np
from scipy import ndimage
import PIL.Image as Image
import tifwork
import sys
sys.path.append('./GUI/')
import imageGUI
#import scipy.signal as sig
'''
def plot(data,title):
plot.i = plot.i + 1
plt.subplot(3,2,plot.i)
plt.imshow(data)
plt.gray()
plt.title(title)
plot.i = 0
'''
def getData(fileName, num):
#get dataset
dataset = tifwork.openTIF(fileName)
#get details of dataset
(cols,rows,bands,bandArray) = tifwork.detailsTIF(dataset)
#get all bands
bandArray = tifwork.getBand(dataset,bands,bandArray)
#input a band
workData = bandArray[:,:,num-1]
#show original image and plot it
imdata = Image.fromarray(workData)
imdata = imdata.convert('L')
imdata.save('original.jpg')
imageGUI.imdisplay('original.jpg','Original',1)
#plot(workData,'Original')
return workData
#print workData
#print bandArray
#get kernel
def getKernel():
print 'Input Size of Kernel'
inp = raw_input()
kernel_size = int(inp)
kernel = np.zeros((kernel_size,kernel_size),dtype = float)
print 'Enter kernel elements in matrix form'
for i in range(0,kernel_size):
for j in range(0,kernel_size):
kernel[i,j] = float(raw_input())
print 'Input Kernel is'
print kernel
return kernel
#getKernel()
def meanFilter(fileName,size,num):
workData = getData(fileName,num)
kernel_size = size
kernel = np.ones((kernel_size,kernel_size),dtype = float)
print kernel
kernel = kernel / (kernel_size**2)
meanFilter = ndimage.convolve(workData, kernel,cval =1.0)
print 'Mean Filter'
meanFilter1 = 2 * workData - meanFilter
mfSave = Image.fromarray(meanFilter)
mfSave1 = Image.fromarray(meanFilter1)
mfSave = mfSave.convert('1')
mfSave1 = mfSave1.convert('1')
mfSave.save('Mean Filter.jpg')
mfSave1.save('Mean Filter1.jpg')
imageGUI.imdisplay('Mean Filter.jpg','Mean Filter',1)
imageGUI.imdisplay('Mean Filter1.jpg','Mean Filter1',1)
def medianFilter(fileName,size,num):
workData = getData(fileName,num)
print 'Input filter size'
#size = int(raw_input())
medFilter = ndimage.median_filter(workData,size)
mfSave = Image.fromarray(medFilter)
mfSave = mfSave.convert('1')
mfSave.save('Median Filter.jpg')
imageGUI.imdisplay('Median Filter.jpg','Median Filter',1)
# print 'med filter' , medFilter[100,:]
def gaussFilter(fileName,sigma,num):
workData = getData(fileName,num)
print 'INput sigma'
#sigma = float(raw_input())
gauFilter = ndimage.gaussian_filter(workData,sigma)
mfSave = Image.fromarray(gauFilter)
mfSave = mfSave.convert('1')
mfSave.save('Gauss Filter.jpg')
imageGUI.imdisplay('Gauss Filter.jpg','Guass Filter',1)
def sobelFilter(fileName,num):
workData = getData(fileName,num)
sobFilter = ndimage.sobel(workData)
mfSave = Image.fromarray(sobFilter)
mfSave = mfSave.convert('1')
mfSave.save('Sobel Filter.jpg')
imageGUI.imdisplay('Sobel Filter.jpg','Sobel Filter',1)
def laplaceFilter(fileName,num):
workData = getData(fileName,num)
lapFilter = ndimage.laplace(workData)
lapFilter = workData + lapFilter
mfSave = Image.fromarray(lapFilter)
mfSave = mfSave.convert('1')
mfSave.save('Laplace Filter.jpg')
imageGUI.imdisplay('Laplace Filter.jpg','Laplace Filter',1)
#High Pass Filters
def fourierFilter(fileName,sigma,num):
workData = getData(fileName,num)
print 'INput sigma'
#sigma = float(raw_input())
fourFilter1 = ndimage.fourier_uniform(workData,sigma)
fourFilter = ndimage.fourier_uniform(fourFilter1,sigma)
mfSave = Image.fromarray(fourFilter)
mfSave = mfSave.convert('L')
mfSave.save('Fourier Filter.jpg')
imageGUI.imdisplay('Fourier Filter.jpg','Fourier Filter',1)
# user defined
def filterUser(fileName,kernel,num):
workData = getData(fileName,num)
userFil = ndimage.convolve(workData,kernel)
imsave = Image.fromarray(userFil)
imsave = imsave.convert('1')
imsave.save('User defined kernel.jpg')
imageGUI.imdisplay('User defined kernel.jpg','User defined kernel',1)
# HIGH PASS PREDEFINED
def hpfEmbossE(fileName,num):
workData = getData(fileName,num)
kernel = np.array([[0,0,0],
[1,0,-1],
[0,0,0]],dtype = float)
eeFilter = ndimage.convolve(workData, kernel)
mfSave = Image.fromarray(eeFilter)
mfSave = mfSave.convert('1')
mfSave.save('Emboss East Filter.jpg')
imageGUI.imdisplay('Emboss East Filter.jpg','Emboss East Filter',1)
def hpfEmbossW(fileName,num):
workData = getData(fileName,num)
kernel = np.array([[0,0,0],
[-1,0,1],
[0,0,0]],dtype = float)
eeFilter = ndimage.convolve(workData, kernel)
mfSave = Image.fromarray(eeFilter)
mfSave = mfSave.convert('1')
mfSave.save('Emboss West Filter.jpg')
imageGUI.imdisplay('Emboss West Filter.jpg','Emboss West Filter',1)
def hpfEdgeDetect(fileName,num):
workData = getData(fileName,num)
kernel = np.array([[-1,-1,-1],
[-1,9,-1],
[-1,-1,-1]],dtype = float)
eeFilter = ndimage.convolve(workData, kernel)
mfSave = Image.fromarray(eeFilter)
mfSave = mfSave.convert('1')
mfSave.save('Edge Detect Filter.jpg')
imageGUI.imdisplay('Edge Detect Filter.jpg','Edge Detect Filter',1)
def hpfN(fileName,num):
workData = getData(fileName,num)
kernel = np.array([[1,1,1],
[1,-2,1],
[-1,-1,-1]],dtype = float)
eeFilter = ndimage.convolve(workData, kernel)
mfSave = Image.fromarray(eeFilter)
mfSave = mfSave.convert('1')
mfSave.save('North.jpg')
imageGUI.imdisplay('North.jpg','North',1)
def hpfNE(fileName,num):
workData = getData(fileName,num)
kernel = np.array([[1,1,1],
[-1,-2,1],
[-1,-1,1]],dtype = float)
eeFilter = ndimage.convolve(workData, kernel)
mfSave = Image.fromarray(eeFilter)
mfSave = mfSave.convert('1')
mfSave.save('NorthE.jpg')
imageGUI.imdisplay('NorthE.jpg','NorthE',1)
def hpfE(fileName,num):
workData = getData(fileName,num)
kernel = np.array([[-1,1,1],
[-1,-2,1],
[-1,1,1]],dtype = float)
eeFilter = ndimage.convolve(workData, kernel)
mfSave = Image.fromarray(eeFilter)
mfSave = mfSave.convert('1')
mfSave.save('East.jpg')
imageGUI.imdisplay('East.jpg','East',1)
def hpfSE(fileName,num):
workData = getData(fileName,num)
kernel = np.array([[-1,-1,1],
[-1,-2,1],
[-1,1,1]],dtype = float)
eeFilter = ndimage.convolve(workData, kernel)
mfSave = Image.fromarray(eeFilter)
mfSave = mfSave.convert('1')
mfSave.save('SouthE.jpg')
imageGUI.imdisplay('SouthE.jpg','SouthE',1)
def hpfS(fileName,num):
workData = getData(fileName,num)
kernel = np.array([[-1,-1,-1],
[1,-2,1],
[1,1,1]],dtype = float)
eeFilter = ndimage.convolve(workData, kernel)
mfSave = Image.fromarray(eeFilter)
mfSave = mfSave.convert('1')
mfSave.save('South.jpg')
imageGUI.imdisplay('South.jpg','South',1)
def hpfSW(fileName,num):
workData = getData(fileName,num)
kernel = np.array([[1,-1,-1],
[1,-2,-1],
[1,1,1]],dtype = float)
eeFilter = ndimage.convolve(workData, kernel)
mfSave = Image.fromarray(eeFilter)
mfSave = mfSave.convert('1')
mfSave.save('SouthW.jpg')
imageGUI.imdisplay('SouthW.jpg','SouthW',1)
def hpfW(fileName,num):
workData = getData(fileName,num)
kernel = np.array([[1,1,-1],
[1,-2,-1],
[1,1,-1]],dtype = float)
eeFilter = ndimage.convolve(workData, kernel)
mfSave = Image.fromarray(eeFilter)
mfSave = mfSave.convert('1')
mfSave.save('West.jpg')
imageGUI.imdisplay('West.jpg','West',1)
def hpfNW(fileName,num):
workData = getData(fileName,num)
kernel = np.array([[1,1,1],
[1,-2,-1],
[1,-1,-1]],dtype = float)
eeFilter = ndimage.convolve(workData, kernel)
mfSave = Image.fromarray(eeFilter)
mfSave = mfSave.convert('1')
mfSave.save('NorthW.jpg')
imageGUI.imdisplay('NorthW.jpg','NorthW',1)
def hpfPrewitt(fileName,num):
workData = getData(fileName,num)
preFilter = ndimage.prewitt(workData)
mfSave = Image.fromarray(preFilter)
mfSave = mfSave.convert('1')
mfSave.save('Prewitt Filter.png')
imageGUI.imdisplay('Prewitt Filter.png','Prewitt',1)