random from a distribution using fx

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
hwttdz
Posts: 22
Joined: 2010-08-29T05:29:08-07:00
Authentication code: 8675308

random from a distribution using fx

Post 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?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: random from a distribution using fx

Post by fmw42 »

hwttdz
Posts: 22
Joined: 2010-08-29T05:29:08-07:00
Authentication code: 8675308

Re: random from a distribution using fx

Post 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.
Post Reply