www.pudn.com > DWM.rar > impulsenoise.m, change:2007-07-20,size:2004b


%%% Add the impulse noise to the image. %%% 
%%% function ImageNoisy=implusenoise(ImageOrg,NoiseDensity,type,noiseinterval); 
% type includes two kinds of impulse noise: 'fixed-value' and 'random-value' 
% 'fixed-value'---the corrupted pixel is either mininoise or maxinoise;  
% 'random-value'---the corrupted pixel is any value between 0 and 255.  
% "noiseinterval" is indictaed as [mininoise maxinoise] 
%%% Both the origianl image and the noisy image are gray-level images between 0~255.  
%%% Programmed by S. Q. SHI in June 21, 2007. %%% 
 
function ImageNoisy=implusenoise(varargin) 
vin=length(varargin); Nin=vin+1; 
error(nargchk(1,Nin,nargin)) 
if vin==4 
        ImageOrg=varargin{1}; 
        NoiseDensity=varargin{2}; 
        code=varargin{3}; 
        NoiseInterval=varargin{4}; 
        MiniNoise=NoiseInterval(1); MaxiNoise=NoiseInterval(2); 
else   warning('The input parameters are not correct! Please check and input again.') 
end 
 
[Row Col]=size(ImageOrg); 
RandMatrix=rand(Row,Col); 
Flag=zeros(Row,Col); % 0-uncorrupted; 1-noise corrupted;; 
 
switch code 
    case 'fixed-value' 
        for i=1:Row 
            for j=1:Col 
                if RandMatrix(i,j)<=NoiseDensity/2  %  with equal probability 
                    Flag(i,j)=1; ImageNoisy(i,j)=MiniNoise; 
                elseif (RandMatrix(i,j)>NoiseDensity/2) & (RandMatrix(i,j)<=NoiseDensity); 
                        Flag(i,j)=1; ImageNoisy(i,j)=MaxiNoise;   
                        else ImageNoisy(i,j)=ImageOrg(i,j); 
                end 
            end 
        end 
        
    case 'random-value' 
        for i=1:Row 
            for j=1:Col 
                NoiseValue=MiniNoise+round((MaxiNoise-MiniNoise)*rand(1));  % uniformly distributed in [mininoise maxinoise]; 
                if RandMatrix(i,j)<=NoiseDensity 
                    Flag(i,j)=1; ImageNoisy(i,j)=NoiseValue; 
                else ImageNoisy(i,j)=ImageOrg(i,j); 
                end 
            end 
        end 
end