## [Octave-cvsupdate] octave-forge/main/image imnoise.m,1.3,1.4

 [Octave-cvsupdate] octave-forge/main/image imnoise.m,1.3,1.4 From: - 2004-02-25 02:34:51 ```Update of /cvsroot/octave/octave-forge/main/image In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv28261 Modified Files: imnoise.m Log Message: (with Stefan van der Walt): correct variance on 'speckle'; clip output [0,1]; add tests Index: imnoise.m =================================================================== RCS file: /cvsroot/octave/octave-forge/main/image/imnoise.m,v retrieving revision 1.3 retrieving revision 1.4 diff -u -d -r1.3 -r1.4 --- imnoise.m 30 Jan 2004 17:59:45 -0000 1.3 +++ imnoise.m 25 Feb 2004 02:27:47 -0000 1.4 @@ -30,17 +30,22 @@ ## multiplicative gaussian noise: A = A + A*noise ## defaults to var=0.04 +## Modified: Stefan van der Walt , 2004-02-24 + function A = imnoise(A, stype, a, b) if (nargin < 2 || nargin > 4 || !is_matrix(A) || !isstr(stype)) usage("B = imnoise(A, type, parameters, ...)"); endif + + valid = (min(A(:)) >= 0 && max(A(:)) <= 1); stype = tolower(stype); if (strcmp(stype, 'gaussian')) if (nargin < 3), a = 0.0; endif if (nargin < 4), b = 0.01; endif - A = A + (a + randn(size(A)) * b); + A = A + (a + randn(size(A)) * sqrt(b)); + ## Variance of Gaussian data with mean 0 is E[X^2] elseif (strcmp(stype, 'salt & pepper')) if (nargin < 3), a = 0.05; endif noise = rand(size(A)); @@ -48,9 +53,20 @@ A(noise >= 1-a/2) = 1; elseif (strcmp(stype, 'speckle')) if (nargin < 3), a = 0.04; endif - A = A .* (1 + randn(size(A))*a); + A = A .* (1 + randn(size(A))*sqrt(a)); else error("imnoise: use type 'gaussian', 'salt & pepper', or 'speckle'"); endif + + if valid + A(A>1) = 1; + A(A<0) = 0; + else + warning("Image should be in [0,1]"); + endif endfunction + +%!assert(var(imnoise(ones(10)/2,'gaussian')(:)),0.01,0.005) # probabilistic +%!assert(length(find(imnoise(ones(10)/2,'salt & pepper')~=0.5)),5,10) # probabilistic +%!assert(var(imnoise(ones(10)/2,'speckle')(:)),0.01,0.005) # probabilistic ```

 [Octave-cvsupdate] octave-forge/main/image imnoise.m,1.3,1.4 From: - 2004-02-25 02:34:51 ```Update of /cvsroot/octave/octave-forge/main/image In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv28261 Modified Files: imnoise.m Log Message: (with Stefan van der Walt): correct variance on 'speckle'; clip output [0,1]; add tests Index: imnoise.m =================================================================== RCS file: /cvsroot/octave/octave-forge/main/image/imnoise.m,v retrieving revision 1.3 retrieving revision 1.4 diff -u -d -r1.3 -r1.4 --- imnoise.m 30 Jan 2004 17:59:45 -0000 1.3 +++ imnoise.m 25 Feb 2004 02:27:47 -0000 1.4 @@ -30,17 +30,22 @@ ## multiplicative gaussian noise: A = A + A*noise ## defaults to var=0.04 +## Modified: Stefan van der Walt , 2004-02-24 + function A = imnoise(A, stype, a, b) if (nargin < 2 || nargin > 4 || !is_matrix(A) || !isstr(stype)) usage("B = imnoise(A, type, parameters, ...)"); endif + + valid = (min(A(:)) >= 0 && max(A(:)) <= 1); stype = tolower(stype); if (strcmp(stype, 'gaussian')) if (nargin < 3), a = 0.0; endif if (nargin < 4), b = 0.01; endif - A = A + (a + randn(size(A)) * b); + A = A + (a + randn(size(A)) * sqrt(b)); + ## Variance of Gaussian data with mean 0 is E[X^2] elseif (strcmp(stype, 'salt & pepper')) if (nargin < 3), a = 0.05; endif noise = rand(size(A)); @@ -48,9 +53,20 @@ A(noise >= 1-a/2) = 1; elseif (strcmp(stype, 'speckle')) if (nargin < 3), a = 0.04; endif - A = A .* (1 + randn(size(A))*a); + A = A .* (1 + randn(size(A))*sqrt(a)); else error("imnoise: use type 'gaussian', 'salt & pepper', or 'speckle'"); endif + + if valid + A(A>1) = 1; + A(A<0) = 0; + else + warning("Image should be in [0,1]"); + endif endfunction + +%!assert(var(imnoise(ones(10)/2,'gaussian')(:)),0.01,0.005) # probabilistic +%!assert(length(find(imnoise(ones(10)/2,'salt & pepper')~=0.5)),5,10) # probabilistic +%!assert(var(imnoise(ones(10)/2,'speckle')(:)),0.01,0.005) # probabilistic ```