psychos.visual.synthetic.gabor_2d#
- psychos.visual.synthetic.gabor_2d(size: Tuple[int, int] = (128, 128), spatial_frequency: float = 5.0, orientation: float = 45.0, phase: float = 0.0, sigma: float | None = 0.25, gamma: float = 1.0, contrast: float = 1.0, baseline: float = 0.0, center: Tuple[float, float] | None = None, rescale: Literal['none', 'normalize', 'clip'] | None = None, vmin: float = 0.0, vmax: float = 1.0, return_envelope: bool = False) ndarray | Tuple[ndarray, ndarray][source]#
Generate a 2D Gabor patch as a numpy array (grayscale), with an optional Gaussian envelope.
- Parameters:
size (Tuple[int, int]) – (height, width) of the output array in pixels.
spatial_frequency (float) – Cycles per entire image width. For orientation=0, this yields exactly spatial_frequency cycles from left to right.
orientation (float) – Gabor orientation in degrees, [0..360]. Note: The function rotates the carrier by (360 - orientation) degrees.
phase (float) – Fraction of 2*pi for the carrier phase, in [0..1].
sigma (Optional[float]) – Standard deviation of the Gaussian envelope, as a fraction of the minimum dimension. If None, no envelope is applied (i.e., the envelope is 1 everywhere).
gamma (float) – Aspect ratio of the Gaussian envelope (width vs. height). If 1.0, the envelope is circular; values < 1 or > 1 yield an elliptical envelope.
contrast (float) – Contrast (amplitude) of the sinusoidal carrier.
baseline (float) – Baseline offset intensity added to the Gabor.
center (Optional[Tuple[float, float]]) – (y_center, x_center) specifying the center in pixel coordinates. If None, defaults to the image center ((h/2), (w/2)).
rescale (Literal["none", "normalize", "clip"]) –
- Rescaling method:
’none’: return the raw computed values.
’normalize’: linearly scale the array so that its min maps to vmin and max to vmax.
’clip’: clip values to the range [vmin, vmax].
vmin (float) – Minimum value for normalization or clipping. Ignored if rescale==’none’.
vmax (float) – Maximum value for normalization or clipping. Ignored if rescale==’none’.
return_envelope (bool) – If True, return a tuple (gabor, envelope), where envelope is the Gaussian envelope used (useful for an alpha channel). If sigma is None, envelope is an array of ones.
- Returns:
If return_envelope is False, returns a 2D array containing the Gabor pattern. Otherwise, returns a tuple: (gabor, envelope).
- Return type:
Union[np.ndarray, Tuple[np.ndarray, np.ndarray]]
Example
>>> import matplotlib.pyplot as plt >>> gabor, env = gabor_2d(size=(256, 256), spatial_frequency=2.0, orientation=45.0, ... phase=0.25, sigma=0.25, gamma=1.0, contrast=1.0, ... baseline=0.0, rescale='normalize', vmin=-1.0, vmax=1.0, ... return_envelope=True) >>> plt.figure(figsize=(10, 5)) >>> plt.subplot(1, 2, 1) >>> plt.imshow(gabor, cmap='gray', origin='lower') >>> plt.title("Gabor Patch") >>> plt.subplot(1, 2, 2) >>> plt.imshow(env, cmap='gray', origin='lower') >>> plt.title("Envelope (Alpha Channel)") >>> plt.show()