bug in -average and -evaluate-sequence mean

Post any defects you find in the released or beta versions of the ImageMagick software here. Include the ImageMagick version, OS, and any command-line required to reproduce the problem. Got a patch for a bug? Post it here.
Post Reply
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

bug in -average and -evaluate-sequence mean

Post by fmw42 »

IM 6.6.1-5 Q16 Mac OSX Tiger.

I believe the algorithm for doing image averaging of more than two images in a sequence is incorrect. It looks like you are simply adding 0.5 time each image before adding to the previous result. The correct formula for sequential averaging is (1/i)*new + (1 - 1/i)*old, where i=number of image. So for two images the first two are combined as 0.5*firstimage + 0.5*secondimage=result2. But then for the third image it would be 0.333333*thirdimage + 0.666667*result2=result3. Then for the fourth image it would be .25*fourthimage + .75*result3=result4.

Here is an example with 4 images:
Image
Image
Image
Image

convert tmp1.png tmp2.png tmp3.png tmp4.png -average tmp_ave.png
Image

convert tmp1.png tmp2.png tmp3.png tmp4.png -evaluate-sequence mean tmp_mean.png
Image

See how each quandrant gets progressively brighter. That is incorrect.

The correct method is equivalent to:

convert \( tmp1.png -evaluate multiply 0.5 \) \( tmp2.png -evaluate multiply 0.5 \) -compose plus -composite tmpA.png
convert \( tmp3.png -evaluate multiply .333333 \) \( tmpA.png -evaluate multiply .666667 \) -compose plus -composite tmpB.png
convert \( tmp4.png -evaluate multiply .25 \) \( tmpB.png -evaluate multiply .75 \) -compose plus -composite tmp_truemean.png
Image

See how each quadrant is the same. This is correct.

I do not know if the old (pre-deprecated) -average worked correctly or not. But the current one that likely just uses -evaluate-sequence is incorrect. Perhaps check the old -average and see what it was doing.

If anyone is on IM 6.6.0.3 or earlier (before -average was deprecated to -evaluate-sequence mean), could you test -average as above and see what you get?
Last edited by fmw42 on 2010-05-07T14:34:48-07:00, edited 1 time in total.
rockcreekdan
Posts: 6
Joined: 2008-05-15T04:06:34-07:00

Re: bug in -average and -evaluate-sequence mean

Post by rockcreekdan »

For image sources:
convert -size 140x92 xc:gray -compose src-over \( rose: -geometry 70x46+0+0 \) -composite tl.png
convert -size 140x92 xc:gray -compose src-over \( rose: -geometry 70x46+70+0 \) -composite tr.png
convert -size 140x92 xc:gray -compose src-over \( rose: -geometry 70x46+0+46 \) -composite bl.png
convert -size 140x92 xc:gray -compose src-over \( rose: -geometry 70x46+70+46 \) -composite br.png

Averaged:
convert tr.png tl.png br.png bl.png -average png:- | display

The result was that each quadrant was the same.
Version: ImageMagick 6.3.7 06/04/09 Q16 http://www.imagemagick.org
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: bug in -average and -evaluate-sequence mean

Post by fmw42 »

thanks. that means that the old -average was implemented correctly but the new -evaluate-sequence mean is wrong as described above.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: bug in -average and -evaluate-sequence mean

Post by fmw42 »

I looked at the code in IM 6.5.9-10 (pre 6.6.0-4 when -evaluate-sequence mean deprecated the old -average) and -average accumulates the pixels from all images, then divides by the total number of images. This is a correct way to handle it. But it appears that -evaluate-sequence mean (and thus the deprecated -average using the same algorithm) is trying to do the sequential update approach as is doing it wrongly per my notes above.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: bug in -average and -evaluate-sequence mean

Post by fmw42 »

so how would your changes fix the multi-image -average problem (when more than two images, say 4 or more)?
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: bug in -average and -evaluate-sequence mean

Post by magick »

We can reproduce the problem you posted and will have a patch in the next point release of ImageMagick. Thanks.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: bug in -average and -evaluate-sequence mean

Post by fmw42 »

Thanks. This seems to be fixed now in IM 6.6.1-6 Q16 Mac OSX Tiger
Post Reply