Skip to content

Commit

Permalink
Merge branch '343-factorize_rectif_grid_initialisation' into 'master'
Browse files Browse the repository at this point in the history
Passer l'objet RectificationGrid en argument de epipolar_triangulation

Closes #343

See merge request 3d/cars-park/shareloc!210
  • Loading branch information
dyoussef committed Feb 28, 2025
2 parents 7ef1413 + 3e58115 commit fb8d82f
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 49 deletions.
23 changes: 6 additions & 17 deletions shareloc/geofunctions/triangulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import numpy as np

# Shareloc imports
from shareloc.geofunctions.rectification_grid import RectificationGrid
from shareloc.geomodels.los import LOS
from shareloc.proj_utils import coordinates_conversion

Expand Down Expand Up @@ -207,8 +206,6 @@ def epipolar_triangulation(
right_min_max=None,
residues=False,
fill_nan=False,
is_displacement_grid=False,
interpolator="linear",
):
"""
epipolar triangulation
Expand All @@ -223,10 +220,10 @@ def epipolar_triangulation(
:type geometrical_model_left: GeomodelTemplate
:param geometrical_model_right: right image geometrical model
:type geometrical_model_right: GeomodelTemplate
:param grid_left: left rectification grid filename
:type grid_left: str
:param grid_right: right rectification grid filename
:type grid_right: str
:param grid_left: interpolated left rectification grid
:type grid_left: shareloc.geofunctions.rectification_grid.RectificationGrid
:param grid_right: interpolated right rectification grid
:type grid_right: shareloc.geofunctions.rectification_grid.RectificationGrid
:param left_min_max: left min/max for los creation, if None model min/max will be used
:type left_min_max: list
:param right_min_max: right min/max for los creation, if None model min/max will be used
Expand Down Expand Up @@ -254,17 +251,9 @@ def epipolar_triangulation(
else:
raise KeyError("matches type should be sift or disp")

# interpolate left
rectif_grid_left = RectificationGrid(
grid_left, is_displacement_grid=is_displacement_grid, interpolator=interpolator
)
rectif_grid_right = RectificationGrid(
grid_right, is_displacement_grid=is_displacement_grid, interpolator=interpolator
)

# interpolate_right
matches_sensor_left = rectif_grid_left.interpolate(epi_pos_left)
matches_sensor_right = rectif_grid_right.interpolate(epi_pos_right)
matches_sensor_left = grid_left.interpolate(epi_pos_left)
matches_sensor_right = grid_right.interpolate(epi_pos_right)
matches_sensor = np.concatenate((matches_sensor_left, matches_sensor_right), axis=1)

# triangulate positions in sensor geometry
Expand Down
78 changes: 46 additions & 32 deletions tests/geofunctions/test_triangulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
from scipy import __version__

# Shareloc imports
from shareloc.geofunctions.rectification_grid import RectificationGrid
from shareloc.geofunctions.triangulation import distance_point_los, epipolar_triangulation, sensor_triangulation
from shareloc.geomodels import GeoModel

Expand Down Expand Up @@ -137,11 +138,14 @@ def test_epi_triangulation_sift():
grid_left_filename = os.path.join(data_path(), "rectification_grids", "left_epipolar_grid.tif")
grid_right_filename = os.path.join(data_path(), "rectification_grids", "right_epipolar_grid.tif")

rectif_grid_left = RectificationGrid(grid_left_filename, is_displacement_grid=True)
rectif_grid_right = RectificationGrid(grid_right_filename, is_displacement_grid=True)

matches_filename = os.path.join(data_path(), "triangulation", "matches-crop.npy")
matches = np.load(matches_filename)

point_ecef, __, __ = epipolar_triangulation(
matches, None, "sift", grid_left, grid_right, grid_left_filename, grid_right_filename, is_displacement_grid=True
matches, None, "sift", grid_left, grid_right, rectif_grid_left, rectif_grid_right
)
valid = [4584341.37359843123704195022583, 572313.675204274943098425865173, 4382784.51356450468301773071289]
assert valid == pytest.approx(point_ecef[0, :], abs=0.5)
Expand All @@ -151,16 +155,17 @@ def test_epi_triangulation_sift():
# issue 328
interpolator = "cubic_legacy"

rectif_grid_left = RectificationGrid(grid_left_filename, is_displacement_grid=True, interpolator=interpolator)
rectif_grid_right = RectificationGrid(grid_right_filename, is_displacement_grid=True, interpolator=interpolator)

point_ecef_cubic, __, __ = epipolar_triangulation(
matches,
None,
"sift",
grid_left,
grid_right,
grid_left_filename,
grid_right_filename,
is_displacement_grid=True,
interpolator=interpolator,
rectif_grid_left,
rectif_grid_right,
)

assert point_ecef_cubic == pytest.approx(point_ecef, abs=0.115)
Expand All @@ -185,6 +190,9 @@ def test_epi_triangulation_sift_rpc():
grid_left_filename = os.path.join(data_path(), "rectification_grids", "left_epipolar_grid.tif")
grid_right_filename = os.path.join(data_path(), "rectification_grids", "right_epipolar_grid.tif")

rectif_grid_left = RectificationGrid(grid_left_filename, is_displacement_grid=True)
rectif_grid_right = RectificationGrid(grid_right_filename, is_displacement_grid=True)

matches_filename = os.path.join(data_path(), "triangulation", "matches-crop.npy")
matches = np.load(matches_filename)

Expand All @@ -194,9 +202,8 @@ def test_epi_triangulation_sift_rpc():
"sift",
geom_model_left,
geom_model_right,
grid_left_filename,
grid_right_filename,
is_displacement_grid=True,
rectif_grid_left,
rectif_grid_right,
)

point_ecef_optim, __, __ = epipolar_triangulation(
Expand All @@ -205,9 +212,8 @@ def test_epi_triangulation_sift_rpc():
"sift",
geom_model_left_optim,
geom_model_right_optim,
grid_left_filename,
grid_right_filename,
is_displacement_grid=True,
rectif_grid_left,
rectif_grid_right,
)
valid = [4584341.37359843123704195022583, 572313.675204274943098425865173, 4382784.51356450468301773071289]
# print(valid - point_ecef[0,:])
Expand All @@ -231,6 +237,9 @@ def test_epi_triangulation_sift_rpc_loc_grid():
grid_left_filename = os.path.join(data_path(), "rectification_grids", id_scene, "left_epi_grid.tif")
grid_right_filename = os.path.join(data_path(), "rectification_grids", id_scene, "right_epi_grid.tif")

rectif_grid_left = RectificationGrid(grid_left_filename, is_displacement_grid=False)
rectif_grid_right = RectificationGrid(grid_right_filename, is_displacement_grid=False)

matches = np.array(
[
[300, 400, 302, 420],
Expand All @@ -244,9 +253,8 @@ def test_epi_triangulation_sift_rpc_loc_grid():
"sift",
geom_model_left,
geom_model_right,
grid_left_filename,
grid_right_filename,
is_displacement_grid=False,
rectif_grid_left,
rectif_grid_right,
)

valid = [4733007.44057619, 2859047.05062282, 3168330.19887048]
Expand Down Expand Up @@ -311,6 +319,9 @@ def test_epi_triangulation_disp_rpc():
grid_left_filename = os.path.join(data_path(), "rectification_grids", "left_epipolar_grid.tif")
grid_right_filename = os.path.join(data_path(), "rectification_grids", "right_epipolar_grid.tif")

rectif_grid_left = RectificationGrid(grid_left_filename, is_displacement_grid=True)
rectif_grid_right = RectificationGrid(grid_right_filename, is_displacement_grid=True)

disp_filename = os.path.join(data_path(), "triangulation", "disparity-crop.pickle")
with open(disp_filename, "rb") as disp_file:
disp = pickle.load(disp_file)
Expand All @@ -320,10 +331,9 @@ def test_epi_triangulation_disp_rpc():
"disp",
geom_model_left,
geom_model_right,
grid_left_filename,
grid_right_filename,
rectif_grid_left,
rectif_grid_right,
residues=True,
is_displacement_grid=True,
)

point_ecef_optim, point_wsg84_optim, _ = epipolar_triangulation(
Expand All @@ -332,10 +342,9 @@ def test_epi_triangulation_disp_rpc():
"disp",
geom_model_left_optim,
geom_model_right_optim,
grid_left_filename,
grid_right_filename,
rectif_grid_left,
rectif_grid_right,
residues=True,
is_displacement_grid=True,
)

# open cloud
Expand Down Expand Up @@ -375,6 +384,9 @@ def test_epi_triangulation_disp_rpc_roi():
grid_left_filename = os.path.join(data_path(), "rectification_grids", "left_epipolar_grid_ventoux.tif")
grid_right_filename = os.path.join(data_path(), "rectification_grids", "right_epipolar_grid_ventoux.tif")

rectif_grid_left = RectificationGrid(grid_left_filename, is_displacement_grid=True)
rectif_grid_right = RectificationGrid(grid_right_filename, is_displacement_grid=True)

disp_filename = os.path.join(data_path(), "triangulation", "disp1_ref.pickle")
with open(disp_filename, "rb") as disp_file:
disp = pickle.load(disp_file)
Expand All @@ -384,11 +396,10 @@ def test_epi_triangulation_disp_rpc_roi():
"disp",
geom_model_left,
geom_model_right,
grid_left_filename,
grid_right_filename,
rectif_grid_left,
rectif_grid_right,
residues=True,
fill_nan=True,
is_displacement_grid=True,
)

point_ecef_optim, point_wgs84_optim, __ = epipolar_triangulation(
Expand All @@ -397,11 +408,10 @@ def test_epi_triangulation_disp_rpc_roi():
"disp",
geom_model_left_optim,
geom_model_right_optim,
grid_left_filename,
grid_right_filename,
rectif_grid_left,
rectif_grid_right,
residues=True,
fill_nan=True,
is_displacement_grid=True,
)

np.testing.assert_allclose(point_ecef[:, 0], point_ecef_optim[:, 0], 0, 2e-7)
Expand Down Expand Up @@ -445,6 +455,9 @@ def test_epi_triangulation_disp_grid():
grid_left_filename = os.path.join(data_path(), "rectification_grids", "left_epipolar_grid.tif")
grid_right_filename = os.path.join(data_path(), "rectification_grids", "right_epipolar_grid.tif")

rectif_grid_left = RectificationGrid(grid_left_filename, is_displacement_grid=True)
rectif_grid_right = RectificationGrid(grid_right_filename, is_displacement_grid=True)

disp_filename = os.path.join(data_path(), "triangulation", "disparity-crop.pickle")
with open(disp_filename, "rb") as disp_file:
disp = pickle.load(disp_file)
Expand All @@ -455,10 +468,9 @@ def test_epi_triangulation_disp_grid():
"disp",
gri_left,
gri_right,
grid_left_filename,
grid_right_filename,
rectif_grid_left,
rectif_grid_right,
residues=True,
is_displacement_grid=True,
)

# open cloud
Expand Down Expand Up @@ -491,6 +503,9 @@ def test_epi_triangulation_disp_grid_masked():
grid_left_filename = os.path.join(data_path(), "rectification_grids", "left_epipolar_grid.tif")
grid_right_filename = os.path.join(data_path(), "rectification_grids", "right_epipolar_grid.tif")

rectif_grid_left = RectificationGrid(grid_left_filename, is_displacement_grid=True)
rectif_grid_right = RectificationGrid(grid_right_filename, is_displacement_grid=True)

disp_filename = os.path.join(data_path(), "triangulation", "disparity-crop.pickle")
with open(disp_filename, "rb") as disp_file:
disp = pickle.load(disp_file)
Expand All @@ -501,10 +516,9 @@ def test_epi_triangulation_disp_grid_masked():
"disp",
gri_left,
gri_right,
grid_left_filename,
grid_right_filename,
rectif_grid_left,
rectif_grid_right,
residues=True,
is_displacement_grid=True,
)

assert np.array_equal(point_ecef[0, :], [0, 0, 0])

0 comments on commit fb8d82f

Please sign in to comment.