-
Notifications
You must be signed in to change notification settings - Fork 0
/
flattening.m
93 lines (77 loc) · 2.73 KB
/
flattening.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
function [ resulting_image ] = flattening( source_image,source_mask )
%UNTITLED9 Summary of this function goes here
% Detailed explanation goes here
source = double(source_image);
resulting_image = source;
[~, ~, channels] = size(source);
[~, xCoordinatesTarget] = find(source_mask);
n = length(xCoordinatesTarget);
loc = source_mask(:);
grid = zeros(size(source_mask));
grid(loc) = 1:n;
for channel = 1:channels
[x, y] = find(grid);
A = zeros(n,n);
B = zeros(n,1);
s = source(:,:,channel);
edge_detected = edge(s,'canny', 0.01);
for i = 1:n %for every pixel in the mask
A(i,i) = 4;
if source_mask(x(i)-1, y(i)) ~= 0 % if the pixel in the mask belongs to Omega
A(i, grid(x(i)-1, y(i)) ) = -1;
else
% if it doesn't add to B the target
B(i) = s(x(i)-1, y(i));
end
if source_mask(x(i)+1, y(i)) ~= 0
A(i, grid(x(i)+1, y(i)) ) = -1;
else
B(i) = B(i) + s(x(i)+1, y(i));
end
if source_mask(x(i), y(i)+1) ~= 0
A(i, grid(x(i), y(i)+1) ) = -1;
else
B(i) = B(i) + s(x(i), y(i)+1);
end
if source_mask(x(i), y(i)-1) ~= 0
A(i, grid(x(i), y(i)-1) ) = -1;
else
B(i) = B(i) + s(x(i), y(i)-1);
end
v=0;
center = s(x(i), y(i)) ;
%NORTH
if ~(edge_detected(x(i), y(i)) || edge_detected(x(i), y(i)+1) )
v = v + 0;
else
v = v + center - s(x(i), y(i)+1);
end
%SOUTH
if ~(edge_detected(x(i), y(i)) || edge_detected(x(i), y(i)-1))
v = v + 0;
else
v = v + center - s(x(i), y(i)-1);
end
%OVEST
if ~(edge_detected(x(i), y(i)) || edge_detected(x(i)+1, y(i)) )
v = v + 0;
else
v = v + center - s(x(i)+1, y(i));
end
%EAST
if ~(edge_detected(x(i), y(i)) || edge_detected(x(i)-1, y(i)))
v = v + 0;
else
v = v + center - s(x(i)-1, y(i));
end
B(i)= B(i) + v;
end
A = sparse(A);
X=A\B;
for index=1:length(X)
[x,y] = find(grid==index);
resulting_image(x,y,channel)=X(index);
end
end
resulting_image = uint8(resulting_image);
end