Skip to content

Commit

Permalink
Merge pull request DLR-RM#479 from DLR-RM/iss_477_fix_stereo_matching
Browse files Browse the repository at this point in the history
Iss 477 fix stereo matching
  • Loading branch information
themasterlink authored Feb 28, 2022
2 parents 09af0bb + 4f33469 commit 9f42f73
Show file tree
Hide file tree
Showing 8 changed files with 15 additions and 12 deletions.
7 changes: 2 additions & 5 deletions blenderproc/python/postprocessing/StereoGlobalMatching.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def stereo_global_matching(color_images: List[np.ndarray], depth_max: Optional[f

depth_frames = []
disparity_frames = []
for frame, color_image in enumerate(color_images):
for color_image in color_images:
depth, disparity = StereoGlobalMatching._sgm(color_image[0], color_image[1], baseline, depth_max, focal_length, window_size, num_disparities, min_disparity, disparity_filter, depth_completion)

depth_frames.append(depth)
Expand Down Expand Up @@ -114,10 +114,7 @@ def _sgm(left_color_image: np.ndarray, right_color_image: np.ndarray, baseline:
filteredImg = cv2.normalize(src=filteredImg, dst=filteredImg, beta=0, alpha=255, norm_type=cv2.NORM_MINMAX)

disparity_to_be_written = filteredImg if disparity_filter else displ
disparity = np.float64(np.copy(disparity_to_be_written)) / 16.0

# Crop and resize, due to baseline, a part of the image on the left can't be matched with the one on the right
disparity = resize(disparity[:, num_disparities:], (left_color_image.shape[1], left_color_image.shape[0]))
disparity = np.float32(np.copy(disparity_to_be_written)) / 16.0

# Triangulation
depth = (1.0 / disparity) * baseline * focal_length
Expand Down
2 changes: 1 addition & 1 deletion blenderproc/scripts/visHdf5Files.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
default_flow_keys = ["forward_flow", "backward_flow"]
default_segmap_keys = ["segmap", ".*_segmaps"]
default_segcolormap_keys = ["segcolormap"]
default_depth_keys = ["distance", "depth"]
default_depth_keys = ["distance", "depth", "stereo-depth"]
all_default_keys = default_rgb_keys + default_flow_keys + default_segmap_keys + default_segcolormap_keys + default_depth_keys
default_depth_max = 20

Expand Down
13 changes: 8 additions & 5 deletions examples/advanced/stereo_matching/README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
# Stereo Matching
![](../../../images/stereo_matching_stereo_pair.jpg)
![](../../../images/stereo_matching_stereo_depth.jpg)
<p align="center">
<img src="../../../images/stereo_matching_color_left.jpg" alt="Front readme image" width=400>
<img src="../../../images/stereo_matching_color_right.jpg" alt="Front readme image" width=400>
</p>
<p align="center">
<img src="../../../images/stereo_matching_stereo_depth.jpg" alt="Front readme image" width=400>
</p>

In the first row we can see the rendered stereo RGB images, left and right respectively, and beneath them we can view
the computed depth image using stereo matching. Note that due to a high discrepancy between the TV and the rest
of the rendered scene, the visualization is not descriptive enough. This discrepancy or high depth values at the TV
is due to a lack of gradient or useful features for stereo matching in this area. However, the depth values in other
the computed depth image using stereo matching. The high depth values from the uniform yellow area to the right are due to a lack of gradient or useful features for stereo matching in this area. However, the depth values in other
areas are consistent and close to the rendered depth images.

## Usage
Expand Down
5 changes: 4 additions & 1 deletion examples/advanced/stereo_matching/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,15 @@
bproc.renderer.enable_depth_output(activate_antialiasing=False)
bproc.material.add_alpha_channel_to_textures(blurry_edges=True)
bproc.renderer.toggle_stereo(True)
bproc.renderer.set_max_amount_of_samples(50)

# render the whole pipeline
data = bproc.renderer.render()

# Apply stereo matching to each pair of images
data["stereo-depth"], data["disparity"] = bproc.postprocessing.stereo_global_matching(data["colors"], disparity_filter=False)
data["stereo-depth"], data["disparity"] = bproc.postprocessing.stereo_global_matching(data["colors"],
disparity_filter=False,
depth_completion=False)

# write the data to a .hdf5 container
bproc.writer.write_hdf5(args.output_dir, data)
Binary file added images/stereo_matching_color_left.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/stereo_matching_color_right.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified images/stereo_matching_stereo_depth.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed images/stereo_matching_stereo_pair.jpg
Binary file not shown.

0 comments on commit 9f42f73

Please sign in to comment.