问题
What is the way to blend multiple images with OpenCV using python? I came across the following snippet:
img = cv2.addWeighted(mountain, 0.3, dog, 0.7, 0)
on https://docs.opencv.org/3.4/d5/dc4/tutorial_adding_images.html
that shows a way to blend 2 images mountain
and dog
. What if I want to blend more than 2 images? How could I do this?
回答1:
Try This:
blendedImage = weight_1 * image_1 + weight_2 * image_2 + ... + weight_n * image_n
回答2:
You can blend all of your images by blending according to follwoing sequence:
- Blend the first two images
- Take the result and blend it with the next image
- and so forth
for idx, img in enumerate(imgs):
if idx == 1:
first_img = img
continue
else:
second_img = img
first_img = cv2.addWeighted(first_img, 0.5, second_img, 0.5, 0)
You might have a problem with the weights of each image, but this is another issues. To achieve an equal weigth for all images you can use the index to calculate the appropriate portion:
for idx, img in enumerate(imgs):
if idx == 1:
first_img = img
continue
else:
second_img = img
second_weight = 1/(idx+1)
first_weight = 1 - second_weight
first_img = cv2.addWeighted(first_img, first_weight, second_img, second_weight, 0)
来源:https://stackoverflow.com/questions/57723968/blending-multiple-images-with-opencv