summaryrefslogtreecommitdiff
path: root/3176/CH5/EX5.1/Ex5_1.sce
blob: d20f4e6b0f4052d261493468c45a26df85c18d91 (plain)
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
//Ex5_1
// Noisy Images and their Histogram
//To plot the PDF of different Noise Distribution and add the same to the gray scale image.
//(I)Gaussian  (II)Uniform  (III)Salt & Pepper (IV)Log Normal  (V)Rayleigh  (VI)Erlang  (VII)Exponetial
// Version : Scilab 5.4.1
// Operating System : Window-xp, Window-7
//Toolbox: Image Processing Design 8.3.1-1
//Toolbox: SIVP 0.5.3.1-2
//Reference book name : Digital Image Processing
//book author: Rafael C. Gonzalez and Richard E. Woods

clc;
close;
clear;
xdel(winsid())//to close all currently open figure(s).

function R=imnoise2(type,M,N,a,b)
    if argn(2)==3 
        a=0; b=1;
    end
    
    select type
        
    case'gaussian'
        rand("normal")
        R=a+b*rand(M,N);
        
    case'uniform'
        R=a+(b-a)*rand(M,N,"uniform");
        
    case'salt & pepper'
        if argn(2)==3
            a = 0.15; b = 0.15;
        end
        if (a+b) > 1
            error('The sum Pa + Pb must not exceed 1.');
        end
        R(1:M,1:N) = 0.5;
        X = rand(M,N);
        [r c] = find(X<=a);
        for i=1:length(r)
                R(r(i),c(i)) = 0; 
        end
        u = a + b;
        [r c] = find(X>a & X<=u);
        for i=1:length(r)
                R(r(i),c(i)) = 255; 
        end      
        
    case'lognormal'
        if argn(2)==3
            a = 1; b = 0.25;
        end
        R = a*exp(b*mtlb_randn(M,N));
        
     case'rayleigh'
        if argn(2)==3
            a = 1; b = 0.25;
        end 
        R = a + ((-b)*(log(1-rand(M,N,"uniform")))).^0.5;       
        
     case'exponential'
        if argn(2)==3
            a = 1;
        end 
        if a<=0
            error('Parameter a must be positive for exponential type.');
        end
        k = -1/a;
        R = k*log(1-rand(M,N,"uniform"));
        
     case'erlang'
         if (b ~= round(b) | b <= 0)
            error('Param b must be positive for integer for Erlang.')
        end
        k = -1/a;
        R = zeros(M,N);
        for j=1:b
            R = R + k*log(1-rand(M,N,"uniform"));
        end              
        
        else
        disp('Unknownfiltertype.')
    end   
    
endfunction



/////////////////////////////////////// Main Programm  /////////////////////////////
gray=imread("Ex5_1.tif");
//gray=rgb2gray(a);
//gray=im2double(gray);
figure,ShowImage(gray,'Gray Image');
title('Original Image');
[M,N]=size(gray);
[count,cell]=imhist(gray);
figure,bar(cell,count,0.2);
mtlb_axis([0 255 0 35000]);
title('Histogram of Original Image');

///////////////////////////////////////    Gaussian Noise    ////////////////////
r1=imnoise2('gaussian',M,N,15,5);   // Generate Gaussian Noise with Given Mean and Variance
gray_noise_gaussian=gray+(r1);
figure,ShowImage(gray_noise_gaussian,'Gray Image with Noise');
title('Gray Image with Noise gaussian');
[count,cell]=imhist(gray_noise_gaussian);
figure;bar(cell,count,1.2);
mtlb_axis([0 255 0 3000]);
title('Gaussian');

///////////////////////////////////////    Rayleigh Noise    ////////////////////
r2=imnoise2('rayleigh',M,N,0,55);   // Generate rayleigh Noise 
gray_noise_rayleigh=gray+(r2);
figure,ShowImage(gray_noise_rayleigh,'Gray Image with Noise');
title('Gray Image with Noise rayleigh');
[count,cell]=imhist(gray_noise_rayleigh);
figure;bar(cell,count,1.2);
mtlb_axis([0 255 0 4000]);
title('Rayleigh');

///////////////////////////////////////    Erlang (Gamma) Noise    ////////////////////
r3=imnoise2('erlang',M,N,2,15);   // Generate erlang Noise 
gray_noise_erlang=gray+(r3);
figure,ShowImage(gray_noise_erlang,'Gray Image with Noise');
title('Gray Image with Noise erlang(Gamma)');
[count,cell]=imhist(gray_noise_erlang);
figure;bar(cell,count,1.2);
mtlb_axis([0 255 0 9500]);
title('Erlang (Gamma)');

///////////////////////////////////////    Exponential Noise    ////////////////////
r4=imnoise2('exponential',M,N,0.15);    //Generate exponential Noise 
gray_noise_exponential=gray+(r4);
figure,ShowImage(gray_noise_exponential,'Gray Image with Noise');
title('Gray Image with Noise exponential');
[count,cell]=imhist(gray_noise_exponential);
figure;bar(cell,count,1.2);
mtlb_axis([0 255 0 4500]);
title('Exponential');

///////////////////////////////////////    Uniform Noise    ////////////////////
r5=imnoise2('uniform',M,N,0,20);   // Generate uniform Noise 
gray_noise_uniform=gray+(r5);
figure,ShowImage(gray_noise_uniform,'Gray Image with Noise');
title('Gray Image with Noise uniform');
[count,cell]=imhist(gray_noise_uniform);
figure;bar(cell,count,1.2);
mtlb_axis([0 255 0 2000]);
title('Uniform');

///////////////////////////////////////    Salt & pepper Noise    ////////////////////
r6=imnoise2('salt & pepper',M,N,0.15,0.15);   // Generate salt & pepper Noise 
gray_noise_salt_pepper=gray+(r6);
figure,ShowImage(gray_noise_salt_pepper,'Gray Image with Noise');
title('Gray Image with Noise salt&pepper');
[count,cell]=imhist(gray_noise_salt_pepper);
figure;bar(cell,count,1.2);
mtlb_axis([0 255 0 35000]);
title('Salt & pepper');

/////////////////////////////////////////    lognormal Noise    ////////////////////
//r7=imnoise2('lognormal',M,N,5,0.65);   // Generate lognormal Noise 
//gray_noise_lognormal=gray+(r7);
//figure,ShowImage(gray_noise_lognormal,'Gray Image with Noise');
//title('Gray Image with Noise lognormal');
//[count,cell]=imhist(gray_noise_lognormal);
//figure;bar(cell,count,1.2);
//mtlb_axis([0 255 0 5500]);
//title('lognormal');