Skip to content

Commit

Permalink
Improve EchoMeasure to avoid crash when candidate markers are align…
Browse files Browse the repository at this point in the history
…ed vertically (#76)
  • Loading branch information
nathanpainchaud authored Aug 3, 2022
1 parent c777491 commit c0c6669
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions vital/utils/image/us/measure.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def _lv_base(
others = ~(left_ventricle + myocardium)
dilated_myocardium = ndimage.binary_dilation(myocardium, structure=EchoMeasure._square_3x3_connectivity)
dilated_others = ndimage.binary_dilation(others, structure=EchoMeasure._square_3x3_connectivity)
y_coords, x_coords = np.where(left_ventricle * dilated_myocardium * dilated_others)
y_coords, x_coords = np.nonzero(left_ventricle * dilated_myocardium * dilated_others)

if (num_markers := len(y_coords)) < 2:
logger.warning(
Expand All @@ -52,12 +52,21 @@ def _lv_base(
)
return np.nan, np.nan

left_half_mask = x_coords < x_coords.mean()
bottom_left_point_idx = y_coords[left_half_mask].argmax()
bottom_right_point_idx = y_coords[~left_half_mask].argmax()
if np.all(x_coords == x_coords.mean()):
# Edge case where the base points are aligned vertically
# Divide frontier into bottom and top halves.
coord_mask = y_coords > y_coords.mean()
left_point_idx = y_coords[coord_mask].argmin()
right_point_idx = y_coords[~coord_mask].argmax()
else:
# Normal case where there is a clear divide between left and right markers at the base
# Divide frontier into left and right halves.
coord_mask = x_coords < x_coords.mean()
left_point_idx = y_coords[coord_mask].argmax()
right_point_idx = y_coords[~coord_mask].argmax()
return (
(y_coords[left_half_mask][bottom_left_point_idx], x_coords[left_half_mask][bottom_left_point_idx]),
(y_coords[~left_half_mask][bottom_right_point_idx], x_coords[~left_half_mask][bottom_right_point_idx]),
(y_coords[coord_mask][left_point_idx], x_coords[coord_mask][left_point_idx]),
(y_coords[~coord_mask][right_point_idx], x_coords[~coord_mask][right_point_idx]),
)

@staticmethod
Expand Down

0 comments on commit c0c6669

Please sign in to comment.