Besides the fact that it was a pain to find out how ImageMagick’s -average option is to be used, it turned out to operate wrongly. The switch calculates the mean of an image sequence, i.e. the mean color values for every pixel. Say, if you have a bunch of images with file names like img*.jpg and want the average saved into avg.jpg, the command line is:

$ convert img*jpg -average avg.jpg

Pretty intuitive. The problem is that *you* define the mean image *Ī*[*n*] of *n* images *I*[*k*] as

$url1%5Cdisplaystyle%5Cbar%20I%5Bn%5D%3D%5Cfrac%7B1%7D%7Bn%7D%5Csum_%7Bk%3D1%7D%5En%20I%5Bk%5D$url2\displaystyle\bar I[n]=\frac{1}{n}\sum_{k=1}^n I[k]$url3\displaystyle\bar I[n]=\frac{1}{n}\sum_{k=1}^n I[k]$url4

while *ImageMagick* does it recursively as

$url1%5Cdisplaystyle%5Ctilde%20I%5Bn%5D%3D%5Cfrac%7B%5Ctilde%20I%5Bn-1%5D%2BI%5Bn%5D%7D%7B2%7D$url2\displaystyle\tilde I[n]=\frac{\tilde I[n-1]+I[n]}{2}$url3\displaystyle\tilde I[n]=\frac{\tilde I[n-1]+I[n]}{2}$url4 (with *Ĩ*[1]=*I*[1]),

giving you wrong weights ^{1}⁄_{2n−k+1} like e.g. for *n*=8

instead of the intended weights ^{1}⁄_{n} like

.

This misbehaviour can be proven e.g. by taking this sequence of a plain blue, green and red image:

and averaging it with the above command. The result is too reddish:

The solution I found was to call convert recursively like this:

#!/bin/bash

i=0

for file in img*jpg; do

echo -n "$file.. "

if [ $i -eq 0 ]; then

cp $file avg.jpg

else

convert $file avg.jpg -fx "(u+$i*v)/$[$i+1]" avg.jpg

fi

i=$[$i+1]

done

By this, the average of the above example images correctly becomes gray:

There might be similar problems with the other image sequence operators, but I haven’t examined them. Maybe I should file a bug.

I love such writeups,short,simple! RT @paux: blogged: Averaging an image sequence with ImageMagick http://7ax.de/0xdq #imageprocessing #bug

Weblog:iamastifterTracked:May 02, 21:06Weblog:subik.atTracked:Jan 27, 10:35