-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
84 lines (63 loc) · 2.73 KB
/
main.py
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
import os
import numpy
import scipy.spatial
import cv2
from matplotlib import pyplot
import utils
DEFAULT_NUM_INTERP = 10
input_path = r'examples/rabbit'
# Preprocess a image list
img_list = [os.sep.join([input_path, x]) for x in os.listdir(input_path) if x.lower().endswith('jpg')]
imgs = [utils.limit_image_size(cv2.imread(x)) for x in img_list]
# Calibrate images
imgs, coords = utils.calibrate_images(imgs)
x_min = numpy.array(coords)[:, 0].min()
x_max = numpy.array(coords)[:, 0].max()
y_min = numpy.array(coords)[:, 1].min()
y_max = numpy.array(coords)[:, 1].max()
w = x_max - x_min
h = y_max - y_min
# Get the region
regions = scipy.spatial.Delaunay(coords)
edges = utils.get_edges_from_triangles(regions.simplices)
qhull = scipy.spatial.ConvexHull(regions.points)
area_ratio = qhull.volume / w / h
qhull_polygon = numpy.array([qhull.points[i] for i in qhull.vertices], dtype=numpy.float32)
vol_samples = DEFAULT_NUM_INTERP / area_ratio
num_x_samples = int(numpy.sqrt(vol_samples * w / h) + 0.5)
num_y_samples = int(numpy.sqrt(vol_samples * w / h) * h / w + 0.5)
depth_map, focus_meas = utils.cal_depth_map(imgs, coords)
pyplot.figure('depth')
pyplot.imshow(depth_map, cmap='gray')
# make samples
x_interval = (x_max - x_min) / num_x_samples
y_interval = (y_max - y_min) / num_y_samples
x_samples = numpy.linspace(x_min + 0.5*x_interval, x_max - 0.5*x_interval, num_x_samples)
y_samples = numpy.linspace(y_min + 0.5*y_interval, y_max - 0.5*y_interval, num_y_samples)
interp_coords = {}
# interpolate with 3 images around, to avoid error form far images and reduce computations
triangles = numpy.sort(regions.simplices)
interp_coords = {tuple(x): [] for x in triangles}
for x in x_samples:
for y in y_samples:
if cv2.pointPolygonTest(qhull_polygon, (x, y), False) > 0:
for triangle in triangles:
polygon = numpy.array([regions.points[i] for i in triangle], dtype=numpy.float32)
if cv2.pointPolygonTest(polygon, (x, y), False) > 0:
interp_coords[tuple(triangle)].append(numpy.array([x, y]))
n_samples = sum([len(x) for x in interp_coords.values()])
for triangle, samples in interp_coords.items():
triangle_images = [imgs[i] for i in triangle]
triangle_coords = [coords[i] for i in triangle]
triangle_interp_images = utils.interpolate_image(triangle_images, triangle_coords, samples)
coords.extend(triangle_coords)
imgs.extend(triangle_interp_images)
# make refocused images
pyplot.figure('triangles')
pyplot.triplot(coords[:, 0], coords[:, 1], regions.simplices)
pyplot.figure('pts')
x, y = coords[0]
pyplot.plot(x, y, 'o')
for x, y in coords[1:]:
pyplot.plot(x, y, 'x')
pyplot.show()