Problem: Composite blend of 1000s of JPGs

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
blaklite
Posts: 2
Joined: 2013-02-12T16:44:32-07:00
Authentication code: 6789

Problem: Composite blend of 1000s of JPGs

Post by blaklite »

Hi all. I'm just starting to get my feet wet with ImageMagick and so far I am very pleased.

However, I started with ImageMagick with one purpose in mind: I wanted to create composites of 100s or 1000s of JPGs at once. In theory this should be pretty simple, but in practice I'm having a lot of trouble. I've searched the forums and found a few examples of different methods, but most of them are considering a dozen or two JPGs, not 1000s.

The problem I've run into is that I run out of memory before the process can complete. I have tried using

Code: Select all

composite *.jpg -blend
and

Code: Select all

convert *.jpg -average
but I end up running out of memory either way. I have 4 gigs of RAM and I have my swap drive set to a fast SSD, but I still exhaust my resources.

In my mind, I don't feel like this operation should require much memory. If you figure that an image is a 3-dimensional matrix (width, height, color x 3 bytes), then a 5 megapixel image should be 5,000,000*3 => 15 MBytes. So loading one raw image should take 15 megs. However, the images don't need to be loaded and stored separately. Instead, we would load the next image and just add the individual pixel values to the first image. Then unload the most recent image, load the next, repeat the process and so on. Eventually you would end up with the sum of all the images pixel values in the 3-D matrix. Finally, divide the individual matrix values by the total number of images and voila! Now you have a composite image.

<Warning: math follows>Using this method, the memory required for 1000 summed images would be 5,000,000*7 => 35 MBytes. We multiply by 7 instead of 3 since 2^18=262144 which is enough to store the maximum pixel intensity (for each R,G,B) of 255 for all 1000 images. 18 bits times 3 (for R, G, and B) gives 54 bits total for each pixel. 7 Bytes are required to provide 54 bits of data.</warning>

So, to me it seems that this type of operation should be very lightweight memory-wise. So that means that either I'm not using the best ImageMagick method, or there is room for some significant optimization in the code.

I'm hoping that someone here with more experience can tell me where (or if) I'm going wrong.

Thank you in advance.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Problem: Composite blend of 1000s of JPGs

Post by fmw42 »

If all you want to do is average a lot of images together, then using a running average technique. See viewtopic.php?f=1&t=21279&p=86721&hilit ... age#p86721

That way you are only average the last average with the next image, so only two or so images are in use at any time.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Problem: Composite blend of 1000s of JPGs

Post by anthony »

The alternative is a batch averaging. Average groups of equal size, then average those. If they are not equal size (like an odd final group) then the final average has a weighting issue.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply