-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSGM.m
76 lines (63 loc) · 2.47 KB
/
SGM.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
function [disp, sCostCube] = SGM(left_raw, right_raw, lateral_paths_num, Dmax)
%input:
% left: left image(gray)
% right: right image(gray)
% lateral_paths_num: lateral cost aggregation paths number
% Dmax: disparity range of the dataset
%return:
% disp: disparity image(without postprocessing)
% sCostCube: aggregated cost cube
[H,W] = size(left_raw);
window_size = 9;
quadratic_interpolation = false;
w = (window_size-1)/2;%padding width
%%padding
left = zeros(H+2*w,W+2*w);
right = zeros(H+2*w,W+2*w);
left(w+1:H+w,w+1:W+w) = left_raw;
right(w+1:H+w,w+1:W+w) = right_raw;
%%raw cost computation
rawCostCube = Inf(H,W,Dmax+1);%the third index denotes disparity-1(1 means disparity is 0)
cube = zeros(W,window_size*window_size);
for i = 1:H
for k = 1:W
cube(k,:) = reshape((right(i:i+window_size-1, k:k+window_size-1)>right(i+w,k+w)),[1,window_size*window_size]);
end
for j = 1:W
if j-1 <= Dmax
top = j-1;
else
top = Dmax;
end
center = left(i+w,j+w);
c1 = reshape(left(i:i+window_size-1, j:j+window_size-1)>center,[1,window_size*window_size]);
for d = 0:top
c2 = cube(j-d,:);
rawCostCube(i,j,d+1) = sum(sum(xor(c1,c2)));
end
% center1 = left(i+w,j+w);
% for d = 0:top
% center2 = right(i+w,j+w-d);
% patch1 = left(i:i+window_size-1, j:j+window_size-1);
% patch2 = right(i:i+window_size-1, j-d:j-d+window_size-1);
% rawCostCube(i,j,d+1) = sum(sum(xor(patch1>center1,patch2>center2)));
% end
end
end
%%cost aggregation
L1 = SGM_left2right_aggregation(rawCostCube);
L2 = SGM_right2left_aggregation(rawCostCube);
L3 = SGM_top2bottom_aggregation(rawCostCube);
L4 = flipud(SGM_top2bottom_aggregation(flipud(rawCostCube)));
if lateral_paths_num == 4
sCostCube = L1 + L2 + L3 + L4;
elseif lateral_paths_num == 8
L5 = SGM_diagonal_1_aggregation(rawCostCube);%left top to right bottom
L6 = SGM_diagonal_2_aggregation(rawCostCube);%right top to left bottom
L7 = flipud(SGM_diagonal_1_aggregation(flipud(rawCostCube)));%left bottom to right top
L8 = flipud(SGM_diagonal_2_aggregation(flipud(rawCostCube)));%right bottom to left top
sCostCube = L1 + L2 + L3 + L4 + L5 + L6 + L7 + L8;
end
%%disparity computation
disp = SGM_disp_select(sCostCube, quadratic_interpolation);
end