Multifractal Spectrum Analysis - CFAImageMFS

Box-counting multifractal analysis for 2D grayscale images with fixed-ROI normalization

Application Scenarios

Usage Examples

Basic Usage

import cv2, numpy as np from FreeAeonFractal.FAImageMFS import CFAImageMFS rgb_image = cv2.imread('./images/face.png') gray_image = cv2.cvtColor(rgb_image, cv2.COLOR_BGR2GRAY) MFS = CFAImageMFS(gray_image, q_list=np.linspace(-5, 5, 51)) df_mass, df_fit, df_spec = MFS.get_mfs() print(df_spec[['q', 'alpha', 'f_alpha']].head(10)) MFS.plot(df_mass, df_fit, df_spec)

GPU Accelerated Version

from FreeAeonFractal.FAImageMFSGPU import CFAImageMFSGPU as CFAImageMFS MFS = CFAImageMFS(gray_image, q_list=np.linspace(-5, 5, 51)) df_mass, df_fit, df_spec = MFS.get_mfs() MFS.plot(df_mass, df_fit, df_spec)

Local Alpha Map (per-pixel Hölder exponent)

MFS = CFAImageMFS(gray_image) alpha_map, info = MFS.compute_alpha_map(scales=None, roi_mode="center", empty_policy="nan") CFAImageMFS.plot_alpha_map(alpha_map)

Batch Processing

import glob, cv2, numpy as np from FreeAeonFractal.FAImageMFS import CFAImageMFS images = [cv2.imread(f, cv2.IMREAD_GRAYSCALE) for f in glob.glob('./images/*.png')] results = CFAImageMFS.get_batch_mfs(images, q_list=np.linspace(-5, 5, 26)) for df_mass, df_fit, df_spec in results: print(df_fit[['q', 'tau', 'Dq']].head())

Class Description

CFAImageMFS

Box-counting multifractal analysis on a 2D grayscale image. Uses fixed square ROI (Scheme A) for consistent ε normalization across scales.

Initialization Parameters

ParameterTypeDefaultDescription
imagenumpy.ndarrayRequiredInput 2D grayscale image
corp_typeint-1Crop type (-1:crop, 0:strict, 1:pad)
q_listarray-likelinspace(-5,5,51)q moment values
with_progressboolTrueShow progress bar
bg_thresholdfloat0.01Background threshold (post-normalization)
bg_reverseboolFalseZero pixels above threshold instead
bg_otsuboolFalseApply Otsu thresholding before normalization
mu_floorfloat1e-12API compatibility only; not used

get_mass_table(max_size, max_scales=80, min_box=2, roi_mode="center")

Compute per-scale partition function table. Returns DataFrame with columns scale, eps, q, value, kind.

fit_tau_and_D1(df_mass, min_points=6, ...)

Fit τ(q) and D(q) via linear regression. Key options: use_middle_scales, fit_scale_frac, if_auto_line_fit, cap_d0_at_2. Returns DataFrame with q, tau, Dq, D1, intercept, r_value, p_value, std_err, n_points.

alpha_falpha_from_tau(df_fit, spline_k=3, exclude_q1=True, spline_s=0)

Compute α(q) and f(α) via spline derivative of τ(q) (Legendre transform). Returns DataFrame with q, tau, Dq, alpha, f_alpha.

get_mfs(max_size, max_scales=80, min_points=6, ...)

Full pipeline: get_mass_table → fit_tau_and_D1 → alpha_falpha_from_tau. Returns (df_mass, df_fit, df_spec).

compute_alpha_map(scales=None, roi_mode="center", empty_policy="nan")

Per-pixel Hölder exponent via streaming OLS with nested-grid optimization. Returns (alpha_map, info).

compute_alpha_map_batch(images, ...) [static]

Batch version using nested-grid streaming OLS — low memory, no full mu_stack materialization.

plot(df_mass, df_fit, df_spec)

2×3 subplots: log M(q,ε) heatmap, f(α) vs α, τ(q), D(q), α(q), f(α) vs q.

plot_alpha_map(alpha_map) [static]

Visualize local α-map using jet colormap.

get_batch_mfs(img_list, ...) [static]

Batch CPU MFS; API-compatible with CFAImageMFSGPU.get_batch_mfs. Returns list of (df_mass, df_fit, df_spec).

Theoretical Background

Core Computation

μᵢ(ε) = mass_i / total_mass # box probability M(q, ε) = Σᵢ μᵢ^q # partition function τ(q) = lim(ε→0) log M(q,ε) / log ε # mass exponent D(q) = τ(q) / (q-1), q≠1 # generalized dimension D(1) = lim(ε→0) Σᵢ μᵢ log μᵢ / log ε # information dimension α(q) = dτ(q)/dq # singularity strength f(α) = q·α - τ(q) # multifractal spectrum

Special Dimensions

Important Notes

  1. Preprocessing: Image auto-normalized to [0,1]; use bg_threshold or bg_otsu=True for background removal
  2. q Range: Negative q → sparse regions; positive q → dense regions; recommend −5 to 5
  3. Results: Wider f(α) curve → stronger multifractality; Δα = α_max − α_min quantifies heterogeneity
  4. Performance: Use CFAImageMFSGPU for 5–20× speedup

References