Re: How to add 5% possion noise for a image in IDL [message #83994] |
Fri, 12 April 2013 06:40  |
dplatten
Messages: 32 Registered: December 2007
|
Member |
|
|
I took what Wayne posted and adapted it a bit. The function below will add a user-provided % of noise to a uniform image, where I'm defining % noise as (stdev / mean) * 100.
It doesn't work for non-uniform images at the moment.
Regards,
David
Test code:
seed = 1001L
testImage = FINDGEN(100,100) * 0.0 + 1000.0
PRINT, "Mean and standard deviation of original image is: " + STRING(MEAN(testImage)) + ", " + STRING(STDDEV(testImage))
noisyImage = DJP_addNoise(testImage, 5.0, seed)
PRINT, "Mean and standard deviation of new image is " + STRING(MEAN(noisyImage)) + ", " + STRING(STDDEV(noisyImage))
cgWindow, 'cgImage', testImage, WTitle='Original image', /Keep_Aspect
cgWindow, 'cgImage', noisyImage, WTitle='Image with noise added', /Keep_Aspect
FUNCTION DJP_addNoise, image, percentNoise, seed
Compile_Opt IDL2
IF(N_ELEMENTS(image) GT 0) THEN BEGIN
; Return an image with each value replaced by a Poisson deviate
h = HISTOGRAM(image, MIN=0, REVERSE=rr)
noisyImage = image
FOR i = 0, N_ELEMENTS(h) - 1 DO BEGIN
IF h[i] NE 0 THEN BEGIN
sub = rr[rr[i]:rr[i+1]-1] ; Get subscripts
currentMean = i+1
noisyImage[sub] = RANDOMU(seed, N_ELEMENTS(sub), POISSON=currentMean)
newMean = SQRT(currentMean) / (percentNoise / 100.0)
noisyImage[sub] += newMean - currentMean ; this adjusts the standard deviation to the required level
noisyImage[sub] *= currentMean / newMean ; this adjusts the mean back to what it was to start with
ENDIF
ENDFOR
RETURN, noisyImage
ENDIF ELSE BEGIN
RETURN, 0
ENDELSE
END
|
|
|