Skip to content

Commit

Permalink
Merge branch '948-new-margins-process' into 'master'
Browse files Browse the repository at this point in the history
Resolve "Pandora: update de la récupération des marges"

Closes #948

See merge request 3d/cars-park/cars!796
  • Loading branch information
dyoussef committed Jan 21, 2025
2 parents 9e52c32 + 2703f0e commit 731556d
Show file tree
Hide file tree
Showing 61 changed files with 86 additions and 14 deletions.
30 changes: 27 additions & 3 deletions cars/applications/dense_matching/census_mccnn_sgm.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,14 @@
# Third party imports
import affine
import numpy as np
import pandora
import rasterio
import xarray as xr
from affine import Affine
from json_checker import And, Checker, Or
from pandora.check_configuration import check_pipeline_section
from pandora.img_tools import add_global_disparity
from pandora.state_machine import PandoraMachine
from scipy.ndimage import generic_filter

import cars.applications.dense_matching.dense_matching_constants as dm_cst
Expand Down Expand Up @@ -225,6 +228,27 @@ def check_conf(self, conf):
self.loader = pandora_loader
self.corr_config = collections.OrderedDict(pandora_loader.get_conf())

# Instantiate margins from pandora check conf
# create the dataset
fake_dataset = xr.Dataset(
data_vars={},
coords={
"band_im": [(None)],
"row": np.arange(10),
"col": np.arange(10),
},
attrs={"disparity_source": [-1, 1]},
)
# Import plugins before checking configuration
pandora.import_plugin()
pandora_machine = PandoraMachine()
corr_config_pipeline = {"pipeline": dict(self.corr_config["pipeline"])}

_ = check_pipeline_section(
corr_config_pipeline, fake_dataset, fake_dataset, pandora_machine
)
self.margins = pandora_machine.margins.global_margins

overloaded_conf["loader_conf"] = self.corr_config

application_schema = {
Expand Down Expand Up @@ -299,7 +323,7 @@ def get_margins_fun(self, grid_left, disp_range_grid):
matching method, to use during resampling
:param grid_left: left epipolar grid
:param disp_min_grid: minimum and maximum disparity grid
:param disp_range_grid: minimum and maximum disparity grid
:return: function that generates margin for given roi
"""
Expand Down Expand Up @@ -408,8 +432,8 @@ def margins_wrapper(row_min, row_max, col_min, col_max):
disp_max = int(math.ceil(disp_max))

# Compute margins for the correlator
# TODO use loader correlators
margins = dm_tools.get_margins(disp_min, disp_max, self.corr_config)
margins = dm_tools.get_margins(self.margins, disp_min, disp_max)

return margins

return margins_wrapper
Expand Down
5 changes: 2 additions & 3 deletions cars/applications/dense_matching/dense_matching.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,13 @@ def get_optimal_tile_size(self, disp_range_grid, max_ram_per_worker):
"""

@abstractmethod
def get_margins_fun(self, grid_left, disp_min_grid, disp_max_grid):
def get_margins_fun(self, grid_left, disp_range_grid):
"""
Get Margins function that generates margins needed by
matching method, to use during resampling
:param grid_left: left epipolar grid
:param disp_min_grid: minimum disparity grid
:param disp_max_grid: maximum disparity grid
:param disp_range_grid: minimum and maximum disparity grid
:return: function that generates margin for given roi
"""
Expand Down
44 changes: 39 additions & 5 deletions cars/applications/dense_matching/dense_matching_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@

import numpy as np
import pandora
import pandora.marge
import xarray as xr

# Third party imports
Expand All @@ -55,19 +54,54 @@
from cars.core import constants_disparity as cst_disp


def get_margins(disp_min, disp_max, corr_cfg):
def get_margins(margin, disp_min, disp_max):
"""
Get margins for the dense matching steps
:param margin: margins object
:type margin: Margins
:param disp_min: Minimum disparity
:type disp_min: int
:param disp_max: Maximum disparity
:type disp_max: int
:param corr_cfg: Correlator configuration
:type corr_cfg: dict
:return: margins of the matching algorithm used
"""
return pandora.marge.get_margins(disp_min, disp_max, corr_cfg["pipeline"])

corner = ["left", "up", "right", "down"]
col = np.arange(len(corner))

left_margins = [
margin.left + disp_max,
margin.up,
margin.right - disp_min,
margin.down,
]
right_margins = [
margin.left - disp_min,
margin.up,
margin.right + disp_max,
margin.down,
]
same_margins = [
max(left, right)
for left, right in zip(left_margins, right_margins, strict=True)
]

margins = xr.Dataset(
{
"left_margin": (
["col"],
same_margins,
)
},
coords={"col": col},
)
margins["right_margin"] = xr.DataArray(same_margins, dims=["col"])

margins.attrs["disp_min"] = disp_min
margins.attrs["disp_max"] = disp_max

return margins


def get_masks_from_pandora(
Expand Down
3 changes: 2 additions & 1 deletion cars/pipelines/default/default_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -1639,7 +1639,8 @@ def sensor_to_depth_maps(self): # noqa: C901
# Get margins used in dense matching,
dense_matching_margins_fun = (
self.dense_matching_app.get_margins_fun(
self.pairs[pair_key]["corrected_grid_left"], disp_range_grid
self.pairs[pair_key]["corrected_grid_left"],
disp_range_grid,
)
)

Expand Down
12 changes: 11 additions & 1 deletion tests/applications/dense_matching/test_dense_matching_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,17 @@ def test_check_conf_with_error(
"min_elevation_offset": 20,
"max_elevation_offset": max_offset, # should be > min
"generate_confidence_intervals": confidence_intervals,
"loader_conf": {"pipeline": {"disparity": {}}},
"loader_conf": {
"input": {},
"pipeline": {
"matching_cost": {
"matching_cost_method": "census",
"window_size": 5,
"subpix": 1,
},
"disparity": {"disparity_method": "wta"},
},
},
}
with pytest.raises(expected_error):
_ = CensusMccnnSgm(conf)
Binary file not shown.
Binary file modified tests/data/ref_output/cloud1_ref_pandora
Binary file not shown.
Binary file modified tests/data/ref_output/color_end2end_gizeh_fill.tif
Binary file not shown.
Binary file modified tests/data/ref_output/color_end2end_paca_aux_filling.tif
Binary file not shown.
Binary file modified tests/data/ref_output/color_end2end_paca_aux_filling_1.tif
Binary file not shown.
Binary file modified tests/data/ref_output/color_end2end_paca_matches_filling.tif
Binary file not shown.
Binary file modified tests/data/ref_output/color_end2end_ventoux.tif
Binary file not shown.
Binary file modified tests/data/ref_output/color_end2end_ventoux_egm96.tif
Binary file not shown.
Binary file not shown.
Binary file modified tests/data/ref_output/color_end2end_ventoux_no_elevation.tif
Binary file not shown.
Binary file modified tests/data/ref_output/color_end2end_ventoux_no_srtm.tif
Binary file not shown.
Binary file modified tests/data/ref_output/color_end2end_ventoux_quality_stats.tif
Binary file not shown.
Binary file modified tests/data/ref_output/color_end2end_ventoux_with_color.tif
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified tests/data/ref_output/dsm_end2end_gizeh_fill.tif
Binary file not shown.
Binary file modified tests/data/ref_output/dsm_end2end_paca_bulldozer.tif
Binary file not shown.
Binary file modified tests/data/ref_output/dsm_end2end_paca_matches_filling.tif
Binary file not shown.
Binary file modified tests/data/ref_output/dsm_end2end_ventoux.tif
Binary file not shown.
Binary file modified tests/data/ref_output/dsm_end2end_ventoux_egm96.tif
Binary file not shown.
Binary file modified tests/data/ref_output/dsm_end2end_ventoux_egm96_custom_geoid.tif
Binary file not shown.
Binary file modified tests/data/ref_output/dsm_end2end_ventoux_no_elevation.tif
Binary file not shown.
Binary file modified tests/data/ref_output/dsm_end2end_ventoux_no_srtm.tif
Binary file not shown.
Binary file modified tests/data/ref_output/dsm_end2end_ventoux_quality_stats.tif
Binary file not shown.
Binary file modified tests/data/ref_output/dsm_end2end_ventoux_with_classif.tif
Binary file not shown.
Binary file modified tests/data/ref_output/dsm_end2end_ventoux_with_color.tif
Binary file not shown.
Binary file modified tests/data/ref_output/dsm_end2end_ventoux_with_snap_to_img1.tif
Binary file not shown.
Binary file modified tests/data/ref_output/dsm_mean_end2end_ventoux_quality_stats.tif
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified tests/data/ref_output/dsm_std_end2end_ventoux_quality_stats.tif
Binary file not shown.
Binary file modified tests/data/ref_output/epi_pc_X_end2end_ventoux_no_elevation.tif
Binary file not shown.
Binary file modified tests/data/ref_output/epi_pc_Y_end2end_ventoux_no_elevation.tif
Binary file not shown.
Binary file modified tests/data/ref_output/epi_pc_Z_end2end_ventoux_no_elevation.tif
Binary file not shown.
Binary file modified tests/data/ref_output/filling_end2end_gizeh_fill.tif
Binary file not shown.
Binary file modified tests/data/ref_output/filling_end2end_ventoux_no_elevation.tif
Binary file not shown.
Binary file modified tests/data/ref_output/mask_end2end_gizeh_fill.tif
Binary file not shown.
Binary file modified tests/data/ref_output/mask_end2end_paca_bulldozer.tif
Binary file not shown.
Binary file modified tests/data/ref_output/mask_end2end_paca_matches_filling.tif
Binary file not shown.
Binary file modified tests/data/ref_output/mask_end2end_ventoux_no_elevation.tif
Binary file not shown.
Binary file modified tests/data/ref_output/performance_map_end2end_ventoux_split.tif
Binary file not shown.
Binary file not shown.
6 changes: 5 additions & 1 deletion tests/pipelines/test_wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
# Third party imports
import pytest
from pandora.img_tools import get_metadata
from pandora.margins import Margins

# CARS imports
import cars.applications.dense_matching.dense_matching_tools as dense_match
Expand Down Expand Up @@ -111,8 +112,11 @@ def test_epipolar_pipeline(
global_disp_min = -19
global_disp_max = 15

# cumulative margins between sgm (40) and matching_cost (ws/2)
margins = Margins(42, 42, 42, 42)

initial_margins = dense_match.get_margins(
global_disp_min, global_disp_max, corr_cfg
margins, global_disp_min, global_disp_max
)
pandora_margins = initial_margins["left_margin"].values

Expand Down

0 comments on commit 731556d

Please sign in to comment.