Creating panoramas using python (image stitching)

Zahid Parvez
2 min readJan 19, 2023

To create a panorama in python we can multiple images taken with overlapping fields of view and stich them into a wider panorama. In fact, this method will work for stitching images in any direction and does not have to be panoramas.

Firstly, Image stitching is the process of combining multiple images with overlapping fields of view to create a single larger image, also known as a panorama. The overlapping field of view should be roughly 40% of the total image, less will work if there are obvious features in the image.

Field of view overlap between 2 images

Algrothim overview

Python’s OpenCV implementation uses an algorithm proposed by Matthew Brown and David G. Low to preform image stitching with minimal user intervention. At a high level the implimentation has 3 steps:

Image registration — the algrothim uses the Scale-invariant feature transform (SIFT) method on the input images to find features on the image. the features are then fed into the RANSAC (Random Sample Consensus) method to estimate the geometric transformation between the images.

Image warping — the geometric transformation estimates are then used to transform the input images and align them to their final position.

Blending — the algorithm then uses the multi band blending method to create a seamless transition between the images.

Python Implementation

The python implementation is quiet straight forward. A Sticher object is created then the stitch function is called to create the output.

def stitch_images(images):
# Create a Stitcher object
# Use cv2.createStitcher() if you are using OpenCV 3
stitcher = cv2.Stitcher_create()

# Stitch the images together
# output status, result image
_ , result = stitcher.stitch(images)

# Return the result
return result
Result of the stitch_images function defined above

As you can see the resulting image has some black boundaries around the edge, this occurs for a number of reasons including misalignment, perspective mish match, and Image distortion. with a slight crop we can remove this:

Output images with the boundaries cropped

If you would like to get a copy of the code used in this article, it can be found here on Github.

--

--

Zahid Parvez

I am an analyst with a passion for data, software, and integration. In my free time, I also like to dabble in design, photography, and philosophy.