Skip to content

Commit

Permalink
Merge branch '157-masques-ajout-des-masques-dans-la-configuration' in…
Browse files Browse the repository at this point in the history
…to 'release'

Resolve "Masques: ajout des masques dans la configuration"

See merge request 3d/PandoraBox/pandora2d!134
  • Loading branch information
lecontm committed Jul 25, 2024
2 parents 68edf3a + 288229b commit 3330bab
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 6 deletions.
24 changes: 18 additions & 6 deletions docs/source/userguide/input.rst
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,16 @@ Input section is composed of the following keys:
- If the estimation step is not present

.. warning::
If interpolation is used as refinement method, row_disparity and col_disparity ranges must have a size greater than or equal to 2.
If interpolation is used as refinement method, row_disparity and col_disparity ranges must have a size greater than or equal to 2.


Image (left and right) and disparity (col_disparity and row_disparity) properties are composed of the following keys:

.. tabs::

.. tab:: Image properties
.. tab:: Image properties

.. list-table::
.. list-table::
:header-rows: 1

* - Name
Expand All @@ -68,10 +68,15 @@ Image (left and right) and disparity (col_disparity and row_disparity) propertie
- int, "NaN" or "inf"
- -9999
- No
* - *mask*
- Path to the mask
- string
- none
- No

.. tab:: Disparity properties

.. list-table::
.. list-table::
:header-rows: 1

* - Name
Expand All @@ -87,12 +92,18 @@ Image (left and right) and disparity (col_disparity and row_disparity) propertie
* - *range*
- The search radius (see :ref:`initial_disparity`)
- int >= 0
-
-
- Yes

.. warning::
With sad/ssd matching_cost_method in the pipeline (see :ref:`Sequencing`) , `nodata` only accepts `int` type.

.. note::
Only one-band masks are accepted by pandora2d. Mask must comply with the following convention :
- Value equal to 0 for valid pixel
- Value not equal to 0 for invalid pixel


**Example**

.. code:: json
Expand All @@ -103,7 +114,8 @@ Image (left and right) and disparity (col_disparity and row_disparity) propertie
{
"left": {
"img": "./data/left.tif",
"nodata": -9999
"nodata": -9999,
"mask": "./data/mask_left.tif"
},
"right": {
"img": "/data/right.tif",
Expand Down
5 changes: 5 additions & 0 deletions pandora2d/check_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
update_conf,
)

from pandora.check_configuration import rasterio_can_open
from pandora2d.state_machine import Pandora2DMachine


Expand Down Expand Up @@ -304,10 +305,12 @@ def get_roi_config(user_cfg: Dict[str, dict]) -> Dict[str, dict]:
"left": {
"img": And(str, rasterio_can_open_mandatory),
"nodata": Or(int, lambda input: np.isnan(input), lambda input: np.isinf(input)),
"mask": And(Or(str, lambda input: input is None), rasterio_can_open),
},
"right": {
"img": And(str, rasterio_can_open_mandatory),
"nodata": Or(int, lambda input: np.isnan(input), lambda input: np.isinf(input)),
"mask": And(Or(str, lambda input: input is None), rasterio_can_open),
},
"col_disparity": {"init": int, "range": And(int, lambda x: x >= 0)},
"row_disparity": {"init": int, "range": And(int, lambda x: x >= 0)},
Expand All @@ -317,9 +320,11 @@ def get_roi_config(user_cfg: Dict[str, dict]) -> Dict[str, dict]:
"input": {
"left": {
"nodata": -9999,
"mask": None,
},
"right": {
"nodata": -9999,
"mask": None,
},
}
}
Expand Down
53 changes: 53 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
import re

import pytest
import rasterio
from pandora.common import write_data_array
import xarray as xr


def pytest_collection_modifyitems(config, items):
Expand Down Expand Up @@ -149,3 +152,53 @@ def correct_multiband_input_cfg(left_rgb_path, right_rgb_path):
"row_disparity": {"init": 1, "range": 2},
}
}


@pytest.fixture
def mask_path(left_img_path, tmp_path):
"""Create a mask and save it in tmp"""

with rasterio.open(left_img_path) as src:
width = src.width
height = src.height

mask = xr.DataArray(data=0, dims=["height", "width"], coords={"height": range(height), "width": range(width)})
mask[0 : int(height / 2), 0 : int(width / 2)] = 1

path = tmp_path / "mask_left.tif"

write_data_array(
data_array=mask,
filename=str(path),
)

return path


@pytest.fixture
def correct_input_with_left_mask(left_img_path, right_img_path, mask_path):
return {
"input": {
"left": {"img": left_img_path, "nodata": -9999, "mask": str(mask_path)},
"right": {
"img": right_img_path,
},
"col_disparity": {"init": 0, "range": 2},
"row_disparity": {"init": 0, "range": 2},
}
}


@pytest.fixture
def correct_input_with_right_mask(left_img_path, right_img_path, mask_path):
return {
"input": {
"left": {
"img": left_img_path,
"nodata": -9999,
},
"right": {"img": right_img_path, "mask": str(mask_path)},
"col_disparity": {"init": 0, "range": 2},
"row_disparity": {"init": 0, "range": 2},
}
}
26 changes: 26 additions & 0 deletions tests/functional_tests/test_pipelines.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,29 @@ def test_optical_flow_configuration(run_pipeline, correct_input_cfg, correct_pip
# Check window_size and step parameters
assert matching_cost_cfg["window_size"] == refinement_cfg["window_size"]
assert matching_cost_cfg["step"] == refinement_cfg["step"]


@pytest.mark.parametrize("input_cfg", ["correct_input_with_left_mask", "correct_input_with_right_mask"])
def test_configuration_with_mask(run_pipeline, input_cfg, correct_pipeline_without_refinement, request):
"""
Description : Test mask configuration
"""
input_cfg = request.getfixturevalue(input_cfg)

configuration = {**input_cfg, **correct_pipeline_without_refinement}

run_dir = run_pipeline(configuration)

with open(run_dir / "output" / "cfg" / "config.json", encoding="utf8") as output_file:
output_config = json.load(output_file)

result = remove_extra_keys(output_config, configuration)

assert result == configuration
assert list(result["pipeline"].keys()) == list(configuration["pipeline"].keys()), "Pipeline order not respected"

# Test for report
with open(run_dir / "output" / "report.json", encoding="utf8") as report_file:
report = json.load(report_file)

assert report["statistics"]["disparity"].keys() == {"row", "col"}

0 comments on commit 3330bab

Please sign in to comment.