bug in -average and -evaluate-sequence mean
Posted: 2010-05-03T11:02:02-07:00
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:
convert tmp1.png tmp2.png tmp3.png tmp4.png -average tmp_ave.png
convert tmp1.png tmp2.png tmp3.png tmp4.png -evaluate-sequence mean tmp_mean.png
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
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?
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:
convert tmp1.png tmp2.png tmp3.png tmp4.png -average tmp_ave.png
convert tmp1.png tmp2.png tmp3.png tmp4.png -evaluate-sequence mean tmp_mean.png
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
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?