From: Pinpress on
Hi,

I know I can put multiple images as "layers" in Photoshop, and then set the transparent value for each layer so that I can have a "fused" composite image. I just wonder how I can achieve the same from within Matlab (because these images are obtained in Matlab in the first place). Any good way to do this?

Thanks very much.
From: Walter Roberson on
Pinpress wrote:

> I know I can put multiple images as "layers" in Photoshop, and then set
> the transparent value for each layer so that I can have a "fused"
> composite image. I just wonder how I can achieve the same from within
> Matlab (because these images are obtained in Matlab in the first place).
> Any good way to do this?

The code is pretty simple:

compimage = zeros(size(layers(:,:,:,1));
for L = 1:size(layers,4)
compimage = compimage * (1-alpha(L)) + alpha(L) * layers(:,:,:,L);
end

You can optimize this a bit in some cases: if alpha is 1 for any layer,
then all layers below that will be invisible, so you can search for the
last alpha == 1 and use that layer as your initial compimage and only
process the things above that. Also, you can skip processing any layer
which has an alpha 0 as it will not add anything to the image.