How to combine multiple images with different size as one?

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

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

Dear Joseph,
Thanks for your helpful response, it is very useful. However, in this case a pre-knowledge of all image sizes is needed since the predefined IT could generate a huge black blank space if the two first arguments of zeros be very different from the size of images (e.g. suppose we have 200 different images, which the larges X-dim among them is 1200, and we supposedly assign the first argument of zeros (for IT) 2000). The problem here is that we have no prior knowledge about image sizes.
Best regards,
well since you know your file names the imageinfo() function can be used to quickly go through and find the maximum dimensions and pad accordingly.
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.
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)

Asked:

on 14 May 2015

Commented:

on 15 May 2015

Community Treasure Hunt

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

Start Hunting!