图像金字塔。创建所需的合成图像时遇到困难

问题描述 投票:0回答:1

我正在尝试做的事情:

合并这两个图像: Text

还有这个:

Text

使用这款面膜:

Text

创建此输出:

Text

编写一个程序来创建带有蒙版的两个图像的合成图像, 基于图像金字塔。

现在,这是我迄今为止尝试过的:

    import cv2
    import numpy as np

# Read the input images and the mask
    image1 = cv2.imread("figure2-assignment3.jpg")
    image2 = cv2.imread("figure3-assignment3.jpg")
    mask = cv2.imread("figure4-assignment3.jpg", cv2.IMREAD_GRAYSCALE)

# Smooth out the mask
    mask = cv2.GaussianBlur(mask, (5, 5), 0)

# Convert mask to float32 and normalize to range [0, 1]
    mask = mask.astype(np.float32) / 255.0

# Duplicate the mask to match the number of channels in the images
    mask = cv2.cvtColor(mask, cv2.COLOR_GRAY2BGR)

# Generate Gaussian pyramids for both images and the mask
    gaussian_pyramid_image1 = [image1]
    gaussian_pyramid_image2 = [image2]
    gaussian_pyramid_mask = [mask]

    for _ in range(6):  
        image1 = cv2.pyrDown(image1)
        gaussian_pyramid_image1.append(image1)
    
    image2 = cv2.pyrDown(image2)
    gaussian_pyramid_image2.append(image2)
    
    mask = cv2.pyrDown(mask)
    gaussian_pyramid_mask.append(mask)

# Generate Laplacian pyramids for both images
    laplacian_pyramid_image1 = [gaussian_pyramid_image1[-1]]
    laplacian_pyramid_image2 = [gaussian_pyramid_image2[-1]]

    for i in range(5, 0, -1):  # Start from the second last level
       image1_up = cv2.pyrUp(gaussian_pyramid_image1[i])
       image2_up = cv2.pyrUp(gaussian_pyramid_image2[i])

    image1_resized = cv2.resize(gaussian_pyramid_image1[i - 1], (image1_up.shape[1], image1_up.shape[0]))
    image2_resized = cv2.resize(gaussian_pyramid_image2[i - 1], (image2_up.shape[1], image2_up.shape[0]))
    
    laplacian_image1 = cv2.subtract(image1_resized, image1_up)
    laplacian_image2 = cv2.subtract(image2_resized, image2_up)
    
    laplacian_pyramid_image1.append(laplacian_image1)
    laplacian_pyramid_image2.append(laplacian_image2)

# Generate Gaussian pyramid for the mask
    gaussian_pyramid_mask = [gaussian_pyramid_mask[-1]]
# Start from the second last level
    for i in range(5, 0, -1):  
        mask_up = cv2.pyrUp(gaussian_pyramid_mask[-1])
        mask_resized = cv2.resize(gaussian_pyramid_mask[-1], (mask_up.shape[1], mask_up.shape[0]))
        gaussian_pyramid_mask.append(mask_resized)

# Combine the corresponding levels of Laplacian pyramids using the mask
    composite_pyramid = []
    for img1, img2, msk in zip(laplacian_pyramid_image1, laplacian_pyramid_image2, gaussian_pyramid_mask):
        img1_resized = cv2.resize(img1, (msk.shape[1], msk.shape[0]))
        img2_resized = cv2.resize(img2, (msk.shape[1], msk.shape[0]))
        composite_level = img1_resized * msk + img2_resized * (1.0 - msk)
        composite_pyramid.append(composite_level)

# Collapse the composite pyramid to obtain the composite image
    composite_image = composite_pyramid[-1]
    for i in range(len(composite_pyramid) - 2, -1, -1):
       composite_image_up = cv2.pyrUp(composite_image)
       composite_image_resized = cv2.resize(composite_pyramid[i], (composite_image_up.shape[1], 
       composite_image_up.shape[0]))
       composite_image = cv2.add(composite_image_resized, composite_image_up)

# Save the composite image
     cv2.imwrite("composite_image_2.jpg", composite_image)

这是我能制作的最好的: Text

现在我可能做错了什么?我可以拿到手,但合成图像的右侧不是正确的。

python opencv computer-vision
1个回答
0
投票

我不会太担心为此使用高斯金字塔或拉普拉斯金字塔。相反,您可以使用提供的蒙版的平滑版本(这可确保平滑的边界)执行 Alpha 混合,以达到所需的输出。这是我解决您问题的方法:

import cv2
import numpy as np

# Read the input images and the mask
mask = cv2.imread("mask.jpeg", cv2.IMREAD_GRAYSCALE)
image1 = cv2.imread("image-1.jpeg")
image2 = cv2.imread("image-2.jpeg")

# Resize images to match mask dimensions
height, width = mask.shape[:2]
image1 = cv2.resize(image1, (width, height))
image2 = cv2.resize(image2, (width, height))

# Smooth out the mask and normalize to range [0, 1]
transparency_gradient = cv2.blur(mask, (25, 25))
transparency_gradient = cv2.cvtColor(transparency_gradient, cv2.COLOR_GRAY2BGR)
transparency_gradient = transparency_gradient / 255.0  # Normalize to range [0, 1]

# Perform manual alpha blending with transparency gradient
composite_image = image1 * transparency_gradient + image2 * (1 - transparency_gradient)

# Save the result
cv2.imwrite("composite_image.png", composite_image)

这会产生最终图像:

祝你好运!愿代码与您同在...

© www.soinside.com 2019 - 2024. All rights reserved.