A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/python/image-pyramid-using-opencv-python/ below:

Image Pyramid using OpenCV | Python

Image Pyramid using OpenCV | Python

Last Updated : 11 Aug, 2025

An image pyramid is a collection of the same image represented at multiple resolutions. Each level in the pyramid is a resized version of the previous one, either reduced (downsampled) or enlarged (upsampled). The images are typically arranged from largest to smallest, forming a pyramid-like structure. It help us handle images at different scales which is useful in many image processing tasks. By working with lower-resolution versions, we can reduce memory usage and processing time.

By scaling images up or down, we can detect objects of different sizes or blend images more smoothly. In this article, we will see how image pyramids work and how we can create and use them effectively using OpenCV in Python.

Types of Image Pyramids

There are two main ways to represent and process image pyramids and each serves a different purpose:

1. Gaussian Pyramid

A Gaussian Pyramid is created by repeatedly blurring an image and downsampling it. Each level in the pyramid is a smaller and smoother version of the one before. This is useful when we need to reduce the image size while preserving important features. In OpenCV, we can build a Gaussian Pyramid using the cv2.pyrDown() function.

2. Laplacian Pyramid

A Laplacian Pyramid is built from a Gaussian Pyramid. It captures the details lost during downsampling. This is done by upsampling a level of the Gaussian pyramid using cv2.pyrUp() and subtracting it from the previous level. The result highlights edges and fine textures. They are useful for image reconstruction and blending where preserving detail is important.

Pyramid Operations in OpenCV

OpenCV provides two important functions to work with image pyramids: cv2.pyrDown() for downsampling and cv2.pyrUp() for upsampling. These functions allow us to reduce or increase the image resolution which is important for creating image pyramids.

1. Pyramid Down with cv2.pyrDown()

This function is used to reduce the size of an image by applying a Gaussian blur and then downsampling. This operation reduces the image's resolution, making it smaller and less computationally expensive while retaining important details.

Python
import cv2
import numpy as np
from google.colab.patches import cv2_imshow

image = cv2.imread('/content/pyramid sample.webp')

downsampled_image = cv2.pyrDown(image)

cv2_imshow(image)
cv2_imshow(downsampled_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output:

Original Image Downsampled_image

The downsampled_image will be half the size (in both width and height) of the original image. The image will be blurred slightly due to the Gaussian filter applied during downsampling.

2. Pyramid Up with cv2.pyrUp()

This function performs the opposite operation of cv2.pyrDown(). It increases the image's size (both width and height) by a factor of 2. After upsampling, a Gaussian blur is applied to make the transition smoother and reduce artifacts.

Python
import cv2
import numpy as np
from google.colab.patches import cv2_imshow

image = cv2.imread('/content/pyramid sample.webp')

upsampled_image = cv2.pyrUp(image)

cv2_imshow(image)
cv2_imshow(upsampled_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output:

Original Image Upsampled_image

The upsampled_image will be double the size of the downsampled_image. While the upsampled image may look blurred, it will be larger than the downsampled one.

Building a Gaussian Pyramid (Multiple Levels)

In image processing, we need to handle images at multiple resolutions. One way to achieve this is by creating an image pyramid where each level of the pyramid represents a version of the image at a different scale.

Here we will create an image pyramid by downsampling an image using the cv2.pyrDown() function. After generating the pyramid, we will display the levels of the pyramid, starting from the smallest version and moving down to the original image.

Python
import cv2
import numpy as np
from google.colab.patches import cv2_imshow

image = cv2.imread('/content/pyramid sample.webp')

pyramid = [image]

for i in range(3): 
    image = cv2.pyrDown(image)
    pyramid.append(image)

for i in range(len(pyramid)-1, -1, -1):  
    print(f"Pyramid Level {i}")
    cv2_imshow(pyramid[i]) 
    cv2.waitKey(0)  
    
cv2.destroyAllWindows()

Output:

Gaussian Pyramid (Multiple Levels) Advantages of Image Pyramids

Image pyramids has various benefits for image processing tasks:

  1. Lowering Resolution: By scaling down the image, we can reduce the resolution and focus on larger features or objects.
  2. Handling Different Image Sizes: It allow us to work with images at various sizes which is useful in applications like object detection and feature matching.
  3. Image Blending: When combining images, they can help blend different resolutions smoothly.
  4. Edge Detection: They are used in edge detection techniques because working with multiple image scales can reveal edges more effectively at different resolutions.
Limitations of Image Pyramids
  1. Loss of Detail: Downsampling may cause loss of fine details, especially at lower pyramid levels.
  2. Increased Memory Usage: Storing multiple levels of the pyramid can consume significant memory, especially for large images.
  3. Computational Overhead: Generating and processing multiple pyramid levels can be computationally expensive.
  4. Limited to Certain Tasks: Not all image processing tasks benefit from using image pyramids, making them less useful in some situations.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4