# Image Pyramids (Blending and reconstruction) – OpenCV 3.4 with python 3 Tutorial 24

Source code Image reconstruction:

[python]

import cv2

import numpy as np

img = cv2.imread("hand.jpg")

# Gaussian Pyramid

layer = img.copy()

gaussian_pyramid = [layer]

for i in range(6):

layer = cv2.pyrDown(layer)

gaussian_pyramid.append(layer)

# Laplacian Pyramid

layer = gaussian_pyramid[5]

laplacian_pyramid = [layer]

for i in range(5, 0, -1):

size = (gaussian_pyramid[i – 1].shape[1], gaussian_pyramid[i – 1].shape[0])

gaussian_expanded = cv2.pyrUp(gaussian_pyramid[i], dstsize=size)

laplacian = cv2.subtract(gaussian_pyramid[i – 1], gaussian_expanded)

laplacian_pyramid.append(laplacian)

reconstructed_image = laplacian_pyramid[0]

for i in range(1, 6):

size = (laplacian_pyramid[i].shape[1], laplacian_pyramid[i].shape[0])

reconstructed_image = cv2.pyrUp(reconstructed_image, dstsize=size)

reconstructed_image = cv2.add(reconstructed_image, laplacian_pyramid[i])

cv2.imshow(str(i), reconstructed_image)

cv2.imshow("original", img)

cv2.waitKey(0)

cv2.destroyAllWindows()

[/python]

Source code Images blending:

[python]

import cv2

import numpy as np

img1 = cv2.imread("baseball_ball.png")

img1 = cv2.resize(img1, (1000, 1000))

img2 = cv2.imread("football_ball.jpg")

img2 = cv2.resize(img2, (1000, 1000))

footbase_ball = np.hstack((img1[:, :500], img2[:, 500:]))

# Gaussian Pyramid 1

layer = img1.copy()

gaussian_pyramid = [layer]

for i in range(6):

layer = cv2.pyrDown(layer)

gaussian_pyramid.append(layer)

# Laplacian Pyramid 1

layer = gaussian_pyramid[5]

laplacian_pyramid = [layer]

for i in range(5, 0, -1):

size = (gaussian_pyramid[i – 1].shape[1], gaussian_pyramid[i – 1].shape[0])

gaussian_expanded = cv2.pyrUp(gaussian_pyramid[i], dstsize=size)

laplacian = cv2.subtract(gaussian_pyramid[i – 1], gaussian_expanded)

laplacian_pyramid.append(laplacian)

# Gaussian Pyramid 2

layer = img2.copy()

gaussian_pyramid2 = [layer]

for i in range(6):

layer = cv2.pyrDown(layer)

gaussian_pyramid2.append(layer)

# Laplacian Pyramid 2

layer = gaussian_pyramid2[5]

laplacian_pyramid2 = [layer]

for i in range(5, 0, -1):

size = (gaussian_pyramid2[i – 1].shape[1], gaussian_pyramid2[i – 1].shape[0])

gaussian_expanded = cv2.pyrUp(gaussian_pyramid2[i], dstsize=size)

laplacian = cv2.subtract(gaussian_pyramid2[i – 1], gaussian_expanded)

laplacian_pyramid2.append(laplacian)

# Laplacian Pyramid Footbase_ball

footbase_ball_pyramid = []

n = 0

for img1_lap, img2_lap in zip(laplacian_pyramid, laplacian_pyramid2):

n += 1

cols, rows, ch = img1_lap.shape

laplacian = np.hstack((img1_lap[:, 0:int(cols/2)], img2_lap[:, int(cols/2):]))

footbase_ball_pyramid.append(laplacian)

# Reconstructed Footbase_ball

footbase_ball_reconstructed = footbase_ball_pyramid[0]

for i in range(1, 6):

size = (footbase_ball_pyramid[i].shape[1], footbase_ball_pyramid[i].shape[0])

footbase_ball_reconstructed = cv2.pyrUp(footbase_ball_reconstructed, dstsize=size)

footbase_ball_reconstructed = cv2.add(footbase_ball_pyramid[i], footbase_ball_reconstructed)

cv2.imshow("Footbase ball reconstructed", footbase_ball_reconstructed)

cv2.imshow("Footbase ball", footbase_ball)

#cv2.imshow("img1", img1)

#cv2.imshow("img2", img2)

cv2.waitKey(0)

cv2.destroyAllWindows()

[/python]

Files:

### Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

### Opencv Beginner Tutorial

- 1) Loading images
- 2) Loading Video and Webcam
- 3) Drawing and writing on images
- 4) Basic operations on images
- 5) Add images and Threshold
- 6) Blending images
- 7) Bitwise Operators
- 8) Trackbars
- 9) Object detection using HSV Color space
- 10) Basic Thresholding
- 11) Histograms
- 12) Basic geometric transformations
- 13) Perspective transformation
- 14) Affine transformation
- 15) Adaptive thresholding5
- 16) Smoothing images
- 17) Morphological transformation
- 18) Edge detection
- 19) Find and Draw Contours
- 20) Template matching
- 21) Lines detection with Hough Transform
- 22) Corners detection
- 23) Image Pyramids
- 24) Image Pyramids (Blending and reconstruction)
- 25) Feature detection (SIFT, SURF, ORB)
- 26) Feature Matching (Brute-Force)
- 27) Mouse Events
- 28) Histogram and Back Projection
- 29) Object tracking with Mean-shift
- 30) Object tracking with Camshift
- 31) Optical Flow with Lucas-Kanade method
- 32) Background Subtraction
- 33) k-Nearest Neighbour classification
- 34) Object tracking using Homography
- 35) Fourier Transform
- 36) Knn handwritten digits recognition
- 37) Face detection using Haar Cascades

### Most Read:

- Train YOLO to detect a custom object (online with free GPU)
- YOLO object detection using Opencv with Python
- Detecting colors (Hsv Color Space) – Opencv with Python
- How to install Python 3 and Opencv 4 on Windows
- Check if two images are equal with Opencv and Python
- How to install Dlib for Python 3 on Windows
- Feature detection (SIFT, SURF, ORB) – OpenCV 3.4 with python 3 Tutorial 25
- Simple shape detection – Opencv with Python 3