- Trending Categories
- Data Structure
- Operating System
- MS Excel
- C Programming
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to find Laplassian pyramids for an image using OpenCV in Python?
We can form the Laplacian Pyramids from the Gaussian Pyramids. OpenCV does not provide any specific function to construct Laplacian Pyramids.
In Laplacian pyramid, images look like edge images only. Laplacian Pyramids are used in image compression as well as in image enhancement.
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 in the Gaussian Pyramid.
To create a level in the Gaussian pyramid, we apply the cv2.pyrDown() or cv2.pyrUp() function.
To construct a three-level Laplacian pyramid, follow the steps given below −
Import the required libraries.
Load the input image.
Then construct the Gaussian pyramid with three levels.
For the Laplacian pyramid, a level is formed by the difference between a level in the Gaussian Pyramid and the expanded version of its upper level in the Gaussian Pyramid. The last level in the Gaussian pyramid remains the same in Laplacian pyramid.
Repeat step 4 to construct all levels of the Laplacian pyramid.
Create windows to display all the layers and display them.
We will use the following image as the Input File in the example below.
In the Python3 program below, we created a three-layer Laplacian pyramid of input image car.jpg.
import cv2 # Load the image img = cv2.imread('car.jpg') lower = img.copy() # Create a Gaussian Pyramid gaussian_pyr = [lower] 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): size = (gaussian_pyr[i - 1].shape, gaussian_pyr[i - 1].shape) gaussian_expanded = cv2.pyrUp(gaussian_pyr[i], dstsize=size) laplacian = cv2.subtract(gaussian_pyr[i-1], gaussian_expanded) laplacian_pyr.append(laplacian) # create three windows to display three layers of images cv2.namedWindow('Layer 1', cv2.WINDOW_AUTOSIZE) cv2.namedWindow('Layer 2', cv2.WINDOW_AUTOSIZE) cv2.namedWindow('Layer 3', cv2.WINDOW_AUTOSIZE) # display all three layers cv2.imshow('Layer 1',laplacian_pyr) cv2.imshow('Layer 2',laplacian_pyr) cv2.imshow('Layer 3',laplacian_pyr) cv2.waitKey(0) cv2.destroyAllWindows()
On execution of the above code, it will open three output windows, each showing a particular layer in the Laplacian pyramid.
Notice that the Layer 2 image is one-fourth of the Layer 1 image. And the same way the Layer 3 image is one-fourth of the Layer 2 image.
Kickstart Your Career
Get certified by completing the courseGet Started