-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFun_SmoothingMatting_CHU_V7.m
More file actions
177 lines (167 loc) · 5.34 KB
/
Fun_SmoothingMatting_CHU_V7.m
File metadata and controls
177 lines (167 loc) · 5.34 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
% function alpha = Fun_SmoothingMatting_CHU_V5(imageL, packL, imageR, packR)
% input: image trimap confidence alpha_hat
% 0727 update alpha before smooth
% confidence=1-CalCost(input,F,B,output);
% conf_norm=(confidence-min(confidence(:)))/(max(confidence(:))-min(confidence(:)));
clear all
addpath('0925')
load('matlab data\test20_R2L_op')
load('matlab data\test20L_lb.mat');
imageL = im2double(imread('test20L.png'));
trimapL = im2double(imread('test20L_tri.png'));
% packL = pack;
alpha_hatL = alpha;
confidenceL = confidence;
%alpha_hatL = im2double(imread('test20L_cs.png'));
load('matlab data\test20R_lb.mat');
imageR = im2double(imread('test20R.png'));
trimapR = im2double(imread('test20R_tri.png'));
%alpha_hatR = im2double(imread('test20R_cs.png'));
% packR = pack;
alpha_hatR = alpha;
confidenceR = confidence;
% clear pack
% 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
trimapR(alpha_hatR>0.96)=1;
consts_mapR = ~(trimapR(:,:,1) > 0 & trimapR(:,:,1) < max(trimapR(:)));
%% smoothing part
% *********** 0725 update alpha_hat *************
if vx(1,30)>0
figure,imshow(alpha_hatR)
LAB = rgb2lab(imageR);
Light = LAB(:,:,1);
Light = Light/max(Light(:));
a1 = alpha_hatR<0.98 & alpha_hatR>0.5;
alp = alpha_hatR;
% % alp(alp>0.6) = 1; % threshold
edg = edge(alp,'sobel');
edg = imdilate(edg,ones(3));
L1 = Light>0.4; %|( Light>0.18 & 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_hatR(a3) = 1;
% % L2 = Light<0.1; % threshold
% % L2(L2==1) = 5;
% % a3 = a2-L2;
% % a3(a3~=0)=1;
% % a3 =~a3;
% % alpha_hatR(a3) = max(alpha_hatR(a3)-0.1,0); % threshold
else
figure, imshow(alpha_hatL)
LAB = rgb2lab(imageL);
Light = LAB(:,:,1);
Light = Light/max(Light(:));
a1 = alpha_hatL<0.98 & alpha_hatL>0.5;
alp = alpha_hatL;
% % alp(alp>0.6) = 1; % threshold
edg = edge(alp,'sobel');
edg = imdilate(edg,ones(3));
L1 = Light>0.2; % 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.1; % threshold
% % L2(L2==1) = 5;
% % a3 = a2-L2;
% % a3(a3~=0)=1;
% % a3 =~a3;
% % alpha_hatL(a3) = max(alpha_hatL(a3)-0.1,0); % threshold
end
% ******* decide left or right image first to get alpha values *******
fL = confidenceL(:) .* ~consts_mapL(:);
fR = confidenceR(:) .* ~consts_mapR(:);
if vx(1,30)>0
I1 = imageR; % right image first, from right to left
I2 = imageL;
f = [fR; fL];
alpha_hat = [alpha_hatR(:); alpha_hatL(:)];
consts_map = [consts_mapR(:);consts_mapL(:)];
consts = consts_mapR; % when use original trimap, give up imerode
trimap1 = trimapR;
else
I1 = imageL; % left image first
I2 = imageR;
f = [fL; fR];
alpha_hat = [alpha_hatL(:); alpha_hatR(:)];
consts_map = [consts_mapL(:);consts_mapR(:)];
consts = consts_mapL;
trimap1 = trimapL;
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)
WhiteBack = ones(h,w,c);
ComF = bsxfun(@times,I1,alpha1)+bsxfun(@times,(1-alpha1),WhiteBack);
% ComF = ComF/255;
figure,imshow(ComF)
figure,imshow(alpha)
% imwrite(alpha1,'test2L_knn_up.png','png')