Crop a fixed-size region surrounding tumor centroid #3040
mattwarkentin
started this conversation in
General
Replies: 2 comments 5 replies
-
Hi @mattwarkentin , I think the easiest thing would be to create your own transformation that inherits from Something like this (untested): class DynamicCrop(MapTransform):
def __init__(self, keys, coord_key):
super().__init__(keys)
self.coord_key = coord_key
def __call__(self, data):
d = dict(data)
coord = d[self.coord_key]
cropper = SpatialCrop(...) # use the coordinates and your desired size to create a cropper
for k in self.keys:
d[k] = cropper(d[k]) |
Beta Was this translation helpful? Give feedback.
1 reply
-
For posterity, this function works as described in the original post and will ALWAYS return an object of the desired size... class DynamicCropOrPad(monai.transforms.MapTransform):
def __init__(self, keys, coord_key, roi_size):
super().__init__(keys)
self.coord_key = coord_key
self.roi_size = roi_size
def __call__(self, data):
d = dict(data)
coord = d[self.coord_key]
cropper = SpatialCrop(roi_center=coord, roi_size=self.roi_size)
resizer = ResizeWithPadOrCrop(spatial_size=self.roi_size)
for k in self.keys:
d[k] = cropper(d[k])
if (tuple(d[k].shape) != self.roi_size):
d[k] = resizer(d[k])
return(d) |
Beta Was this translation helpful? Give feedback.
4 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hi,
I am trying to perform a transform that, for each subject, will crop a volume of fixed size centred around a coordinate. The coordinate corresponds to the center of the tumor in
'mask'
. This is a pretty common task. However, the coordinate varies for each subject because it corresponds to the location of a tumor. It seems like when you instantiate theSpatialCrop
/SpatialCropd
transforms you need to input aroi_center
, but shouldn't this support centers that are patient-specific?Something like this seems like it would make sense:
The code below is as close as I've gotten to what I desire so far, but it firsts crops based on the masks bounding box, then resizes to the desired dimension
(50, 50, 50)
. But if the mask is smaller than 50x50x50, the final image centers on the immediate nodule bounding box and is padded with black space, not ideal.What I want is a crop of
'img'
that is 50x50x50 in size and is centred on the tumor.Beta Was this translation helpful? Give feedback.
All reactions