How to combine multiple images with different size as one?

12 views (last 30 days)
Dear All, while the title seems very common, I cannot figure it out yet. My problems is as following: I should read multiple images from net (e.g. using imread()) in a loop, and after doing some modification on each image(e.g. using insertText()), display them together as a single image. For instance,
n = length(LinkNames);
for ci=1:n
I=imread(LinkNames{ci});
I=insertText(I,[10,10], 'Something');
IT(:,:,:,ci)=I;
end
montage(IT)
This works if all images are the same size, unless montage produces a common error, because simply it fails to when images are not the same size (apart from the error in IT). Apparently, there are various ways ranging from subimage, subplot, or user defined functions such as, imdisp. But this is not satisfactory, because subplot and subimage generate some margins and the final single image is not very neat. Another way, is to save images with print(); in this way all images (originally with different size) will be saved with same size. However, print demands the images to be displayed (which again is not favorable sometimes, because user may not want to share each figure with others). Therefore, i will deeply appreciate if anyone could assist me with this problem.
Thanks in advance.

Accepted Answer

Joseph Cheng
Joseph Cheng on 14 May 2015
Edited: Joseph Cheng on 14 May 2015
you can zero pad your images so they are all the same size
IT = zeros(256,256,1,25);
for ind= 1:25
rows = 128+randi(128);
columns = 128+randi(128);
IT(1:rows,1:columns,1,ind)= randi(255,rows,columns);
end
cmap = colormap(gray);
montage(IT,cmap)
you probably want to center the images differently but i'll leave that to you
  4 Comments
Joseph Cheng
Joseph Cheng on 14 May 2015
Additionally since i'm specifically identifying the index locations of 1:row and 1:col of the loaded image it'll automatically fill in the extra spots with 0's.
clear IT
IT(:,:,1) = magic(3)
for ind= 2:5
rows = 5+randi(10);
columns = 5+randi(10);
IT(1:rows,1:columns,1,ind)= randi(255,rows,columns);
end
IT(:,:,1)
as you can see the first one has been adjusted to match the size of the max.
Ive J
Ive J on 15 May 2015
Well, I found it very useful, and deeply appreciate your help. Nevertheless, it seems that MATLAB should improve these little but noisy limitations within its image related functions.
Best regards,

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!