Actually the dst is created based on following formula:

dst = src1*alpha + src2*beta + gamma

Which says that when you multiply your images that are in fact 3D arrays with alpha you are multiplying all the items. For example, for a blue pixel you have [255, 0, 0] and the white [255, 255, 255], and when you are adding the matrices together, if you want the result to be blue you should convert the white pixels to 0 which is in fact black (doesn't make sense from physics perspective tho lol). You can simply find the white pixels using advanced numpy indexing then convert them to zero.

import cv2    

img1 = cv2.imread('img1.png')
img2 = cv2.imread('img2.png')

img1[img1[:, :, 1:].all(axis=-1)] = 0
img2[img2[:, :, 1:].all(axis=-1)] = 0

dst = cv2.addWeighted(img1, 1, img2, 1, 0)

cv2.imshow('sas', dst)
cv2.waitKey(0)
cv2.destroyAllWindows()

Result:

Answer from Kasravnd on Stack Overflow
🌐
GitHub
github.com › pydemo › overlay
GitHub - pydemo/overlay: Overlay 2 images using python and OpenCV · GitHub
#OVERLAY OPACITY = 0.7 added_image = cv2.addWeighted(new_background,0.6,square,0.4,0) cv2.imshow('adjusted', added_image) cv2.waitKey() cv2.imwrite(out, added_image)
Author: pydemo
🌐
Master Data Science
datahacker.rs › 012-blending-and-pasting-images-using-opencv
#012 Blending and Pasting Images Using OpenCV - Master Data Science
January 5, 2021 - To successfully apply this process in OpenCV we need to select Region of Interest (ROI) in the first image, and then apply masking and some logical operations to overlay second image over the first image.
🌐
TheAILearner
theailearner.com › 2019 › 03 › 26 › image-overlays-using-bitwise-operations-opencv-python
Image Overlays using Bitwise Operations OpenCV-Python | TheAILearner
March 26, 2019 - In this blog, we will learn how to overlay non-rectangular ROI to another image. Put the TheAILearner text image(shown in the left) above an image (Right one). Because the TheAILearner text is non-rectangular, we will be using OpenCV cv2.bitwise_and(img1, img2, mask) where the mask is an 8-bit single channel array, that specifies elements of the output array to be changed. For Bitwise_and you need to know the following two rules
🌐
GeeksforGeeks
geeksforgeeks.org › python › transparent-overlays-with-python-opencv
Transparent overlays with Python OpenCV - GeeksforGeeks
July 23, 2025 - import cv2 import numpy as np # Loading our images # Background/Input image background = cv2.imread('Assets/img1.jpg') # Overlay image overlay_image = cv2.imread('Assets/overlay3.png') # Resize the overlay image to match the bg image dimensions overlay_image = cv2.resize(overlay_image, (1000, 1000)) h, w = overlay_image.shape[:2] # Create a new np array shapes = np.zeros_like(background, np.uint8) # Put the overlay at the bottom-right corner shapes[background.shape[0]-h:, background.shape[1]-w:] = overlay_image # Change this into bool to use it as mask mask = shapes.astype(bool) # We'll create
🌐
OpenCV
docs.opencv.org › 3.4.20 › d5 › dc4 › tutorial_adding_images.html
OpenCV: Adding (blending) two images using OpenCV
By varying \(\alpha\) from \(0 \rightarrow 1\) this operator can be used to perform a temporal cross-dissolve between two images or videos, as seen in slide shows and film productions (cool, eh?)
🌐
PyImageSearch
pyimagesearch.com › home › blog › transparent overlays with opencv
Transparent overlays with OpenCV - PyImageSearch
April 17, 2021 - This tutorial demonstrates how to use OpenCV to create transparent overlays with the cv2.addWeighted function and OpenCV + Python bindings.
Find elsewhere
🌐
GitConnected
levelup.gitconnected.com › how-to-approach-image-overlay-problems-ad2d4a8e22bc
How to approach image overlay problems | by Shaurya Agarwal | Level Up Coding
December 14, 2021 - Similarly, if you’d change the pixel values to [255, 0, 0], that area would become BLUE (OpenCV reads the images in BGR format). ... Similarly, those pixel values can be replaced by another image, just by using the pixel values of that image. In order to do that, you must reshape the overlaying image to the size whose pixels values you want to replace.
🌐
YouTube
youtube.com › watch
OpenCV Python Image Overlay - YouTube
🎁 Get FREE Robotics & AI Resources (Guide, Textbooks, Courses, Resume Template, Code & Discounts) – Sign up via the pop-up at https://kevinwoodrobotics.com/...
Published: June 3, 2023
🌐
OpenCV
forum.opencv.org › python
Overlaying multiple frame - Python - OpenCV
August 30, 2022 - I have been trying to overlay multiple frame together from an array of image, but it only generate first frame (img[0]) as output. for i in range(0,len(img),1): dst = cv2.addWeighted(first_frame,0.5,img[i],1,0) cv2.imwrite("dst.jpg",dst)
🌐
Iditect
iditect.com › faq › python › combining-two-images-with-opencv-in-python.html
Combining Two Images with OpenCV in python
Description: This query focuses on pasting one image onto another image at a specified position (x, y coordinates) using OpenCV in Python, often used for image composition or annotation. ... import cv2 # Load images background_image = cv2.imread('background.jpg') overlay_image = cv2.imread('overlay.png') # Define position to paste overlay image (top-left corner) x, y = 100, 50 # Paste overlay image onto background image background_image[y:y+overlay_image.shape[0], x:x+overlay_image.shape[1]] = overlay_image # Display combined image cv2.imshow('Combined Image', background_image) cv2.waitKey(0) cv2.destroyAllWindows()
🌐
GitHub
gist.github.com › robsears › 4260425
Overlay an image in OpenCV using Python · GitHub
August 7, 2018 - Overlay an image in OpenCV using Python. GitHub Gist: instantly share code, notes, and snippets.
🌐
Quora
quora.com › How-can-I-overlay-2-images-of-different-sizes-using-Python
How to overlay 2 images of different sizes using Python - Quora
Answer (1 of 2): overlay of 2 images of different sizes I posted details on github: pydemo/overlay Background image Foreground image Get images width/height [code]b_h, b_w, b_ch = background.shape o_h, o_w, o_ch = overlay.shape [/code]Scale background image [code] W = 800 imgScale =...
Top answer
1 of 1
9

Homographies compose, so if you have the homographies between img1 and img2 and between img2 and img3 then the composition of those two homographies gives the homography between img1 and img3.

Your sizes are off of course because you're trying to match img3 to the stitched image containing img1 and img2. But you don't need to do that. Don't stitch them until you have all the homographies between each successive pair of images. Then you can proceed in one of two ways; work from the back or work from the front. I'll use for e.g. h31 to refer to the homography which warps img3 into coordinates of img1.

From the front (pseudocode):

warp img2 into coordinates of img1 with h21
warp img3 into coordinates of img1 with h31 = h32 @ h21
warp img4 into coordinates of img1 with h41 = h43 @ h31
...
stitch/blend images together

Here @ is the matrix multiplication operator, which will achieve our homography composition (note that it is safest to divide by the final entry in the homography to ensure that they're all scaled the same).

From the back (pseudocode):

...
warp prev stitched img into coordinates of img3 with h43
stitch warped stitched img with img3
warp prev stitched img into coordinates of img2 with h32
stitch warped stitched img with img2
warp prev stitched img into coordinates of img1 with h21
stitch warped stitched img with img1

The idea is either you start from the front, and warp everything into the first images coordinate frame, or start from the back, warp to the previous image and stitch, and then warp that stitched image into the previous image, and repeat. I think the first method is probably easier. In either case you have to worry about the propagation of errors in your homography estimation as they will build up over multiple composed homographies.

This is the naïve approach to blend multiple images together with just the homographies. The more sophisticated method is to use bundle adjustment, which takes into account feature points across all images. Then for good blending the steps are gain compensation to remove camera gain adjustments and vignetting, and then multi-band blending to prevent blurring. See the seminal paper from Brown and Lowe here and a brilliant example and free demo software here.

🌐
Medium
medium.com › @shivam77kushwah › combining-two-images-to-form-a-single-image-2905ceab5b5
Combining Two Images to Form a Single Image and Swapping Cropped Parts of Images Using OpenCV in Python | by Shivam kushwah | Medium
July 31, 2023 - Combining images is a common task in image processing and computer vision. It allows us to merge multiple images to create collages, compare visualizations, or overlay information. In this tutorial, we will use the powerful OpenCV library in Python to combine two images and save the output.
🌐
TheAILearner
theailearner.com › 2019 › 03 › 18 › add-image-to-a-live-camera-feed-using-opencv-python
Add image to a live camera feed using OpenCV-Python | TheAILearner
September 3, 2019 - OpenCV has a built-in function that does the exact same thing as shown below · The idea is that first, we will select which image we want to overlay (another image will serve as the background).