Skip to content

Commit

Permalink
Merge branch '110-add-georef-to-disparity-map' into 'release'
Browse files Browse the repository at this point in the history
Resolve "Formater: mise en place du géoréférencement"

See merge request 3d/PandoraBox/pandora2d!96
  • Loading branch information
lecontm committed May 22, 2024
2 parents f830bb4 + 16acf69 commit 0939322
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 17 deletions.
Binary file modified data_samples/images/maricopa.zip
Binary file not shown.
21 changes: 18 additions & 3 deletions pandora2d/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,28 @@ def save_dataset(dataset: xr.Dataset, cfg: Dict, output: str) -> None:
mkdir_p(output)

# save disp map for row
write_data_array(dataset["row_map"], os.path.join(output, "row_disparity.tif"))
write_data_array(
dataset["row_map"],
os.path.join(output, "row_disparity.tif"),
crs=dataset.attrs["crs"],
transform=dataset.attrs["transform"],
)

# save disp map for columns
write_data_array(dataset["col_map"], os.path.join(output, "columns_disparity.tif"))
write_data_array(
dataset["col_map"],
os.path.join(output, "columns_disparity.tif"),
crs=dataset.attrs["crs"],
transform=dataset.attrs["transform"],
)

# save correlation score
write_data_array(dataset["correlation_score"], os.path.join(output, "correlation_score.tif"))
write_data_array(
dataset["correlation_score"],
os.path.join(output, "correlation_score.tif"),
crs=dataset.attrs["crs"],
transform=dataset.attrs["transform"],
)


def dataset_disp_maps(
Expand Down
6 changes: 5 additions & 1 deletion pandora2d/state_machine.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,11 @@ def disp_maps_run(self, cfg: Dict[str, dict], input_step: str) -> None:
map_col,
self.cost_volumes.coords,
correlation_score,
{"invalid_disp": cfg["pipeline"]["disparity"]["invalid_disparity"]},
{
"invalid_disp": cfg["pipeline"]["disparity"]["invalid_disparity"],
"crs": self.left_img.crs,
"transform": self.left_img.transform,
},
)

def refinement_run(self, cfg: Dict[str, dict], input_step: str) -> None:
Expand Down
2 changes: 2 additions & 0 deletions tests/functional_tests/matching_cost/test_subpix.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ def create_datasets(self, data_left, data_right):
"crs": None,
"col_disparity_source": [-2, 2],
"row_disparity_source": [-2, 2],
"transform": None,
}

right = xr.Dataset(
Expand All @@ -66,6 +67,7 @@ def create_datasets(self, data_left, data_right):
"valid_pixels": 0,
"no_data_mask": 1,
"crs": None,
"transform": None,
}

return left, right
Expand Down
6 changes: 2 additions & 4 deletions tests/unit_tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,9 @@ def correct_input_cfg(left_img_path, right_img_path):
"input": {
"left": {
"img": left_img_path,
"nodata": "NaN",
},
"right": {
"img": right_img_path,
"nodata": -9999,
},
"right": {"img": right_img_path, "nodata": -9999},
"col_disparity": [-2, 2],
"row_disparity": [-2, 2],
}
Expand Down
66 changes: 59 additions & 7 deletions tests/unit_tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,21 @@
import numpy as np
import pytest
import xarray as xr
import rasterio
from skimage.io import imsave

from pandora2d import common
from rasterio import Affine
from pandora2d import common, run
from pandora2d.check_configuration import check_conf
from pandora2d.img_tools import create_datasets_from_inputs
from pandora2d import matching_cost, disparity, refinement
from pandora2d.state_machine import Pandora2DMachine


class TestSaveDataset:
"""Test save_dataset method"""

@pytest.fixture
def create_test_dataset(self):
def create_test_dataset(self, attributes):
"""
Create a test dataset
"""
Expand All @@ -59,21 +62,45 @@ def create_test_dataset(self):
dataarray_col = xr.DataArray(col, dims=dims, coords=coords)
dataarray_score = xr.DataArray(score, dims=dims, coords=coords)

dataset = xr.Dataset({"row_map": dataarray_row, "col_map": dataarray_col, "correlation_score": dataarray_score})
dataset = xr.Dataset(
{"row_map": dataarray_row, "col_map": dataarray_col, "correlation_score": dataarray_score},
attrs=attributes,
)

return dataset

def test_save_dataset(self, create_test_dataset, correct_input_cfg):
@pytest.mark.parametrize(
"attributes",
[
{"crs": "EPSG:32632", "transform": Affine(25.94, 0.00, -5278429.43, 0.00, -25.94, 14278941.03)},
{"crs": None, "transform": Affine(1.0, 0.0, 0.0, 0.0, 1.0, 0.0)},
],
)
def test_save_dataset(self, create_test_dataset, correct_input_cfg, attributes):
"""
Function for testing the dataset_save function
"""

common.save_dataset(create_test_dataset, correct_input_cfg, "./tests/res_test/")
assert os.path.exists("./tests/res_test/")

# Test columns disparity map
assert os.path.exists("./tests/res_test/columns_disparity.tif")
columns_disparity = rasterio.open("./tests/res_test/columns_disparity.tif")
assert columns_disparity.crs == attributes["crs"]
assert columns_disparity.transform == attributes["transform"]

# Test row disparity map
assert os.path.exists("./tests/res_test/row_disparity.tif")
row_disparity = rasterio.open("./tests/res_test/row_disparity.tif")
assert row_disparity.crs == attributes["crs"]
assert row_disparity.transform == attributes["transform"]

# Test correlation score map
assert os.path.exists("./tests/res_test/correlation_score.tif")
correlation_score = rasterio.open("./tests/res_test/correlation_score.tif")
assert correlation_score.crs == attributes["crs"]
assert correlation_score.transform == attributes["transform"]

os.remove("./tests/res_test/columns_disparity.tif")
os.remove("./tests/res_test/row_disparity.tif")
Expand Down Expand Up @@ -107,7 +134,7 @@ def left_image(self, tmp_path):
"""
Create a fake image to test dataset_disp_maps method
"""
image_path = tmp_path / "left_img.png"
image_path = tmp_path / "left_img.tif"
data = np.full((10, 10), 1, dtype=np.uint8)
imsave(image_path, data)

Expand All @@ -118,7 +145,7 @@ def right_image(self, tmp_path):
"""
Create a fake image to test dataset_disp_maps method
"""
image_path = tmp_path / "right_img.png"
image_path = tmp_path / "right_img.tif"
data = np.full((10, 10), 1, dtype=np.uint8)
imsave(image_path, data)

Expand Down Expand Up @@ -314,3 +341,28 @@ def test_dataset_disp_maps_with_pipeline_computation(self, roi, step, left_image
)

assert disparity_maps.equals(dataset_ground_truth)


def test_disparity_map_output_georef(correct_pipeline, correct_input_cfg):
"""
Test outputs georef with crs and transform
"""

img_left, img_right = create_datasets_from_inputs(input_config=correct_input_cfg["input"])

# Stock crs and transform information from input
img_left.attrs["crs"] = "EPSG:32632"
img_left.attrs["transform"] = Affine(25.94, 0.00, -5278429.43, 0.00, -25.94, 14278941.03)

pandora2d_machine = Pandora2DMachine()
# Delete refinement to fastest result
del correct_pipeline["pipeline"]["refinement"]

correct_input_cfg.update(correct_pipeline)

checked_cfg = check_conf(correct_input_cfg, pandora2d_machine)

dataset, _ = run(pandora2d_machine, img_left, img_right, checked_cfg)

assert "EPSG:32632" == dataset.attrs["crs"]
assert Affine(25.94, 0.00, -5278429.43, 0.00, -25.94, 14278941.03) == dataset.attrs["transform"]
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def result(self, left_img_path, right_img_path):
return img_tools.create_datasets_from_inputs(_make_input_section(left_img_path, right_img_path))

def test_use_function_from_pandora(self, mocker, input_section):
"""Test we use `create_datset_from_inputs` from pandora.
"""Test we use `create_dataset_from_inputs` from pandora.
We assume this function is well tested in Pandora and that we just need to test that we use it.
"""
Expand Down
2 changes: 1 addition & 1 deletion tests/unit_tests/test_matching_cost/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@

@pytest.fixture()
def squared_image_size():
return (10, 10)
return 10, 10


@pytest.fixture()
Expand Down

0 comments on commit 0939322

Please sign in to comment.