尝试实现著名的橙子/苹果金字塔混合(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
算术运算相比,最大、最小和平均像素值都相同。
EDIT : Code Added
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()
img
是numpy数组,请检查img.dtype
。) - Warren Weckessernumpy.ndarray
,数据类型为uint8
。 - Elia