-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFun_SmoothingMatting_CHU_V5.m
More file actions
207 lines (192 loc) · 6.13 KB
/
Fun_SmoothingMatting_CHU_V5.m
File metadata and controls
207 lines (192 loc) · 6.13 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
% function alpha = Fun_SmoothingMatting_CHU_V5(imageL, packL, imageR, packR)
% input: image trimap confidence alpha_hat
% 0722 try to getNewTrimap
% confidence=1-CalCost(input,F,B,output);
% conf_norm=(confidence-min(confidence(:)))/(max(confidence(:))-min(confidence(:)));
clear all
load('matlab data\test9_L2R_op')
load('matlab data\test9L_smooth_autotri.mat');
imageL = imread('test9L.png');
% imageL = IColor; % original image
packL = pack;
load('matlab data\test9R_smooth_autotri.mat');
imageR = imread('test9R.png');
% imageR = IColor;
packR = pack;
clear pack %IColor
% Parameters
omega = 1e-1;
lambda = 100;
[h,w,c] = size(imageR);
img_size = w * h;
% Normalize inputs
% ****** left image *********
imageL = double(imageL);
alpha_hatL = double(packL(:,:,1)); % baby2 size 415
confidenceL = double(packL(:,:,2));
alpha_hatL = alpha_hatL(1:h,1:w); % baby2 different size
confidenceL = confidenceL(1:h,1:w); % baby2 different size
trimapL = im2double(imread('test9L_autotri.png')); % baby2 size 413
% trimapL = rgb2gray(trimapL); % auto trimap does not need this line
clear packL
if max(imageL(:)) > 1
imageL = imageL / 255;
end
if max(confidenceL(:)) > 1
confidenceL = confidenceL / 255;
end
if max(alpha_hatL(:)) > 1
alpha_hatL = alpha_hatL / 255;
end
consts_mapL = ~(trimapL(:,:,1) > 0 & trimapL(:,:,1) < max(trimapL(:)));
% 1 = foreground and background pixels, 0 = unknown pixels
% ****** right image **********
imageR = double(imageR);
alpha_hatR = double(packR(:,:,1));
confidenceR = double(packR(:,:,2));
alpha_hatR = alpha_hatR(1:h,1:w); % baby2 different size
confidenceR = confidenceR(1:h,1:w); % baby2 different size
% trimapR = packR(:,:,3);
% trimapR = trimapR(:,1:w2);
trimapR = im2double(imread('test9R_autotri.png'));
% trimapR = rgb2gray(trimapR); % auto trimap does not need this line
clear packR
if max(imageR(:)) > 1
imageR = imageR / 255;
end
if max(confidenceR(:)) > 1
confidenceR = confidenceR / 255;
end
if max(alpha_hatR(:)) > 1
alpha_hatR = alpha_hatR / 255;
end
consts_mapR = ~(trimapR(:,:,1) > 0 & trimapR(:,:,1) < max(trimapR(:)));
%% smoothing part
% *********** 0725 update alpha_hat *************
if vx(1,30)>0
LAB = rgb2lab(imageR);
Light = LAB(:,:,1);
Light = Light/max(Light(:));
a1 = alpha_hatR<1 & alpha_hatR>0;
alp = alpha_hatR;
alp(alp>0.4) = 1; % threshold
edg = edge(alp,'sobel');
edg = imdilate(edg,ones(2));
L1 = Light>0.45; % threshold
L1 = double(L1);
L1(L1==1) = 5;
a2 = a1-edg;
a2(a2==1) = 5;
a2(a2~=5) = 3;
a3 = a2-L1;
a3(a3~=0)=1;
a3 =~a3;
alpha_hatR(a3) = 1;
L2 = Light<0.25; % threshold
L2(L2==1) = 5;
a3 = a2-L2;
a3(a3~=0)=1;
a3 =~a3;
alpha_hatR(a3) = max(alpha_hatR(a3)-0.2); % threshold
else
LAB = rgb2lab(imageL);
Light = LAB(:,:,1);
Light = Light/max(Light(:));
a1 = alpha_hatL<1 & alpha_hatL>0;
alp = alpha_hatL;
alp(alp>0.45) = 1; % threshold
edg = edge(alp,'sobel');
edg = imdilate(edg,ones(2));
L1 = Light>0.5; % threshold
L1 = double(L1);
L1(L1==1) = 5;
a2 = a1-edg;
a2(a2==1) = 5;
a2(a2~=5) = 3;
a3 = a2-L1;
a3(a3~=0)=1;
a3 =~a3;
alpha_hatL(a3) = 1;
L2 = Light<0.26; % threshold
L2(L2==1) = 5;
a3 = a2-L2;
a3(a3~=0)=1;
a3 =~a3;
alpha_hatL(a3) = max(alpha_hatL(a3)-0.3); % threshold
end
% a1 = alpha_hatR<1 & alpha_hatR>0;
% alp = alpha_hatR;
% alp(alp>0.4) = 1;
% ed = edge(alp,'sobel');
% ed = imdilate(ed,ones(2));
% LAB = rgb2lab(imageR);
% Light = LAB(:,:,1);
% Light = Light/max(Light(:));
% L1 = Light>0.6;
% L1 = double(L1);
% L1(L1==1) = 5;
% a2 = a1-ed;
% a2(a2==1) = 5;
% a2(a2~=5) = 3;
% a3 = a2-L1;
% a3(a3~=0)=1;
% a3=~a3;
% alpha_hatR(a3) = 1;
% L2 = Light<0.16;
% L2(L2==1) = 5;
% a3 = a2-L2;
% a3(a3~=0)=1;
% a3=~a3;
% alpha_hatR(a3) = max(alpha_hatR(a3)-0.1);
% ******* decide left or right image first to get alpha values *******
if vx(1,30)>0
I1 = imageR; % right image first, from right to left
I2 = imageL;
% [newtrimapR, alpha_hatR, confidenceR] = getNewTrimap_CHU(trimapR,alpha_hatR,alpha_hatL,confidenceR,vx);
% consts = ~(newtrimapR(:,:,1) > 0 & newtrimapR(:,:,1) < max(newtrimapR(:)));
trimap1 = newtrimapR;
% [alpha_hatR, confidenceR] = getNewTrimap_CHU(trimapR,alpha_hatR,alpha_hatL,confidenceR,vx);
% fR = confidenceR(:) .* ~consts_mapR(:);
fR = confidenceR(:) .* ~consts(:);
fL = confidenceL(:) .* ~consts_mapL(:);
f = [fR; fL];
alpha_hat = [alpha_hatR(:); alpha_hatL(:)];
consts_map = [consts(:);consts_mapL(:)];
% consts_map = [consts_mapR(:);consts_mapL(:)];
% consts = consts_mapR;
else
I1 = imageL; % left image first
I2 = imageR;
[newtrimapL, alpha_hatL, confidenceL] = getNewTrimap_CHU(trimapL,alpha_hatL,alpha_hatR,confidenceL,vx);
consts = ~(newtrimapL(:,:,1) > 0 & newtrimapL(:,:,1) < max(newtrimapL(:)));
% [alpha_hatL, confidenceL] = getNewTrimap_CHU(trimapL,alpha_hatL,alpha_hatR,confidenceL,vx);
% fL = confidenceL(:) .* ~consts_mapL(:);
trimap1 = newtrimapL;
fL = confidenceL(:) .* ~consts(:);
fR = confidenceR(:) .* ~consts_mapR(:);
f = [fL; fR];
alpha_hat = [alpha_hatL(:); alpha_hatR(:)];
consts_map = [consts(:);consts_mapR(:)];
% consts_map = [consts_mapL(:);consts_mapR(:)];
% consts = consts_mapL;
end
D = spdiags(consts_map, 0, 2*img_size, 2*img_size);
P = spdiags(f, 0, 2*img_size, 2*img_size);
K = lambda * D + omega * P;
% Generate matting Laplacian matrix
% L = getLaplacian_CHU_V5(I1, I2, consts,vx);
L = getLaplacian_CHU_V6(I1, I2,trimap1,edg,vx);
% Solve for alpha
x = (L + K) \ (K * alpha_hat(:));
alpha = max(min(reshape(x,h,2*w),1),0);
alpha1 = alpha(:,1:w);
k = alpha1<0.1 ;
alpha1(k)=0;
k = alpha1>0.9;
alpha1(k) = 1;
figure,imshow(alpha)
% figure,imshow(alpha1)
WhiteBack = ones(h,w,c);
ComF = bsxfun(@times,I1,alpha1)+bsxfun(@times,(1-alpha1),WhiteBack);
% ComF = ComF/255;
% figure,imshow(ComF)