Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ def _find_best_match(
score_map = _get_score_map(
comparison_array=rotated,
template=grid_cell.cell_data_filled,
template_mask=grid_cell.valid_mask,
)
score, x, y = _compute_best_score_from_maps(
score_map=score_map, fill_fraction_mask=fill_fraction_mask
Expand Down Expand Up @@ -237,7 +238,7 @@ def _compute_best_score_from_maps(


def _get_score_map(
comparison_array: FloatArray2D, template: FloatArray2D
comparison_array: FloatArray2D, template: FloatArray2D, template_mask: BinaryMask
) -> FloatArray2D:
"""
Compute a normalized cross-correlation score map for one reference cell.
Expand All @@ -248,12 +249,14 @@ def _get_score_map(

:param comparison_array: NaN-free float32-compatible comparison image, padded by a full cell on each side.
:param template: Reference grid cell whose ``cell_data`` is used as the template; must contain no NaN values.
:param template_mask: Mask with the pixel values that should be matched (i.e. no NaNs).
:returns: Float64 score map of shape ``(H - cell_height + 1, W - cell_width + 1)`` with values in ``[-1, 1]``.
"""

score_map = cv2.matchTemplate(
image=comparison_array.astype(np.float32),
templ=template.astype(np.float32),
method=cv2.TM_CCOEFF_NORMED,
mask=template_mask.astype(np.uint8),
)
return np.asarray(score_map, dtype=np.float64)
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from scipy.constants import mega

from container_models.base import ConfigBaseModel, FloatArray2D
from container_models.base import ConfigBaseModel, FloatArray2D, BinaryMask
from conversion.data_formats import Mark, MarkType


Expand Down Expand Up @@ -246,3 +246,8 @@ def fill_fraction(self) -> float:
def cell_data_filled(self) -> FloatArray2D:
"""Cell data where NaN values are replaced with the sentinel value."""
return np.nan_to_num(self.cell_data, nan=self.nan_fill_value, copy=True)

@cached_property
def valid_mask(self) -> BinaryMask:
"""Mask where the cell data has valid values (i.e. no NaNs)."""
return ~np.isnan(self.cell_data)
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,9 @@ def test_get_score_map_self_match_peak_at_cell_top_left(self):

# Act
score_map = _get_score_map(
comparison_array=filled, template=grid_cell.cell_data_filled
comparison_array=filled,
template=grid_cell.cell_data_filled,
template_mask=grid_cell.valid_mask,
)

# Asserts
Expand Down
Loading