-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoissonImageIntegration.m
140 lines (120 loc) · 3.49 KB
/
poissonImageIntegration.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
function output = poissonImageIntegration(LAP, DEST, mask, tx, ty, ww, hh)
% Ic = imcrop(I1,[min(c) min(r) maxW maxH]); % crop the image in the RIO
% % crop mask - make the mask RGB (3 layers)
% Mc = zeros(size(Ic)); % make a copy of Ic
%
% % get laplacian of the source image
% H =fspecial('laplacian',0);
% LAP = imfilter(double(I1),H,'same');
% Id = imcrop(LAP,[min(c) min(r) maxW maxH]);
%
% % multiple the Mask by the Image to get only the pixels in the RIO
% LAP = immultiply(Id,Mc);
%
%
% [H,W,C] = size(LAP);
% find DEST boundary points
se = strel('disk',1);
outer_bp = imdilate(mask,se) - mask;
fd_bp = DEST.*outer_bp; % f* boundary points (DEST)
% only process roi with mask
roi_mask = mask(ty-1:ty+hh,tx-1:tx+ww); % bw roi mask
I = DEST;
dest_roi = I(ty-1:ty+hh,tx-1:tx+ww); % destination roi
f_idx = find(roi_mask==1);
[Row,Col] = ind2sub(size(roi_mask),f_idx);
roi_bp = fd_bp(ty-1:ty+hh,tx-1:tx+ww); % destination boundary intensity
roi_outer_bp = outer_bp(ty-1:ty+hh,tx-1:tx+ww); % roi outer boundary
roi_lap = LAP(ty-1:ty+hh,tx-1:tx+ww); % source laplacian roi
f = zeros(size(roi_bp));
dest_bp = roi_bp;
% stupid modification
% dest_bp(dest_bp~=0) = dest_bp(dest_bp~=0)-20;
%
lap = roi_lap;
indexArray1 = [];
indexArray2 = [];
valueArray = [];
b = zeros(length(f_idx),1);
% create linear system using sparse matrix
for i = 1:length(f_idx)
n = 0;
r = Row(i);
c = Col(i);
b(i) = b(i)-lap(r,c);
if roi_mask(r+1,c)
n = n+1;
indexArray1(end+1) = i;
indexArray2(end+1) = find(f_idx==(f_idx(i)+1));
valueArray(end+1) = -1;
else
if roi_outer_bp(r+1,c)
n = n+1;
b(i) = b(i)+dest_bp(r+1,c);
end
end
if roi_mask(r-1,c),
n = n+1;
indexArray1(end+1) = i;
indexArray2(end+1) = find(f_idx==(f_idx(i)-1));
valueArray(end+1) = -1;
else
if roi_outer_bp(r-1,c)
n = n+1;
b(i) = b(i)+dest_bp(r-1,c);
end
end
if roi_mask(r,c+1),
n = n+1;
indexArray1(end+1) = i;
indexArray2(end+1) = find(f_idx==(f_idx(i)+size(roi_mask,1)));
valueArray(end+1) = -1;
else
if roi_outer_bp(r,c+1)
n = n+1;
b(i) = b(i)+dest_bp(r,c+1);
end
end
if roi_mask(r,c-1),
n = n+1;
indexArray1(end+1) = i;
indexArray2(end+1) = find(f_idx==(f_idx(i)-size(roi_mask,1)));
valueArray(end+1) = -1;
else
if roi_outer_bp(r,c-1)
n = n+1;
b(i) = b(i)+dest_bp(r,c-1);
end
end
if n
indexArray1(end+1) = i;
indexArray2(end+1) = i;
valueArray(end+1) = n;
end
end
A = sparse(indexArray1,indexArray2,valueArray,length(f_idx),length(f_idx));
x = ones(length(f_idx),1);
% jacobi method
x_old = x;
x_new = x_old;
th0 = 0;
while (1)
sigma = A*x_old-diag(diag(A))*x_old;
x_new = (b-sigma)./full(diag(A));
th1 = max(abs(x_new - x_old)) % new threshold
if max(abs(x_new - x_old)) < 0.01
break;
else
th0 = max(abs(x_new - x_old)); % old threshold
x_old = x_new;
end
end
result = zeros(size(roi_mask));
result(f_idx) = x_new;
f = result;
tmp = dest_roi;
tmp(f_idx) = x_new;
dest_roi = tmp;
I(ty-1:ty+hh,tx-1:tx+ww) = dest_roi; % change dest roi to calculated roi
output = I;
return