Skip to content

Commit

Permalink
Merge branch '971-deal_with_margins' into 'master'
Browse files Browse the repository at this point in the history
Gestion des marges sur la grille de rectification

Closes #971

See merge request 3d/cars-park/cars!798
  • Loading branch information
dyoussef committed Jan 23, 2025
2 parents a610e47 + 7380dd1 commit 75ea689
Show file tree
Hide file tree
Showing 21 changed files with 101 additions and 12 deletions.
26 changes: 19 additions & 7 deletions cars/applications/resampling/resampling_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,13 +310,26 @@ def resample_image(
oversampling = int(res_x)
assert res_x == oversampling

grid_origin_x = grid_reader.transform[2]
grid_origin_y = grid_reader.transform[5]
assert grid_origin_x == grid_origin_y
grid_margin = -grid_origin_x / oversampling - 0.5
assert grid_margin == int(grid_margin)
grid_margin = int(grid_margin)

# Convert resampled region to grid region with oversampling
grid_region = [
math.floor(xmin / oversampling),
math.floor(ymin / oversampling),
math.ceil(xmax / oversampling),
math.ceil(ymax / oversampling),
]
grid_region = np.array(
[
math.floor(xmin / oversampling),
math.floor(ymin / oversampling),
math.ceil(xmax / oversampling),
math.ceil(ymax / oversampling),
]
)
# Out region of epipolar image
out_region = oversampling * grid_region
# Grid region
grid_region += grid_margin

grid_window = Window.from_slices(
(grid_region[1], grid_region[3] + 1),
Expand Down Expand Up @@ -399,7 +412,6 @@ def resample_image(
).astype(int)

# extract exact region
out_region = oversampling * np.array(grid_region)
ext_region = block_region - out_region
block_resamp = block_resamp[
...,
Expand Down
15 changes: 12 additions & 3 deletions cars/core/geometry/shareloc_geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ def __init__(
geoid=None,
default_alt=None,
pairs_for_roi=None,
rectification_grid_margin=0,
):
super().__init__(
geometry_plugin,
Expand All @@ -75,6 +76,7 @@ def __init__(
self.dem_roi = None
self.roi_shareloc = None
self.elevation = None
self.rectification_grid_margin = rectification_grid_margin

# compute roi only when generating geometry object with dem
# even if dem is None
Expand Down Expand Up @@ -136,12 +138,15 @@ def get_roi(self, pairs_for_roi, epsg, margin=0.006):
coords_list.extend(self.image_envelope(image1, geomodel1))
# Footprint of right image
coords_list.extend(self.image_envelope(image2, geomodel2))
# Epipolar extent
# Footprint of rectification grid (with margins)
image1 = SharelocGeometry.load_image(image1)
geomodel1 = self.load_geom_model(geomodel1)
geomodel2 = self.load_geom_model(geomodel2)
epipolar_extent = rectif.get_epipolar_extent(
image1, geomodel1, geomodel2
image1,
geomodel1,
geomodel2,
grid_margin=self.rectification_grid_margin,
)
lat_min, lon_min, lat_max, lon_max = list(epipolar_extent)
coords_list.extend([(lon_min, lat_min), (lon_max, lat_max)])
Expand Down Expand Up @@ -380,6 +385,7 @@ def generate_epipolar_grids(
shareloc_model2,
self.elevation,
epi_step=epipolar_step,
margin=self.rectification_grid_margin,
)

# rearrange output to match the expected structure of CARS
Expand All @@ -390,7 +396,10 @@ def generate_epipolar_grids(
epipolar_size_x = int(np.floor(epipolar_size_x))
epipolar_size_y = int(np.floor(epipolar_size_y))

origin = [0.0, 0.0]
origin = [
float(-self.rectification_grid_margin * epipolar_step),
float(-self.rectification_grid_margin * epipolar_step),
]
spacing = [float(epipolar_step), float(epipolar_step)]

# alt_to_disp_ratio does not consider image resolution
Expand Down
3 changes: 2 additions & 1 deletion cars/pipelines/parameters/sensor_inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,8 @@ def check_geometry_plugin(conf_inputs, conf_advanced, conf_geom_plugin):
# Initialize the desired geometry plugin without elevation information
geom_plugin_without_dem_and_geoid = (
AbstractGeometry( # pylint: disable=abstract-class-instantiated
conf_geom_plugin, default_alt=sens_cst.CARS_DEFAULT_ALT
conf_geom_plugin,
default_alt=sens_cst.CARS_DEFAULT_ALT,
)
)

Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ install_requires =
cars-resample==0.1.*
cars-filter==0.1.*
vlsift==0.1.*
shareloc==0.2.4
shareloc==0.2.5
bulldozer-dtm==1.0.*

package_dir =
Expand Down
23 changes: 23 additions & 0 deletions tests/core/geometry/test_shareloc_geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,29 @@ def test_get_roi():
]
np.testing.assert_allclose(roi, ref_roi)

# Add a 5 pixel margin on rectification grid
geo_plugin_with_margin_on_grid = (
AbstractGeometry( # pylint: disable=abstract-class-instantiated
"SharelocGeometry",
dem=dem,
geoid=geoid,
rectification_grid_margin=5,
)
)
pairs_for_roi = [(sensor1, geomodel1, sensor2, geomodel2)]
roi = geo_plugin_with_margin_on_grid.get_roi(
pairs_for_roi, 4326, margin=0.005
)
ref_roi = [
44.198651,
5.185954,
44.21382,
5.203201,
]
# Returned ROI is the footprint of the rectification
# It takes into account the 5 pixels margin
np.testing.assert_allclose(roi, ref_roi)


@pytest.mark.unit_tests
def test_sensors_arrangement_left_right():
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
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_0.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_aux_filling_2.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_with_color.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_with_color.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.
44 changes: 44 additions & 0 deletions tests/test_end2end.py
Original file line number Diff line number Diff line change
Expand Up @@ -3265,6 +3265,15 @@ def test_end2end_ventoux_with_color():
input_config_sparse_res["output"].update(output_config)

sparse_res_pipeline = default.DefaultPipeline(input_config_sparse_res)

# Add a margin on recitification grid
# TODO change this when the new API with
# bicubic interpolator is implemented
geom_plugin_1 = sparse_res_pipeline.geom_plugin_with_dem_and_geoid
geom_plugin_2 = sparse_res_pipeline.geom_plugin_without_dem_and_geoid
geom_plugin_1.rectification_grid_margin = 5
geom_plugin_2.rectification_grid_margin = 5

sparse_res_pipeline.run()

out_dir = input_config_sparse_res["output"]["directory"]
Expand Down Expand Up @@ -3338,6 +3347,15 @@ def test_end2end_ventoux_with_color():
input_config_dense_dsm["output"]["product_level"] = ["dsm"]

dense_dsm_pipeline = default.DefaultPipeline(input_config_dense_dsm)

# Add a margin on recitification grid
# TODO change this when the new API with
# bicubic interpolator is implemented
geom_plugin_1 = dense_dsm_pipeline.geom_plugin_with_dem_and_geoid
geom_plugin_2 = dense_dsm_pipeline.geom_plugin_without_dem_and_geoid
geom_plugin_1.rectification_grid_margin = 5
geom_plugin_2.rectification_grid_margin = 5

dense_dsm_pipeline.run()

out_dir = input_config_dense_dsm["output"]["directory"]
Expand Down Expand Up @@ -4755,6 +4773,19 @@ def test_end2end_paca_with_mask():
dense_dsm_pipeline_bulldozer = default.DefaultPipeline(
input_config_dense_dsm
)

# Add a margin on recitification grid
# TODO change this when the new API with
# bicubic interpolator is implemented
geom_plugin_1 = (
dense_dsm_pipeline_bulldozer.geom_plugin_with_dem_and_geoid
)
geom_plugin_2 = (
dense_dsm_pipeline_bulldozer.geom_plugin_without_dem_and_geoid
)
geom_plugin_1.rectification_grid_margin = 5
geom_plugin_2.rectification_grid_margin = 5

dense_dsm_pipeline_bulldozer.run()

out_dir = input_config_dense_dsm["output"]["directory"]
Expand Down Expand Up @@ -4851,6 +4882,19 @@ def test_end2end_paca_with_mask():
dense_dsm_pipeline_matches = default.DefaultPipeline(
input_config_dense_dsm
)

# Add a margin on recitification grid
# TODO change this when the new API with
# bicubic interpolator is implemented
geom_plugin_1 = (
dense_dsm_pipeline_matches.geom_plugin_with_dem_and_geoid
)
geom_plugin_2 = (
dense_dsm_pipeline_matches.geom_plugin_without_dem_and_geoid
)
geom_plugin_1.rectification_grid_margin = 5
geom_plugin_2.rectification_grid_margin = 5

dense_dsm_pipeline_matches.run()

# copy2(
Expand Down

0 comments on commit 75ea689

Please sign in to comment.