Image Pyramid using OpenCV Python

inIn this tutorial, we will get to know the method to make Image Pyramid using OpenCV Python. Here, we will get to know about Image Pyramid and its functions using OpenCV Python. Also, we will see a Python program to implement it and see how it works for better understanding.

So let’s move on…

Image Pyramid

Stack of images with different resolutions are called Image Pyramids. Image Pyramids are one of the most important concept of image processing. When we want to change the resolutions of images in our Python code then image pyramid technique comes handy and is the easiest way to change the resolution of the image.

Pyramid UP

The input image is initially up-sampled and then blurred in Pyramid UP . pyrUP() function performs the Pyramid UP operation. The pyrUp() function increases the size to double of its original size  cv2.destroyAllWindows()

Pyramid Down

The input image is initially blurred and then down-sampled in Pyramid Down. pyrDown() function performs the Pyramid Down operation. The pyrDown() function decreases the size to half of its original size.

Image Pyramids have some advantages also like edge detection, lowering the resolution, image blending etc.

Here is my script for image pyramids:-

import numpy as np
from cv2 import cv2

img = cv2.imread('lena.jpg')
#Copy of image
layer = img.copy() 

gp = [layer]

for i in range(6):
    layer = cv2.pyrDown(layer)   #Pyramid Down Operation
    gp.append(layer)             #Appending to the list created
    #cv2.imshow(str(i), layer)

layer = gp[5]
cv2.imshow('upper level Gaussian Pyramid', layer)
lp = [layer]

for i in range(5, 0, -1):
    gaussian_extended = cv2.pyrUp(gp[i])        #Pyramid UP Operation
    laplacian = cv2.subtract(gp[i-1], gaussian_extended)
    cv2.imshow(str(i), laplacian)

cv2.imshow('Original Image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

 

For image pyramid check out this:-

https://docs.opencv.org/3.4/d4/d1f/tutorial_pyramids.html

Also read about:-

Edge detection using OpenCV in Python

Leave a Reply