A Python tool for batch image alignment and local difference detection using SIFT features and perspective transformation.
This tool performs the following operations:
- Image Registration: Aligns multiple overlay images to a reference image using SIFT feature matching and homography estimation
- Perspective Transformation: Applies perspective transforms to warp overlay images to match the reference coordinate system
- Difference Detection: Identifies local differences between reference and aligned images using SIFT descriptor comparison
- Multi-layer TIFF Generation: Creates a multi-page TIFF file with reference image, difference highlights, and aligned overlays
- SIFT-based Feature Matching: Uses OpenCV's SIFT detector with FLANN matcher for robust feature correspondence
- RANSAC Homography: Robust homography estimation with outlier rejection
- Grid Sampling: Efficient difference detection via regular grid sampling
- Feathered Difference Masks: Gaussian blur-based edge feathering for smooth transitions
- Multi-layer Output: Saves results as multi-page TIFF with transparency support
- Visual Preview: Generates comparison plots with legends showing detected regions and differences
- Clone or download this repository
- Install dependencies:
pip install -r requirements.txtpython diff_map_aligner.py <path_to_reference_image> <path_to_overlays_folder><path_to_reference_image>: Path to the reference/base image (e.g.,base.jpg)<path_to_overlays_folder>: Path to folder containing overlay images to align (e.g.,overlays/)
python diff_map_aligner.py base.jpg overlays/This will:
- Read
base.jpgas the reference image - Process all supported images in the
overlays/folder - Generate
output.tifwith aligned layers - Display an interactive comparison plot
Edit the parameters in the main execution block:
batch_paste_with_preview_and_correct_tiff(
sys.argv[1],
sys.argv[2],
"output.tif",
sample_step=16, # Sampling interval in pixels (larger = faster but coarser)
diff_thresh=128 # SIFT descriptor distance threshold (larger = stricter)
)| Parameter | Default | Description |
|---|---|---|
sample_step |
16 | Pixel interval for grid sampling. Larger values are faster but less dense. |
diff_thresh |
128 | SIFT descriptor L2 distance threshold for detecting differences. Higher values are stricter. |
output_tif |
output.tif |
Output multi-page TIFF filename |
- JPEG (
.jpg,.jpeg) - PNG (
.png) - TIFF (
.tif,.tiff) - BMP (
.bmp)
- Page 1: Reference image (RGB)
- Page 2+: For each successfully matched overlay:
- Difference highlight layer (RGBA with feathered alpha mask)
- Aligned overlay layer (RGBA with content mask)
A matplotlib figure showing:
- Left panel: Reference image
- Right panel: Composite preview with:
- Colored borders indicating each overlay region
- Red dots marking detected local differences
- Legend: Region identification and difference point count
- Detect SIFT keypoints in reference and overlay images
- Use FLANN (Fast Library for Approximate Nearest Neighbors) to find descriptor matches
- Apply Lowe's ratio test (0.82 threshold) to filter ambiguous matches
- Use RANSAC to estimate perspective transformation matrix
- Require minimum 10 inliers for valid homography
- Warp overlay image to reference coordinate system
- Sample SIFT descriptors on a regular grid within the warped image
- Compare descriptors with reference image at same locations
- Flag points where L2 distance exceeds
diff_thresh - Create difference mask by drawing circles at flagged points
- Apply Gaussian blur for edge feathering
The code includes a union-find based connected component filter that can be enabled to remove isolated difference points and keep only the largest connected component.
- Memory: Loads full reference image into memory; processes overlays sequentially
- Speed: SIFT detection and matching typically dominate runtime
- Adjust
sample_stepto control difference detection resolution - Larger
sample_step= fewer sampled points = faster processing
- Adjust
- Verify file path is correct
- Ensure image format is supported
- Check file is not corrupted
- Overlay image may not have enough overlapping features with reference
- Verify images show similar content
- Try adjusting SIFT parameters in code
- Increase
diff_threshif detection is too strict - Verify images actually have differences
- Check
sample_stepisn't so large that sampling misses details
- opencv-python: Computer vision algorithms (SIFT, homography, image processing)
- numpy: Numerical computations and array operations
- matplotlib: Result visualization
- Pillow: Multi-page TIFF I/O and image format conversion