-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmethodTwo.m
136 lines (132 loc) · 6.02 KB
/
methodTwo.m
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
%**************************************************************************
% this function apply method two to noisy image *
% inputs:img: noisy input image *
% original: original image without noise *
% output:output: noise removed image result *
%**************************************************************************
function output = methodTwo(img,original)
output = img;
P = zeros(256,1); % vector for probability distribution of pixels
frequency=zeros(256,1); % vector for frequency of pixles
mySize = size(original,1) * size(original,2);
for i=1:size(original,1) % calculating probability of pixels
for j=1:size(original,2)
value=img(i,j);
frequency(value+1)=frequency(value+1)+1;
P(value+1)=frequency(value+1) / mySize;
end
end
m = 0;
for i=1:256 % calculating weighted mean of all pixels
m = m + ((i-1)*P(i));
end
vari = 0;
for i=1:256 % calculating variance of image pixels
vari = vari + (((i-1)-m)^2 * P(i));
end
previo = zeros(size(img,1)*size(img,2) , 1); % vector for previous replacements
t=1;
for i=1:(size(img,1))
for j =1:(size(img,2))
for m=3:2:5
windowSize = m;
k = (windowSize - 1)/2;
x1 = i-k;
x2 = i+k;
y1 = j-k;
y2 = j+k;
if i-k < 1
x1 = 1;
end
if i+k > size(img,1)
x2 = size(img,1);
end
if j-k < 1
y1 = 1;
end
if j+k > size(img,2)
y2 = size(img,2);
end
window = img(x1:x2 , y1:y2);
window = window / 4;
Gmax = double(max(max(window)));
Gmin = double(min(min(window)));
if img(i,j) > Gmin && img(i,j) < Gmax % it is not noisy pixel
final = img(i,j) / 4;
break;
else
if m==3 % size of window 3
check = 0;
final = -1;
for v = 1:size(window,1)
for w = 1:size(window,2)
if window(v,w) == Gmin || window(v,w) == Gmax % if it is noisy pixel
continue;
elseif check == 0 % all pixels in window is not noisy pixels
final = window(v,w);
check = 1;
continue;
else
continue;
end
end
end
if final == -1 % increase window size
continue;
else
break;
end
else
temp = 0;
for v = 1:size(window,1)
for w = 1:size(window,2)
if window(v,w) == Gmin || window(v,w) == Gmax
continue;
else
temp = 1;
end
end
end
if temp == 0 % all window is noisy pixels
if t-3 < 1 && t-2 >= 1
final = mean(previo(t-2:t-1));
break;
elseif t-3 < 1 && t-2 < 1 && t-1 >= 1
final = previo(t-1);
break;
elseif t-3 < 1 && t-2 < 1 && t-1 < 1
final = img(i,j) / 4;
break;
else
final = mean(previo(t-3:t-1));
break;
end
else % all window pixels not noisy
avg = mean(mean(window));
med = median(window , [1 2]);
alpha = abs(Gmax + Gmin - 2*avg);
beta = abs(Gmax + Gmin - 2*med);
if alpha < beta
final = avg;
else
final = med;
end
threshold = (100 / (sqrt((sqrt(vari))...
/30)) ) - 40;
if abs(double(final) - double(img(i,j) / 4)) <= threshold
final = img(i,j) / 4;
break;
else
break;
end
end
end
end
end
previo(t) = double(final);
t = t + 1;
final = uint8(final * 4);
output(i,j) = final;
end
end
end