Draw a transparent image ontop of another transparent image

Hi all! For quite some time I have been trying to find a way to compose two images, both with alpha channels, ontop of each other with a A over B approach - so the result I’m looking for would be equivalent with opening these two images in photoshop in two layers and save that as a png, or how for instance QPainter::drawPixmap in Qt works. I initially thought this would be fairly trivial to do in opencv, but have understood that there is no straight forward way to do this and I wonder if perhaps someone here would know how this could be done?

Thanks in advance :slight_smile:

some discussion: function for compositing two images that is aware of alpha-channel · Issue #20780 · opencv/opencv · GitHub
(I would not recommend using doubles for this, terribly expensive)

top voted question on SO, with some fair solutions, and a ton of garbage: python - Using openCV to overlay transparent image onto another image - Stack Overflow

In general, MOST code thrown onto the internet by people is BELOW acceptable quality standards.

Thanks for the quick reply! Yes, I have looked into both of those posts earlier and then especially the latter one which provided a few somewhat promising results, but not fully what I wanted - but I will revisit the examples again and see if I might have missed a possible solution there. If I’m allowed to dream, it would be awesome if the composition operation could be done using UMat instead of Mat too, since that’s what I’m using up until the point where I want to compose the images, to both be able to use the gpu and also to avoid conversion from UMat to Mat which I have realized is very expensive. But at this point I would be perfectly happy to have it working with Mat alone :slight_smile: