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
|
# Copyright (c) DataLab Platform Developers, BSD 3-Clause license, see LICENSE file.
"""
Fourier analysis module
-----------------------
This module provides 2D Fourier transform utilities and frequency domain operations
for image processing.
Features include:
- 2D FFT/IFFT functions with optional shifting
- Spectral analysis (magnitude spectrum, phase spectrum, power spectral density)
- Frequency domain filtering and deconvolution
- Zero padding utilities for FFT operations
These tools support various frequency domain image processing operations
including filtering, spectral analysis, and deconvolution.
"""
from __future__ import annotations
import warnings
import numpy as np
import scipy.signal as sps
from sigima.tools.checks import check_2d_array, normalize_kernel
# pylint: disable=invalid-name # Allows short reference names like x, y, ...
@check_2d_array
def fft2d(z: np.ndarray, shift: bool = True) -> np.ndarray:
"""Compute FFT of complex array `z`
Args:
z: Input data
shift: Shift zero frequency to center (default: True)
Returns:
FFT of input data
"""
z1 = np.fft.fft2(z)
if shift:
z1 = np.fft.fftshift(z1)
return z1
@check_2d_array
def ifft2d(z: np.ndarray, shift: bool = True) -> np.ndarray:
"""Compute inverse FFT of complex array `z`
Args:
z: Input data
shift: Shift zero frequency to center (default: True)
Returns:
Inverse FFT of input data
"""
if shift:
z = np.fft.ifftshift(z)
z1 = np.fft.ifft2(z)
return z1
@check_2d_array
def magnitude_spectrum(z: np.ndarray, log_scale: bool = False) -> np.ndarray:
"""Compute magnitude spectrum of complex array `z`
Args:
z: Input data
log_scale: Use log scale (default: False)
Returns:
Magnitude spectrum of input data
"""
z1 = np.abs(fft2d(z))
if log_scale:
z1 = 20 * np.log10(z1.clip(1e-10))
return z1
@check_2d_array
def phase_spectrum(z: np.ndarray) -> np.ndarray:
"""Compute phase spectrum of complex array `z`
Args:
z: Input data
Returns:
Phase spectrum of input data (in degrees)
"""
return np.rad2deg(np.angle(fft2d(z)))
@check_2d_array
def psd(z: np.ndarray, log_scale: bool = False) -> np.ndarray:
"""Compute power spectral density of complex array `z`
Args:
z: Input data
log_scale: Use log scale (default: False)
Returns:
Power spectral density of input data
"""
z1 = np.abs(fft2d(z)) ** 2
if log_scale:
z1 = 10 * np.log10(z1.clip(1e-10))
return z1
@check_2d_array
def gaussian_freq_filter(
data: np.ndarray, f0: float = 0.1, sigma: float = 0.05
) -> np.ndarray:
"""
Apply a 2D Gaussian bandpass filter in the frequency domain to an image.
This function performs a 2D Fast Fourier Transform (FFT) on the input image,
applies a Gaussian filter centered at frequency `f0` with standard deviation `sigma`
(both expressed in cycles per pixel), and then transforms the result back to the
spatial domain.
Args:
data: Input image data.
f0: Center frequency of the Gaussian filter (cycles/pixel).
sigma: Standard deviation of the Gaussian filter (cycles/pixel).
Returns:
The filtered image.
"""
n, m = data.shape
fx = np.fft.fftshift(np.fft.fftfreq(m, d=1))
fy = np.fft.fftshift(np.fft.fftfreq(n, d=1))
fx_grid, fy_grid = np.meshgrid(fx, fy)
freq_radius = np.hypot(fx_grid, fy_grid)
# Create the 2D Gaussian bandpass filter
gaussian_filter = np.exp(-0.5 * ((freq_radius - f0) / sigma) ** 2)
# Apply FFT, filter in frequency domain, and inverse FFT
fft_data = fft2d(data, shift=True)
filtered_fft = fft_data * gaussian_filter
zout = ifft2d(filtered_fft, shift=True)
return zout.real
@check_2d_array(non_constant=True)
def convolve(
data: np.ndarray,
kernel: np.ndarray,
normalize_kernel_flag: bool = True,
) -> np.ndarray:
"""
Perform 2D convolution with a kernel using scipy.signal.convolve.
This function adds optional kernel normalization to the standard scipy convolution.
Args:
data: Input image (2D array).
kernel: Convolution kernel.
normalize_kernel_flag: If True, normalize kernel so that ``kernel.sum() == 1``
to preserve image brightness.
Returns:
Convolved image (same shape as input).
Raises:
ValueError: If kernel is empty or null.
"""
if kernel.size == 0 or not np.any(kernel):
raise ValueError("Convolution kernel cannot be null.")
# Optionally normalize the kernel
if normalize_kernel_flag:
kernel = normalize_kernel(kernel)
# Use scipy.signal.convolve with 'same' mode to preserve image size
return sps.convolve(data, kernel, mode="same", method="auto")
@check_2d_array(non_constant=True)
def deconvolve(
data: np.ndarray,
kernel: np.ndarray,
reg: float = 0.0,
boundary: str = "edge",
normalize_kernel_flag: bool = True,
) -> np.ndarray:
"""
Perform 2D FFT deconvolution with correct 'same' geometry (no shift).
The kernel (PSF) must be centered (impulse at center for identity kernel).
Odd kernel sizes are recommended.
Args:
data: Input image (2D array).
kernel: Point Spread Function (PSF), centered.
reg: Regularization parameter (if >0, Wiener/Tikhonov inverse:
``H* / (|H|^2 + reg))``.
boundary: Padding mode ('edge' for constant plateau,
'reflect' for symmetric mirror).
normalize_kernel_flag: If True, normalize kernel so that ``kernel.sum() == 1``
to preserve image brightness.
Returns:
Deconvolved image (same shape as input).
Raises:
ValueError: If kernel is empty or null.
"""
if kernel.size == 0 or not np.any(kernel):
raise ValueError("Deconvolution kernel cannot be null.")
# Optionally normalize the kernel
if normalize_kernel_flag:
kernel = normalize_kernel(kernel)
H, W = data.shape
kh, kw = kernel.shape
if kh % 2 == 0 or kw % 2 == 0:
# Warning for even-sized kernels (off-by-one in centered FFT)
warnings.warn(
f"Deconvolution kernel has even dimension(s) ({kh}×{kw}); "
f"odd dimensions recommended for centered FFT."
)
# Symmetric padding for centered 'same' convolution
top = kh // 2
bottom = kh - 1 - top
left = kw // 2
right = kw - 1 - left
data_pad = np.pad(data, ((top, bottom), (left, right)), mode=boundary)
Hp, Wp = data_pad.shape # = H+kh-1, W+kw-1
# Centered PSF to OTF conversion (avoid off-by-one for even sizes)
kernel_pad = np.zeros_like(data_pad, dtype=float)
r0 = Hp // 2 - kh // 2
c0 = Wp // 2 - kw // 2
kernel_pad[r0 : r0 + kh, c0 : c0 + kw] = kernel
H_otf = np.fft.fft2(np.fft.ifftshift(kernel_pad)) # center → (0,0)
# FFT of padded image (no shift)
Z = np.fft.fft2(data_pad)
# Frequency domain inversion
if reg > 0.0:
Hc = np.conj(H_otf)
X = Z * Hc / (np.abs(H_otf) ** 2 + float(reg))
else:
eps = 1e-12
X = Z / (H_otf + eps)
data_true_pad = np.fft.ifft2(X).real
# Central crop to restore original geometry
out = data_true_pad[top : top + H, left : left + W]
return out
|