Skip to content

Commit 8f50c90

Browse files
committed
Compute and clean depthmaps in parallel
1 parent f63d516 commit 8f50c90

File tree

1 file changed

+34
-7
lines changed

1 file changed

+34
-7
lines changed

opensfm/dense.py

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11

22
import logging
3+
from multiprocessing import Pool
34

45
import cv2
56
import numpy as np
@@ -14,31 +15,49 @@
1415
def compute_depthmaps(data, graph, reconstruction):
1516
"""Compute and refine depthmaps for all shots."""
1617
logger.info('Computing neighbors')
17-
neighbors = {}
18+
processes = data.config.get('processes', 1)
1819
num_neighbors = data.config['depthmap_num_neighbors']
20+
21+
neighbors = {}
1922
for shot in reconstruction.shots.values():
2023
neighbors[shot.id] = find_neighboring_images(
2124
shot, graph, reconstruction, num_neighbors)
2225

26+
arguments = []
2327
for shot in reconstruction.shots.values():
2428
if len(neighbors[shot.id]) <= 1:
2529
continue
26-
logger.info("Computing depthmap for image {}".format(shot.id))
27-
compute_depthmap(data, graph, reconstruction, neighbors, shot)
30+
arguments.append((data, graph, reconstruction, neighbors, shot))
31+
parallel_run(compute_depthmap, arguments, processes)
2832

33+
arguments = []
2934
for shot in reconstruction.shots.values():
3035
if len(neighbors[shot.id]) <= 1:
3136
continue
32-
logger.info("Cleaning depthmap for image {}".format(shot.id))
33-
clean_depthmap(data, graph, reconstruction, neighbors, shot)
37+
arguments.append((data, graph, reconstruction, neighbors, shot))
38+
parallel_run(clean_depthmap, arguments, processes)
3439

3540
merge_depthmaps(data, graph, reconstruction, neighbors)
3641

3742

38-
def compute_depthmap(data, graph, reconstruction, neighbors, shot):
43+
def parallel_run(function, arguments, num_processes):
44+
"""Run function for all arguments using multiple processes."""
45+
num_processes = min(num_processes, len(arguments))
46+
if num_processes == 1:
47+
return [function(arg) for arg in arguments]
48+
else:
49+
p = Pool(num_processes)
50+
return p.map(function, arguments)
51+
52+
53+
def compute_depthmap(arguments):
3954
"""Compute depthmap for a single shot."""
55+
data, graph, reconstruction, neighbors, shot = arguments
56+
4057
if data.raw_depthmap_exists(shot.id):
58+
logger.info("Using precomputed raw depthmap {}".format(shot.id))
4159
return
60+
logger.info("Computing depthmap for image {}".format(shot.id))
4261

4362
min_depth, max_depth = compute_depth_range(graph, reconstruction, shot)
4463

@@ -75,9 +94,14 @@ def compute_depthmap(data, graph, reconstruction, neighbors, shot):
7594
plt.show()
7695

7796

78-
def clean_depthmap(data, graph, reconstruction, neighbors, shot):
97+
def clean_depthmap(arguments):
98+
"""Clean depthmap by checking consistency with neighbors."""
99+
data, graph, reconstruction, neighbors, shot = arguments
100+
79101
if data.clean_depthmap_exists(shot.id):
102+
logger.info("Using precomputed clean depthmap {}".format(shot.id))
80103
return
104+
logger.info("Cleaning depthmap for image {}".format(shot.id))
81105

82106
dc = csfm.DepthmapCleaner()
83107
dc.set_same_depth_threshold(data.config['depthmap_same_depth_threshold'])
@@ -108,6 +132,9 @@ def clean_depthmap(data, graph, reconstruction, neighbors, shot):
108132

109133

110134
def merge_depthmaps(data, graph, reconstruction, neighbors):
135+
"""Merge depthmaps into a single point cloud."""
136+
logger.info("Merging depthmaps")
137+
111138
# Load clean depthmaps.
112139
depths, planes = {}, {}
113140
for sid in neighbors:

0 commit comments

Comments
 (0)