Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix lmd #17

Merged
merged 3 commits into from
Dec 15, 2024
Merged
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
12 changes: 8 additions & 4 deletions src/dvpio/read/shapes/lmd_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
import lmd.lib as pylmd
import numpy as np
import shapely
from spatialdata.models import ShapesModel
from spatialdata.models import PointsModel, ShapesModel

from .geometry import transform_shapes


def read_lmd(path: str, calibration_points_image: gpd.GeoDataFrame, switch_orientation: bool = True) -> ShapesModel:
def read_lmd(path: str, calibration_points_image: PointsModel, switch_orientation: bool = False) -> ShapesModel:
"""Read and parse LMD-formatted masks for the use in spatialdata

Wrapper for pyLMD functions.
Expand All @@ -17,8 +17,8 @@ def read_lmd(path: str, calibration_points_image: gpd.GeoDataFrame, switch_orien
path
Path to LMD-formatted segmentation masks in .xml format
calibration_points_image
Calibration points of the image as geopandas.DataFrame, with 3 calibration points
in geometry column
Calibration points of the image as DataFrame, with 3 calibration points. Point coordinates are
stored as seperate columns in `x` and `y` column.
switch_orientation
Per default, LMD is working in a (x, y) coordinate system while the image coordinates are in a (row=y, col=x)
coordinate system. If True, transform the coordinate systems by mirroring the coordinate system at the
Expand All @@ -41,6 +41,10 @@ def read_lmd(path: str, calibration_points_image: gpd.GeoDataFrame, switch_orien
geometry=[shapely.Point(point) for point in lmd_shapes.calibration_points],
)

calibration_points_image = gpd.GeoDataFrame(
geometry=[shapely.Point(row["x"], row["y"]) for _, row in calibration_points_image.iterrows()]
)

if len(calibration_points_lmd) < 3:
raise ValueError(f"Require at least 3 calibration points, but only received {len(calibration_points_lmd)}")
if len(calibration_points_lmd) != len(calibration_points_image):
Expand Down
6 changes: 3 additions & 3 deletions tests/read/shapes/test_lmd_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from numpy.typing import NDArray
from scipy.optimize import linear_sum_assignment as lsa
from scipy.spatial.distance import cdist
from shapely import Point
from spatialdata.models import PointsModel

from dvpio.read.shapes import read_lmd

Expand All @@ -13,7 +13,7 @@ def _get_centroid_xy(geometry: gpd.GeoSeries) -> NDArray[np.float64]:
return np.array(geometry.apply(lambda geom: [geom.centroid.x, geom.centroid.y]).tolist())


calibration_points_image = gpd.GeoDataFrame(geometry=[Point([1015, 15]), Point([205, 15]), Point([15, 1015])])
calibration_points_image = PointsModel.parse(np.array([[15, 1015], [15, 205], [1015, 15]]))


@pytest.mark.parametrize(
Expand All @@ -27,7 +27,7 @@ def _get_centroid_xy(geometry: gpd.GeoSeries) -> NDArray[np.float64]:
],
)
def test_read_lmd(path: str, calibration_points: NDArray[np.float64], ground_truth_path: str) -> None:
lmd_shapes = read_lmd(path, calibration_points, switch_orientation=True)
lmd_shapes = read_lmd(path, calibration_points, switch_orientation=False)
lmd_centroids = _get_centroid_xy(lmd_shapes["geometry"])

ground_truth = gpd.read_file(ground_truth_path)
Expand Down
Loading