尝试实现著名的Orange / Apple金字塔混合(cv2 Image Pyramids)。
注意:两个图像的形状均为307x307。
但是,由于结果图像由于cv2.subtract
和cv2.add
中的裁剪值而变得模糊(如cv2 vs numpy Matrix Arithmetics中所述),因此我使用numpy
算法代替StackOverflow: Reconstructed Image after Laplacian Pyramid Not the same as original image中的建议。
我已经通过在一个图像上执行金字塔测试了这一点,并且与使用cv2
算术相反,使用金字塔构造的结果图像具有相同的Max,Min,Average像素值。
但是,在金字塔等级7上,结果图像会出现红点的“噪点”,而在等级9上,结果图像会出现很多绿色像素噪声。 Images of levels 6, 7, 9 - Imgur Album。
任何想法为什么会发生这种情况?我会说金字塔9级绿色噪声是由于图像低于1x1形状而发生的。但是7级金字塔上的红点呢?
numberOfPyramids = 9
# generate Gaussian pyramids for A and B Images
GA = A.copy()
GB = B.copy()
gpA = [GA]
gpB = [GB]
for i in xrange(numberOfPyramids):
GA = cv2.pyrDown(GA)
GB = cv2.pyrDown(GB)
gpA.append(GA)
gpB.append(GB)
# generate Laplacian Pyramids for A and B Images
lpA = [gpA[numberOfPyramids - 1]]
lpB = [gpB[numberOfPyramids - 1]]
for i in xrange(numberOfPyramids - 1, 0, -1):
geA = cv2.pyrUp(gpA[i], dstsize = np.shape(gpA[i-1])[:2])
geB = cv2.pyrUp(gpB[i], dstsize = np.shape(gpB[i-1])[:2])
laplacianA = gpA[i - 1] - geA if i != 1 else cv2.subtract(gpA[i-1], geA)
laplacianB = gpB[i - 1] - geB if i != 1 else cv2.subtract(gpB[i-1], geB)
lpA.append(laplacianA)
lpB.append(laplacianB)
# Now add left and right halves of images in each level
LS = []
for la, lb in zip(lpA, lpB):
_, cols, _ = la.shape
ls = np.hstack((la[:, : cols / 2], lb[:, cols / 2 :]))
LS.append(ls)
# now reconstruct
ls_ = LS[0]
for i in xrange(1, numberOfPyramids):
ls_ = cv2.pyrUp(ls_, dstsize = np.shape(LS[i])[:2])
ls_ = ls_ + LS[i] if i != numberOfPyramids - 1 else cv2.add(ls_, LS[i])
cv2.imshow(namedWindowName, ls_)
cv2.waitKey()
最佳答案
阅读有关拉普拉斯金字塔的原始article之后,我发现我误解了这种方法,因为我们使用了其他像素信息,因此可以完全重建原始图像而不会造成模糊。确实,削波值导致模糊。好了,现在我们再回到开头:)
因此,您发布的代码仍是裁剪值,我建议您使用int16保存拉普拉斯金字塔,而不要使用cv2.subtract。希望它能工作。