我正在研究将图像与OpenCV库混合在一起的项目。尝试重建金字塔的层时,即使我认为正确设置大小,也会出现错误。在代码末尾,我收到此错误消息(位于#Reconstructed Faces
),感谢您的帮助:
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_'
我的代码:
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()
答案 0 :(得分:0)
在此行:
faces_reconstructed = cv2.pyrUp(faces_reconstructed, dstsize=size)
请删除第二个参数,应该可以正常工作
faces_reconstructed = cv2.pyrUp(faces_reconstructed)