You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Python的OpenCV混合多张图像?已掌握两图混合方法

How to Blend More Than Two Images in OpenCV

Great question! The cv2.addWeighted() function works perfectly for two images, but extending this to multiple images just requires building on the same weighted addition logic. Here are two straightforward methods to do this:

Method 1: Iterative Use of cv2.addWeighted()

You can blend images one pair at a time. The key here is to adjust weights at each step to maintain the overall weight ratio. Let's say you have 3 images with weights that add up to 1 (for natural brightness):

import cv2
import numpy as np

# Load your images (make sure they're the same size first!)
img_mountain = cv2.imread('mountain.jpg')
img_dog = cv2.imread('dog.jpg')
img_sunset = cv2.imread('sunset.jpg')  # Example third image

# Define weights (sum to 1 for balanced brightness)
w_mountain, w_dog, w_sunset = 0.2, 0.3, 0.5

# Step 1: Blend first two images with proportional weights for this pair
temp_blend = cv2.addWeighted(img_mountain, w_mountain/(w_mountain + w_dog), 
                             img_dog, w_dog/(w_mountain + w_dog), 0)

# Step 2: Blend the result with the third image, using their total combined weights
final_blend = cv2.addWeighted(temp_blend, w_mountain + w_dog, 
                              img_sunset, w_sunset, 0)

# Display the result
cv2.imshow('Multi-Image Blend', final_blend)
cv2.waitKey(0)
cv2.destroyAllWindows()

This approach scales to any number of images—just keep iterating, blending the current result with the next image using the correct weight ratios.

Method 2: Direct Weighted Sum with NumPy

For a more efficient approach (especially with many images), you can convert images to float arrays, compute the weighted sum directly, then convert back to the standard 8-bit image format. This avoids repeated calls to cv2.addWeighted():

import cv2
import numpy as np

# Load images and standardize their sizes (critical for element-wise operations)
img_list = [cv2.imread('mountain.jpg'), cv2.imread('dog.jpg'), cv2.imread('sunset.jpg')]
target_shape = img_list[0].shape
for i in range(1, len(img_list)):
    img_list[i] = cv2.resize(img_list[i], (target_shape[1], target_shape[0]))

# Define weights (sum to 1 for natural brightness)
weights = [0.2, 0.3, 0.5]

# Convert to float to prevent overflow during calculations
float_imgs = [img.astype(np.float32) for img in img_list]

# Calculate the weighted sum
final_blend_float = sum(img * w for img, w in zip(float_imgs, weights))

# Convert back to uint8 (standard image format)
final_blend = final_blend_float.astype(np.uint8)

# Display the result
cv2.imshow('Direct Weighted Blend', final_blend)
cv2.waitKey(0)
cv2.destroyAllWindows()

Important Notes

  • Image Sizes: All images must have the same dimensions (width, height, and number of color channels). Resize or crop images to match if needed.
  • Weight Sum: While weights don't have to sum to 1, doing so ensures the blended image has natural brightness. If weights sum to more than 1, you may get overexposed pixels; less than 1 will result in a darker image.
  • Data Types: Using float32 for intermediate calculations prevents integer overflow, which can cause distorted colors or artifacts.

内容的提问来源于stack exchange,提问作者Amanda

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 07:18:58