Page 1 of 1

random from a distribution using fx

Posted: 2011-04-02T07:46:05-07:00
by hwttdz
I'm trying to compose an fx operation where

output value = max( 0, input value + value from normal distribution with sigma)

and I see I can get a random number, but I'd rather not make some crazy expression to go from uniform [0,1] random to normal random, besides doing so might be slow in imagemagick. Is there another way?

Re: random from a distribution using fx

Posted: 2011-04-02T10:53:56-07:00
by fmw42

Re: random from a distribution using fx

Posted: 2011-04-02T11:02:22-07:00
by hwttdz
What I arrived at (with linebreaks to improve readibility) is:

-fx 'iso=32;
rone=rand();
rtwo=rand();
myn=sqrt(-2*ln(rone))*cos(2*Pi*rtwo);
myntwo=sqrt(-2*ln(rtwo))*cos(2*Pi*rone);
pnoise=sqrt(p)*myn*sqrt(iso)*channel(4.28,3.86,6.68,0)/255;
max(0,p+pnoise)'

So you can see, while I needed a gaussian distribution, I'm adding noise that is proportional to the gaussian. Of course if I can rework this using image multiplication with a random image it will go more quickly (do you see an easy way to do that, I worry about the noise values being truncated if they go below zero). The Box-Muller is definitely the way to go here. What I'm doing it reproducing the noise I observe in my camera at high iso's so that I can test a few different noise removal protocols.

Or rather this was my first attempt. I now have a mix of shot noise and random noise because I feel it better approximates my noise of my sensor.