1+ import numpy as np
2+ from imagepy .core .engine import Simple , Filter
3+ from numpy .fft import fft2 , ifft2 , fftshift , ifftshift
4+ from imagepy import IPy
5+
6+ class FFT (Simple ):
7+ title = 'FFT'
8+ note = ['8-bit' , '16-bit' , 'int' , 'float' ]
9+ para = {'shift' :True , 'slice' :False }
10+ view = [(bool , 'shift' , 'zero center' ),
11+ (bool , 'slice' , 'slices' )]
12+
13+ def run (self , ips , imgs , para = None ):
14+ if not para ['slice' ]: imgs = [ips .img ]
15+ shift = fftshift if para ['shift' ] else lambda x :x
16+ rst = []
17+ for i in range (len (imgs )):
18+ rst .append (shift (fft2 (imgs [i ])))
19+ self .progress (i , len (imgs ))
20+ IPy .show_img (rst , '%s-fft' % ips .title )
21+
22+ class LogPower (Simple ):
23+ title = 'Log Power'
24+ note = ['complex' ]
25+ para = {'slice' :False , 'type' :'float' , 'log' :2.718 }
26+ view = [(float , 'log' , (2 ,30 ), 3 , 'log' , '' ),
27+ (list , 'type' , ['uint8' , 'int' , 'float' ], str , 'type' , '' ),
28+ (bool , 'slice' , 'slices' )]
29+
30+ def run (self , ips , imgs , para = None ):
31+ if not para ['slice' ]: imgs = [ips .img ]
32+ tp = {'uint8' :np .uint8 , 'int' :np .int32 , 'float' :np .float32 }
33+ rst , tp = [], tp [para ['type' ]]
34+ for i in range (len (imgs )):
35+ zs = np .log (np .abs (imgs [i ]))
36+ zs /= np .log (para ['log' ])
37+ rst .append (zs .astype (tp ))
38+ self .progress (i , len (imgs ))
39+ IPy .show_img (rst , '%s-fft' % ips .title )
40+
41+ class IFFT (Simple ):
42+ title = 'Inverse FFT'
43+ note = ['complex' ]
44+ para = {'shift' :True , 'slice' :False , 'type' :'float' }
45+ view = [(list , 'type' , ['uint8' , 'int' , 'float' ], str , 'type' , '' ),
46+ (bool , 'shift' , 'zero center' ),
47+ (bool , 'slice' , 'slices' )]
48+
49+ def run (self , ips , imgs , para = None ):
50+ if not para ['slice' ]: imgs = [ips .img ]
51+ shift = ifftshift if para ['shift' ] else lambda x :x
52+ tp = {'uint8' :np .uint8 , 'int' :np .int32 , 'float' :np .float32 }
53+ rst , tp = [], tp [para ['type' ]]
54+ for i in range (len (imgs )):
55+ rst .append (ifft2 (shift (ips .img )).astype (tp ))
56+ self .progress (i , len (imgs ))
57+ IPy .show_img (rst , '%s-ifft' % ips .title )
58+
59+ class Shift (Filter ):
60+ title = 'Zero Center'
61+ note = ['complex' ]
62+
63+ def run (self , ips , snap , img , para = None ):
64+ return fftshift (img )
65+
66+ class IShift (Filter ):
67+ title = 'Zero Edge'
68+ note = ['complex' ]
69+
70+ def run (self , ips , snap , img , para = None ):
71+ return ifftshift (img )
72+
73+ plgs = [FFT , IFFT , '-' , Shift , IShift , LogPower ]
0 commit comments