0
|
1 ## Copyright (C) 2000 Paul Kienzle |
|
2 ## |
|
3 ## This program is free software; you can redistribute it and/or modify |
|
4 ## it under the terms of the GNU General Public License as published by |
|
5 ## the Free Software Foundation; either version 2 of the License, or |
|
6 ## (at your option) any later version. |
|
7 ## |
|
8 ## This program is distributed in the hope that it will be useful, |
|
9 ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 ## GNU General Public License for more details. |
|
12 ## |
|
13 ## You should have received a copy of the GNU General Public License |
|
14 ## along with this program; if not, write to the Free Software |
|
15 ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
16 |
|
17 ## usage: B = imnoise (A, type) |
|
18 ## |
|
19 ## Adds noise to image in A. |
|
20 ## |
|
21 ## imnoise (A, 'gaussian' [, mean [, var]]) |
|
22 ## additive gaussian noise: A = A + noise |
|
23 ## defaults to mean=0, var=0.01 |
|
24 ## |
|
25 ## imnoise (A, 'salt & pepper' [, density]) |
|
26 ## lost pixels: A = 0 or 1 for density*100% of the pixels |
|
27 ## defaults to density=0.05, or 5% |
|
28 ## |
|
29 ## imnoise (A, 'speckle' [, var]) |
|
30 ## multiplicative gaussian noise: A = A + A*noise |
|
31 ## defaults to var=0.04 |
|
32 |
|
33 function A = imnoise(A, stype, a, b) |
|
34 |
|
35 if (nargin < 3 || nargin > 4 || !is_matrix(A) || !isstr(stype)) |
|
36 usage("B = imnoise(A, type, parameters, ...)"); |
|
37 endif |
|
38 |
|
39 stype = tolower(stype); |
|
40 if (strcmp(stype, 'gaussian')) |
|
41 if (nargin < 3), a = 0.0; endif |
|
42 if (nargin < 4), b = 0.01; endif |
|
43 A = A + (a + randn(size(A)) * b); |
|
44 elseif (strcmp(stype, 'salt & pepper')) |
|
45 if (nargin < 3), a = 0.05; endif |
|
46 noise = rand(size(A)); |
|
47 A(noise <= a/2) = 0; |
|
48 A(noise >= 1-a/2) = 1; |
|
49 elseif (strcmp(stype, 'speckle')) |
|
50 if (nargin < 3), a = 0.04; endif |
|
51 A = A * (1 + randn(size(A))*a); |
|
52 else |
|
53 error("imnoise: use type 'gaussian', 'salt & pepper', or 'speckle'"); |
|
54 endif |
|
55 |
|
56 endfunction |