How to find Laplassian pyramids for an image using OpenCV in Python?

We can form Laplacian Pyramids from Gaussian Pyramids in OpenCV. While OpenCV doesn't provide a direct function to construct Laplacian Pyramids, we can create them by computing differences between Gaussian pyramid levels.

In a Laplacian pyramid, images appear as edge-like representations and are commonly used in image compression and image enhancement applications.

How Laplacian Pyramids Work

A level in the Laplacian Pyramid is formed by the difference between that level in the Gaussian Pyramid and the expanded version of its upper level. The process involves:

  • Creating a Gaussian pyramid using cv2.pyrDown()

  • Expanding higher levels using cv2.pyrUp()

  • Computing the difference between corresponding levels

  • The topmost level remains unchanged

Creating a Laplacian Pyramid

Here's how to construct a three-level Laplacian pyramid ?

import cv2
import numpy as np

# Create a sample image for demonstration
img = np.random.randint(0, 256, (256, 256, 3), dtype=np.uint8)

# Create a Gaussian Pyramid
gaussian_pyr = [img.copy()]
lower = img.copy()

for i in range(3):
    lower = cv2.pyrDown(lower)
    gaussian_pyr.append(lower)

# Last level of Gaussian remains same in Laplacian
laplacian_top = gaussian_pyr[-1]

# Create a Laplacian Pyramid
laplacian_pyr = [laplacian_top]

for i in range(3, 0, -1):
    # Get size of current level
    size = (gaussian_pyr[i - 1].shape[1], gaussian_pyr[i - 1].shape[0])
    
    # Expand the higher level
    gaussian_expanded = cv2.pyrUp(gaussian_pyr[i], dstsize=size)
    
    # Compute difference
    laplacian = cv2.subtract(gaussian_pyr[i-1], gaussian_expanded)
    laplacian_pyr.append(laplacian)

print(f"Number of Gaussian levels: {len(gaussian_pyr)}")
print(f"Number of Laplacian levels: {len(laplacian_pyr)}")
print(f"Original image shape: {img.shape}")
print(f"Top Laplacian level shape: {laplacian_pyr[0].shape}")
Number of Gaussian levels: 4
Number of Laplacian levels: 4
Original image shape: (256, 256, 3)
Top Laplacian level shape: (32, 32, 3)

Complete Example with Image Loading

Here's a complete example that loads an image file and creates the Laplacian pyramid ?

import cv2
import numpy as np

# For demonstration, create a synthetic image
# In practice, use: img = cv2.imread('your_image.jpg')
img = np.zeros((200, 200, 3), dtype=np.uint8)
cv2.rectangle(img, (50, 50), (150, 150), (255, 255, 255), -1)
cv2.circle(img, (100, 100), 30, (0, 0, 0), -1)

# Create Gaussian Pyramid
gaussian_pyr = [img.copy()]
lower = img.copy()

for i in range(3):
    lower = cv2.pyrDown(lower)
    gaussian_pyr.append(lower)

# Create Laplacian Pyramid
laplacian_top = gaussian_pyr[-1]
laplacian_pyr = [laplacian_top]

for i in range(3, 0, -1):
    size = (gaussian_pyr[i - 1].shape[1], gaussian_pyr[i - 1].shape[0])
    gaussian_expanded = cv2.pyrUp(gaussian_pyr[i], dstsize=size)
    laplacian = cv2.subtract(gaussian_pyr[i-1], gaussian_expanded)
    laplacian_pyr.append(laplacian)

# Print information about each level
for i, level in enumerate(laplacian_pyr):
    print(f"Laplacian Level {i}: {level.shape}")
Laplacian Level 0: (25, 25, 3)
Laplacian Level 1: (50, 50, 3)
Laplacian Level 2: (100, 100, 3)
Laplacian Level 3: (200, 200, 3)

Key Properties

Laplacian pyramids have several important characteristics:

  • Edge Enhancement: Each level highlights edges and details at different scales

  • Size Reduction: Each level is one-fourth the size of the previous level

  • Reconstruction: The original image can be perfectly reconstructed from the pyramid

  • Compression: Useful for multi-resolution image compression

Conclusion

Laplacian pyramids are created by computing differences between Gaussian pyramid levels and their expanded versions. They provide edge-enhanced representations at multiple scales, making them valuable for image processing applications like compression and enhancement.

Updated on: 2026-03-26T22:01:18+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements