-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrof_denoise.m
53 lines (50 loc) · 1.64 KB
/
rof_denoise.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
% https://www.mathworks.com/matlabcentral/fileexchange/22410-rof-denoising-algorithm
%imshow(pic-rof_img)
%% ROFdenoise
%
% This denoising method is based on total-variation, originally proposed by
% Rudin, Osher and Fatemi. In this particular case fixed point iteration
% is utilized.
%
% For the included image, a fairly good result is obtained by using a
% theta value around 12-16. A possible addition would be to analyze the
% residual with an entropy function and add back areas that have a lower
% entropy, i.e. there are some correlation between the surrounding pixels.
%
% Philippe Magiera & Carl Löndahl, 2008
%
function A = rof_denoise(Image, Theta)
[Image_h, Image_w] = size(Image);
g = 1; dt = 1/4; nbrOfIterations = 5;
Image = double(Image);
p = zeros(Image_h,Image_w,2);
d = zeros(Image_h,Image_w,2);
div_p = zeros(Image_h,Image_w);
for i = 1:nbrOfIterations
for x = 1:Image_w
for y = 2:Image_h-1
div_p(y,x) = p(y,x,1) - p(y-1,x,1);
end
end
for x = 2:Image_w-1
for y = 1:Image_h
div_p(y,x) = div_p(y,x) + p(y,x,2) - p(y,x-1,2);
end
end
% Handle boundaries
div_p(:,1) = p(:,1,2);
div_p(:,Image_w) = -p(:,Image_w-1,2);
div_p(1,:) = p(1,:,1);
div_p(Image_h,:) = -p(Image_h-1,:,1);
% Update u
u = Image-Theta*div_p;
% Calculate forward derivatives
du(:,:,2) = u(:,[2:Image_w, Image_w])-u;
du(:,:,1) = u([2:Image_h, Image_h],:)-u;
% Iterate
d(:,:,1) = (1+(dt/Theta/g).*abs(sqrt(du(:,:,1).^2+du(:,:,2).^2)));
d(:,:,2) = (1+(dt/Theta/g).*abs(sqrt(du(:,:,1).^2+du(:,:,2).^2)));
p = (p-(dt/Theta).*du)./d;
end
A = u;
end