-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsolvePoisson2.m
172 lines (158 loc) · 5.17 KB
/
solvePoisson2.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
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
171
function [ out ] = solvePoisson2(hotfix,src,tgt,context,select,xt,yt)
%laplacian operator
laplacian = [0 1 0; 1 -4 1; 0 1 0];
%get size of target image
[width height] = size(tgt);
%scan through image...
%if the pixel is in the context:
% coordinate to value
% and count pixels in context
%else if the pixel is on the edge of the context:
% get average pixel values of edge target pixels
% fixes 'bleeding' problem
N=0;
c2v = zeros(width,height);
cnt=0;
pav = 0;
edgepixels=0;
edgefound = 0;
for i=1:width
for j=1:height
if(select(i,j)==0 || context(i,j)==2)
cnt = cnt+1;
c2v(i,j) = cnt;
N = N+1;
elseif(hotfix==1)
edgefound = 0;
if(j<height)
if(select(i,j+1) == 0 || context(i,j+1) == 2)
if(edgefound == 0)
pav = pav+tgt(i,j);
edgepixels = edgepixels + 1;
edgefound = 1;
end
end
end
if(j>1)
if(select(i,j-1) == 0 || context(i,j-1) == 2)
if(edgefound == 0)
pav = pav+tgt(i,j);
edgepixels = edgepixels + 1;
edgefound = 1;
end
end
end
if(i<width)
if(select(i+1,j) == 0 || context(i+1,j) == 2)
if(edgefound == 0)
pav = pav+tgt(i,j);
edgepixels = edgepixels + 1;
edgefound = 1;
end
end
end
if(i>1)
if(select(i-1,j) == 0 || context(i-1,j) == 2)
if(edgefound == 0)
pav = pav+tgt(i,j);
edgepixels = edgepixels + 1;
end
end
end
end
end
end
if(hotfix==1)
pav = pav/edgepixels;
end
%allocate sparse matrix
A = spalloc(N,N,5*N);
%boundary conditions
b = zeros(1,N);
%apply laplacian operator
src = conv2(double(src), -laplacian, 'same');
%iterate through A
cnt=0;
for i=1:width
for j=1:height
if(select(i,j)==0 || context(i,j) == 2)
cnt = cnt + 1;
A(cnt,cnt) = 4;
%check for neighbors
if(j<height)
if(select(i,j+1) == 0 || context(i,j+1) == 2)
p = c2v(i,j+1);
A(cnt, p) = -1;
else
b(cnt) = b(cnt) + tgt(i,j+1);
end
else
if(hotfix==1)
b(cnt) = b(cnt) + pav;
elseif(hotfix==0)
b(cnt) = b(cnt) + tgt(i,j);
end
end
if(j>1)
if(select(i,j-1) == 0 || context(i,j-1) == 2)
p = c2v(i,j-1);
A(cnt, p) = -1;
else
b(cnt) = b(cnt) + tgt(i,j-1);
end
else
if(hotfix==1)
b(cnt) = b(cnt) + pav;
elseif(hotfix==0)
b(cnt) = b(cnt) + tgt(i,j);
end
end
if(i<width)
if(select(i+1,j) == 0 || context(i+1,j) == 2)
p = c2v(i+1,j);
A(cnt, p) = -1;
else
b(cnt) = b(cnt) + tgt(i+1,j);
end
else
if(hotfix==1)
b(cnt) = b(cnt) + pav;
elseif(hotfix==0)
b(cnt) = b(cnt) + tgt(i,j);
end
end
if(i>1)
if(select(i-1,j) == 0 || context(i-1,j) == 2)
p = c2v(i-1,j);
A(cnt, p) = -1;
else
b(cnt) = b(cnt) + tgt(i-1,j);
end
else
if(hotfix==1)
b(cnt) = b(cnt) + pav;
elseif(hotfix==0)
b(cnt) = b(cnt) + tgt(i,j);
end
end
%add guidance vector field
b(cnt) = b(cnt) + src(i-xt,j-yt);
end
end
end
%solve for x
x = A\(b');
%x = pinv(A)*(b');
%x = lscov(A,b);
%x = bicg(A, b', [], 400);
out = tgt;
%build output image
for i=1:width
for j=1:height
if(select(i,j)==0 || context(i,j)==2)
p = c2v(i,j);
out(i,j) = x(p);
end
end
end
end