Coverage for src/spectroflat/shift/sub_shift.py: 100%

16 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2024-03-28 07:59 +0000

1#!/usr/bin/env python3 

2# -*- coding: utf-8 -*- 

3""" 

4Util for sub pixel shifting 

5 

6@author: hoelken 

7""" 

8import numpy as np 

9from scipy import ndimage, signal 

10 

11 

12class Shifter: 

13 """ 

14 Helper class to shift images in subpixel amounts using FFT 

15 """ 

16 

17 @staticmethod 

18 def d1shift(img: np.array, offset: float) -> np.array: 

19 """ 

20 Creates a FFTd version of the image and shifts it by the offset amount. 

21 

22 ## Params 

23 - `ìmg`: An 1d array object representing the image row 

24 - `offset`: The amount the center is shifted 

25 """ 

26 window = signal.windows.kaiser(len(img), 2.5) 

27 ft_img = np.fft.fft(img * window) 

28 result = ndimage.fourier_shift(ft_img, shift=-offset) 

29 return np.fft.ifft(result).real 

30 

31 @staticmethod 

32 def d2shift(img, offset): 

33 """ 

34 Creates a FFTd version of the image and shifts it by the offset amount. 

35 

36 ## Params 

37 - `ìmg`: An array object representing the image 

38 - `offset`: The amount the center is shifted must be float or iterable. 

39 If float the same offset will be applied to all axes, if iterable an offset for each axis must be provided 

40 """ 

41 window2d = np.sqrt(np.outer(signal.windows.kaiser(img.shape[0], 2.5), 

42 signal.windows.kaiser(img.shape[1], 2.5))) 

43 ft_img = np.fft.fft2(img * window2d) 

44 result = ndimage.fourier_shift(ft_img, shift=-offset) 

45 return np.fft.ifft2(result).real