Skip to content

Commit

Permalink
Merge branch 'fix-step-in-get-coordinates-method' into 'release'
Browse files Browse the repository at this point in the history
Fix step in get coordinates method

See merge request 3d/PandoraBox/pandora!342
  • Loading branch information
lecontm committed Apr 3, 2024
2 parents bfd7d2d + c1515db commit d26c84f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 17 deletions.
24 changes: 14 additions & 10 deletions pandora/matching_cost/matching_cost.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,23 +250,25 @@ def compute_cost_volume(
:rtype: xarray.Dataset
"""

def get_coordinates(self, margin: int, img_coordinates: np.ndarray) -> np.ndarray:
def get_coordinates(self, margin: int, img_coordinates: np.ndarray, step: int) -> np.ndarray:
"""
In the case of a ROI, computes the right coordinates to be sure to process the first point of the ROI.
:param margin: ROI margin
:type margin: int
:param img_coordinates: coordinates of the ROI with margins
:type img_coordinates: np.ndarray
:param step: matching cost step value
:type step: int
:return: a np.ndarray that contains the right coordinates
:rtype: np.ndarray
"""

index_compute_col = np.arange(img_coordinates[0], img_coordinates[-1] + 1, self._step_col) # type: np.ndarray
index_compute_col = np.arange(img_coordinates[0], img_coordinates[-1] + 1, step) # type: np.ndarray

# Check if the first column of roi is inside the final CV (with the step)
if margin % self._step_col != 0:
if margin < self._step_col:
if margin % step != 0:
if margin < step:
# For example, given left_margin = 2 and step = 3
#
# roi_img = M M M M M M M
Expand All @@ -278,7 +280,7 @@ def get_coordinates(self, margin: int, img_coordinates: np.ndarray) -> np.ndarra
# Our starting point would be at index left_margin = 2 --> starting point = 1st point of ROI
# We are directly on the first point to compute

index_compute_col = np.arange(img_coordinates[0] + margin, img_coordinates[-1] + 1, self._step_col)
index_compute_col = np.arange(img_coordinates[0] + margin, img_coordinates[-1] + 1, step)
else:
# For example, given left_margin = 3 and step = 2
#
Expand All @@ -303,8 +305,8 @@ def get_coordinates(self, margin: int, img_coordinates: np.ndarray) -> np.ndarra
# With a step of 3, the first point of ROI is calculated without cropping margins too much

# give the number of the first column to compute
start = self._step_col - (self.find_nearest_multiple_of_step(margin) - margin)
index_compute_col = np.arange(img_coordinates[0] + start, img_coordinates[-1] + 1, self._step_col)
start = step - (self.find_nearest_multiple_of_step(margin, step) - margin)
index_compute_col = np.arange(img_coordinates[0] + start, img_coordinates[-1] + 1, step)

return index_compute_col

Expand Down Expand Up @@ -332,7 +334,7 @@ def grid_estimation(

# Get the index of the columns that should be computed
if cfg and "ROI" in cfg:
index_compute_col = self.get_coordinates(cfg["ROI"]["margins"][0], c_col)
index_compute_col = self.get_coordinates(cfg["ROI"]["margins"][0], c_col, self._step_col)
else:
index_compute_col = np.arange(c_col[0], c_col[-1] + 1, self._step_col) # type: np.ndarray # type: ignore

Expand Down Expand Up @@ -583,16 +585,18 @@ def get_min_max_from_grid(disp_min: np.ndarray, disp_max: np.ndarray) -> Tuple[i
"""
return int(np.nanmin(disp_min)), int(np.nanmax(disp_max))

def find_nearest_multiple_of_step(self, value: int) -> int:
def find_nearest_multiple_of_step(self, value: int, step: int) -> int:
"""
In case value is not a multiple of step, find nearest greater value for which it is the case.
:param value: Initial value.
:type: value: int
:param step: matching cost step value
:type step: int
:return: nearest multiple of step.
:rtype: int
"""
while value % self._step_col != 0:
while value % step != 0:
value += 1
return value

Expand Down
10 changes: 3 additions & 7 deletions tests/test_matching_cost/test_matching_cost.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
from pandora.criteria import validity_mask
from pandora.margins.descriptors import HalfWindowMargins
from pandora.img_tools import create_dataset_from_inputs, add_disparity, add_disparity_grid
from tests import common
from tests import common # pylint: disable=no-name-in-module


@pytest.fixture()
Expand Down Expand Up @@ -135,11 +135,8 @@ def test_get_coordinates(self, step, margin, img_coordinates, ground_truth):
# Create matching cost object
matching_cost_ = matching_cost.AbstractMatchingCost(**common.basic_pipeline_cfg["matching_cost"])

# Update step for matching cost
matching_cost_._step_col = step # pylint: disable=protected-access

# Compute new indexes
index_to_compute = matching_cost_.get_coordinates(margin, img_coordinates)
index_to_compute = matching_cost_.get_coordinates(margin, img_coordinates, step)

np.testing.assert_array_equal(index_to_compute, ground_truth)

Expand Down Expand Up @@ -496,9 +493,8 @@ def test_find_nearest_multiple_of_step(self, step_col, value, expected):
matching_cost_ = matching_cost.AbstractMatchingCost(
**{"matching_cost_method": "census", "window_size": 5, "subpix": 4, "band": "b"}
)
matching_cost_._step_col = step_col # pylint:disable=protected-access

result = matching_cost_.find_nearest_multiple_of_step(value)
result = matching_cost_.find_nearest_multiple_of_step(value, step_col)

assert result == expected

Expand Down

0 comments on commit d26c84f

Please sign in to comment.