This script aligns multispectral images from slightly misaligned cameras using phase correlation.
- High-precision alignment: Uses phase correlation with 100x upsampling for sub-pixel accuracy
- Automatic cropping: Crops all images to the overlapping valid region
- Debug visualization: Color-coded overlay to verify alignment quality
- Flexible reference selection: Choose any band as the reference (default: band 1)
- Preserves data types: Maintains original bit depth (8-bit or 16-bit TIFF)
python align.py test_dataOutput will be saved to test_data/aligned_output/ by default.
python align.py test_data --reference 3python align.py test_data --debugpython align.py test_data --output my_aligned_imagespython align.py test_data --base-name IMG_0002python align.py test_data --align-thermalThis upsamples the thermal band to the reference resolution and uses Mutual Information (MI) for robust multi-modal translation alignment, then crops to the same valid region and saves to the output directory.
python align.py test_data --align-thermal --manual-thermal --thermal-range-min 28924 --thermal-range-max 30754- Arrow keys: move thermal (Up/Down/Left/Right)
- +/- keys: zoom in/out (small steps)
- Buttons: Up/Down/Left/Right/Zoom+/Zoom-/Confirm/Reset
- Enter: confirm and save
- The console prints the correction:
shift_y,shift_x, andscale - Only the thermal display is min–max rescaled (by the provided DN range) for visualization; saved files are unchanged in radiometry
python align.py test_data --align-thermal \
--thermal-shift-y -72.000 --thermal-shift-x -43.000 --thermal-scale 1.17258- Applies the provided translation and scale to the thermal band, saves the cropped, aligned thermal TIFF.
- Combine with
--debugand optional--thermal-range-min/maxto visually verify overlay. - Coordinate convention: positive
shift_ymoves thermal down; positiveshift_xmoves thermal right.
python align.py test_data --upsample 200 --interpolation lanczosHigher upsample values (up to 1000) give better sub-pixel precision but are slower.
For images with rotation, scale, or shear differences (affine distortions):
python align.py test_data --alignment-mode affineFor images with perspective/parallax distortions (e.g., from different camera angles):
python align.py test_data --alignment-mode homographyAlignment modes:
translation(default): Simple lateral shifts (fastest, works for most multispectral cameras)affine: Handles rotation, scale, shear, and translation (parallax from small angle differences)homography: Full perspective transform using feature matching (parallax from larger angle differences or lens distortions)
- Image loading: Loads all images (first 5 from each set) and normalizes them
- Transform estimation: Depending on alignment mode:
- Translation: Phase cross-correlation for sub-pixel lateral shifts
- Affine: ECC (Enhanced Correlation Coefficient) refinement for rotation, scale, shear + translation
- Homography: ORB feature detection and RANSAC matching for full perspective transforms
- Alignment: Applies computed transformations using high-quality interpolation
- Valid region: Calculates the overlapping valid region present in all aligned images
- Cropping: Crops all images to the common valid region
- Saving: Saves aligned images to output directory (default:
input_dir/aligned_output/)
The script uses several techniques for maximum precision:
-
Multiple Alignment Modes:
- Translation (default): Fourier-based phase correlation for sub-pixel lateral shifts
- Most accurate for simple camera offsets
- Fastest processing
- Works well with texture (like wheat fields)
- Affine: ECC-based registration handles rotation, scale, shear + translation
- Use when cameras have slight rotation or scale differences
- Handles parallax from small viewing angle differences
- Good for drone imagery with minor pitch/roll variations
- Homography: Feature-based perspective transform
- Handles full parallax distortions and lens effects
- Uses ORB features with RANSAC for robustness
- Best for images from significantly different viewpoints
- Translation (default): Fourier-based phase correlation for sub-pixel lateral shifts
-
Sub-pixel Upsampling (for translation mode): Default factor of 100 means 0.01 pixel precision
- Use
--upsample 200for 0.005 pixel precision - Use
--upsample 500or1000for ultimate precision (slower)
- Use
-
High-Quality Interpolation:
--interpolation cubic(default): Good balance of speed and quality--interpolation lanczos: Highest quality, slightly slower--interpolation linear: Fastest, lower quality
-
Tips for best results:
- Choose the right alignment mode:
- Start with
translation(default) for most multispectral camera systems - Use
affineif you see rotation or scale misalignment - Use
homographyfor complex parallax (different mounting angles, lens distortions)
- Start with
- Use the band with the most texture/contrast as reference
- Ensure images are properly exposed (not over/under-exposed)
- For wheat, green or red bands typically work best as reference
- The script preserves full precision throughout (float32 operations)
- Choose the right alignment mode:
When --debug is enabled, the script displays:
- Color overlay: Each band is assigned a different color (Red, Green, Blue, Cyan, Magenta, Yellow)
- Perfect alignment = uniform colors without color fringing
- Misalignment = visible color edges
- Individual bands: Grid view of all aligned bands
- Aligned images are saved to
aligned_output/subdirectory by default - Files keep their original names for easy tracking
- All images are cropped to the same size to ensure perfect pixel-to-pixel alignment
- Original bit depth (8-bit or 16-bit) is preserved
- Python 3.7+
- numpy
- opencv-python
- scikit-image
- matplotlib
- SimpleITK
Install with:
pip install -r requirements.txt