Overview
This example adapts the basic blending techniques to create panoramas. The key differences from simple blending are:- Positioning images side-by-side with appropriate overlap
- Using masks that favor the center of each image
- Handling multiple images in sequence
Two-Image Panorama
#include "blending.h"
#include "utils.h"
void create_panorama() {
// Load left and right images
Image left_img = create_image("left.jpeg");
Image right_img = create_image("right.jpeg");
// Images should have similar heights and overlapping content
// Typical overlap: 20-40% for good results
// Create masks with larger transition zones for panoramas
float overlap_ratio = 0.3f; // 30% overlap
// Left image: fade from full to transparent on the right side
Image mask_left = create_image_mask(
left_img.width,
left_img.height,
overlap_ratio,
0, // Left side: opaque
1 // Right side: fade to transparent
);
// Right image: fade from transparent to full on the right side
Image mask_right = create_image_mask(
right_img.width,
right_img.height,
overlap_ratio,
1, // Left side: fade from transparent
0 // Right side: opaque
);
// Calculate the overlap in pixels
int overlap_pixels = (int)(left_img.width * overlap_ratio);
// Total width = left width + right width - 2 * overlap
int pano_width = left_img.width + right_img.width - (2 * overlap_pixels);
// Assume images have the same height
int pano_height = left_img.height;
StitchRect output_rect = {0, 0, pano_width, pano_height};
// Create multiband blender with 5-6 bands for best quality
int num_bands = 6;
Blender *blender = create_blender(MULTIBAND, output_rect, num_bands);
// Feed left image at origin
StitchPoint left_pos = {0, 0};
feed(blender, &left_img, &mask_left, left_pos);
// Position right image with calculated overlap
StitchPoint right_pos = {
left_img.width - (2 * overlap_pixels),
0
};
feed(blender, &right_img, &mask_right, right_pos);
// Perform the blend
blend(blender);
// Save panorama
if (blender->result.data != NULL) {
if (save_image(&blender->result, "panorama.jpg")) {
printf("Panorama saved successfully\n");
}
}
// Cleanup
destroy_blender(blender);
destroy_image(&left_img);
destroy_image(&right_img);
destroy_image(&mask_left);
destroy_image(&mask_right);
}
Multi-Image Panorama
For panoramas with more than two images:Vertical Panoramas
For vertical panoramas (top to bottom), adapt the positioning:Best Practices
Image Capture
- Use a tripod for consistent alignment
- Overlap adjacent images by 20-40%
- Maintain consistent exposure across all shots
- Use manual focus to prevent focus shifts
- Shoot in RAW format for better post-processing
Blending Configuration
- Use multiband blending (6-7 bands) for best quality
- Increase overlap ratio for complex scenes
- Consider feather blending for quick previews
Performance Tips
- Process images at reduced resolution for preview
- Use feather blending during development
- Switch to multiband for final output
- Process panoramas in parallel when creating multiple outputs
Common Issues
Visible Seams
- Increase the number of bands (try 6-7 instead of 5)
- Ensure proper overlap between images
- Check that masks are correctly oriented
- Verify images have similar exposure
Alignment Issues
- This library handles blending, not alignment
- Pre-align images using feature matching before stitching
- Use proper overlap ratio (20-40%)
- Ensure images were captured with proper technique
Memory Issues
- Process fewer images at once
- Reduce image resolution before processing
- Use feather blending instead of multiband
Expected Output
A successful panorama will have:- Seamless transitions between images
- No visible seams or color discontinuities
- Proper perspective throughout
- Natural-looking blend zones
- Feather blending: ~0.2-0.3 seconds
- Multiband blending: ~1.0-1.5 seconds