Log in

View Full Version : Averaging multiple images with Avisynth - possible?


blubb444
22nd December 2012, 20:43
What I want to do is take very many static images (or frames from a video) and average them into one single image (denoising), I've been looking for months for a program/tool that can easily do it but yet haven't found anything that works well. All I found were some very cumbersome workarounds, like for example loading them all up in GIMP as layers and set their opacity to 100%/[number of images] (has to be done manually for them all :/) or stack Avisynth's Merge function several times (in that case the number has to be 2^n if I'm not mistaken and it takes too much time to write as well plus you'd probably get rounding errors)
I'm thinking of doing the following:
- Separate the incoming image into planes (Y, U, V or R, G, B)
- For each plane, create a 2D integer array (32bit should do, 16bit would limit the number of incoming images to 256) the size of the plane which simply adds up the pixel values of each incoming image
- Merge the three planes somehow to get a "raw 32bit" YUV/RGB... image and optionally save that to the hard disk for further editing
- Take the raw 32 bit image and convert it into an 8 bit array by dividing all the values by number of pictures and then rounding (or optionally dithering), then into suitable format of choice (PNG/BMP... (has to be converted to RGB beforehand of course, or is there a lossless image format that supports YUV?)), and export that to hard disk

Doesn't sound like a very complicated thing to do, but unfortunately I'm not deep enough into the "Avisynth language" to know how to create such array variables or even how to access single pixel values of a video, so could someone help out here?

(Oh and of course RAM could become a problem with very large files, for example a 20MP image without chroma subsampling would hit the 2GB so maybe I'd have to work with intermediate files)

Asmodian
22nd December 2012, 21:14
You might try the Average (http://forum.doom9.org/showthread.php?t=118430) plugin.

I like using Median (http://forum.doom9.org/showthread.php?p=1169990#post1169990) more when de-noising video using multiple captures but it might be different for high resolution images.

jmac698
23rd December 2012, 07:18
I've had the same problem, it turns out that astrophotography software does this, practically any one will do. Try deepskystacker, if you're good at guessing you can probably get it done in a few minutes.

I also asked to do it in avisynth and someone wrote a script, I'll have to try to find it.

There's a difference in noise reduction between median and mean. For median it's sqrt(2n/pi) for mean its sqrt(n). For 9 images that's 3 and 2.4 (3 being better, from averaging, and that's times reduction). So mean is better for normal guassian noise. However if there's impulse noise (like dropouts), then some median is better. You can average a bunch of small medians in combination to get the equivalent of both.

edit I wrote a plugin just for this http://forum.doom9.org/showthread.php?p=1581666#post1581666
it sucks though, I think rt_stats has that functionality now.

blubb444
24th December 2012, 15:22
Thanks so far, I'll eventually give your suggestions a try.
But about the difference between "median" and "mean".. I thought median meant sorting an (odd-numbered) set of values and use the middle one while mean is simply adding up all values of the set and dividing it by their number, for example the following set:
{1, 1, 1, 3, 4} would have a median of 1 (=the third-biggest/smallest value) and a mean of 2 (=10/5), no?
EDIT: Ooops, nevermind, I guess you were talking about the amount of noise it reduces in which case it makes sense...

jmac698
24th December 2012, 20:51
That's true, and for even amounts I think median can be defined where the two middle values are averaged. Also when I talked about combining the two, what you can do is sort the values, throw out the lowest and highest, then average the rest. This would get rid of any specific noise that is quite black or white, relatively. Or you can find the standard deviation of the values and throw out anything that's beyond a certain standard deviation, and average the rest, or average them all but switch to a different mode if one looks extreme.

Using some form of median will reduce white lines, called dropouts, in VHS, or gamma rays, which are high energy radiation from distant stars that normally pass through us, but light up camera pixels bright white occasionally.

As for combining them both, you could use the median2 script (search for g-force helpers median2) but in combinations like t1=median2(i1,i2,i3) t2=median2(i1,i2,i4) t3=median2(i1,i3,i4) etc. then average(t1,t2,t3)

As far as DeepSkyStacker, click on Open Picture Files, open up a directory full of jpg's, click Check All, click Stack Checked Pictures.
Click under Alignment Method, select No Alignment. For stacking mode select standard. No background calibration. Stacking mode Average.
Click Go, click Save Picture to File.

blubb444
8th February 2013, 01:48
Just wanted to let anyone interested in the subject know that I've now found a neat tool that does most of what I want: http://code.google.com/p/imagestack/