-1

I am trying to use OpenCV to add an image onto another.

I want to be able to combine this:

enter image description here

and this:

enter image description here

(note the circle has a transparent background) to create this:

enter image description here

Unfortunately, all of the solutions I've found online just give me this:

enter image description here

I've looked at at least 10 different on this sight solutions but I cannot find a working one.

Please advise. Thanks!

Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
  • duplicate of various questions on "compositing", "blending", "overlaying" etc – Christoph Rackwitz Dec 31 '21 at 21:18
  • Does this answer your question? [Using openCV to overlay transparent image onto another image](https://stackoverflow.com/questions/40895785/using-opencv-to-overlay-transparent-image-onto-another-image) – Christoph Rackwitz Dec 31 '21 at 21:19
  • @ChristophRackwitz The post linked does not answer this question. They are different problems. – Red Dec 31 '21 at 21:21
  • yes it does. explain why you assert that it doesn't. the linked post clearly shows how to composite one picture containing alpha channel (transparency) onto another, and this question is precisely that. – Christoph Rackwitz Dec 31 '21 at 21:30
  • @ChristophRackwitz Please link me the answer from the post you linked that you think solves this problem, and I'll elaborate. – Red Dec 31 '21 at 21:30
  • this https://stackoverflow.com/a/59211216/2602877 or this https://stackoverflow.com/a/54058766/2602877. I checked them both. why are you insisting? I'm not asking you to explain why I'm wrong. I'm asking you to figure out where you are mistaken. – Christoph Rackwitz Dec 31 '21 at 21:31
  • @ChristophRackwitz Thanks for the links. I've checked them, and they explain the OP's note: *"Unfortunately, all of the solutions I've found online just give me this:"*. They do produce the [image the OP is getting](https://i.stack.imgur.com/LmDaB.png), not [what the OP is aiming for](https://i.stack.imgur.com/dsJGo.png). – Red Dec 31 '21 at 21:36
  • And please, I'm not saying anyone's wrong here (sorry if I come across as such). I know we are all trying to help out the OP and this site. – Red Dec 31 '21 at 21:40
  • OP hasn't seen that question or the answers, so OP's assertion is irrelevant. the two answers I linked produce OP's desired output. why can't you see that? why do you insist that this *question* _isn't_ a duplicate when it is, and why do you not see that at least two of the answers on that question *are* answers to this question? do you think I don't understand what an alpha channel is supposed to do? I wrote this for a reason: https://github.com/opencv/opencv/issues/20780 – Christoph Rackwitz Dec 31 '21 at 21:41
  • @ChristophRackwitz No, I tested out the answers, and they did not produce the desired output on my machine. – Red Dec 31 '21 at 21:44
  • then you must have made a mistake (I hope it's that). I checked both solutions against the data in this and the other question, and the results are as OP desires, with minor issues (data type conversion). – Christoph Rackwitz Dec 31 '21 at 21:50
  • @ChristophRackwitz Thanks, I'll have another look. But in the meantime, let's wait for the OP to respond and see if the linked post solves their problem, and if not, why. – Red Dec 31 '21 at 21:53
  • Please provide enough code so others can better understand or reproduce the problem. – Community Jan 08 '22 at 20:15

1 Answers1

1

Here is how you can use a simple mask:

import cv2

background = cv2.imread("background.png")
circle = cv2.imread("circle.png", cv2.IMREAD_UNCHANGED)

mask = circle[..., 3] != 0
background[mask] = circle[..., :3][mask]

cv2.imshow("Image", background)
cv2.waitKey(0)

Output:

enter image description here

Red
  • 26,798
  • 7
  • 36
  • 58