Laplacian Pyramid not matching

asked 2019-11-29 05:32:18 -0500

lusi gravatar image

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 ...
(more)
edit retag flag offensive close merge delete

Comments

the sizes you pass to pyrUp/pyrDown are invalid

https://docs.opencv.org/master/d4/d86...

berak gravatar imageberak ( 2019-11-29 05:41:23 -0500 )edit

Alright, thank you. I found the mistake!

lusi gravatar imagelusi ( 2019-11-29 06:03:33 -0500 )edit
1

I thought I found the problem. But it didnt resolve. I changed the pyramid to this, in hope to fix the sizes. maybe that is the wrong way?

# Laplacian Pyramid imagemerge
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
        cols, rows, ch = img1_lap.shape
        print(cols)
        laplacian = np.vstack((img1_lap[:, 0:int(cols)], img2_lap[:, 0:int(cols)], img3_lap[:, 0:int(cols)], img4_lap[:, 0:int(cols)]))
        faces_pyramid.append(laplacian)
lusi gravatar imagelusi ( 2019-11-29 06:16:12 -0500 )edit

This way it works,but I only see 1 of the 4 images. but it is styliezed

# 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
        cols, rows, ch = img1_lap.shape
        laplacian = np.hstack((img1_lap[:, 0:int(cols/2)], img2_lap[:, int(cols/2):]))
        faces_pyramid.append(laplacian)
lusi gravatar imagelusi ( 2019-11-29 06:25:52 -0500 )edit