1

I work on a project on blending images together with the OpenCV library. I get an error when I try to reconstruct the layers of the pyramids, even though I thought to set the size correctly. At the end of the code, I get this error message (at #Reconstructed Faces), any help is appreciated:

Traceback (most recent call last):
  File "morph.py", line 104, in <module>
    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_'

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()
germi
  • 4,628
  • 1
  • 21
  • 38

1 Answers1

0

In the line:

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

please remove the second parameter, should work fine:

faces_reconstructed = cv2.pyrUp(faces_reconstructed)
lenik
  • 23,228
  • 4
  • 34
  • 43
  • then I get an error that the channels (the size) do not match```(-209:Sizes of input arguments do not match) The operation is neither 'array op array' (where arrays have the same size and the same number of channels), nor 'array op scalar', nor 'scalar op array' in function 'cv::arithm_op``` – Luis Michael Luethi Nov 29 '19 at 11:11
  • in the following line`faces_reconstructed = cv2.add(faces_pyramid[i], faces_reconstructed)` – Luis Michael Luethi Nov 29 '19 at 11:18