Combining images to make a composite image

15 views (last 30 days)
N B on 15 Aug 2018
Answered: DGM on 19 Jun 2022
I have created five heat maps using some custom colour maps. They look like this:
I would now like to combine them to make a composite image, ideally something that looks a little like the right hand side of this after all five images have been added:
Any help about how to get started on the problem would be most welcome.
Does this overlaying of images have a formal name? Searching online yielded no useful answers, maybe I'm using the wrong search terms.
N B on 22 Aug 2018
This works very nicely for two images, thanks. But I cannot get it to work for more than two images - ideally i'd like up to five.

Sign in to comment.

Answers (2)

Image Analyst
Image Analyst on 16 Aug 2018
Do they all have mutually exclusive pixels? That is no pixels are colored by more than one color map? If so, just convert to rgb using ind2rgb() and then add them all together.
Image Analyst
Image Analyst on 22 Aug 2018
You can still average them together, you'll just get blended/mixed colors.

Sign in to comment.

DGM on 19 Jun 2022
There are a number of ways to do this. As usual, I'm going to generalize a bit since I doubt OP is the one who will see this answer.
I'm going to make a few assumptions.
  • The images are already given. I'm not going to consider possible methods to construct the final image directly from the original data.
  • The images consist of colored content against a black background. Each image is represented by one hue.
  • The non-black regions may overlap (stated).
  • The images are all the same size.
The task is then one of image blending/compositing. As usual, I'm going to use some MIMT tools for certain tasks.
There are a number of simple blending approaches that can be used. Any blend mode with a neutral response at FG=0 might be something to consider. Consider the simple blend tests with the following step swatches:
%% simple color blending
fg = imread('fgswatch.png');
bg = imread('bgswatch.png');
%outpict = imblend(fg,bg,1,'screen');
%outpict = imblend(fg,bg,1,'addition');
%outpict = imblend(fg,bg,1,'lightenrgb');
outpict = imblend(fg,bg,1,'lighteny');
screen addition
lighten rgb lighten y
The takeaway is that there are lots of ways to combine images, but each method has different properties that can be either advantageous or counterproductive depending on the context and expectations. These example modes are commutative, so the layer order has no influence over the degree to which a given layer is visible in the output. The mixing of the different colors may produce intermediate hues that would make reading the results ambiguous. The hard masking behavior of 'lighten y' (equivalent to Photoshop's 'lighter color') prevents color mixing, but it produces new edge features and obscures the underlying content.
We can apply these blending methods to a more complicated set of images. Consider the following set of four images:
% read all four demo images
% this could also just be done with a loop
B = mimread('Bdemo_*.png'); % read into a cell array
B = imstacker(B,'padding',0); % stack on dim4
%outpict = mergedown(B,1,'screen');
%outpict = mergedown(B,1,'addition');
%outpict = mergedown(B,1,'lightenrgb');
%outpict = max(B,[],4); % same as lightenrgb for default parameters (not shown)
outpict = mergedown(B,1,'lighteny');
lighten rgb
lighten y
The differences may be subtle, but they may matter. Whether any of these is appropriate is a decision for the reader. I imagine that 'screen' and 'lightenrgb' are the most plausible, but needs differ.
Compositing or alpha blending might suggest another set of possible methods. Again, consider some swatch tests. If you were to do a simple scalar alpha blend, you'd get an overly dark image. This is simply average the images, and you'd be skewing the average by incorporating a bunch of black background content. The result would likely look dark and muted.
%% naive scalar opacity blend
% this is obviously garbage due to the black bg muting everything
fg = imread('fgswatch.png');
bg = imread('bgswatch.png');
% compose output image
outpict = imblend(fg,bg,0.5,'normal');
naive alpha blending
The contrast of the result can be stretched if desired, but there may not be much overhead before clipping occurs.
% compose output image
outpict = imblend(fg,bg,0.5,'normal');
outpict = im2uint8(mat2gray(outpict)); % adjust contrast
adjusted naive alpha
Lastly, a more complex approach can be taken. As the background is a solid color (black), it's possible to eliminate it. In that way, only the non-black content is being blended. In this example, MIMT color2alpha() is used to convert black to alpha content. The rest is simple composition as before. It should be noted that this method is not commutative. The layer order will directly influence which image content will be present in overlap regions.
% compose output image
fg = color2alpha(fg,[0 0 0]); % convert FG to an RGBA image
outpict = replacepixels(fg,bg,1); % this is an RGB image
As before, these methods can be applied to our four random test images:
% read all four demo images
B = mimread('Bdemo_*.png'); % read into a cell array
B = imstacker(B,'padding',0); % stack on dim4
% naive uniform opacity blend (averaging)
%outpict = cast(mean(B,4),class(B));
% same, but with contrast adjustment
%outpict = imcast(mat2gray(mean(im2double(B),4)),class(B));
% composition of color content only
outpict = B(:,:,:,1); % base layer is the first frame
for f = 2:size(B,4)
fg = color2alpha(B(:,:,:,f),[0 0 0]); % this is an RGBA image
outpict = replacepixels(fg,outpict,1);
naive scalar alpha
adjusted scalar alpha
As mentioned before, most of these examples are based on MIMT tools. MIMT is available on the File Exchange.
This thread is a similar exploration of light-on-black multilayer blending/composition.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!