Ask Your Question

Revision history [back]

Laplacian Pyramid not matching

Hi, I try to adapt this code here but I get this error, when I try to reconstruct the image from the layers and blend them together in the end of the code.

 faces_reconstructed = cv2.pyrUp(faces_reconstructed, dstsize=size)
cv2.error: OpenCV(4.1.1) C:\projects\opencv-python\opencv\modules\imgproc\src\pyramids.cpp:880: error: (-215:Assertion failed) std::abs(dsize.width - ssize.width*2) == dsize.width % 2 && std::abs(dsize.height - ssize.height*2) == dsize.height % 2 in function 'cv::pyrUp_'

I asked on another forum and someone suggested to delete the size, but then I get an error at faces_reconstructed = cv2.add(faces_pyramid[i], faces_reconstructed) since the size does not match (I suggest, eventhough the images all have the same size). Any help is appreciated.

my code:

import cv2
import numpy as np
width = 800
height = 200
img1 = cv2.imread("head.jpg")
img1 = cv2.resize(img1, (width, height))
img2 = cv2.imread("eye.jpg")
img2 = cv2.resize(img2, (width, height))
img3 = cv2.imread("nose.jpg")
img3= cv2.resize(img3, (width, height))
img4 = cv2.imread("mouth.jpg")
img4 = cv2.resize(img4, (width, height))
facestack = np.vstack((img1[:,:], img2[:,:], img3[:,:], img4[:,:]))
cv2.imshow("faces",facestack)

# 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)
    #cv2.imshow(str(i), 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)


# Gaussian Pyramid 3
layer = img3.copy()
gaussian_pyramid3 = [layer]
for i in range(6):
    layer = cv2.pyrDown(layer)
    gaussian_pyramid3.append(layer)

# Laplacian Pyramid 3
layer = gaussian_pyramid3[5]
laplacian_pyramid3 = [layer]
for i in range(5, 0, -1):
    size = (gaussian_pyramid3[i - 1].shape[1], gaussian_pyramid3[i - 1].shape[0])
    gaussian_expanded = cv2.pyrUp(gaussian_pyramid3[i], dstsize=size)
    laplacian = cv2.subtract(gaussian_pyramid3[i - 1], gaussian_expanded)
    laplacian_pyramid3.append(laplacian)


# Gaussian Pyramid 4
layer = img4.copy()
gaussian_pyramid4 = [layer]
for i in range(6):
    layer = cv2.pyrDown(layer)
    gaussian_pyramid4.append(layer)

# Laplacian Pyramid 4
layer = gaussian_pyramid4[5]
laplacian_pyramid4 = [layer]

for i in range(5, 0, -1):
    size = (gaussian_pyramid4[i - 1].shape[1], gaussian_pyramid4[i - 1].shape[0])
    gaussian_expanded = cv2.pyrUp(gaussian_pyramid4[i], dstsize=size)
    laplacian = cv2.subtract(gaussian_pyramid4[i - 1], gaussian_expanded)
    laplacian_pyramid4.append(laplacian)



# Laplacian Pyramid Footbase_ball
faces_pyramid = []
n = 0
for img1_lap, img2_lap, img3_lap, img4_lap in zip(laplacian_pyramid, laplacian_pyramid2, laplacian_pyramid3, laplacian_pyramid4):
    n += 1
    laplacian = np.vstack((img1_lap, img2_lap,img3_lap,img4_lap))
    #cv2.imshow(str(n), img2_lap)
    faces_pyramid.append(laplacian)





#Reconstructed Faces
faces_reconstructed = faces_pyramid[0]
for i in range(1, 6):
    size = (faces_pyramid[i].shape[1], faces_pyramid[i].shape[0])
    faces_reconstructed = cv2.pyrUp(faces_reconstructed, dstsize=size)
    faces_reconstructed = cv2.add(faces_pyramid[i], faces_reconstructed)
cv2.imshow("Faces reconstructed", faces_reconstructed)

cv2.imshow("Faces", faces)
#cv2.imshow("img1", img1)
#cv2.imshow("img2", img2)
cv2.waitKey(0)
cv2.destroyAllWindows()