Skip to content
Advertisement

OpenCV Composting 2 images of differing size

I need to alpha-blend 2 images that are not the same size. I’ve managed to get them to composite by resizing to the same size, so I’ve got part of the logic:

JavaScript

I’m wondering if I need to make a mask that is the same size as the larger image and then use that with my first image. If so, I don’t know how to do masking yet in OpenCV…. this is but a tiny portion of my project so it’s not something I’ve been able to spend a ton of time researching to learn how masking works.

I have searched all over but the code I’m finding does things like ‘adds’ the images together (side by side).

Advertisement

Answer

To combine the two images you can make use of numpy slicing to select the portion of the background image where you want to blend the foreground, then insert the newly blended portion in your background again.

JavaScript

edit: To place the foreground at a specified location you use numpy indexing as before. Numpy indexing is very powerful and you will find it useful on many occasions. I linked the documentation above. Is really worth to take a look at.

JavaScript
Advertisement