forked from sightmachine/SimpleCV
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDFT.py
More file actions
801 lines (644 loc) · 27.7 KB
/
Copy pathDFT.py
File metadata and controls
801 lines (644 loc) · 27.7 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
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
from SimpleCV.base import np, warnings
from SimpleCV.ImageClass import Image
class DFT:
"""
**SUMMARY**
The DFT class is the refactored class to crate DFT filters which can
be used to filter images by applying Digital Fourier Transform. This
is a factory class to create various DFT filters.
**PARAMETERS**
Any of the following parameters can be supplied to create
a simple DFT object.
* *width* - width of the filter
* *height* - height of the filter
* *channels* - number of channels of the filter
* *size* - size of the filter (width, height)
* *_numpy* - numpy array of the filter
* *_image* - SimpleCV.Image of the filter
* *_dia* - diameter of the filter
(applicable for gaussian, butterworth, notch)
* *_type* - Type of the filter
* *_order* - order of the butterworth filter
* *_freqpass* - frequency of the filter (lowpass, highpass, bandpass)
* *_xCutoffLow* - Lower horizontal cut off frequency for lowpassfilter
* *_yCutoffLow* - Lower vertical cut off frequency for lowpassfilter
* *_xCutoffHigh* - Upper horizontal cut off frequency for highpassfilter
* *_yCutoffHigh* - Upper vertical cut off frequency for highassfilter
**EXAMPLE**
>>> gauss = DFT.createGaussianFilter(dia=40, size=(512,512))
>>> dft = DFT()
>>> butterworth = dft.createButterworthFilter(dia=300, order=2, size=(300, 300))
"""
width = 0
height = 0
channels = 1
_numpy = None
_image = None
_dia = 0
_type = ""
_order = 0
_freqpass = ""
_xCutoffLow = 0
_yCutoffLow = 0
_xCutoffHigh = 0
_yCutoffHigh = 0
def __init__(self, **kwargs):
for key in kwargs:
if key == 'width':
self.width = kwargs[key]
elif key == 'height':
self.height = kwargs[key]
elif key == 'channels':
self.channels = kwargs[key]
elif key == 'size':
self.width, self.height = kwargs[key]
elif key == 'numpyarray':
self._numpy = kwargs[key]
elif key == 'image':
self._image = kwargs[key]
elif key == 'dia':
self._dia = kwargs[key]
elif key == 'type':
self._type = kwargs[key]
elif key == 'order':
self._order = kwargs[key]
elif key == 'frequency':
self._freqpass = kwargs[key]
elif key == 'xCutoffLow':
self._xCutoffLow = kwargs[key]
elif key == 'yCutoffLow':
self._yCutoffLow = kwargs[key]
elif key == 'xCutoffHigh':
self._xCutoffHigh = kwargs[key]
elif key == 'yCutoffHigh':
self._yCutoffHigh = kwargs[key]
def __repr__(self):
return "<SimpleCV.DFT Object: %s %s filter of size:(%d, %d) and channels: %d>" %(self._type, self._freqpass, self.width, self.height, self.channels)
def __add__(self, flt):
if not isinstance(flt, type(self)):
warnings.warn("Provide SimpleCV.DFT object")
return None
if self.size() != flt.size():
warnings.warn("Both SimpleCV.DFT object must have the same size")
return None
flt_numpy = self._numpy + flt._numpy
flt_image = Image(flt_numpy)
retVal = DFT(numpyarray=flt_numpy, image=flt_image, size=flt_image.size())
return retVal
def __invert__(self, flt):
return self.invert()
def _updateParams(self, flt):
self.channels = flt.channels
self._dia = flt._dia
self._type = flt._type
self._order = flt._order
self._freqpass = flt._freqpass
self._xCutoffLow = flt._xCutoffLow
self._yCutoffLow = flt._yCutoffLow
self._xCutoffHigh = flt._xCutoffHigh
self._yCutoffHigh = flt._yCutoffHigh
def invert(self):
"""
**SUMMARY**
Invert the filter. All values will be subtracted from 255.
**RETURNS**
Inverted Filter
**EXAMPLE**
>>> flt = DFT.createGaussianFilter()
>>> invertflt = flt.invert()
"""
flt = self._numpy
flt = 255 - flt
img = Image(flt)
invertedfilter = DFT(numpyarray=flt, image=img,
size=self.size(), type=self._type)
invertedfilter._updateParams(self)
return invertedfilter
@classmethod
def createGaussianFilter(self, dia=400, size=(64, 64), highpass=False):
"""
**SUMMARY**
Creates a gaussian filter of given size.
**PARAMETERS**
* *dia* - int - diameter of Gaussian filter
- list - provide a list of three diameters to create
a 3 channel filter
* *size* - size of the filter (width, height)
* *highpass*: - bool
True: highpass filter
False: lowpass filter
**RETURNS**
DFT filter.
**EXAMPLE**
>>> gauss = DFT.createGaussianfilter(200, (512, 512),
highpass=True)
>>> gauss = DFT.createGaussianfilter([100, 120, 140], (512, 512),
highpass=False)
>>> img = Image('lenna')
>>> gauss.applyFilter(img).show()
"""
if isinstance(dia, list):
if len(dia) != 3 and len(dia) != 1:
warnings.warn("diameter list must be of size 1 or 3")
return None
stackedfilter = DFT()
for d in dia:
stackedfilter = stackedfilter._stackFilters(self.createGaussianFilter(d, size, highpass))
image = Image(stackedfilter._numpy)
retVal = DFT(numpyarray=stackedfilter._numpy, image=image,
dia=dia, channels = len(dia), size=size,
type="Gaussian", frequency=stackedfilter._freqpass)
return retVal
freqpass = "lowpass"
sz_x, sz_y = size
x0 = sz_x/2
y0 = sz_y/2
X, Y = np.meshgrid(np.arange(sz_x), np.arange(sz_y))
D = np.sqrt((X-x0)**2+(Y-y0)**2)
flt = 255*np.exp(-0.5*(D/dia)**2)
if highpass:
flt = 255 - flt
freqpass = "highpass"
img = Image(flt)
retVal = DFT(size=size, numpyarray=flt, image=img, dia=dia,
type="Gaussian", frequency=freqpass)
return retVal
@classmethod
def createButterworthFilter(self, dia=400, size=(64, 64), order=2, highpass=False):
"""
**SUMMARY**
Creates a butterworth filter of given size and order.
**PARAMETERS**
* *dia* - int - diameter of Gaussian filter
- list - provide a list of three diameters to create
a 3 channel filter
* *size* - size of the filter (width, height)
* *order* - order of the filter
* *highpass*: - bool
True: highpass filter
False: lowpass filter
**RETURNS**
DFT filter.
**EXAMPLE**
>>> flt = DFT.createButterworthfilter(100, (512, 512), order=3,
highpass=True)
>>> flt = DFT.createButterworthfilter([100, 120, 140], (512, 512),
order=3, highpass=False)
>>> img = Image('lenna')
>>> flt.applyFilter(img).show()
"""
if isinstance(dia, list):
if len(dia) != 3 and len(dia) != 1:
warnings.warn("diameter list must be of size 1 or 3")
return None
stackedfilter = DFT()
for d in dia:
stackedfilter = stackedfilter._stackFilters(self.createButterworthFilter(d, size, order, highpass))
image = Image(stackedfilter._numpy)
retVal = DFT(numpyarray=stackedfilter._numpy, image=image,
dia=dia, channels = len(dia), size=size,
type=stackedfilter._type, order=order,
frequency=stackedfilter._freqpass)
return retVal
freqpass = "lowpass"
sz_x, sz_y = size
x0 = sz_x/2
y0 = sz_y/2
X, Y = np.meshgrid(np.arange(sz_x), np.arange(sz_y))
D = np.sqrt((X-x0)**2+(Y-y0)**2)
flt = 255/(1.0 + (D/dia)**(order*2))
if highpass:
frequency = "highpass"
flt = 255 - flt
img = Image(flt)
retVal = DFT(size=size, numpyarray=flt, image=img, dia=dia,
type="Butterworth", frequency=freqpass)
return retVal
@classmethod
def createLowpassFilter(self, xCutoff, yCutoff=None, size=(64, 64)):
"""
**SUMMARY**
Creates a lowpass filter of given size and order.
**PARAMETERS**
* *xCutoff* - int - horizontal cut off frequency
- list - provide a list of three cut off frequencies
to create a 3 channel filter
* *yCutoff* - int - vertical cut off frequency
- list - provide a list of three cut off frequencies
to create a 3 channel filter
* *size* - size of the filter (width, height)
**RETURNS**
DFT filter.
**EXAMPLE**
>>> flt = DFT.createLowpassFilter(xCutoff=75, size=(320, 280))
>>> flt = DFT.createLowpassFilter(xCutoff=[75], size=(320, 280))
>>> flt = DFT.createLowpassFilter(xCutoff=[75, 100, 120],
size=(320, 280))
>>> flt = DFT.createLowpassFilter(xCutoff=75, yCutoff=35,
size=(320, 280))
>>> flt = DFT.createLowpassFilter(xCutoff=[75], yCutoff=[35],
size=(320, 280))
>>> flt = DFT.createLowpassFilter(xCutoff=[75, 100, 125], yCutoff=35,
size=(320, 280))
>>> # yCutoff will be [35, 35, 35]
>>> flt = DFT.createLowpassFilter(xCutoff=[75, 113, 124],
yCutoff=[35, 45, 90],
size=(320, 280))
>>> img = Image('lenna')
>>> flt.applyFilter(img).show()
"""
if isinstance(xCutoff, list):
if len(xCutoff) != 3 and len(xCutoff) != 1:
warnings.warn("xCutoff list must be of size 3 or 1")
return None
if isinstance(yCutoff, list):
if len(yCutoff) != 3 and len(yCutoff) != 1:
warnings.warn("yCutoff list must be of size 3 or 1")
return None
if len(yCutoff) == 1:
yCutoff = [yCutoff[0]]*len(xCutoff)
else:
yCutoff = [yCutoff]*len(xCutoff)
stackedfilter = DFT()
for xfreq, yfreq in zip(xCutoff, yCutoff):
stackedfilter = stackedfilter._stackFilters(self.createLowpassFilter(xfreq, yfreq, size))
image = Image(stackedfilter._numpy)
retVal = DFT(numpyarray=stackedfilter._numpy, image=image,
xCutoffLow=xCutoff, yCutoffLow=yCutoff,
channels=len(xCutoff), size=size,
type=stackedfilter._type, order=self._order,
frequency=stackedfilter._freqpass)
return retVal
w, h = size
xCutoff = np.clip(int(xCutoff), 0, w/2)
if yCutoff is None:
yCutoff = xCutoff
yCutoff = np.clip(int(yCutoff), 0, h/2)
flt = np.zeros((w, h))
flt[0:xCutoff, 0:yCutoff] = 255
flt[0:xCutoff, h-yCutoff:h] = 255
flt[w-xCutoff:w, 0:yCutoff] = 255
flt[w-xCutoff:w, h-yCutoff:h] = 255
img = Image(flt)
lowpassFilter = DFT(size=size, numpyarray=flt, image=img,
type="Lowpass", xCutoffLow=xCutoff,
yCutoffLow=yCutoff, frequency="lowpass")
return lowpassFilter
@classmethod
def createHighpassFilter(self, xCutoff, yCutoff=None, size=(64, 64)):
"""
**SUMMARY**
Creates a highpass filter of given size and order.
**PARAMETERS**
* *xCutoff* - int - horizontal cut off frequency
- list - provide a list of three cut off frequencies
to create a 3 channel filter
* *yCutoff* - int - vertical cut off frequency
- list - provide a list of three cut off frequencies
to create a 3 channel filter
* *size* - size of the filter (width, height)
**RETURNS**
DFT filter.
**EXAMPLE**
>>> flt = DFT.createHighpassFilter(xCutoff=75, size=(320, 280))
>>> flt = DFT.createHighpassFilter(xCutoff=[75], size=(320, 280))
>>> flt = DFT.createHighpassFilter(xCutoff=[75, 100, 120],
size=(320, 280))
>>> flt = DFT.createHighpassFilter(xCutoff=75, yCutoff=35,
size=(320, 280))
>>> flt = DFT.createHighpassFilter(xCutoff=[75], yCutoff=[35],
size=(320, 280))
>>> flt = DFT.createHighpassFilter(xCutoff=[75, 100, 125], yCutoff=35,
size=(320, 280))
>>> # yCutoff will be [35, 35, 35]
>>> flt = DFT.createHighpassFilter(xCutoff=[75, 113, 124],
yCutoff=[35, 45, 90],
size=(320, 280))
>>> img = Image('lenna')
>>> flt.applyFilter(img).show()
"""
if isinstance(xCutoff, list):
if len(xCutoff) != 3 and len(xCutoff) != 1:
warnings.warn("xCutoff list must be of size 3 or 1")
return None
if isinstance(yCutoff, list):
if len(yCutoff) != 3 and len(yCutoff) != 1:
warnings.warn("yCutoff list must be of size 3 or 1")
return None
if len(yCutoff) == 1:
yCutoff = [yCutoff[0]]*len(xCutoff)
else:
yCutoff = [yCutoff]*len(xCutoff)
stackedfilter = DFT()
for xfreq, yfreq in zip(xCutoff, yCutoff):
stackedfilter = stackedfilter._stackFilters(
self.createHighpassFilter(xfreq, yfreq, size))
image = Image(stackedfilter._numpy)
retVal = DFT(numpyarray=stackedfilter._numpy, image=image,
xCutoffHigh=xCutoff, yCutoffHigh=yCutoff,
channels=len(xCutoff), size=size,
type=stackedfilter._type, order=self._order,
frequency=stackedfilter._freqpass)
return retVal
lowpass = self.createLowpassFilter(xCutoff, yCutoff, size)
w, h = lowpass.size()
flt = lowpass._numpy
flt = 255 - flt
img = Image(flt)
highpassFilter = DFT(size=size, numpyarray=flt, image=img,
type="Highpass", xCutoffHigh=xCutoff,
yCutoffHigh=yCutoff, frequency="highpass")
return highpassFilter
@classmethod
def createBandpassFilter(self, xCutoffLow, xCutoffHigh, yCutoffLow=None,
yCutoffHigh=None, size=(64, 64)):
"""
**SUMMARY**
Creates a banf filter of given size and order.
**PARAMETERS**
* *xCutoffLow* - int - horizontal lower cut off frequency
- list - provide a list of three cut off frequencies
* *xCutoffHigh* - int - horizontal higher cut off frequency
- list - provide a list of three cut off frequencies
* *yCutoffLow* - int - vertical lower cut off frequency
- list - provide a list of three cut off frequencies
* *yCutoffHigh* - int - verical higher cut off frequency
- list - provide a list of three cut off frequencies
to create a 3 channel filter
* *size* - size of the filter (width, height)
**RETURNS**
DFT filter.
**EXAMPLE**
>>> flt = DFT.createBandpassFilter(xCutoffLow=75,
xCutoffHigh=190, size=(320, 280))
>>> flt = DFT.createBandpassFilter(xCutoffLow=[75],
xCutoffHigh=[190], size=(320, 280))
>>> flt = DFT.createBandpassFilter(xCutoffLow=[75, 120, 132],
xCutoffHigh=[190, 210, 234],
size=(320, 280))
>>> flt = DFT.createBandpassFilter(xCutoffLow=75, xCutoffHigh=190,
yCutoffLow=60, yCutoffHigh=210,
size=(320, 280))
>>> flt = DFT.createBandpassFilter(xCutoffLow=[75], xCutoffHigh=[190],
yCutoffLow=[60], yCutoffHigh=[210],
size=(320, 280))
>>> flt = DFT.createBandpassFilter(xCutoffLow=[75, 120, 132],
xCutoffHigh=[190, 210, 234],
yCutoffLow=[70, 110, 112],
yCutoffHigh=[180, 220, 220],
size=(320, 280))
>>> img = Image('lenna')
>>> flt.applyFilter(img).show()
"""
lowpass = self.createLowpassFilter(xCutoffLow, yCutoffLow, size)
highpass = self.createHighpassFilter(xCutoffHigh, yCutoffHigh, size)
lowpassnumpy = lowpass._numpy
highpassnumpy = highpass._numpy
bandpassnumpy = lowpassnumpy + highpassnumpy
bandpassnumpy = np.clip(bandpassnumpy, 0, 255)
img = Image(bandpassnumpy)
bandpassFilter = DFT(size=size, image=img,
numpyarray=bandpassnumpy, type="bandpass",
xCutoffLow=xCutoffLow, yCutoffLow=yCutoffLow,
xCutoffHigh=xCutoffHigh, yCutoffHigh=yCutoffHigh,
frequency="bandpass", channels=lowpass.channels)
return bandpassFilter
@classmethod
def createNotchFilter(self, dia1, dia2=None, cen=None, size=(64, 64), type="lowpass"):
"""
**SUMMARY**
Creates a disk shaped notch filter of given diameter at given center.
**PARAMETERS**
* *dia1* - int - diameter of the disk shaped notch
- list - provide a list of three diameters to create
a 3 channel filter
* *dia2* - int - outer diameter of the disk shaped notch
used for bandpass filter
- list - provide a list of three diameters to create
a 3 channel filter
* *cen* - tuple (x, y) center of the disk shaped notch
if not provided, it will be at the center of the
filter
* *size* - size of the filter (width, height)
* *type*: - lowpass or highpass filter
**RETURNS**
DFT notch filter
**EXAMPLE**
>>> notch = DFT.createNotchFilter(dia1=200, cen=(200, 200),
size=(512, 512), type="highpass")
>>> notch = DFT.createNotchFilter(dia1=200, dia2=300, cen=(200, 200),
size=(512, 512))
>>> img = Image('lenna')
>>> notch.applyFilter(img).show()
"""
if isinstance(dia1, list):
if len(dia1) != 3 and len(dia1) != 1:
warnings.warn("diameter list must be of size 1 or 3")
return None
if isinstance(dia2, list):
if len(dia2) != 3 and len(dia2) != 1:
warnings.warn("diameter list must be of size 3 or 1")
return None
if len(dia2) == 1:
dia2 = [dia2[0]]*len(dia1)
else:
dia2 = [dia2]*len(dia1)
if isinstance(cen, list):
if len(cen) != 3 and len(cen) != 1:
warnings.warn("center list must be of size 3 or 1")
return None
if len(cen) == 1:
cen = [cen[0]]*len(dia1)
else:
cen = [cen]*len(dia1)
stackedfilter = DFT()
for d1, d2, c in zip(dia1, dia2, cen):
stackedfilter = stackedfilter._stackFilters(self.createNotchFilter(d1, d2, c, size, type))
image = Image(stackedfilter._numpy)
retVal = DFT(numpyarray=stackedfilter._numpy, image=image,
dia=dia1+dia2, channels = len(dia1), size=size,
type=stackedfilter._type,
frequency=stackedfilter._freqpass)
return retVal
w, h = size
if cen is None:
cen = (w/2, h/2)
a, b = cen
y, x = np.ogrid[-a:w-a, -b:h-b]
r = dia1/2
mask = x*x + y*y <= r*r
flt = np.ones((w, h))
flt[mask] = 255
if type == "highpass":
flt = 255-flt
if dia2 is not None:
a, b = cen
y, x = np.ogrid[-a:w-a, -b:h-b]
r = dia2/2
mask = x*x + y*y <= r*r
flt1 = np.ones((w, h))
flt1[mask] = 255
flt1 = 255 - flt1
flt = flt + flt1
np.clip(flt, 0, 255)
type = "bandpass"
img = Image(flt)
notchfilter = DFT(size=size, numpyarray=flt, image=img, dia=dia1,
type="Notch", frequency=type)
return notchfilter
def applyFilter(self, image, grayscale=False):
"""
**SUMMARY**
Apply the DFT filter to given image.
**PARAMETERS**
* *image* - SimpleCV.Image image
* *grayscale* - if this value is True we perfrom the operation on the
DFT of the gray version of the image and the result is
gray image. If grayscale is true we perform the
operation on each channel and the recombine them to
create the result.
**RETURNS**
Filtered Image.
**EXAMPLE**
>>> notch = DFT.createNotchFilter(dia1=200, cen=(200, 200),
size=(512, 512), type="highpass")
>>> img = Image('lenna')
>>> notch.applyFilter(img).show()
"""
if self.width == 0 or self.height == 0:
warnings.warn("Empty Filter. Returning the image.")
return image
w, h = image.size()
if grayscale:
image = image.toGray()
fltImg = self._image
if fltImg.size() != image.size():
fltImg = fltImg.resize(w, h)
filteredImage = image.applyDFTFilter(fltImg)
return filteredImage
def getImage(self):
"""
**SUMMARY**
Get the SimpleCV Image of the filter
**RETURNS**
Image of the filter.
**EXAMPLE**
>>> notch = DFT.createNotchFilter(dia1=200, cen=(200, 200),
size=(512, 512), type="highpass")
>>> notch.getImage().show()
"""
if isinstance(self._image, type(None)):
if isinstance(self._numpy, type(None)):
warnings.warn("Filter doesn't contain any image")
self._image = Image(self._numpy)
return self._image
def getNumpy(self):
"""
**SUMMARY**
Get the numpy array of the filter
**RETURNS**
numpy array of the filter.
**EXAMPLE**
>>> notch = DFT.createNotchFilter(dia1=200, cen=(200, 200),
size=(512, 512), type="highpass")
>>> notch.getNumpy()
"""
if isinstance(self._numpy, type(None)):
if isinstance(self._image, type(None)):
warnings.warn("Filter doesn't contain any image")
self._numpy = self._image.getNumpy()
return self._numpy
def getOrder(self):
"""
**SUMMARY**
Get order of the butterworth filter
**RETURNS**
order of the butterworth filter
**EXAMPLE**
>>> flt = DFT.createButterworthFilter(order=4)
>>> print flt.getOrder()
"""
return self._order
def size(self):
"""
**SUMMARY**
Get size of the filter
**RETURNS**
tuple of (width, height)
**EXAMPLE**
>>> flt = DFT.createGaussianFilter(size=(380, 240))
>>> print flt.size()
"""
return (self.width, self.height)
def getDia(self):
"""
**SUMMARY**
Get diameter of the filter
**RETURNS**
diameter of the filter
**EXAMPLE**
>>> flt = DFT.createGaussianFilter(dia=200, size=(380, 240))
>>> print flt.getDia()
"""
return self._dia
def getType(self):
"""
**SUMMARY**
Get type of the filter
**RETURNS**
type of the filter
**EXAMPLE**
>>> flt = DFT.createGaussianFilter(dia=200, size=(380, 240))
>>> print flt.getType() # Gaussian
"""
return self._type
def stackFilters(self, flt1, flt2):
"""
**SUMMARY**
Stack three signle channel filters of the same size to create
a 3 channel filter.
**PARAMETERS**
* *flt1* - second filter to be stacked
* *flt2* - thrid filter to be stacked
**RETURNS**
DFT filter
**EXAMPLE**
>>> flt1 = DFT.createGaussianFilter(dia=200, size=(380, 240))
>>> flt2 = DFT.createGaussianFilter(dia=100, size=(380, 240))
>>> flt2 = DFT.createGaussianFilter(dia=70, size=(380, 240))
>>> flt = flt1.stackFilters(flt2, flt3) # 3 channel filter
"""
if not(self.channels == 1 and flt1.channels == 1 and flt2.channels == 1):
warnings.warn("Filters must have only 1 channel")
return None
if not (self.size() == flt1.size() and self.size() == flt2.size()):
warnings.warn("All the filters must be of same size")
return None
numpyflt = self._numpy
numpyflt1 = flt1._numpy
numpyflt2 = flt2._numpy
flt = np.dstack((numpyflt, numpyflt1, numpyflt2))
img = Image(flt)
stackedfilter = DFT(size=self.size(), numpyarray=flt, image=img, channels=3)
return stackedfilter
def _stackFilters(self, flt1):
"""
**SUMMARY**
stack two filters of same size. channels don't matter.
**PARAMETERS**
* *flt1* - second filter to be stacked
**RETURNS**
DFT filter
"""
if isinstance(self._numpy, type(None)):
return flt1
if not self.size() == flt1.size():
warnings.warn("All the filters must be of same size")
return None
numpyflt = self._numpy
numpyflt1 = flt1._numpy
flt = np.dstack((numpyflt, numpyflt1))
stackedfilter = DFT(size=self.size(), numpyarray=flt,
channels=self.channels+flt1.channels,
type=self._type, frequency=self._freqpass)
return stackedfilter