DEV Community

Farzon Lotfi
Farzon Lotfi

Posted on • Originally published at blog.farzon.org on

Seamless Image Merging: A Guide to Laplacian Pyramid Blending

If you have ever tried to create a multi-panel collage by stitching many images together you have likely run into the problem of harsh, visible seams. Slightly better would be some naive alpha blending. Slightly worse are simple cut-and-paste jobs. Both leave unnatural transitions or ghosting artifacts.

To get those buttery-smooth, seamless transitions, we can use a classic computer vision technique: Laplacian Pyramid Blending. Below you can see how awesome the results can be.

Example of Laplacian Blending

Building the Pyramids

To blend two images (let's call them Image A and Image B) without harsh seams, we have to treat the broad strokes (low frequencies, like lighting and color) differently than the fine details (high frequencies, like sharp edges). We do this using "Image Pyramids."

  • The Gaussian Pyramid (REDUCE): We take our original image and run a Gaussian filter (blur) over it, then downsample it (e.g., shrink an 8x8 image to a 4x4, then 2x2, then 1x1). Each step is a new "level" in our pyramid, representing progressively coarser, lower-frequency data.
# Example: Generating a Gaussian Pyramid
G = image.copy()
gaussian_pyramid = [G]
for i in range(6): # Building 6 levels
    G = cv2.pyrDown(G)
    gaussian_pyramid.append(G)
Enter fullscreen mode Exit fullscreen mode
  • The Laplacian Pyramid (EXPAND): This isolates the details. We take a smaller, coarser level of the Gaussian pyramid and expand it to match the size of the level below it. Because expanding is basically guessing the pixels in between, it's not perfect. We subtract this expanded image from the actual image at that level to get an "error image." This error image is the Laplacian. It basically looks like an edge map containing all the high-frequency details.
# Example: Generating a Laplacian Pyramid
laplacian_pyramid = [gaussian_pyramid[5]]
for i in range(5, 0, -1):
    GE = cv2.pyrUp(gaussian_pyramid[i])
    L = cv2.subtract(gaussian_pyramid[i-1], GE)
    laplacian_pyramid.append(L)
Enter fullscreen mode Exit fullscreen mode

The Blending Process

Suppose you have Image A, Image B, and a Mask (Region R) that tells the computer where the blend should happen. In the process I learned the blending works from coarse to fine:

  1. Build the Laplacian pyramids for Image A and Image B (L_A and L_B).
  2. Build a Gaussian pyramid for your Mask (G_R). Blurring the mask as it gets smaller ensures that the transition zone widens for lower frequencies.
  3. Form a combined Laplacian pyramid (L_O) using the Mask's Gaussian pyramid as weights. The formula at each level looks like this: L_O(i,j) = G_R(i,j) * L_A(i,j) + (1 - G_R(i,j)) * L_B(i,j)
  4. Finally, collapse the combined pyramid (L_O) by continually expanding and adding the levels back together to get the final blended image.

The Code Implementation

I wrote a Python implementation that handles this exact mathematical process. It computes the pyramids, applies the blending step at each level, and reconstructs the output.

You can check out the full source code on my GitHub here: Laplacian Blending with Python.

or checkout the colab directly: Open In Colab

Conclusion

By breaking images down into their frequency bands using Gaussian and Laplacian pyramids, we can blend the structural details and the broad lighting separately. The result is a composite image that fools the human eye into seeing butter smooth transition. Grab the code, create some image masks, and try making your own seamless collages!


Credit Where It's Due: Computational Photography Course At Georgia Tech

I have to mention that a fantastic resource for learning these concepts is Georgia Tech’s CS 6475: Computational Photography class. I took this course when I was in grad school and it was one of my favorite courses. If you want a deep dive into the math and theory, I highly recommend checking out these excellent course notes compiled by Monzer Saleh. The theory above leans on the foundational concepts taught in that course.

Top comments (0)