Skip to contents
library(aforoR)
library(ggplot2)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

About this tutorial

In otolith shape analysis, choosing the right mathematical descriptor is crucial for capturing the biological information relevant to your study. The aforoR package utilizes Wavelets and Curvature Scale Space (CSS), while standard Elliptic Fourier Descriptors (EFDs) are widely implemented elsewhere (e.g. Momocs). This vignette structurally and mathematically compares these three fundamental approaches, guiding researchers on when and how to apply them.

1. Technical Overview

Descriptor Domain Base Mathematical Core Invariance properties Software Best used for…
Elliptic Fourier (EFD) Frequency Harmonic Series (Periodic sum of ellipses) Translation, Rotation, Scale (Normalized) Momocs / Momocs2 General shape classification, morphospace analysis.
Wavelets Time-Frequency Multi-resolution convolution (Spline à trous) Standardized Scale, Position matching aforoR High-frequency margin details, stock discrimination, non-stationary signals.
Curvature Scale Space (CSS) Multi-scale Geometry Contour Gaussian smoothing Translation, Rotation, Uniform Scale aforoR Explicit morphological feature detection (indentations), partial matching.
# Load a sample otolith image for demonstration frameworks
img_path <- system.file("extdata", "otolith.jpg", package = "aforoR")
binary_img <- preprocess_image(img_path)
contour <- extract_contour(binary_img)

# Professional visualization theme used throughout
aforo_theme <- function() {
  theme_minimal() +
    theme(
      plot.title = element_text(size = 12, face = "bold", hjust = 0.5),
      axis.title = element_text(size = 10),
      axis.text = element_text(size = 8, color = "black"),
      panel.grid.minor = element_blank(),
      panel.grid.major = element_blank(),
      axis.line = element_line(color = "black", linewidth = 0.3),
      axis.ticks = element_line(color = "black", linewidth = 0.3)
    )
}

2. Elliptic Fourier Descriptors (EFDs)

EFDs decompose a closed and bounded contour into a sum of harmonically related ellipses. Given a parametric 2D contour (x(t),y(t))(x(t), y(t)) where tt is the perimeter arc length, the coordinate functions can be expanded in a Fourier series:

x(t)=a0+n=1N(ancos(2nπtT)+bnsin(2nπtT))x(t) = a_0 + \sum_{n=1}^{N} \left( a_n \cos\left(\frac{2n\pi t}{T}\right) + b_n \sin\left(\frac{2n\pi t}{T}\right) \right)y(t)=c0+n=1N(cncos(2nπtT)+dnsin(2nπtT))y(t) = c_0 + \sum_{n=1}^{N} \left( c_n \cos\left(\frac{2n\pi t}{T}\right) + d_n \sin\left(\frac{2n\pi t}{T}\right) \right)

Where an,bn,cn,dna_n, b_n, c_n, d_n are the Fourier coefficients for the nn-th harmonic, reconstructing the global footprint parametrically.

Harmonization and Normalization

To achieve size and rotational invariance, contours are typically normalized based on the first harmonic (n=1)(n=1), which dictates the main elliptical axis. Furthermore, determining the optimal number of harmonics NN is standardized using accumulated power (or Fourier energy geometry), selecting NN such that upwards of 99.99%99.99\% of the contour’s spatial power is mathematically explained, mitigating the threshold of the Nyquist aliasing constraints.

  • Low Harmonics (n=1-5): Define overall morphology (aspect ratio, large-scale structural curvature).
  • High Harmonics (n>15): Responsible for defining very fine, localized margin granularity.

3. Wavelet Transform

Traditional Fourier analysis fails to capture the spatial localization of shape irregularities (e.g. an abrupt bump or notch is mathematically smeared across all frequencies). The Discrete Wavelet Transform (DWT), however, operates efficiently in the time-frequency domain, circumventing the inherent non-stationarity limits of EFDs when describing otoliths with highly differentiated structural lobes or localized serrations.

The transform is modeled by projecting the 1D shape signal f(t)f(t) —often extracted as the polar boundary radius ρ(θ)\rho(\theta) or successive perimeter step lengths— into translated (shift parameter bb) and dilated (scale parameter aa) versions of an active, localized, oscillatory mother wavelet ψ(t)\psi(t):

W(a,b)=1af(t)ψ(tba)¯dtW(a,b) = \frac{1}{\sqrt{a}} \int f(t) \, \overline{\psi \left(\frac{t - b}{a}\right)} dt

The ‘à trous’ Algorithm Feature

The aforoR methodological core leverages the strictly stationary “à trous” algorithm interconnected with cubic B-splines. Decisively, this computational approach aggressively avoids decimation (downsampling) at higher multi-resolution frequency scales. It continuously propagates the projection vector length maintaining exactly 512 equidistant mathematical coordinates at all topological scales, guaranteeing strict homology between biologically diverse structures and allowing pixel-perfect comparative matching mappings between taxa.

  • High Scales (7-9): Exclusively reconstruct structural macro-shape geometric features and basic proportional bounds.
  • Mid Scales (4-6): Essential for discriminating distinct populations exploiting mid-frequency crenulations and species-specific prominent edge dynamics.
  • Low Scales (1-3): Associated directly with micro-texture variance and inherent, unavoidable biological signal noise.

4. Curvature Scale Space (CSS)

Unlike strict frequency mappings or wavelets combinations, Curvature Scale Space (CSS) intrinsically analyzes the structural bounds applying continuous topological geometry matching mechanisms. The core processing engine applies an incrementally inflating spectrum of Gaussian smoothing generic convolution kernels g(u,σ)g(u, \sigma) uniformly across the parameterized contour coordinates (x(u),y(u))(x(u), y(u)) to track the macro-evolution of shape dynamics mathematically.

Let the subsequent smoothed planar functions for a given scale severity σ\sigma be determined as: X(u,σ)=x(u)*g(u,σ)X(u, \sigma) = x(u) * g(u, \sigma)Y(u,σ)=y(u)*g(u,σ)Y(u, \sigma) = y(u) * g(u, \sigma)

The dynamic metric associated with the object boundaries, the instantaneous curvature κ\kappa, is then computed by continuously evaluating its inflection properties against zero-crossing limits:

κ(u,σ)=Xu(u,σ)Yuu(u,σ)Xuu(u,σ)Yu(u,σ)(Xu(u,σ)2+Yu(u,σ)2)3/2\kappa(u, \sigma) = \frac{X_u(u, \sigma) Y_{uu}(u, \sigma) - X_{uu}(u, \sigma) Y_u(u, \sigma)}{\left(X_u(u, \sigma)^2 + Y_u(u, \sigma)^2\right)^{3/2}}

Topological CSS Image Generation

Tracking the continuous κ=0\kappa=0 points analytically plotted against the standard parametric smoothing width σ\sigma generates the uniquely definitive layout known as the CSS Image tree.

High-arcing vertical peaks persisting across the mathematical CSS map visibly and explicitly denote the deepest, most resilient, and biologically robust indentations (e.g. surviving the excisura major indentation bounds or profound sub-rostral lobes). When directly predicting generic taxa differentiation, these specific structural apexes mathematically yield fully invariant morphological fingerprinting (Mokhtarian & Mackworth, 1986).

# CSS topological mapping execution directly embedded inside aforoR methodologies
css_contours_analysis <- calculate_css(contour)
plot(css_contours_analysis)

5. Pre-processing: Generalized Procrustes Analysis (GPA)

Before calculating some shape descriptors, it’s often critical to completely neutralize non-shape variations such as spatial translation, scaling (size), and arbitrary rotation across all your specimens. While descriptors like Wavelets handle standardized scaling internally, explicit spatial alignment guarantees strict pointwise homology, minimizing structural noise across the cohort.

The Generalized Procrustes Analysis (GPA) algorithm iteratively superimposes a set of shapes to find an optimal consensus shape, minimizing the sum of squared distances between corresponding points.

Within aforoR, you can apply this automatically using the process_images() orchestrator function by setting procrustes = TRUE. This will collect all valid contours from a folder, align them simultaneously using the internal Momocs::fgProcrustes engine, and only then proceed to calculate Fourier, Wavelets, and morphometrics.

# Example of batch processing with GPA alignment
# Download the dataset from Zenodo: https://zenodo.org/api/records/18443318/draft/files/images.zip/content
# Extract it and set the path below to your local folder.

folder_path <- "path/to/your/images/folder" # Replace with your actual path

process_images(
  folder = folder_path,
  procrustes = TRUE,    # Applies GPA across all contours in the folder
  wavelets = TRUE,
  ef = TRUE,
  testing = TRUE,       # Saves visual diagnostic plots 
  save = TRUE           # Exports aligned CSVs
)

Note: Even when shapes are mathematically scaled to match the consensus during GPA, process_images() safely computes fundamental physical morphometrics (Area, Perimeter, PCA-aligned Length/Width, Feret_Max/Feret_Min, and PCA_Angle) from the original raw contours to prevent the loss of real-world biological size metrics.

References

  • Kuhl, F.P. and Giardina, C.R. (1982). Elliptic Fourier features of a closed contour. Computer Graphics and Image Processing.
  • Mokhtarian, F. and Mackworth, A.K. (1986). Scale-based description and recognition of planar curves and two-dimensional shapes. IEEE.
  • Parisi-Baradad, V. et al. (2005). Otolith shape contour analysis using mathematical descriptors. Computers in Biology and Medicine.