How do Laplacian pyramids work in OpenCV

This recipe explains how do Laplacian pyramids work in OpenCV

Recipe Objective: How do laplacian pyramids work in OpenCV?

In this recipe, we are going to understand what is Laplacian Pyramids

Step 1: Import the libraries and read the image

Let us first import the necessary libraries and read the image. The image that we are using here is the one shown below. Let us read the image in grayscale this time for better results.

Input Image

import numpy as np
import cv2
image = cv2.imread('edgeflower.jpg',0)

Step 2: Create a Gaussian Pyramid

There is no pre-defined function for creating a Laplacian pyramid in OpenCV. Laplacian pyramids can be obtained by the difference between that layer and its lower layer (i.e., expanded layer) in the Gaussian pyramid. We already know that Laplacian is a high pass filter, and for this reason, we obtain the edges of the image as output in each layer.

Let us now create a gaussian pyramid, and in the next step, we shall calculate the difference between layers to obtain the Laplacian pyramid.

To begin with, let us create an array called gaussian in which we will store all the layers of the Gaussian pyramid and initialize a variable called gaussian_layer with a copy of the image that we have read using the copy() function.

gaussian = []
gaussian_layer= image.copy()

We can easily create a Gaussian pyramid using the cv2.pyrDown() function inside a loop.

for i in range(3):
    gaussian_layer = cv2.pyrDown(gaussian_layer)
    gaussian.append(gaussian_layer)
    cv2.imshow('Gaussian Layer -{}'.format(i),gaussian_layer)
cv2.waitKey(0)
cv2.destroyAllWindows()

In the above chunk of code, we perform two other functions inside the loop for every iteration. One is appending the gaussian layer that we obtained from the cv2.pyrdown() function to the list that we created earlier using the append() function. The other displays each layer of the Gaussian pyramid using the cv2.imshow() function.

Output:

Gaussian

Step 3: Create a Laplacian Pyramid

Once we have the list of gaussian layers ready, we can proceed with creating the Laplacian pyramid. To do that, let us first initialize a list called Laplacian which will store all the layers of the Laplacian pyramid with the top image of the Gaussian pyramid. (i.e., the last element of the gaussian list).

laplacian = [gaussian[-1]]

Then by iterating through the gaussian list in reverse order, let us perform the following in each iteration.

  1. Obtain the expanded layer of the current layer using the cv2.pyrup() function
  2. Obtain the Laplacian layer by calculating the difference between the current gaussian layer and the expanded gaussian layer using the cv2.subtract() function.
  3. Append each Laplacian layer to the laplacian list.
  4. Display each Laplacian layer.
for i in range(2,0,-1):
    size = (gaussian[i - 1].shape[1], gaussian[i - 1].shape[0])
    gaussian_expanded = cv2.pyrUp(gaussian[i], dstsize=size)
    laplacian_layer = cv2.subtract(gaussian[i-1], gaussian_expanded)
    laplacian.append(laplacian_layer)
    cv2.imshow('laplacian layer -{}'.format(i-1),laplacian_layer)
cv2.waitKey(0)
cv2.destroyAllWindows()

As is well known that the dimensions of the two images that we subtract must be of the same dimension, we calculate the size of the layer behind the current layer and pass that size as the output size to the dstsize parameter of the cv2.pyrUp() function. The dstsize parameter is an optional parameter of both cv2.pyrUp() and cv2.pyrDown() functions which returns the output image in the desired dimension.

Output:

Laplacian

What Users are saying..

profile image

Abhinav Agarwal

Graduate Student at Northwestern University
linkedin profile url

I come from Northwestern University, which is ranked 9th in the US. Although the high-quality academics at school taught me all the basics I needed, obtaining practical experience was a challenge.... Read More

Relevant Projects

Digit Recognition using CNN for MNIST Dataset in Python
In this deep learning project, you will build a convolutional neural network using MNIST dataset for handwritten digit recognition.

Learn to Build Generative Models Using PyTorch Autoencoders
In this deep learning project, you will learn how to build a Generative Model using Autoencoders in PyTorch

Ecommerce product reviews - Pairwise ranking and sentiment analysis
This project analyzes a dataset containing ecommerce product reviews. The goal is to use machine learning models to perform sentiment analysis on product reviews and rank them based on relevance. Reviews play a key role in product recommendation systems.

ML Model Deployment on AWS for Customer Churn Prediction
MLOps Project-Deploy Machine Learning Model to Production Python on AWS for Customer Churn Prediction

Build Customer Propensity to Purchase Model in Python
In this machine learning project, you will learn to build a machine learning model to estimate customer propensity to purchase.

Build a Review Classification Model using Gated Recurrent Unit
In this Machine Learning project, you will build a classification model in python to classify the reviews of an app on a scale of 1 to 5 using Gated Recurrent Unit.

Build a Multi ClassText Classification Model using Naive Bayes
Implement the Naive Bayes Algorithm to build a multi class text classification model in Python.

Linear Regression Model Project in Python for Beginners Part 1
Machine Learning Linear Regression Project in Python to build a simple linear regression model and master the fundamentals of regression for beginners.

MLOps using Azure Devops to Deploy a Classification Model
In this MLOps Azure project, you will learn how to deploy a classification machine learning model to predict the customer's license status on Azure through scalable CI/CD ML pipelines.

Tensorflow Transfer Learning Model for Image Classification
Image Classification Project - Build an Image Classification Model on a Dataset of T-Shirt Images for Binary Classification