Skip to content
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
4 changes: 3 additions & 1 deletion monai/transforms/intensity/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -2293,6 +2293,7 @@ def __call__(self, image: NdarrayOrTensor):
class ComputeHoVerMaps(Transform):
"""Compute horizontal and vertical maps from an instance mask
It generates normalized horizontal and vertical distances to the center of mass of each region.
Input data with the size of [1xHxW[xD]], which channel dim will temporarily removed for calculating coordinates.

Args:
dtype: the data type of output Tensor. Defaults to `"float32"`.
Expand All @@ -2311,6 +2312,7 @@ def __call__(self, mask: NdarrayOrTensor):

h_map = instance_mask.astype(self.dtype, copy=True)
v_map = instance_mask.astype(self.dtype, copy=True)
instance_mask = instance_mask.squeeze(0) # remove channel dim

for region in skimage.measure.regionprops(instance_mask):
v_dist = region.coords[:, 0] - region.centroid[0]
Expand All @@ -2325,5 +2327,5 @@ def __call__(self, mask: NdarrayOrTensor):
h_map[h_map == region.label] = h_dist
v_map[v_map == region.label] = v_dist

hv_maps = convert_to_tensor(np.stack([h_map, v_map]), track_meta=get_track_meta())
hv_maps = convert_to_tensor(np.concatenate([h_map, v_map]), track_meta=get_track_meta())
Comment thread
Nic-Ma marked this conversation as resolved.
return hv_maps
10 changes: 5 additions & 5 deletions tests/test_compute_ho_ver_maps.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@
_, has_skimage = optional_import("skimage", "0.19.0", min_version)


INSTANCE_MASK = np.zeros((16, 16), dtype="int16")
INSTANCE_MASK[5:8, 4:11] = 1
INSTANCE_MASK[3:5, 6:9] = 1
INSTANCE_MASK[8:10, 6:9] = 1
INSTANCE_MASK[13:, 13:] = 2
INSTANCE_MASK = np.zeros((1, 16, 16), dtype="int16")
INSTANCE_MASK[:, 5:8, 4:11] = 1
INSTANCE_MASK[:, 3:5, 6:9] = 1
INSTANCE_MASK[:, 8:10, 6:9] = 1
INSTANCE_MASK[:, 13:, 13:] = 2
H_MAP = torch.zeros((16, 16), dtype=torch.float32)
H_MAP[5:8, 4] = -1.0
H_MAP[5:8, 5] = -2.0 / 3.0
Expand Down
10 changes: 5 additions & 5 deletions tests/test_compute_ho_ver_maps_d.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@
_, has_skimage = optional_import("skimage", "0.19.0", min_version)


INSTANCE_MASK = np.zeros((16, 16), dtype="int16")
INSTANCE_MASK[5:8, 4:11] = 1
INSTANCE_MASK[3:5, 6:9] = 1
INSTANCE_MASK[8:10, 6:9] = 1
INSTANCE_MASK[13:, 13:] = 2
INSTANCE_MASK = np.zeros((1, 16, 16), dtype="int16")
INSTANCE_MASK[:, 5:8, 4:11] = 1
INSTANCE_MASK[:, 3:5, 6:9] = 1
INSTANCE_MASK[:, 8:10, 6:9] = 1
INSTANCE_MASK[:, 13:, 13:] = 2
H_MAP = torch.zeros((16, 16), dtype=torch.float32)
H_MAP[5:8, 4] = -1.0
H_MAP[5:8, 5] = -2.0 / 3.0
Expand Down