Overview
Pyramid processing functions implement multi-scale decomposition of video frames using Gaussian and Laplacian pyramids. These pyramids enable EVM to operate at different spatial frequencies, essential for isolating different vital signs.build_gaussian_pyramid
Builds a Gaussian pyramid by iteratively downsampling the input frame.Parameters
Input frame in BGR format. Should be a valid numpy array representing an image.
Number of pyramid levels to generate. Default from
config.LEVELS_RPI.- Level 0: Original resolution
- Level 1: 1/2 resolution
- Level 2: 1/4 resolution
- Level 3: 1/8 resolution
Returns
List of downsampled frames at different resolutions. Length is
levels + 1.build_laplacian_pyramid
Builds a Laplacian pyramid from a Gaussian pyramid by computing differences between levels.Parameters
Pre-built Gaussian pyramid from
build_gaussian_pyramid.Returns
List of Laplacian pyramid levels. Each level contains high-frequency spatial details.
Returns empty list if input is invalid.
Algorithm
For each leveli from 0 to n-2:
- Expand (upsample) level
i+1to match size of leveli - Compute difference:
Laplacian[i] = Gaussian[i] - expanded - The last level equals the last Gaussian level
collapse_laplacian_pyramid
Reconstructs the original image from a Laplacian pyramid.Parameters
Laplacian pyramid to reconstruct.
Returns
Reconstructed image at original resolution. Returns empty array if pyramid is invalid.
Algorithm
Iterates from the coarsest level to finest:- Start with the last (coarsest) level
- For each level from
n-2to 0:- Expand current result to match level size
- Add Laplacian details:
result = expanded + Laplacian[i]
build_video_pyramid_stack
Builds Laplacian pyramids for all frames in a video sequence.Parameters
List of video frames in BGR format.
Number of pyramid levels to build for each frame.
Returns
List of Laplacian pyramids, one per frame. Each pyramid is itself a list of pyramid levels.Structure:
pyramids[frame_idx][pyramid_level]Usage Example
extract_pyramid_level
Extracts a specific pyramid level from all frames and normalizes dimensions.Parameters
Stack of Laplacian pyramids from
build_video_pyramid_stack.Pyramid level to extract (0 = finest, higher = coarser).
Returns
4D tensor with shape
(T, H, W, C) where:T= number of framesH= height at specified pyramid levelW= width at specified pyramid levelC= number of color channels (3 for BGR)
Dimension Normalization
The function handles frames with inconsistent sizes:- Collects all frames at the specified level
- Determines the most common shape
- Resizes frames that don’t match to the target shape
- Returns a uniform tensor
Complete Pipeline Example
Pyramid Visualization
Spatial Frequency Decomposition
Level 0
320x240Highest spatial frequencyFine details
Level 1
160x120High-mid frequencyMedium details
Level 2
80x60Mid-low frequencyRespiratory rate
Level 3
40x30Lowest frequencyHeart rate
Error Handling
All functions include comprehensive error handling:- Invalid Input: Returns empty structures for None or invalid inputs
- Pyramid Building Failures: Returns single-level pyramids containing original frame
- Dimension Mismatches: Automatically resizes to most common shape
- Exception Logging: Prints diagnostic messages for debugging
Performance Considerations
Memory Usage
Pyramids use additional memory:- Original frame: 320x240x3 = 230KB (float32)
- Level 1: 160x120x3 = 58KB
- Level 2: 80x60x3 = 14KB
- Level 3: 40x30x3 = 3.6KB
- Total per frame: ~305KB
- For 200 frames: ~61MB
Processing Time
Benchmark on Raspberry Pi 4:- Gaussian pyramid: ~5-8ms per frame
- Laplacian pyramid: ~3-5ms per frame
- Total stack (200 frames): ~1.5-2.5 seconds
Related Functions
- EVMProcessor.process_dual_band - Uses pyramid processing
- apply_temporal_bandpass - Filters pyramid tensors