-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Yiheng Wang <vennw@nvidia.com>
- Loading branch information
1 parent
1058d4b
commit 5c8fb69
Showing
4 changed files
with
51 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
from typing import Dict | ||
|
||
import numpy as np | ||
from einops import rearrange | ||
from monai.transforms.transform import Transform | ||
|
||
|
||
class OrientationGuidanceMultipleLabelDeepEditd(Transform): | ||
def __init__(self, ref_image="image", label_names=None): | ||
""" | ||
Convert the guidance to the RAS orientation | ||
""" | ||
self.ref_image = ref_image | ||
self.label_names = label_names | ||
|
||
def transform_points(self, point, affine): | ||
"""transform point to the coordinates of the transformed image | ||
point: numpy array [bs, N, 3] | ||
""" | ||
bs, N = point.shape[:2] | ||
point = np.concatenate((point, np.ones((bs, N, 1))), axis=-1) | ||
point = rearrange(point, "b n d -> d (b n)") | ||
point = affine @ point | ||
point = rearrange(point, "d (b n)-> b n d", b=bs)[:, :, :3] | ||
return point | ||
|
||
def __call__(self, data): | ||
d: Dict = dict(data) | ||
for key_label in self.label_names.keys(): | ||
points = d.get(key_label, []) | ||
if len(points) < 1: | ||
continue | ||
reoriented_points = self.transform_points( | ||
np.array(points)[None], | ||
np.linalg.inv(d[self.ref_image].meta["affine"].numpy()) @ d[self.ref_image].meta["original_affine"], | ||
) | ||
d[key_label] = reoriented_points[0] | ||
return d |