Skip to content

Commit ea96481

Browse files
committed
Customizable resolution for object measures
1 parent 7c1f595 commit ea96481

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

flamingo_tools/measurements.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
def _measure_volume_and_surface(mask, resolution):
2727
# Use marching_cubes for 3D data
28-
verts, faces, normals, _ = marching_cubes(mask, spacing=(resolution,) * 3)
28+
verts, faces, normals, _ = marching_cubes(mask, spacing=resolution)
2929

3030
mesh = trimesh.Trimesh(vertices=verts, faces=faces, vertex_normals=normals)
3131
surface = mesh.area
@@ -61,9 +61,9 @@ def _get_bounding_box_and_center(table, seg_id, resolution, shape, dilation):
6161
)
6262

6363
center = (
64-
int(row.anchor_z.item() / resolution),
65-
int(row.anchor_y.item() / resolution),
66-
int(row.anchor_x.item() / resolution),
64+
int(row.anchor_z.item() / resolution[0]),
65+
int(row.anchor_y.item() / resolution[1]),
66+
int(row.anchor_x.item() / resolution[2]),
6767
)
6868

6969
return bb, center
@@ -236,7 +236,7 @@ def compute_object_measures_impl(
236236
image: np.typing.ArrayLike,
237237
segmentation: np.typing.ArrayLike,
238238
n_threads: Optional[int] = None,
239-
resolution: float = 0.38,
239+
resolution: Optional[Tuple[float, float, float]] = (0.38, 0.38, 0.38),
240240
table: Optional[pd.DataFrame] = None,
241241
feature_set: str = "default",
242242
background_mask: Optional[np.typing.ArrayLike] = None,
@@ -307,7 +307,7 @@ def compute_object_measures(
307307
image_key: Optional[str] = None,
308308
segmentation_key: Optional[str] = None,
309309
n_threads: Optional[int] = None,
310-
resolution: float = 0.38,
310+
resolution: Optional[Tuple[float, float, float]] = (0.38, 0.38, 0.38),
311311
force: bool = False,
312312
feature_set: str = "default",
313313
s3_flag: bool = False,

reproducibility/object_measures/repro_object_measures.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from multiprocessing import cpu_count
55
from typing import Optional
66

7+
import numpy as np
78
import flamingo_tools.s3_utils as s3_utils
89
from flamingo_tools.measurements import compute_object_measures, compute_sgn_background_mask
910

@@ -18,6 +19,7 @@ def repro_object_measures(
1819
):
1920
s3_flag = True
2021
input_key = "s0"
22+
default_resolution = 0.38
2123
default_component_list = [1]
2224
default_bg_mask = None
2325

@@ -29,10 +31,15 @@ def repro_object_measures(
2931
cochlea = dic["cochlea"]
3032
image_channels = dic["image_channel"] if isinstance(dic["image_channel"], list) else [dic["image_channel"]]
3133
seg_channel = dic["segmentation_channel"]
34+
resolution = tuple(dic["resolution"]) if "resolution" in dic else default_resolution
3235
component_list = dic["component_list"] if "component_list" in dic else default_component_list
3336
bg_mask = dic["background_mask"] if "background_mask" in dic else default_bg_mask
3437
print(f"Processing cochlea {cochlea}")
3538

39+
if not isinstance(resolution, float):
40+
assert len(resolution) == 3
41+
resolution = np.array(resolution)[::-1]
42+
3643
for img_channel in image_channels:
3744

3845
print(f"Processing image channel {img_channel}")
@@ -88,6 +95,7 @@ def repro_object_measures(
8895
median_only=median_only,
8996
background_mask=bg_mask,
9097
n_threads=n_threads,
98+
resolution=resolution,
9199
)
92100

93101

0 commit comments

Comments
 (0)