Script to average images

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?".
Woody
Posts: 8
Joined: 2012-06-24T20:47:59-07:00
Authentication code: 13

Script to average images

Post by Woody »

Hi.

After some searching I've found this forum. What I want to do is take a series of images eg 1.jpg thro 999.jpg and average them:

1 thro 5
2 thro 6
3 thro 7

and so on.

I figure the script would use a command such as

convert -average P2.JPG P2.JPG P3.JPG P4.JPG P5.JPG new1.jpp

but I don't know enough about scripting to get this working, nor am I super familiar with ImageMagick!

Even better would be if the images were weighted during the calculation.

The hint say to post version: Version: ImageMagick 6.5.7-8 2012-04-30 Q16 http://www.imagemagick.org

Thanks

Woody
User avatar
whugemann
Posts: 289
Joined: 2011-03-28T07:11:31-07:00
Authentication code: 8675308
Location: Münster, Germany 52°N,7.6°E

Re: Script to average images

Post by whugemann »

And your OS is ... (?)
Wolfgang Hugemann
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Script to average images

Post by fmw42 »

The current command is

convert image1 ... imageN -evaluate-sequence mean result

But that would require you to have enough memory to hold all 999 images in memory, which sounds impractical.

The best way would be to do a loop over each image and do a running average.

It would be something like this in unix

numimages=4
convert 1.jpg tmpA.mpc
i=1
while [ $i -lt $numimages ]; do
j=`expr $i + 1`
echo "i=$i; j=$j"
convert $j.jpg tmpB.mpc
#compute running average percentages
new=`convert xc: -format "%[fx:100/$j]" info:`
old=`convert xc: -format "%[fx:100-$new]" info:`
composite -blend $old%x$new% -gravity center tmpA.mpc tmpB.mpc tmpA.mpc
i=`expr $i + 1`
done
convert tmpA.mpc result.jpg
rm tmpA.mpc tmpA.cache tmpB.mpc tmpb.cache



If you just want to average over 5 images at a time then

convert image1 ... image5 -evaluate-sequence result

should suffice

If you need to do that shifting the image by 1 each time, then write a loop that would take the first 5 and average and output, then on the next iteration of the loop increment each image by 1.

totnum=999
seqnum=5
num=$((totnum-seqnum))
i=1
while [ $i -le $num ]; do
i1=$i
i2=$((i+1))
i3=$((i+2))
i4=$((i+3))
i5=$((i+4))
convert $i1.jpg $i2.jpg $i3.jpg $i4.jpg $i5.jpg -evaluate-sequence mean result$i.jpg
i=$((i+1))
done
Last edited by fmw42 on 2012-07-09T12:07:03-07:00, edited 1 time in total.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Script to average images

Post by anthony »

I did something similar but only composing two images.

I read in one list, added an 'blank' image on the end, opened parenthesis, cloned that list and insert that added image on the end to the beginning, then inserted a "null:" seperator image before closing parenthesis.

At this point you have two lists seperated by a "null:" image with the second list 'rolled' or displaced by one image.

You can now use -layers composite to merge the two lists using some "compose" method (like a 50% blend)

The result is each image averaged with the next, with the ends averaged with a 'blank' image, and all in one IM command.

But only two images have been merged together. With more work you can average more images together, but only two images at a time.

I have used this to generate images with 'symbols' indicating how the next and previous 'images' relates to the current image.

See -layers composite (compose two lists of images together in sequence)
http://www.imagemagick.org/Usage/anim_mods/#composite
YES this is in the Animations Area of IM examples, but animations are just lists of images!!!!
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Woody
Posts: 8
Joined: 2012-06-24T20:47:59-07:00
Authentication code: 13

Re: Script to average images

Post by Woody »

been offline for a couple days: OS is OSX, can do 10.6, 10.6 and 10.7
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Script to average images

Post by fmw42 »

The scripts I wrote above (untested for the most part) should work fine on Mac OSX. I do my devepment on Mac OSX Snow Leopard. But the scripts should work on any unix system.
Woody
Posts: 8
Joined: 2012-06-24T20:47:59-07:00
Authentication code: 13

Re: Script to average images

Post by Woody »

thanks!

Whats the best numbering scheme eg 1.jpg 2.jpg 3.jpg etc?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Script to average images

Post by fmw42 »

Woody wrote:thanks!

Whats the best numbering scheme eg 1.jpg 2.jpg 3.jpg etc?

For an average it does not matter what order the images are in unless you need to see intermediate results and need them averaged in a special order. If you want you can add leading zeroes to your numbers to get them to be processed in order; otherwise, 1 and 10 are next to each other.
Woody
Posts: 8
Joined: 2012-06-24T20:47:59-07:00
Authentication code: 13

Re: Script to average images

Post by Woody »

I created a script from your code:

Code: Select all

#!/bin/bash
totnum=14
seqnum=5
num=$((totnum-seqnum))
while [ $i -le $num ]; do
i1=$i
i2=$((i+1))
i3=$((i+2))
i4=$((i+3))
i5=$((i+4))
convert $i1.jpg $i2.jpg $i3.jpg $i4.jpg $i5.jpg -evaluate-sequence mean result$i.jpg
i=$((i+1))
done
saved it as image_averager.sh

gave it execute permission i.e. chmod u+x

when I run the script I get

image_averager.sh: line 5: [: le: unary operator expected

Any ideas why? This is likely a noob question.... I am a noob :-)
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Script to average images

Post by anthony »

Before the while add

Code: Select all

  i=0
or perhaps i=1 depends on what number you images start at.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Woody
Posts: 8
Joined: 2012-06-24T20:47:59-07:00
Authentication code: 13

Re: Script to average images

Post by Woody »

ahhh thank you!

OK, follow on question: can ImageMagick weight the images i.e. the highest numbered contributes say 50%, then the next 25%, then 12.5%, and then 2 lots of 6.25% (i.e. to give 100%) ?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Script to average images

Post by fmw42 »

see -evaluate multiply xx http://www.imagemagick.org/script/comma ... 2#evaluate

you will need to precompute for each numbered image what factor you want in some list or array. Then put each image in your average in parens \( x.jpg -evaluate multiply $xfactor \)

see
http://www.imagemagick.org/Usage/basics/#parenthesis
Woody
Posts: 8
Joined: 2012-06-24T20:47:59-07:00
Authentication code: 13

Re: Script to average images

Post by Woody »

The script runs fine on OSX / Lion, trying it on Linux / Ubuntu, but its not working, I get

convert: unrecognized option `-evaluate-sequence' @ convert.c/ConvertImageCommand/1347.

Any ideas?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Script to average images

Post by fmw42 »

your Linux version of IM is probably too old for the -evaluate-sequence mean (introduced about 6.6.0-4). try -average on the older systems.
Woody
Posts: 8
Joined: 2012-06-24T20:47:59-07:00
Authentication code: 13

Re: Script to average images

Post by Woody »

that works - thanks!

BTW In case you are wondering what I'm doing, I will share with you when its working end-end :-)
Post Reply