6

How can i overlay two images without losing the intensity of the colors of the two images.

I have Image1 and Image2:

  1. enter image description here 2. enter image description here

I tried using 0.5 alpha and beta but it gives me a merged image with half the color intensity

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

enter image description here

but when i try using 1 to both alpha and beta channels, it only gives me the merged regions.

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

enter image description here

I have to get an output that looks like this.

enter image description here

3
  • Can you add your original image too? Commented Aug 31, 2016 at 12:55
  • i added original image1 and image2 Commented Aug 31, 2016 at 13:14
  • In BGR, purple(255, 0, 255) = blue(255, 0, 0) + red(0, 0, 255) Commented Oct 30, 2018 at 8:46

1 Answer 1

6

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:

enter image description here

Sign up to request clarification or add additional context in comments.

6 Comments

dst = img1 + img2 should work well after setting the white to black, and it's easier / shorter
@Miki Indeed, I just wanted to give a general answer, specially close to OP's intend.
Thank you so much! but how can i set the black back to white on dst? thank you so much!
@MonTolentino There is no black in dst any more. It's blue, purple and red. Because you mixed the images.
Ok i did it using img[np.where((img == [0,0,0]).all(axis = 2))] = [255,255,255]
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.