Superimposing three images of different sizes

11 views (last 30 days)
Linde
Linde on 3 Mar 2014
Answered: DGM on 10 Apr 2022
I am trying to superimpose three (or more) images. However, the image (/matrix) sizes are not the same, which is fine when using imfuse for two images. However, when using
overlay_im = cat(3, C, A, B); imshow(overlay_im);
this is a problem. (Error message: Dimensions of matrices being concatenated are not consistent.)
Is there any other way to superimpose the images?
  1 Comment
Linde
Linde on 3 Mar 2014
I was thinking of adding columns/rows with value zero to the matrices where needed to give them the same dimensions, but I'm not sure how to do this? A better idea would also be appreciated.

Sign in to comment.

Answers (2)

Image Analyst
Image Analyst on 3 Mar 2014
Exactly where in the larger image do you want to place the smaller image?
  2 Comments
Image Analyst
Image Analyst on 4 Mar 2014
Just index it in there. For example to put into the red channel
[rows, columns, numberOfColorChannels] = size(smallGrayImage);
bigRGBImage(1:rows, 1:columns, 1) = smallGrayImage;

Sign in to comment.


DGM
DGM on 10 Apr 2022
Usually when overlaying images as with imfuse(), the purpose is to compare differences in local object content. If the images are not the same size, then it's unclear how the object content should be colocated. Determining that is an exercise for the reader.
Assuming the following:
  • there are exactly three images
  • the images are all single-channel images
  • images are not to be rescaled
  • images are to be centered within the output geometry
  • images are to be stacked on dim3 to form a false RGB image
then the result can be realized with a bunch of padarray() tedium:
A = imread('cameraman.tif');
B = imread('tire.tif');
C = imread('pout.tif');
% calculate output geometry and required padding
sz = [size(A,1) size(A,2);
size(B,1) size(B,2);
size(C,1) size(C,2)];
smax = max(sz,[],1);
padsz = (smax-sz)/2;
% pad each image with black to center it
A = padarray(A,ceil(padsz(1,:)),0,'pre');
A = padarray(A,floor(padsz(1,:)),0,'post');
B = padarray(B,ceil(padsz(2,:)),0,'pre');
B = padarray(B,floor(padsz(2,:)),0,'post');
C = padarray(C,ceil(padsz(3,:)),0,'pre');
C = padarray(C,floor(padsz(3,:)),0,'post');
% concatenate the images to form a false RGB image
ST = cat(3,A,B,C);
% so long as there are three images, the result is handled as an RGB image
% if there were more images, the result would not be compatible with imshow()
imshow(ST)
Or you can do the same thing with MIMT imstacker() in two lines:
A = imread('cameraman.tif');
B = imread('tire.tif');
C = imread('pout.tif');
% stack images on dim4, with central gravity and black padding
ST = imstacker({A,B,C},'padding',0);
% if the images are single-channel, then it can be reduced to a 3D array
ST = squeeze(ST);
% since there are three images, the result is compatible with imshow()
imshow(ST)
The result being identical to above.
When feature alignment is unimportant, MIMT imstacker is generally more convenient. It allows simple specification of padding color, alignment gravity, and fitting methods.
All gravity options with default 'max' size and 'rigid' fit (union of geometries):
All gravity options with 'min' size and 'rigid' fit (intersection of geometries):
... and so on.
If it's desired to do this with more than three images, or with images which are not single-channel, then this falsecolor visualization method simply is not an option. There are still other options in blending or composition, but one would have to decide what the goals are, as simply overlaying a bunch of things is going to rapidly turn into incomprehensible colored garbage.

Community Treasure Hunt

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

Start Hunting!