-
Notifications
You must be signed in to change notification settings - Fork 0
/
final_gui.py
1161 lines (956 loc) · 41.3 KB
/
final_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
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
###########################################################################
## Python code generated with wxFormBuilder (version Apr 10 2019)
## http://www.wxformbuilder.org/
##
## PLEASE DO *NOT* EDIT THIS FILE!
###########################################################################
import wx
import wx.xrc
from PIL import Image
import numpy as np
import cv2
import os
import math
import sys
from scipy import ndimage
import matplotlib
matplotlib.use('WXAgg')
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
from matplotlib.patches import Rectangle
import matplotlib.pyplot as plt
###########################################################################
## Class MyFrame3
###########################################################################
class BandRejectFilter:
image = None
filter = None
cutoff1 = None
cutoff2 = None
order = None
output = None
def __init__(self, image, filter_name, cutoff1 =170, cutoff2=250, order = 0):
self.image = image
if filter_name == 'gaussian_l':
self.filter = self.get_gaussian_low_pass_filter
elif filter_name == 'gaussian_h':
self.filter = self.get_gaussian_high_pass_filter
elif filter_name == 'gaussian_BRF':
self.filter = self.get_gaussian_BRF
self.cutoff1 = cutoff1
self.cutoff2 = cutoff2
self.order = order
def get_gaussian_BRF(self, shape, cutoff1, cutoff2):
height = shape[0]
width = shape[1]
gaussianBRF = np.zeros((height, width))
gaussianImageL = self.get_gaussian_low_pass_filter(shape, cutoff1)
gaussianImageH = self.get_gaussian_high_pass_filter(shape, cutoff2)
for u in range(height):
for v in range(width):
gaussianBRF[u, v] = 1 - (gaussianImageL[u, v] * gaussianImageH[u, v])
return gaussianBRF
def get_gaussian_low_pass_filter(self, shape, cutoff1):
height = shape[0]
width = shape[1]
m = height / 2
n = width / 2
gaussianImageL = np.zeros((height, width))
for u in range(height):
for v in range(width):
dist = self.getDist(u, v, m, n)
gaussianImageL[u,v] = math.pow(math.e, -math.pow(dist,2)/(2*math.pow(cutoff1,2)))
return gaussianImageL
def get_gaussian_high_pass_filter(self, shape, cutoff2):
#Hint: May be one can use the low pass filter function to get a high pass mask
height = shape[0]
width = shape[1]
gaussianImageL = self.get_gaussian_low_pass_filter(shape, cutoff2)
gaussianImageH = np.zeros((height, width))
for u in range(height):
for v in range(width):
gaussianImageH[u,v] = 1 - gaussianImageL[u,v]
return gaussianImageH
def post_process_image(self, image):
imageMin = np.min(image)
imageMax = np.max(image)
newImage = np.uint8(255 / (imageMax - imageMin) * (image - imageMin) + .5)
return newImage
def filtering(self):
# 1 and 2
fftImage = np.fft.fft2(self.image)
shiftImage = np.fft.fftshift(fftImage)
# 3
mask = self.filter(self.image.shape, self.cutoff1, self.cutoff2)
# 4, 5 and 6
filteredImage = shiftImage * mask
inverseShiftImage = np.fft.ifftshift(filteredImage)
inverseShiftImage = np.fft.ifft2(inverseShiftImage)
# 7
magnitudeDFT = np.log(np.abs(shiftImage))
mask = np.log(np.abs(mask))
mask = np.abs(mask)
mask = 1000 * mask
print(mask)
magnitudeDFT = magnitudeDFT*20
#magnitudeDFT = np.log(abs(shiftImage))
#magnitudeDFT = np.log(cv2.magnitude(shiftImage[:,:,0], shiftImage[:,:,1]))
#magnitudeDFT, angle = cv2.cartToPolar(np.real(filteredImage), np.imag(filteredImage))
magnitudeImage = np.abs(inverseShiftImage)
magnitudeFilteredfft = np.log(np.abs(inverseShiftImage))
# 8
#magnitudeDFT = self.post_process_image(magnitudeDFT)
magnitudeFilteredfft = self.post_process_image(magnitudeFilteredfft)
finalFilteredImage = self.post_process_image(magnitudeImage)
return [finalFilteredImage, magnitudeDFT, mask]
#mask = mask.astype(np.uint8)
#shiftImage = shiftImage.astype(np.complex_)
#fftImage = fftImage.astype(np.uint8)
#return [finalFilteredImage, shiftImage, magnitudeImage]
def getDist(self, u, v, m, n):
a = math.pow((u-m), 2)
b = math.pow((v-n), 2)
return math.sqrt(a+b)
class adaptive_filter:
def __init__(self, image, adaptive_filter_name):
self.image=image
#self.filter_h = 3
#self.filter_w = 3
if adaptive_filter_name=='reduction':
self.filter=self.get_adaptive_reduction_filter
elif adaptive_filter_name=='median':
self.filter=self.get_adaptive_median_filter
local_count = 0
local_zero = 0
local_reduced_intensity = 0
def reduction_filter(self,image_slice, value, filter_h, filter_w, variance):
v = variance
if v == 0:
return value
else:
local_var = int(ndimage.variance(image_slice))
m = image_slice.mean()
if variance == local_var:
self.local_count = self.local_count + 1
return np.uint8(m)
elif int(local_var) == 0:
self.local_zero = self.local_zero + 1
return value
else:
self.local_reduced_intensity = self.local_reduced_intensity + 1
return (value - (v/local_var)*(value - m))
def median_filter(self, image_slice, value, filter_h, filter_w,max_int):
w = image_slice.shape[1]
for i in range(w):
max_int = max_int+1
vert_start = int(1/2*(max_int-filter_h))
horiz_start = int(1/2*(max_int-filter_w))
vert_end = int(1/2*(max_int+filter_h))
horiz_end = int(1/2*(max_int+filter_w))
windowS = image_slice[vert_start:vert_end , horiz_start:horiz_end]
try:
zmed= np.median(windowS)
except ValueError:
zmed = 0
try:
zmin = np.min(windowS)
except ValueError:
zmin = 0
try:
zmax = np.max(windowS)
except ValueError:
zmax = 0
if zmed>zmin and zmed<zmax:
if value>zmin and value<zmax:
return value
else:
return zmed
else:
if (filter_h + 2)> max_int or (filter_w+ 2)> max_int:
return zmed
else:
return self.median_filter(image_slice, value, filter_h+2, filter_w+2, max_int)
def get_adaptive_reduction_filter(self, image):
filter_h = 3
img_var = int(ndimage.variance(image))
filter_w = 3
pad_h = int((filter_h - 1) / 2)
pad_w = int((filter_w - 1) / 2)
pad_img = np.pad(image, ((pad_h, pad_h), (pad_w, pad_w)), 'constant', constant_values=0)
height, width = image.shape
filtered_image = np.zeros((height, width))
for u in range(height):
for v in range(width):
temp_image=pad_img[u:(u+3),v:(v+3)]
filtered_image[u][v] = self.reduction_filter(temp_image,pad_img[u][v],filter_h,filter_w,img_var)
return filtered_image
def get_adaptive_median_filter(self, image):
filter_h = 3
img_var = ndimage.variance(image)
filter_w = 3
max_int = 0
pad_h = int((filter_h - 1) / 2)
pad_w = int((filter_w - 1) / 2)
pad_img = np.pad(image, ((pad_h, pad_h), (pad_w, pad_w)), 'constant', constant_values=0)
height, width = image.shape
filtered_image = np.zeros((height, width))
for u in range(height):
for v in range(width):
temp = pad_img[u:(u+3),v:(v+3)]
filtered_image[u][v] = self.median_filter(image,pad_img[u][v],filter_h,filter_w,max_int)
return filtered_image
def filtering(self):
if self.filter==self.get_adaptive_reduction_filter:
filtered_image=self.get_adaptive_reduction_filter(self.image)
elif self.filter==self.get_adaptive_median_filter:
filtered_image=self.get_adaptive_median_filter(self.image)
return filtered_image
class RectangleSelectImagePanel(wx.Panel):
# majority of code in this class was found on the internet
def __init__(self, parent, pathToImage=None):
# Initialise the parent
wx.Panel.__init__(self, parent)
# Intitialise the matplotlib figure
self.figure = plt.figure()
# Create an axes, turn off the labels and add them to the figure
self.axes = plt.Axes(self.figure,[0,0,1,1])
self.axes.set_axis_off()
self.figure.add_axes(self.axes)
# Add the figure to the wxFigureCanvas
self.canvas = FigureCanvas(self, -1, self.figure)
self.Image = None
# Initialise the rectangle
self.rect = Rectangle((0,0), 1, 1, facecolor='None', edgecolor='green')
self.x0 = None
self.y0 = None
self.x1 = None
self.y1 = None
self.axes.add_patch(self.rect)
# Sizer to contain the canvas
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.sizer.Add(self.canvas, 3, wx.ALL)
self.SetSizer(self.sizer)
self.Fit()
# Connect the mouse events to their relevant callbacks
self.canvas.mpl_connect('button_press_event', self._onPress)
self.canvas.mpl_connect('button_release_event', self._onRelease)
self.canvas.mpl_connect('motion_notify_event', self._onMotion)
# Lock to stop the motion event from behaving badly when the mouse isn't pressed
self.pressed = False
# If there is an initial image, display it on the figure
if pathToImage is not None:
self.setImage(pathToImage)
def _onPress(self, event):
''' Callback to handle the mouse being clicked and held over the canvas'''
# Check the mouse press was actually on the canvas
if event.xdata is not None and event.ydata is not None:
# Upon initial press of the mouse record the origin and record the mouse as pressed
self.pressed = True
self.rect.set_linestyle('dashed')
self.x0 = event.xdata
self.y0 = event.ydata
def _onRelease(self, event):
'''Callback to handle the mouse being released over the canvas'''
# Check that the mouse was actually pressed on the canvas to begin with and this isn't a rouge mouse
# release event that started somewhere else
if self.pressed:
# Upon release draw the rectangle as a solid rectangle
self.pressed = False
self.rect.set_linestyle('solid')
# Check the mouse was released on the canvas, and if it wasn't then just leave the width and
# height as the last values set by the motion event
if event.xdata is not None and event.ydata is not None:
self.x1 = event.xdata
self.y1 = event.ydata
# Set the width and height and origin of the bounding rectangle
self.boundingRectWidth = self.x1 - self.x0
self.boundingRectHeight = self.y1 - self.y0
self.bouningRectOrigin = (self.x0, self.y0)
# Draw the bounding rectangle
self.rect.set_width(self.boundingRectWidth)
self.rect.set_height(self.boundingRectHeight)
self.rect.set_xy((self.x0, self.y0))
self.canvas.draw()
int_x0 = int(self.x0)
int_x1 = int(self.x1)
int_y0 = int(self.y0)
int_y1 = int(self.y1)
temp_array = np.zeros((np.abs(int_y1 - int_y0), np.abs(int_x1 - int_x0)), dtype = np.uint8)
y = 0
x = 0
if (int_y1 > int_y0):
if (int_x1 > int_x0):
for row in range(int_y0, int_y1):
x = 0
for col in range(int_x0, int_x1):
temp_array[y][x] = self.image[row][col]
x = x + 1
y = y + 1
else:
for row in range(int_y0, int_y1):
x = 0
for col in range(int_x1, int_x0):
temp_array[y][x] = self.image[row][col]
x = x + 1
y = y + 1
elif (int_y1 < int_y0):
if (int_x1 > int_x0):
for row in range(int_y1, int_y0):
x = 0
for col in range(int_x0, int_x1):
temp_array[y][x] = self.image[row][col]
x = x + 1
y = y + 1
else:
for row in range(int_y1, int_y0):
x = 0
for col in range(int_x1, int_x0):
temp_array[y][x] = self.image[row][col]
x = x + 1
y = y + 1
#cv2.imshow('image', temp_array)
#cv2.waitKey(0)
#cv2.destroyAllWindows()
plt.close()
plt.figure()
vals = temp_array.flatten()
# calculate histogram
counts, bins = np.histogram(vals, range(257))
# plot histogram centered on values 0..255
plt.bar(bins[:-1] - 0.5, counts, width=1, edgecolor='none')
plt.xlim([-0.5, 255.5])
plt.show()
def _onMotion(self, event):
'''Callback to handle the motion event created by the mouse moving over the canvas'''
# If the mouse has been pressed draw an updated rectangle when the mouse is moved so
# the user can see what the current selection is
if self.pressed:
# Check the mouse was released on the canvas, and if it wasn't then just leave the width and
# height as the last values set by the motion event
if event.xdata is not None and event.ydata is not None:
self.x1 = event.xdata
self.y1 = event.ydata
# Set the width and height and draw the rectangle
self.rect.set_width(self.x1 - self.x0)
self.rect.set_height(self.y1 - self.y0)
self.rect.set_xy((self.x0, self.y0))
self.canvas.draw()
def setImage(self, pathToImage):
'''Sets the background image of the canvas'''
# Load the image into matplotlib and PIL
self.image = cv2.imread(pathToImage, cv2.IMREAD_GRAYSCALE)
image = self.image
imPIL = Image.open(pathToImage)
# Save the image's dimensions from PIL
self.imageSize = imPIL.size
# Add the image to the figure and redraw the canvas. Also ensure the aspect ratio of the image is retained.
self.axes.imshow(image, cmap = "gray")
self.canvas.draw()
class BandPassFilter:
image = None
filter = None
cutoff1 = None
cutoff2 = None
order = None
output = None
def __init__(self, image, filter_name, cutoff1, cutoff2, order = 0):
self.image = image
if filter_name == 'gaussian_l':
self.filter = self.get_gaussian_low_pass_filter
elif filter_name == 'gaussian_h':
self.filter = self.get_gaussian_high_pass_filter
elif filter_name == 'gaussian_BPF':
self.filter = self.get_gaussian_BPF
self.cutoff1 = cutoff1
self.cutoff2 = cutoff2
self.order = order
def get_gaussian_BPF(self, shape, cutoff1, cutoff2):
height = shape[0]
width = shape[1]
gaussianBPF = np.zeros((height, width))
gaussianImageL = self.get_gaussian_low_pass_filter(shape, cutoff1)
gaussianImageH = self.get_gaussian_high_pass_filter(shape, cutoff2)
for u in range(height):
for v in range(width):
gaussianBPF[u, v] = gaussianImageL[u, v] * gaussianImageH[u, v]
return gaussianBPF
def get_gaussian_low_pass_filter(self, shape, cutoff1):
height = shape[0]
width = shape[1]
m = height / 2
n = width / 2
gaussianImageL = np.zeros((height, width))
for u in range(height):
for v in range(width):
dist = self.getDist(u, v, m, n)
gaussianImageL[u,v] = math.pow(math.e, -math.pow(dist,2)/(2*math.pow(cutoff1,2)))
return gaussianImageL
def get_gaussian_high_pass_filter(self, shape, cutoff2):
#Hint: May be one can use the low pass filter function to get a high pass mask
height = shape[0]
width = shape[1]
gaussianImageL = self.get_gaussian_low_pass_filter(shape, cutoff2)
gaussianImageH = np.zeros((height, width))
for u in range(height):
for v in range(width):
gaussianImageH[u,v] = 1 - gaussianImageL[u,v]
return gaussianImageH
def post_process_image(self, image):
imageMin = np.min(image)
imageMax = np.max(image)
newImage = np.uint8(255 / (imageMax - imageMin) * (image - imageMin) + .5)
return newImage
def filtering(self):
# 1 and 2
fftImage = np.fft.fft2(self.image)
shiftImage = np.fft.fftshift(fftImage)
# 3
mask = self.filter(self.image.shape, self.cutoff1, self.cutoff2)
# 4, 5 and 6
filteredImage = shiftImage * mask
inverseShiftImage = np.fft.ifftshift(filteredImage)
inverseShiftImage = np.fft.ifft2(inverseShiftImage)
# 7
magnitudeDFT = np.log(np.abs(shiftImage))
mask = np.log(np.abs(mask))
mask = np.abs(mask)
magnitudeDFT = magnitudeDFT*20
#magnitudeDFT = np.log(cv2.magnitude(shiftImage[:,:,0], shiftImage[:,:,1]))
#magnitudeDFT, angle = cv2.cartToPolar(np.real(filteredImage), np.imag(filteredImage))
magnitudeImage = np.log(np.abs(inverseShiftImage))
magnitudeFilteredfft = np.log(np.abs(inverseShiftImage))
# 8
#magnitudeDFT = self.post_process_image(magnitudeDFT)
magnitudeFilteredfft = self.post_process_image(magnitudeFilteredfft)
finalFilteredImage = self.post_process_image(magnitudeImage)
return [finalFilteredImage, magnitudeDFT, mask]
#mask = mask.astype(np.uint8)
#shiftImage = shiftImage.astype(np.complex_)
#fftImage = fftImage.astype(np.uint8)
#return [finalFilteredImage, shiftImage, magnitudeImage]
def getDist(self, u, v, m, n):
a = math.pow((u-m), 2)
b = math.pow((v-n), 2)
return math.sqrt(a+b)
class orderstatistic_filter:
image = None
filter = None
#filter = 3x3
#define constructor with parameters: image, filter_name
def __init__(self, image, filter_name):
self.image = image
if filter_name == 'minimum':
self.filter = self.get_min_filter
elif filter_name == 'maximum':
self.filter = self.get_max_filter
elif filter_name == 'median':
self.filter = self.get_median_filter
elif filter_name == 'mean':
self.filter = self.get_alpha_trimmed_mean_filter
def get_min_filter(self, image):
filter_h = 3
filter_w = 3
pad_h = int((filter_h - 1) / 2)
pad_w = int((filter_w - 1) / 2)
pad_img = np.pad(image, ((pad_h, pad_h), (pad_w, pad_w)), 'constant', constant_values=0)
height, width = image.shape
new_img = np.zeros((height, width))
for i in range(height):
for j in range(width):
temp_img = pad_img[i:(i+filter_h), j:(j+filter_w)]
# use min to find minimum value
new_img[i, j] = np.min(temp_img)
return new_img
def get_max_filter(self, image):
filter_h = 3
filter_w = 3
pad_h = int((filter_h - 1) / 2)
pad_w = int((filter_w - 1) / 2)
pad_img = np.pad(image, ((pad_h, pad_h), (pad_w, pad_w)), 'constant', constant_values=0)
height, width = image.shape
new_img = np.zeros((height, width))
for i in range(height):
for j in range(width):
temp = pad_img[i:(i + filter_h), j:(j + filter_w)]
# use max to find maximum value
new_img[i, j] = np.max(temp)
return new_img
def get_median_filter(self, image):
filter_h = 3
filter_w = 3
pad_h = int((filter_h - 1) / 2)
pad_w = int((filter_w - 1) / 2)
pad_img = np.pad(image, ((pad_h, pad_h), (pad_w, pad_w)), 'constant', constant_values=0)
height, width = image.shape
new_img = np.zeros((height, width))
for i in range(height):
for j in range(width):
temp = pad_img[i:(i + filter_h), j:(j + filter_w)]
# use np.median to find the median value
new_img[i, j] = np.median(temp)
return new_img
def get_alpha_trimmed_mean_filter(self, image):
filter_h = 3
filter_w = 3
pad_h = int((filter_h - 1) / 2)
pad_w = int((filter_w - 1) / 2)
pad_img = np.pad(image, ((pad_h, pad_h), (pad_w, pad_w)), 'constant', constant_values=0)
height, width = image.shape
new_img = np.zeros((height, width))
for i in range(height):
for j in range(width):
temp = pad_img[i:(i + filter_h), j:(j + filter_w)]
# to get alpha trimmed mean we need to first sort the values that were picked by the filter and then delete the first and last value then take the mean
np.sort(temp)
for x in [8,0]:
temp = np.delete(temp, x)
new_img[i,j] = np.mean(temp)
return new_img
def filtering(self):
if self.filter == self.get_min_filter:
filtered_image = self.get_min_filter(self.image)
elif self.filter == self.get_max_filter:
filtered_image = self.get_max_filter(self.image)
elif self.filter == self.get_median_filter:
filtered_image = self.get_median_filter(self.image)
elif self.filter == self.get_alpha_trimmed_mean_filter:
filtered_image = self.get_alpha_trimmed_mean_filter(self.image)
return filtered_image
class mean_filter:
image=None
filter=None
order=None
#define constructor with parameter: image, filter_name, filter window height AND WIDTH, order(contraharmonic only)
def __init__(self,image,mean_filter_name,order=0):
self.image=image
if mean_filter_name=='arithmetic':
self.filter=self.get_arithmetic_mean_filter
elif mean_filter_name=='geometric':
self.filter=self.get_geometric_mean_filter
elif mean_filter_name=='harmonic':
self.filter=self.get_harmonic_mean_filter
elif mean_filter_name=='contraharmonic':
self.filter=self.get_contraharmonic_mean_filter
self.order=order
def get_arithmetic_mean_filter(self,image):
#apply zero padding, default window size 3x3
#reference: https://stackoverflow.com/questions/44145948/numpy-padding-array-with-zeros
# https://docs.scipy.org/doc/numpy/reference/generated/numpy.pad.html
filter_h=3
filter_w=3
pad_h=int((filter_h-1)/2)
pad_w=int((filter_w-1)/2)
padded_image=np.pad(image,((pad_h,pad_h),(pad_w,pad_w)),'constant',constant_values=0)
#get width&height
height,width=image.shape
filtered_image=np.zeros((height,width))
#self note: if size of image on GUI is diff, need to adjust***
for row in range(height):
for col in range(width):
#get window of small part of image
temp_image=padded_image[row:(row+filter_h),col:(col+filter_w)]
#use np sum to get summation of all pixels and get mean of all pixels
filtered_image[row,col]=1/(filter_h*filter_w)*np.sum(temp_image)
return filtered_image
def get_geometric_mean_filter(self,image):
"""geometric mean filter achieves smoothing
comparable to the arithmetic mean filter, but it tends to lose
less image detail in the process"""
#https://en.wiktionary.org/wiki/%CE%A0
#apply zero padding
filter_h=3
filter_w=3
pad_h=int((filter_h-1)/2)
pad_w=int((filter_w-1)/2)
padded_image=np.pad(image,((pad_h,pad_h),(pad_w,pad_w)),'constant',constant_values=0)
#get width&height
height,width=image.shape
filtered_image=np.zeros((height,width))
for row in range(height):
for col in range(width):
#get window of small part of image
temp_image=padded_image[row:(row+filter_h),col:(col+filter_w)]
# get Product over a set of terms: (Pi? Π?)
product=1.0
for p_h in range(filter_h):
for p_w in range(filter_w):
product=temp_image[p_h,p_w]*product
filtered_image[row,col]=np.power(product,1/(filter_h*filter_w))
return filtered_image
def get_harmonic_mean_filter(self,image):
"""
Works well for SALT noise & Gaussian noise, fails for PEPPER noise
"""
#apply zero padding
filter_h=3
filter_w=3
pad_h=int((filter_h-1)/2)
pad_w=int((filter_w-1)/2)
padded_image=np.pad(image,((pad_h,pad_h),(pad_w,pad_w)),'constant',constant_values=0)
#get width&height
height,width=image.shape
filtered_image=np.zeros((height,width))
for row in range(height):
for col in range(width):
#get window of small part of image
temp_image=padded_image[row:(row+filter_h),col:(col+filter_w)]
#mn-->filter window_h&w
#f(x,y)=mn/ summation of (1/g(s,t)), g(s,t) is denominator
# so, we only care about positive g(s,t) values.
#get summation of 1/g(s,t)
sum_g=0
for s_h in range(filter_h):
for s_w in range(filter_w):
#only care about positive values
if temp_image[s_h,s_w]!=0:
sum_g+=1/temp_image[s_h,s_w]
filtered_image[row,col]=filter_h*filter_w/sum_g
return filtered_image
def get_contraharmonic_mean_filter(self,image,order):
"""well suited for reducing the effects of SALT-and-PEPPER
noise. Q>0 for pepper noise and Q<0 for salt noise. (Q: order of filter)
"""
#apply zero padding
filter_h=3
filter_w=3
pad_h=int((filter_h-1)/2)
pad_w=int((filter_w-1)/2)
padded_image=np.pad(image,((pad_h,pad_h),(pad_w,pad_w)),'constant',constant_values=0)
#get width&height
height,width=image.shape
filtered_image=np.zeros((height,width))
for row in range(height):
for col in range(width):
#get window of small part of image
temp_image=padded_image[row:(row+filter_h),col:(col+filter_w)]
#f(x,y)=summation of g(s,t)^(Q+1) / summation of g(s,t)^(Q)
#similar to harmonic: g(s,t)^(Q) is denominator cant be zero
sum_g1=0
sum_g2=0
for s_h in range(filter_h):
for s_w in range(filter_w):
#get power of Q of each intensity and sum
sum_g1+=np.power(temp_image[s_h,s_w], order+1)
sum_g2+=np.power(temp_image[s_h,s_w], order)
if sum_g2!=0:
filtered_image[row,col]=sum_g1/sum_g2
else:
filtered_image[row,col]=0
return filtered_image
def filtering(self):
if self.filter==self.get_arithmetic_mean_filter:
filtered_image=self.get_arithmetic_mean_filter(self.image)
elif self.filter==self.get_geometric_mean_filter:
filtered_image=self.get_geometric_mean_filter(self.image)
elif self.filter==self.get_harmonic_mean_filter:
filtered_image=self.get_harmonic_mean_filter(self.image)
elif self.filter==self.get_contraharmonic_mean_filter:
filtered_image=self.get_contraharmonic_mean_filter(self.image,self.order)
return filtered_image
class MyFrame3 ( wx.Frame ):
def __init__( self, parent ):
wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = wx.EmptyString, pos = wx.DefaultPosition, size = wx.Size( 635,439 ), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL )
self.SetSizeHints( wx.DefaultSize, wx.DefaultSize )
bSizer16 = wx.BoxSizer( wx.HORIZONTAL )
gSizer10 = wx.GridSizer( 0, 2, 0, 0 )
self.m_bitmap6 = wx.StaticBitmap( self, wx.ID_ANY, wx.NullBitmap, wx.DefaultPosition, wx.DefaultSize, 0 )
gSizer10.Add( self.m_bitmap6, 0, wx.ALL, 5 )
self.m_bitmap7 = wx.StaticBitmap( self, wx.ID_ANY, wx.NullBitmap, wx.DefaultPosition, wx.DefaultSize, 0 )
gSizer10.Add( self.m_bitmap7, 0, wx.ALL, 5 )
self.m_bitmap8 = wx.StaticBitmap( self, wx.ID_ANY, wx.NullBitmap, wx.DefaultPosition, wx.DefaultSize, 0 )
gSizer10.Add( self.m_bitmap8, 0, wx.ALL, 5 )
self.m_bitmap9 = wx.StaticBitmap( self, wx.ID_ANY, wx.NullBitmap, wx.DefaultPosition, wx.DefaultSize, 0 )
gSizer10.Add( self.m_bitmap9, 0, wx.ALL, 5 )
bSizer16.Add( gSizer10, 1, wx.EXPAND, 5 )
bSizer22 = wx.BoxSizer( wx.VERTICAL )
self.m_radioBtn38 = wx.RadioButton( self, wx.ID_ANY, u"Original Image", wx.DefaultPosition, wx.DefaultSize, 0 )
bSizer22.Add( self.m_radioBtn38, 0, wx.ALL, 5 )
m_choice2Choices = [ u"Select Mean Filter", u"Arithmetic Mean Filter", u"Geometric Mean Filter", u"Harmonic Mean Filter", u"Contraharmonic Mean Filter"]
self.m_choice2 = wx.Choice( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, m_choice2Choices, 0 )
self.m_choice2.SetSelection( 0 )
bSizer22.Add( self.m_choice2, 0, wx.ALL, 5 )
m_choice3Choices = [ u"Select Order Statistic Filter", u"Minimum Order Statistic Filter", u"Maximum Order Statistic Filter", u"Median Order Statistic Filter", u"Trimmed Mean Order Statistic Filter" ]
self.m_choice3 = wx.Choice( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, m_choice3Choices, 0 )
self.m_choice3.SetSelection( 0 )
bSizer22.Add( self.m_choice3, 0, wx.ALL, 5 )
m_choice4Choices = [ u"Select Adaptive Filter", u"Adaptive Reduction Filter", u"Adaptive Median Filter" ]
self.m_choice4 = wx.Choice( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, m_choice4Choices, 0 )
self.m_choice4.SetSelection( 0 )
bSizer22.Add( self.m_choice4, 0, wx.ALL, 5 )
self.m_radioBtn32 = wx.RadioButton( self, wx.ID_ANY, u"Band Reject Filter", wx.DefaultPosition, wx.DefaultSize, 0 )
bSizer22.Add( self.m_radioBtn32, 0, wx.ALL, 5 )
self.m_radioBtn33 = wx.RadioButton( self, wx.ID_ANY, u"Notch Filter", wx.DefaultPosition, wx.DefaultSize, 0 )
bSizer22.Add( self.m_radioBtn33, 0, wx.ALL, 5 )
bSizer23 = wx.BoxSizer( wx.HORIZONTAL )
bSizer22.Add( bSizer23, 0, wx.EXPAND, 5 )
self.m_radioBtn34 = wx.RadioButton( self, wx.ID_ANY, u"Band Pass Filter", wx.DefaultPosition, wx.DefaultSize, 0 )
bSizer22.Add( self.m_radioBtn34, 0, wx.ALL, 5 )
self.m_button198 = wx.Button( self, wx.ID_ANY, u"Noise Sampling", wx.DefaultPosition, wx.DefaultSize, 0 )
bSizer22.Add( self.m_button198, 0, wx.ALL|wx.EXPAND, 5 )
self.m_button199 = wx.Button( self, wx.ID_ANY, u"Pick Image", wx.DefaultPosition, wx.DefaultSize, 0 )
bSizer22.Add( self.m_button199, 0, wx.ALL|wx.EXPAND, 5 )
self.m_button200 = wx.Button( self, wx.ID_ANY, u"Save Image", wx.DefaultPosition, wx.DefaultSize, 0 )
bSizer22.Add( self.m_button200, 0, wx.ALL|wx.EXPAND, 5 )
bSizer16.Add( bSizer22, 0, wx.EXPAND, 5 )
self.SetSizer( bSizer16 )
self.Layout()
self.Centre( wx.BOTH )
def getBound(self, event):
self.m_bitmap8.SetBitmap(wx.NullBitmap)
self.Refresh()
self.m_bitmap9.SetBitmap(wx.NullBitmap)
self.Refresh()
lBound = self.ask(message = "Insert your lower bound")
lBound = int(lBound)
hBound = self.ask(message = "Insert your upper bound")
hBound = int(hBound)
self.onButtonNotchFilter(lBound, hBound)
def onButtonBandPassFilter(self, event):
self.m_bitmap9.SetBitmap(wx.NullBitmap)
self.m_bitmap8.SetBitmap(wx.NullBitmap)
self.Refresh()
lCutoff = self.ask(message = "Insert your lower cutoff")
lCutoff = int(lCutoff)
hCutoff = self.ask(message = "Insert your upper cutoff")
hCutoff = int(hCutoff)
Filter_obj = BandPassFilter(self.orig_image, "gaussian_BPF", lCutoff, hCutoff)
output = Filter_obj.filtering()
self.image = output
cv2.imwrite("test.png", output[0])
img = wx.Image("test.png", wx.BITMAP_TYPE_ANY).ConvertToGreyscale().ConvertToBitmap()
self.m_bitmap7.SetBitmap(img)
os.remove("test.png")
cv2.imwrite("test.png", output[2])
img = wx.Image("test.png", wx.BITMAP_TYPE_ANY).ConvertToGreyscale().ConvertToBitmap()
self.m_bitmap8.SetBitmap(img)
os.remove("test.png")
cv2.imwrite("test.png", output[1])
img = wx.Image("test.png", wx.BITMAP_TYPE_ANY).ConvertToGreyscale().ConvertToBitmap()
self.m_bitmap9.SetBitmap(img)
os.remove("test.png")
def onButtonAdaptiveFilter(self, event):
self.m_bitmap9.SetBitmap(wx.NullBitmap)
self.m_bitmap8.SetBitmap(wx.NullBitmap)
self.Refresh()
if (self.m_choice4.GetStringSelection() == "Adaptive Reduction Filter"):
Filter_obj = adaptive_filter(self.orig_image, "reduction")
output = Filter_obj.filtering()
elif (self.m_choice4.GetStringSelection() == "Adaptive Median Filter"):
Filter_obj = adaptive_filter(self.orig_image, "median")
output = Filter_obj.filtering()
else:
self.m_bitmap7.SetBitmap(wx.NullBitmap)
self.Refresh()
return
self.image = output
cv2.imwrite("test.png", output)
img = wx.Image("test.png", wx.BITMAP_TYPE_ANY).ConvertToGreyscale().ConvertToBitmap()
self.m_bitmap7.SetBitmap(img)
os.remove("test.png")
def onButtonOrderStatisticFilter(self, event):
self.m_bitmap9.SetBitmap(wx.NullBitmap)
self.m_bitmap8.SetBitmap(wx.NullBitmap)
self.Refresh()
if (self.m_choice3.GetStringSelection() == "Minimum Order Statistic Filter"):
Filter_obj = orderstatistic_filter(self.orig_image, "minimum")
output = Filter_obj.filtering()
elif (self.m_choice3.GetStringSelection() == "Maximum Order Statistic Filter"):
Filter_obj = orderstatistic_filter(self.orig_image, "maximum")
output = Filter_obj.filtering()
elif (self.m_choice3.GetStringSelection() == "Median Order Statistic Filter"):
Filter_obj = orderstatistic_filter(self.orig_image, "median")
output = Filter_obj.filtering()
elif (self.m_choice3.GetStringSelection() == "Trimmed Mean Order Statistic Filter"):
Filter_obj = orderstatistic_filter(self.orig_image, "mean")
output = Filter_obj.filtering()
else:
self.m_bitmap7.SetBitmap(wx.NullBitmap)
self.Refresh()
return
self.image = output
cv2.imwrite("test.png", output)
img = wx.Image("test.png", wx.BITMAP_TYPE_ANY).ConvertToGreyscale().ConvertToBitmap()
self.m_bitmap7.SetBitmap(img)
os.remove("test.png")
def post_process_image(self, image):
image = np.array(image)
minimumValue = np.amin(image)
maximumValue = np.amax(image)
P = (255/(maximumValue-minimumValue))
outputImage = image
cols, rows = image.shape
for j in range(rows-1):
for i in range(cols-1):
outputImage[i, j] = P*(image[i, j]-minimumValue)
return outputImage
def ask(parent=None, message='', default_value=''):
dlg = wx.TextEntryDialog(parent, message, default_value)
dlg.ShowModal()
result = dlg.GetValue()
dlg.Destroy()
return result
def onButtonMeanFilter(self, event):
self.m_bitmap9.SetBitmap(wx.NullBitmap)
self.m_bitmap8.SetBitmap(wx.NullBitmap)
self.Refresh()
if (self.m_choice2.GetStringSelection() == "Arithmetic Mean Filter"):
Filter_obj = mean_filter(self.orig_image, "arithmetic")
output = Filter_obj.filtering()
elif (self.m_choice2.GetStringSelection() == "Geometric Mean Filter"):
Filter_obj = mean_filter(self.orig_image, "geometric")
output = Filter_obj.filtering()
elif (self.m_choice2.GetStringSelection() == "Harmonic Mean Filter"):
Filter_obj = mean_filter(self.orig_image, "harmonic")
output = Filter_obj.filtering()
elif (self.m_choice2.GetStringSelection() == "Contraharmonic Mean Filter"):
order = self.ask(message = "Insert your order")
order = int(order)
Filter_obj = mean_filter(self.orig_image, "contraharmonic", order)
output = Filter_obj.filtering()
else:
self.m_bitmap7.SetBitmap(wx.NullBitmap)
self.Refresh()
return
self.image = output
cv2.imwrite("test.png", output)
img = wx.Image("test.png", wx.BITMAP_TYPE_ANY).ConvertToGreyscale().ConvertToBitmap()
self.m_bitmap7.SetBitmap(img)
os.remove("test.png")
def onButtonOpenFile(self, event):
f = wx.FileDialog(self, "Open image file", wildcard="Image files (*.png)|*.png", style=wx.FD_OPEN | wx.FD_FILE_MUST_EXIST)
if f.ShowModal() == wx.ID_CANCEL:
return
# Proceed loading the file chosen by the user
pathname = f.GetPath()
try:
with open(pathname, 'r') as file:
self.m_bitmap6.SetBitmap(wx.NullBitmap)
self.Refresh()
self.image = cv2.imread(pathname, cv2.IMREAD_GRAYSCALE)
self.orig_image = self.image
img = wx.Image(pathname, wx.BITMAP_TYPE_ANY).ConvertToGreyscale().ConvertToBitmap()
self.m_bitmap6.SetBitmap(img)
self.m_bitmap9.SetBitmap(wx.NullBitmap)
self.m_bitmap8.SetBitmap(wx.NullBitmap)