python产生混合的拉普拉斯金字塔
时间: 2023-10-13 08:24:50 浏览: 81
以下是Python代码示例,用于生成混合的拉普拉斯金字塔:
```python
import cv2
import numpy as np
def generate_laplacian_pyramid(img):
pyramid = []
pyramid.append(img)
for i in range(6):
img = cv2.pyrDown(img)
pyramid.append(img)
laplacian_pyramid = []
for i in range(5, 0, -1):
GE = cv2.pyrUp(pyramid[i])
L = cv2.subtract(pyramid[i-1], GE)
laplacian_pyramid.append(L)
laplacian_pyramid.append(pyramid[0])
return laplacian_pyramid
def blend_images(img1, img2, mask):
blended = np.zeros_like(img1)
for i in range(3):
blended[:,:,i] = img1[:,:,i] * (1 - mask) + img2[:,:,i] * mask
return blended
img1 = cv2.imread('image1.jpg')
img2 = cv2.imread('image2.jpg')
mask = cv2.imread('mask.jpg', 0) / 255.0
laplacian_pyramid1 = generate_laplacian_pyramid(img1)
laplacian_pyramid2 = generate_laplacian_pyramid(img2)
mask_pyramid = generate_laplacian_pyramid(mask)
blended_pyramid = []
for i in range(len(laplacian_pyramid1)):
blended = blend_images(laplacian_pyramid1[i], laplacian_pyramid2[i], mask_pyramid[i])
blended_pyramid.append(blended)
blended_image = blended_pyramid[0]
for i in range(1, len(blended_pyramid)):
blended_image = cv2.add(cv2.pyrUp(blended_image), blended_pyramid[i])
cv2.imshow('Blended Image', blended_image)
cv2.waitKey(0)
```
阅读全文