Running out of memory using imfuse repeatedly

2 views (last 30 days)
I want to iterate into a loop and based on the i'th of a vector, i want to concatenate some specific picture to the last picture. The loop has to be iterated 16 time so at the end I expect to see an image which consists of 16 images. But I get the Running out of Memory error.
I thought the problem is with the loop so I manually copied the code. But the error still exists. Below you can see a piece of code. "addGen" is a function which generates the address of the image which needs to be added and "chars" is some vector. Is there any alernative to do so or any solution to solve the memory problem?f
image = imfuse(image,imread(addGen(chars(3))),'montage');
image = imfuse(image,imread(addGen(chars(4))),'montage');
image = imfuse(image,imread(addGen(chars(5))),'montage');
image = imfuse(image,imread(addGen(chars(6))),'montage');
image = imfuse(image,imread(addGen(chars(7))),'montage');
image = imfuse(image,imread(addGen(chars(8))),'montage');
image = imfuse(image,imread(addGen(chars(9))),'montage');

Accepted Answer

DGM on 18 Jun 2022
Edited: DGM on 18 Jun 2022
The reason you're running out of memory is simple. Whenever imfuse() concatenates two images using the 'montage' option, the result is twice the width of the widest image. Consequently, the array size doubles every iteration. Assuming each image is the same size, the output will be 65535 times as wide as the input images.
How do you solve the problem? Don't use imfuse() for general-purpose concatenation or image blending/composition. It's a simple tool for comparing two images.
If the images are the same height, they can be simply concatenated using basic array concatenation []. If they need to be padded, then you need to decide how much each side needs to be padded for the alignment to be appropriate for your needs. Use padarray()/imcrop()/imresize() to make the image geometries compatible as you see fit.
You could also read all the images into a cell array and then use imtile(), but you'll ultimately have less control over the behavior.

More Answers (1)

Jan on 15 May 2021
"Out of memory" means, that your RAM is exhausted. The efficient solution is to install more RAM and to care for freeing as much memory as possible, e.g. by clearing other variables, which are not used anymore or to stop other applications.
How large are the images? How much RAM is available on your computer?
  1 Comment
Amir mo
Amir mo on 15 May 2021
4 Gygabyte ram and the pictures are really small 32 KBs bitmaps

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!