Skip to content

Commit

Permalink
Merge branch '76-disparite-initiale-variable-ajout-dans-le-fichier-de…
Browse files Browse the repository at this point in the history
…-configuration' into 'release'

Resolve "Disparité initiale variable: ajout dans le fichier de configuration"

See merge request 3d/PandoraBox/pandora2d!129
  • Loading branch information
lecontm committed Jul 23, 2024
2 parents 1083422 + d7e3e60 commit a6e6802
Show file tree
Hide file tree
Showing 27 changed files with 363 additions and 340 deletions.
4 changes: 2 additions & 2 deletions data_samples/json_conf_files/a_basic_pipeline.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
"img": "./maricopa/right.tif",
"nodata": -9999
},
"col_disparity": [-2, 2],
"row_disparity": [-2, 2]
"col_disparity": {"init": 0, "range": 2},
"row_disparity": {"init": 0, "range": 2}
},
"pipeline": {
"matching_cost": {
Expand Down
6 changes: 3 additions & 3 deletions data_samples/json_conf_files/a_dichotomy_pipeline.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
"img": "./maricopa/right.tif",
"nodata": -9999
},
"col_disparity": [-2, 2],
"row_disparity": [-2, 2]
"col_disparity": {"init": 0, "range": 2},
"row_disparity": {"init": 0, "range": 2}
},
"pipeline": {
"matching_cost": {
Expand All @@ -27,4 +27,4 @@
"filter": "bicubic"
}
}
}
}
14 changes: 7 additions & 7 deletions notebooks/introduction_and_basic_usage.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,8 @@
"input_config = {\n",
" \"left\": {\"img\": img_left_path, \"nodata\": np.nan},\n",
" \"right\": {\"img\": img_right_path, \"nodata\": np.nan},\n",
" \"col_disparity\": [-2, 2],\n",
" \"row_disparity\": [-2, 2],\n",
" \"col_disparity\": {\"init\": 0, \"range\": 2},\n",
" \"row_disparity\": {\"init\": 0, \"range\": 2},\n",
"}"
]
},
Expand Down Expand Up @@ -334,8 +334,8 @@
" \"right\": {\n",
" \"img\": \"data/right.tif\",\n",
" },\n",
" \"col_disparity\": [-2, 2],\n",
" \"row_disparity\": [-2, 2],\n",
" \"col_disparity\": {\"init\": 0, \"range\": 2},\n",
" \"row_disparity\": {\"init\": 0, \"range\": 2},\n",
" },\n",
" \"pipeline\":{\n",
" \"matching_cost\" : {\n",
Expand Down Expand Up @@ -547,8 +547,8 @@
" \"right\": {\n",
" \"img\": \"data/right.tif\",\n",
" },\n",
" \"col_disparity\": [-2, 2],\n",
" \"row_disparity\": [-2, 2],\n",
" \"col_disparity\": {\"init\": 0, \"range\": 2},\n",
" \"row_disparity\": {\"init\": 0, \"range\": 2},\n",
" },\n",
" \"pipeline\":{\n",
" \"matching_cost\" : {\n",
Expand Down Expand Up @@ -789,7 +789,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.12"
"version": "3.12.3"
}
},
"nbformat": 4,
Expand Down
14 changes: 9 additions & 5 deletions notebooks/usage_dichotomy.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,8 @@
" \"img\": img_right_path,\n",
" \"nodata\": np.nan,\n",
" },\n",
" \"col_disparity\": [-3, 3],\n",
" \"row_disparity\": [-3, 3],\n",
" \"col_disparity\": {\"init\": 0, \"range\": 3},\n",
" \"row_disparity\": {\"init\": 0, \"range\": 3},\n",
"}"
]
},
Expand Down Expand Up @@ -432,12 +432,16 @@
"from matplotlib import colors\n",
"\n",
"plt.rcParams[\"figure.figsize\"] = (20, 8)\n",
"min_max_disp_col = [input_config[\"col_disparity\"][\"init\"] - input_config[\"col_disparity\"][\"range\"], \n",
" input_config[\"col_disparity\"][\"init\"] + input_config[\"col_disparity\"][\"range\"]]\n",
"\n",
"min_max_disp_row = [input_config[\"row_disparity\"][\"init\"] - input_config[\"row_disparity\"][\"range\"], \n",
" input_config[\"row_disparity\"][\"init\"] + input_config[\"row_disparity\"][\"range\"]]\n",
"\n",
"def plot_result(\n",
" dataset,\n",
" dmin=min(input_config[\"row_disparity\"][0], input_config[\"col_disparity\"][0]),\n",
" dmax=max(input_config[\"row_disparity\"][1], input_config[\"col_disparity\"][1]),\n",
" dmin=min(min_max_disp_row[0], min_max_disp_col[0]),\n",
" dmax=max(min_max_disp_row[1], min_max_disp_col[1]),\n",
" palette=plt.cm.bwr.with_extremes(over=\"y\", under=\"g\", bad=\"k\"),\n",
" title=\"Without refinement.\",\n",
" colorbarlabel=\"Disparity\",\n",
Expand Down Expand Up @@ -566,7 +570,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.12"
"version": "3.12.3"
}
},
"nbformat": 4,
Expand Down
18 changes: 9 additions & 9 deletions notebooks/usage_step_roi_config.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,8 @@
"input_config = {\n",
" \"left\": {\"img\": img_left_path, \"nodata\": np.nan},\n",
" \"right\": {\"img\": img_right_path, \"nodata\": np.nan},\n",
" \"col_disparity\": [-2, 2],\n",
" \"row_disparity\": [-2, 2],\n",
" \"col_disparity\": {\"init\": 0, \"range\": 2},\n",
" \"row_disparity\": {\"init\": 0, \"range\": 2},\n",
"}"
]
},
Expand Down Expand Up @@ -261,8 +261,8 @@
" \"img\": img_right_path,\n",
" \"nodata\": \"NaN\",\n",
" },\n",
" \"col_disparity\": [-3, 3],\n",
" \"row_disparity\": [-3, 3],\n",
" \"col_disparity\": {\"init\": 0, \"range\": 3},\n",
" \"row_disparity\": {\"init\": 0, \"range\": 3},\n",
" },\n",
" \"pipeline\":{\n",
" \"matching_cost\" : {\n",
Expand Down Expand Up @@ -448,8 +448,8 @@
" \"img\": img_right_path,\n",
" \"nodata\": \"NaN\",\n",
" },\n",
" \"col_disparity\": [-3, 3],\n",
" \"row_disparity\": [-3, 3],\n",
" \"col_disparity\": {\"init\": 0, \"range\": 3},\n",
" \"row_disparity\": {\"init\": 0, \"range\": 3},\n",
" },\n",
" \"pipeline\":{\n",
" \"matching_cost\" : {\n",
Expand Down Expand Up @@ -636,8 +636,8 @@
" \"img\": img_right_path,\n",
" \"nodata\": \"NaN\",\n",
" },\n",
" \"col_disparity\": [-3, 3],\n",
" \"row_disparity\": [-3, 3],\n",
" \"col_disparity\": {\"init\": 0, \"range\": 3},\n",
" \"row_disparity\": {\"init\": 0, \"range\": 3},\n",
" },\n",
" \"ROI\":{\n",
" \"col\": {\"first\": 10, \"last\": 100},\n",
Expand Down Expand Up @@ -820,7 +820,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.16"
"version": "3.12.3"
}
},
"nbformat": 4,
Expand Down
40 changes: 20 additions & 20 deletions pandora2d/check_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

from __future__ import annotations

from typing import Dict, List
from typing import Dict

import numpy as np
import xarray as xr
Expand All @@ -34,7 +34,6 @@
from pandora.img_tools import get_metadata
from pandora.check_configuration import (
check_dataset,
check_disparities_from_input,
check_images,
concat_conf,
get_config_input,
Expand Down Expand Up @@ -101,8 +100,6 @@ def check_input_section(user_cfg: Dict[str, dict], estimation_config: dict = Non
check_images(cfg["input"])

if estimation_config is None:
check_disparities_from_input(cfg["input"]["col_disparity"], None)
check_disparities_from_input(cfg["input"]["row_disparity"], None)
left_image_metadata = get_metadata(cfg["input"]["left"]["img"])
check_disparity_ranges_are_inside_image(
left_image_metadata, cfg["input"]["row_disparity"], cfg["input"]["col_disparity"]
Expand All @@ -112,24 +109,24 @@ def check_input_section(user_cfg: Dict[str, dict], estimation_config: dict = Non


def check_disparity_ranges_are_inside_image(
image_metadata: xr.Dataset, row_disparity_range: List, col_disparity_range: List
image_metadata: xr.Dataset, row_disparity: Dict, col_disparity: Dict
) -> None:
"""
Raise an error if disparity ranges are out off image.
:param image_metadata:
:type image_metadata: xr.Dataset
:param row_disparity_range:
:type row_disparity_range: List
:param col_disparity_range:
:type col_disparity_range: List
:param row_disparity:
:type row_disparity: Dict
:param col_disparity:
:type col_disparity: Dict
:return: None
:rtype: None
:raises: ValueError
"""
if np.abs(row_disparity_range).min() > image_metadata.sizes["row"]:
if np.abs(row_disparity["init"]) - row_disparity["range"] > image_metadata.sizes["row"]:
raise ValueError("Row disparity range out of image")
if np.abs(col_disparity_range).min() > image_metadata.sizes["col"]:
if np.abs(col_disparity["init"]) - col_disparity["range"] > image_metadata.sizes["col"]:
raise ValueError("Column disparity range out of image")


Expand Down Expand Up @@ -251,20 +248,21 @@ def check_right_nodata_condition(cfg_input: Dict, cfg_pipeline: Dict) -> None:
)


def check_disparity_range_size(disparity: list[int] | str, title: str) -> None:
def check_disparity_range_size(disparity: Dict | str, title: str) -> None:
"""
Check that disparity ranges with a size < 5 are not allowed for interpolation refinement method.
Check that disparity ranges (dmax - dmin) with a size < 5 are not allowed for interpolation refinement method.
:param disparity: disparity range
:type disparity: list[int] | str
:type disparity: Dict | str
:param cfg_pipeline: pipeline section of configuration
:type cfg_pipeline: Dict
"""

if isinstance(disparity, list):
if (abs(disparity[1] - disparity[0]) + 1) < 5:
if isinstance(disparity, Dict):
if disparity["range"] < 2:
raise ValueError(
title + " disparity range with a size < 5 are not allowed with interpolation refinement method"
title + " disparity range (dmax - dmin) with a size < 5 are not allowed "
"with interpolation refinement method"
)

if isinstance(disparity, str):
Expand Down Expand Up @@ -311,8 +309,8 @@ def get_roi_config(user_cfg: Dict[str, dict]) -> Dict[str, dict]:
"img": And(str, rasterio_can_open_mandatory),
"nodata": Or(int, lambda input: np.isnan(input), lambda input: np.isinf(input)),
},
"col_disparity": [int, int],
"row_disparity": [int, int],
"col_disparity": {"init": int, "range": And(int, lambda x: x >= 0)},
"row_disparity": {"init": int, "range": And(int, lambda x: x >= 0)},
}

default_short_configuration_input = {
Expand All @@ -326,7 +324,9 @@ def get_roi_config(user_cfg: Dict[str, dict]) -> Dict[str, dict]:
}
}

default_configuration_disp = {"input": {"col_disparity": [-9999, -9995], "row_disparity": [-9999, -9995]}}
default_configuration_disp = {
"input": {"col_disparity": {"init": -9997, "range": 2}, "row_disparity": {"init": -9997, "range": 2}}
}

roi_configuration_schema = {
"row": {"first": And(int, lambda x: x >= 0), "last": And(int, lambda x: x >= 0)},
Expand Down
18 changes: 9 additions & 9 deletions pandora2d/estimation/estimation.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def desc(self) -> None:
print(f"{self._estimation_method} estimation measure")

@abstractmethod
def compute_estimation(self, img_left: xr.Dataset, img_right: xr.Dataset) -> Tuple[list, list, np.ndarray, dict]:
def compute_estimation(self, img_left: xr.Dataset, img_right: xr.Dataset) -> Tuple[Dict, Dict, np.ndarray, dict]:
"""
Compute the phase cross correlation method
Expand All @@ -114,26 +114,26 @@ def compute_estimation(self, img_left: xr.Dataset, img_right: xr.Dataset) -> Tup
:param img_right: xarray.Dataset containing :
- im : 2D (row, col) xarray.DataArray
:type img_right: xr.Dataset
:return:row disparity: list
col disparity: list
:return:row disparity: Dict
col disparity: Dict
Calculated shifts: list
Extra information about estimation : dict
:rtype: list, list, np.ndarray, dict
:rtype: dict, dict, np.ndarray, dict
"""

@staticmethod
def update_cfg_with_estimation(
cfg: Dict, disp_col: list, disp_row: list, shifts: np.ndarray, extra_dict: dict = None
cfg: Dict, disp_col: Dict, disp_row: Dict, shifts: np.ndarray, extra_dict: dict = None
) -> Dict:
"""
Save calculated shifts in a configuration dictionary
:param cfg: user configuration
:type cfg: dict
:param disp_col: list of min and max disparity in column
:type disp_col: [int, int]
:param disp_row: list of min and max disparity in row
:type disp_row: [int, int]
:param disp_col: dict with init and range for disparity in column
:type disp_col: {"init" : int, "range" : int >= 0}
:param disp_row: dict with init and range for disparity in row
:type disp_row: {"init" : int, "range" : int >= 0}
:param shifts: computed global shifts between left and right
:type shifts: [np.float32, np.float32]
:param extra_dict: Dictionary containing extra information about estimation
Expand Down
12 changes: 3 additions & 9 deletions pandora2d/estimation/phase_cross_correlation.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def check_conf(self, cfg: Dict) -> Dict:

return cfg

def compute_estimation(self, img_left: xr.Dataset, img_right: xr.Dataset) -> Tuple[list, list, np.ndarray, dict]:
def compute_estimation(self, img_left: xr.Dataset, img_right: xr.Dataset) -> Tuple[Dict, Dict, np.ndarray, dict]:
"""
Compute the phase cross correlation method
Expand All @@ -116,14 +116,8 @@ def compute_estimation(self, img_left: xr.Dataset, img_right: xr.Dataset) -> Tup
# reformat outputs
phasediff = "{:.{}e}".format(phasediff, 8)
# -shifts because of pandora2d convention
min_col = round(-shifts[1]) - int(self._range_col)
max_col = round(-shifts[1]) + int(self._range_col)

min_row = round(-shifts[0]) - int(self._range_row)
max_row = round(-shifts[0]) + int(self._range_row)

row_disparity = [min_row, max_row]
col_disparity = [min_col, max_col]
row_disparity = {"init": round(-shifts[0]), "range": int(self._range_row)}
col_disparity = {"init": round(-shifts[1]), "range": int(self._range_col)}

logging.info("Estimation result is %s in columns and %s in row", -shifts[1], -shifts[0])
logging.debug("Translation invariant normalized RMS error between left and right is %s", error)
Expand Down
Loading

0 comments on commit a6e6802

Please sign in to comment.