visfeats.spatial_frequency_features#

visfeats.spatial_frequency_features(image: Image, size: int = 1024, min_frequency: float = 10.0, max_frequency: float = 256.0, log_bins: int = 17, pad: bool = True, return_spectrum: Literal[False] = False) → Dict[str, float]#
visfeats.spatial_frequency_features(image: Image, size: int = 1024, min_frequency: float = 10.0, max_frequency: float = 256.0, log_bins: int = 17, pad: bool = True, *, return_spectrum: Literal[True]) → Tuple[Dict[str, float], NDArray[float64]]

Calculate features related to the spatial frequency content of an image.

Parameters:
imageImage

Input image.

sizeint, default=1024

Square analysis resolution.

min_frequencyfloat, default=10.0

Lowest radial spatial frequency to analyze, in cycles/image.

max_frequencyfloat, default=256.0

Highest radial spatial frequency to analyze, in cycles/image.

log_binsint, default=17

Number of log-spaced frequency bins used before fitting. Redies et al. [2] used 17 bins (Figure 3a).

padbool, default=True

Whether to pad non-square images to a square with the mean gray value before resizing, as in Redies et al. [1]. If False, the image is resized directly to size x size, as in the original method [2], which distorts the aspect ratio of non-square images.

return_spectrumbool, default=False

If True, also return the radially averaged power spectrum (see Returns).

Returns:
featuresdict of {str: float}

A mapping of feature names to values:

Key

Description

fourier_slope

Slope of log power versus log frequency (about -2 for natural scenes and many artworks).

fourier_deviation

Mean squared residual (log10 units) of the binned data around the fitted line.

dimensionality

Slope of log magnitude versus log rank of the ranked spectrum (see Notes).

spectral_centroid

Power-weighted mean spatial frequency, in cycles/image.

spectral_variance

Circular variance of the power spectrum over orientation, in [0, 1] (see Notes).

spectrumndarray, shape (n_frequencies, 2)

Only returned when return_spectrum=True, as the second element of a (features, spectrum) tuple. This is the radially averaged spectrum before log-binning, restricted to frequencies between min_frequency and max_frequency with non-zero power. Axes are:

  • axis 0: radial frequency bin, in ascending order (one bin per cycle/image);

  • axis 1: column 0 = mean radial frequency of the Fourier components in the bin (cycles/image), column 1 = mean power in the bin (squared amplitude, arbitrary units).

Both columns are linear (not log-transformed). For amplitude instead of power, use np.sqrt(spectrum[:, 1]).

Notes

The function follows these steps:

  1. Convert image to grayscale, optionally (pad) pad to a square with the mean gray value, and resize to a size x size square by bicubic interpolation [1,2]_.

  2. Compute the 2D Fourier transform and average it in annular bins of 1 cycle/image up to the Nyquist frequency, giving the mean radial frequency, mean power and mean magnitude of each annulus.

  3. fourier_slope and fourier_deviation: keep bins between min_frequency and max_frequency, average log power and log frequency within log_bins log-spaced bins, and fit a least-squares line. The slope is fourier_slope; the mean squared residual is fourier_deviation [2].

  4. dimensionality: using the radially averaged magnitude of all bins except DC (no frequency restriction), rank the components from largest to smallest and fit a line to log magnitude versus log rank [3].

  5. spectral_centroid: using the full 2D power spectrum without DC, the mean radial frequency weighted by power (center of mass).

  6. spectral_variance: circular variance, 1 - R, of the spectral orientation weighted by power, where R is the length of the power-weighted mean resultant vector of the doubled orientation angles (the spectrum is point-symmetric). 0 means all power lies along one orientation; 1 means power is spread evenly across orientations.

[1]

Redies, C., Grebenkina, M., Mohseni, M., Kaduhm, A., & Dobel, C. (2020). Global Image Properties Predict Ratings of Affective Pictures. Frontiers in Psychology, 11. https://doi.org/10.3389/fpsyg.2020.00953

[2] (1,2,3)

Redies, C., Hasenstein, J., & Denzler, J. (2007). Fractal-like image statistics in visual art: Similarity to natural scenes. Spatial Vision, 21(1–2), 137–148. https://doi.org/10.1163/156856807782753921

[3]

Uran, C., Peter, A., Lazar, A., Barnes, W., Klon-Lipok, J., Shapcott, K. A., Roese, R., Fries, P., Singer, W., & Vinck, M. (2022). Predictive coding of natural images by V1 firing rates and rhythmic synchronization. Neuron, 110(7), 1240-1257.e8. https://doi.org/10.1016/j.neuron.2022.01.002