OpenCV Java: how to superimpose three images that have the same size?

In my Android project, I have 3 bitmaps (with transparency). They have the same size.

I want to superimpose these 3 bitmaps.

I wonder how to do that (in the fastest way).

public Bitmap fusionBitmaps(Bitmap bmp1, Bitmap bmp2, Bitmap bmp3)

// OpenCV code (Java if possible) to do that ?

return fusionBitmap;


Thanks !

1 Like

that’s a common task, yet OpenCV hasn’t grown any procedures for dealing with it easily.

you said you’re on Android. Android probably has methods for alpha-blending/compositing pictures. getting the GPU to composite your pictures is probably the cheapest way. it certainly is if you only need to display the composite, but also if you need to continue working with the composite (save it or whatever).

math basics: you have two pictures, A as the base, B being the overlay (on top). B’s alpha channel determines, per-pixel, how much of a pixel’s color comes from B (and the remainder/complement comes from A), so:

C = B * alpha + A * (1 - alpha)

if alpha values were in the range of 0…1. if they’re in the range of 0…255, you’d need an appropriate division to finish it off, and wide enough data types in between (16 bits at least).

if you had to use OpenCV for this, it might involve cv::split() to isolate color and alpha channels, and some basic math operations on whole Mat objects.

someone else’s writeup: