From ed8a2fe316e29fdd10edb2ef0bb8498c18c86219 Mon Sep 17 00:00:00 2001 From: yxdragon Date: Thu, 14 Mar 2019 09:11:08 +0800 Subject: [PATCH] fft --- imagepy/core/engine/filter.py | 4 +- imagepy/core/wraper/imageplus.py | 1 + imagepy/menus/Image/Type/convert_plg.py | 2 +- imagepy/menus/Process/FFT/__init__.py | 0 imagepy/menus/Process/FFT/fft_plgs.py | 73 +++++++++++++++++++ .../menus/Process/Hydrology/hydrology_plgs.py | 6 +- imagepy/menus/Process/__init__.py | 2 +- imagepy/ui/canvas.py | 2 +- 8 files changed, 82 insertions(+), 8 deletions(-) create mode 100644 imagepy/menus/Process/FFT/__init__.py create mode 100644 imagepy/menus/Process/FFT/fft_plgs.py diff --git a/imagepy/core/engine/filter.py b/imagepy/core/engine/filter.py index 023ded6b..2d1beec5 100644 --- a/imagepy/core/engine/filter.py +++ b/imagepy/core/engine/filter.py @@ -30,7 +30,7 @@ def process_one(plg, ips, src, img, para, callafter=None): TaskManager.add(plg) start = time() transint = '2int' in plg.note and ips.dtype in (np.uint8, np.uint16) - transfloat = '2float' in plg.note and not ips.dtype in (np.float32, np.float64) + transfloat = '2float' in plg.note and not ips.dtype in (np.complex128, np.float32, np.float64) if transint: buf = img.astype(np.int32) src = src.astype(np.int32) @@ -53,7 +53,7 @@ def process_stack(plg, ips, src, imgs, para, callafter=None): TaskManager.add(plg) start = time() transint = '2int' in plg.note and ips.dtype in (np.uint8, np.uint16) - transfloat = '2float' in plg.note and not ips.dtype in (np.float32, np.float64) + transfloat = '2float' in plg.note and not ips.dtype in (np.complex128, np.float32, np.float64) if transint: buf = imgs[0].astype(np.int32) src = src.astype(np.int32) diff --git a/imagepy/core/wraper/imageplus.py b/imagepy/core/wraper/imageplus.py index 338a1c03..e930c651 100644 --- a/imagepy/core/wraper/imageplus.py +++ b/imagepy/core/wraper/imageplus.py @@ -8,6 +8,7 @@ def get_img_type(imgs): if imgs[0].dtype == np.int32:return '32-int' if imgs[0].dtype == np.float32:return '32-float' if imgs[0].dtype == np.float64:return '64-float' + if imgs[0].dtype == np.complex128:return 'complex' class ImagePlus: """ImagePlus: a class to make operation more flexible """ diff --git a/imagepy/menus/Image/Type/convert_plg.py b/imagepy/menus/Image/Type/convert_plg.py index f9469414..15393dbd 100644 --- a/imagepy/menus/Image/Type/convert_plg.py +++ b/imagepy/menus/Image/Type/convert_plg.py @@ -163,5 +163,5 @@ def run(self, ips, imgs, para = None): k = 255.0/(max(1e-10, maxv-minv)) img64.append(imgs[i].astype(np.float64)) ips.set_imgs(img64) - + plgs = [To8bit, ToRGB, '-', ToUint16, ToInt32, ToFloat32, ToFloat64] \ No newline at end of file diff --git a/imagepy/menus/Process/FFT/__init__.py b/imagepy/menus/Process/FFT/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/imagepy/menus/Process/FFT/fft_plgs.py b/imagepy/menus/Process/FFT/fft_plgs.py new file mode 100644 index 00000000..a1a7c09b --- /dev/null +++ b/imagepy/menus/Process/FFT/fft_plgs.py @@ -0,0 +1,73 @@ +import numpy as np +from imagepy.core.engine import Simple, Filter +from numpy.fft import fft2, ifft2, fftshift, ifftshift +from imagepy import IPy + +class FFT(Simple): + title = 'FFT' + note = ['8-bit', '16-bit', 'int', 'float'] + para = {'shift':True, 'slice':False} + view = [(bool, 'shift', 'zero center'), + (bool, 'slice', 'slices')] + + def run(self, ips, imgs, para = None): + if not para['slice']: imgs = [ips.img] + shift = fftshift if para['shift'] else lambda x:x + rst = [] + for i in range(len(imgs)): + rst.append(shift(fft2(imgs[i]))) + self.progress(i, len(imgs)) + IPy.show_img(rst, '%s-fft'%ips.title) + +class LogPower(Simple): + title = 'Log Power' + note = ['complex'] + para = {'slice':False, 'type':'float', 'log':2.718} + view = [(float, 'log', (2,30), 3, 'log', ''), + (list, 'type', ['uint8', 'int', 'float'], str, 'type', ''), + (bool, 'slice', 'slices')] + + def run(self, ips, imgs, para = None): + if not para['slice']: imgs = [ips.img] + tp = {'uint8':np.uint8, 'int':np.int32, 'float':np.float32} + rst, tp = [], tp[para['type']] + for i in range(len(imgs)): + zs = np.log(np.abs(imgs[i])) + zs /= np.log(para['log']) + rst.append(zs.astype(tp)) + self.progress(i, len(imgs)) + IPy.show_img(rst, '%s-fft'%ips.title) + +class IFFT(Simple): + title = 'Inverse FFT' + note = ['complex'] + para = {'shift':True, 'slice':False, 'type':'float'} + view = [(list, 'type', ['uint8', 'int', 'float'], str, 'type', ''), + (bool, 'shift', 'zero center'), + (bool, 'slice', 'slices')] + + def run(self, ips, imgs, para = None): + if not para['slice']: imgs = [ips.img] + shift = ifftshift if para['shift'] else lambda x:x + tp = {'uint8':np.uint8, 'int':np.int32, 'float':np.float32} + rst, tp = [], tp[para['type']] + for i in range(len(imgs)): + rst.append(ifft2(shift(ips.img)).astype(tp)) + self.progress(i, len(imgs)) + IPy.show_img(rst, '%s-ifft'%ips.title) + +class Shift(Filter): + title = 'Zero Center' + note = ['complex'] + + def run(self, ips, snap, img, para = None): + return fftshift(img) + +class IShift(Filter): + title = 'Zero Edge' + note = ['complex'] + + def run(self, ips, snap, img, para = None): + return ifftshift(img) + +plgs = [FFT, IFFT, '-', Shift, IShift, LogPower] \ No newline at end of file diff --git a/imagepy/menus/Process/Hydrology/hydrology_plgs.py b/imagepy/menus/Process/Hydrology/hydrology_plgs.py index 79d79115..19a362a2 100644 --- a/imagepy/menus/Process/Hydrology/hydrology_plgs.py +++ b/imagepy/menus/Process/Hydrology/hydrology_plgs.py @@ -99,11 +99,11 @@ def run(self, ips, snap, img, para = None): class ARidge(Filter): title = 'Active Ridge' - note = ['8-bit', 'not_slice', 'auto_snap', 'not_channel'] + note = ['8-bit', 'not_slice', 'auto_snap', 'not_channel', 'req_roi'] para = {'sigma':1.0, 'ud':True, 'type':'white line'} - view = [(float, (0,5), 1, 'sigma', 'sigma', 'pix'), - (list, 'type', ['white line', 'gray line', 'white line on ori'], str, 'output', ''), + view = [(float, 'sigma', (0,5), 1, 'sigma', 'pix'), + (list, 'type', ['white line', 'gray line', 'white line on ori'], str, 'output', ''), (bool, 'ud', 'ascend')] def run(self, ips, snap, img, para = None): diff --git a/imagepy/menus/Process/__init__.py b/imagepy/menus/Process/__init__.py index ce93e566..3abb3703 100644 --- a/imagepy/menus/Process/__init__.py +++ b/imagepy/menus/Process/__init__.py @@ -1 +1 @@ -catlog = ['Math', 'Binary', 'Filters', '-', 'Threshold', 'Hydrology', 'Features', 'Segment', 'repair_plg', '-', 'calculator_plg'] \ No newline at end of file +catlog = ['Math', 'Binary', 'Filters', 'FFT', '-', 'Threshold', 'Hydrology', 'Features', 'Segment', 'repair_plg', '-', 'calculator_plg'] \ No newline at end of file diff --git a/imagepy/ui/canvas.py b/imagepy/ui/canvas.py index 4ab3c1eb..f6b59ad7 100644 --- a/imagepy/ui/canvas.py +++ b/imagepy/ui/canvas.py @@ -180,8 +180,8 @@ def merge(self, img, back, M, O, mode, shape, win, lookup): if img.ndim == 2: rstarr = np.zeros(shape, dtype=img.dtype) my_transform(img, M, offset=O, output=rstarr, k=1, clip=False) + if rstarr.dtype == np.complex128: rstarr = np.abs(rstarr) rstarr = lookup(rstarr) - if img.ndim == 3: rstarr = np.zeros((win[3], win[2], 3), dtype=img.dtype) for i in range(3):