Page 1 of 1

bug in -average and -evaluate-sequence mean

Posted: 2010-05-03T11:02:02-07:00
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?

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

Posted: 2010-05-04T05:34:10-07:00
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

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

Posted: 2010-05-04T11:32:19-07:00
by fmw42
thanks. that means that the old -average was implemented correctly but the new -evaluate-sequence mean is wrong as described above.

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

Posted: 2010-05-04T19:03:01-07:00
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.

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

Posted: 2010-05-05T17:17:45-07:00
by fmw42
so how would your changes fix the multi-image -average problem (when more than two images, say 4 or more)?

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

Posted: 2010-05-07T06:31:20-07:00
by magick
We can reproduce the problem you posted and will have a patch in the next point release of ImageMagick. Thanks.

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

Posted: 2010-05-07T14:31:28-07:00
by fmw42
Thanks. This seems to be fixed now in IM 6.6.1-6 Q16 Mac OSX Tiger