/usr/lib/scilab-sivp/macros/imnoise.sci is in scilab-sivp 0.5.3+svn287-2ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 | ////////////////////////////////////////////////////////////
// SIVP - Scilab Image and Video Processing toolbox
// Copyright (C) 2006 Shiqi Yu
//
// SIP - Scilab Image Processing toolbox
// Copyright (C) 2002-2004 Ricardo Fabbri
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
////////////////////////////////////////////////////////////
//
// salt & pepper (drop-out noise):
// d: density, from 0 to 1
//
// gaussian
// m: mean (from 0 to 1)
// v: variance (from 0 to 1)
//
// imnoise(img, 'localvar',V);
// Additive gaussian noise where the variance at Img(i,j) is
// V(i,j).
// imnoise(img, 'localvar',x, y);
//
//
// speckle (multiplicative noise)
// Image = Image + prob*Image
// where "prob" is a gaussian(0-1) distributed noise
// v: variance
//
function imn = imnoise(im, noise_type, param1, param2)
//function imn = imnoise(im, noise_type, param1, param2, param3)
//source image type
imtype = typeof(im(1));
//convert image to double
im=im2double(im);
// directly im+rand(im) will cause "inconsistent addition".
// so firstly reshape the input matrix
// what causes the problem? SIVP hypermat creation code?
im=matrix(im(:), size(im));
//Gaussian noise
if (noise_type == 'gaussian' | noise_type == 'Gaussian') then
if ~exists('param1','local')
m=0
else
m=param1
end
if ~exists('param2','local')
v=0.01
else
v=param2
end
// Set the current random generator
// to a Gaussian (with mean 0 and variance 1) random number
// generator.
//
// rand(4,5,3, 'normal') will return a matrix sized 4x5x3x1
// but rand('normal');rand(4,5,3) will return a matrix sized 4x5x3x1
// besides, rand(im, 'normal') will return error,
// so here we have to use rand('normal') set to a Gassian generator.
old_rand_gen=rand('info');
rand('normal');
imn = im + sqrt(v)*rand(im) + m;
rand(old_rand_gen);
elseif noise_type == 'localvar'
if argn(2) < 3 then
error('Too few arguments for noise type ''localvar''.');
//only one parameter
elseif argn(2) == 3 then
if( or(size(im)<>size(param1))) then
error("The first parameter for ''localvar'' should have the same"+...
" size with the input image.");
end
old_rand_gen=rand('info');
rand('normal');
imn = matrix(im(:), size(im)) + sqrt(param1).*rand(im);
rand(old_rand_gen);
//two parameters
elseif argn(2) == 4 then
if( or(size(param1)<>size(param2))) then
error("The two parameters for ''localvar'' should have the same size.");
end
minp1 = min(param1);
maxp1 = max(param1);
imn = min(max(im(:),minp1),maxp1); //max(im,minp1) can't work
//because im is a hypermat.
imn = matrix(interp1(param1(:),param2(:),imn),size(im));
old_rand_gen=rand('info');
rand('normal');
imn = im + sqrt(imn).*rand(im);
rand(old_rand_gen);
end
//salt & pepper noise
elseif noise_type == 'salt & pepper' | noise_type == 'salt and pepper'
if ~exists('param1','local')
d=0.05
else
d=param1
if( d < 0 | d > 1) then
error("The parameter for ''salt & pepper'' noise should in range [0,1].");
end
end
// logic:
//
// If prob <= d, then modify pix value like this:
// if prob <= d/2
// put salt
// else
// put pepper.
//
old_rand_gen=rand('info');
rand('uniform');
prob=rand(im);
rand(old_rand_gen);
imn=im;
imn(prob < d/2) = 0;
imn(prob >=d/2 & prob < d) = 1;
elseif noise_type=='speckle'
if ~exists('param1','local')
v=0.04
else
v=param1
if( d < 0) then
error("The parameter for ''speckle'' noise should >=0.");
end
end
old_rand_gen=rand('info');
rand('uniform');
imn = im + im .* (sqrt(v) * (rand(im)-0.5) );
rand(old_rand_gen);
elseif noise_type == 'poisson'
error('Not yet implemented');
else
error('Invalid noise type.');
end
//conver the output image to the same type as the input image
select imtype
case 'uint8' then
imn = im2uint8(imn);
case 'int8' then
imn = im2int8(imn);
case 'uint16' then
imn = im2uint16(imn);
case 'int16' then
imn = im2int16(imn);
case 'int32' then
imn = im2int32(imn);
case 'constant' then
imn = im2double(imn);
else
error("Data type " + imtype + " is not supported.");
end
endfunction
|