-
Notifications
You must be signed in to change notification settings - Fork 27
/
demo_5_perspective_eikonal_sl.m
139 lines (118 loc) · 4.48 KB
/
demo_5_perspective_eikonal_sl.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
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Demo of viscosity solution to perspective shape-from-shading under
% vertical lighting
%
% Based on the paper:
% E. Cristiani, M. Falcone and A. Seghini
% "Some remarks on perspective shape-from-shading models"
% In Proc. SSVM 2007, pp. 276-287
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear
close all
addpath('Toolbox/')
addpath('Data/')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Data
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
load('bunny.mat');
scale_factor = 1; % Set to <1 to downscale data for faster result
u = -imresize(u,scale_factor); % Ground truth depth map
mask = u>-Inf; % Pixels to be reconstructed
mask_eroded = imerode(mask,strel('square',3)); % Eroded mask
boundary = mask-mask_eroded; % Boundary of the eroded mask
K = K*scale_factor; % Intrinsics matrix
% Simulate image using finite differences and ground truth depth map
[y,x] = meshgrid(1:size(mask,2),1:size(mask,1));
x = x-K(2,3);
y = y-K(1,3);
problem.I = min(0.98,max(0.02,simulate_image(x,y,u,-K(1,1),mask,[0;0;1],1)));
% Load image
%~ problem.I = min(0.98,max(0.02,imresize(I,scale_factor)));
% Load intrinsics
problem.f = -K(1,1); % Focal length
problem.x0 = K(2,3); % x coordinates of the principal point (x is "bottom")
problem.y0 = K(1,3); % y coordinates of the principal point (y is "right")
% Create mask: here we force u=ground truth on the boundaries
% and Create g (force u = g wherever mask=0)
problem.mask = mask; % Take the whole mask
problem.g = min(u(problem.mask>0))*ones(size(problem.I)); % Fix depth at boundary to max ground truth value
%~ problem.mask = mask_eroded; % Take the eroded mask
%~ problem.g = max(u(problem.mask>0))*ones(size(problem.I)); % Fix depth at max ground truth value
%~ problem.g(boundary>0) = u(boundary>0); % On the boundary fix ground truth values
figure(145)
subplot(2,2,1)
imshow(uint8(255*problem.I))
axis image
axis off
title('$$I$$','Interpreter','Latex','Fontsize',18);
subplot(2,2,3)
imagesc(problem.g)
colorbar
axis image
axis off
title('$$g$$','Interpreter','Latex','Fontsize',18);
subplot(2,2,4)
imagesc(problem.mask)
colorbar
axis image
axis off
title('Mask','Interpreter','Latex','Fontsize',18);
drawnow
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Parameters
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
parameters.delta = 1/max(size(I,1),size(I,2)); % Grid size
parameters.h = 0.999*parameters.delta; % Stepsize
parameters.xi = 1e-8; % Stopping criterion
parameters.maxit = 1e6; % Stopping criterion
parameters.N_theta = 16; % Number of angles for discretizing the unit ball
parameters.display = 2; % 0: no display, 1: numerics, 2: 3D
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Main code
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[u,x,y] = eikonal_sl_perspective(problem,parameters);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Display result
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
u(problem.mask==0) = NaN;
figure('Name','Simulated image')
surf(x,y,u,ones(size(u)));
shading flat
colormap gray
axis equal
%~ axis off
camproj('perspective')
campos([0 0 0])
camtarget([0 0 -1])
camup([-1 0 0])
camva = rad2deg(2*atan(0.5*size(problem.I,2)/(problem.f)));
material([0 1 0]);
camlight('headlight','infinite')
camlight('headlight','infinite')
xlabel('$$x(X,Y)$$','Interpreter','Latex','Fontsize',18)
ylabel('$$y(X,Y)$$','Interpreter','Latex','Fontsize',18)
print -dpng frontal_view
figure('Name','3D-reconstruction')
surf(x,y,u,ones(size(u)));
shading flat
colormap gray
axis equal
camproj('perspective')
campos([0 0 0])
camtarget([0 0 -1])
camup([-1 0 0])
camva = rad2deg(2*atan(0.5*size(problem.I,2)/(problem.f)));
material([0 1 0]);
camlight('headlight','infinite')
camlight('headlight','infinite')
campos([2 0 0])
xtarget = round(problem.x0);
ytarget = round(problem.y0);
camtarget([x(xtarget,ytarget) y(xtarget,ytarget) u(xtarget,ytarget)])
xlabel('$$x(X,Y)$$','Interpreter','Latex','Fontsize',18)
ylabel('$$y(X,Y)$$','Interpreter','Latex','Fontsize',18)
zlabel('$$u(X,Y)$$','Interpreter','Latex','Fontsize',18)
%~ title('3D-reconstruction','Interpreter','Latex','Fontsize',18,'Location','West')
print -dpng lateral_view