Blending multiple images with OpenCV

柔情痞子 提交于 2021-01-28 18:16:27

问题


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:

  1. Blend the first two images
  2. Take the result and blend it with the next image
  3. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!