Skip to content

Commit

Permalink
Merge pull request #34 from jnolan14/master
Browse files Browse the repository at this point in the history
Added 'reshape_on_bbox' method to the FramedImage class
  • Loading branch information
jnolan14 authored Jul 16, 2024
2 parents df81f1e + ecf5737 commit c0f7824
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions surfa/image/framed.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from surfa.core.array import check_array
from surfa.core.slicing import sane_slicing
from surfa.core.slicing import slicing_parameters
from surfa.core.slicing import fit_slicing_to_shape
from surfa.transform import cast_space
from surfa.transform.geometry import ImageGeometry
from surfa.transform.geometry import cast_image_geometry
Expand Down Expand Up @@ -503,14 +504,16 @@ def reorient(self, orientation, copy=True):
voxsize=voxsize)
return self.new(data, target_geom)

def reshape(self, shape, copy=True):
def reshape(self, shape, center='image', copy=True):
"""
Returns a volume fit to a given shape. Image will be centered in the conformed volume.
Returns a volume fit to a given shape. Image will be centered in the conformed volume, or bbox of the original image.
Parameters
----------
shape : tuple of int
Target shape.
center : str
Center the reshaped image on image center or bbox: ['image', 'bbox']
copy : bool
Return copy of image even if target shape is already satisfied.
Expand All @@ -528,6 +531,19 @@ def reshape(self, shape, copy=True):
if np.array_equal(self.baseshape, shape):
return self.copy() if copy else self

# validate 'center'
valid_centers = ['image', 'bbox']
if center not in valid_centers:
raise ValueError(f'Unsupported argument for "center", must be in {valid_centers}')

if center == 'bbox':
# get the bbox
c_bbox = self.bbox()
# reshape the bbox to the target size
bbox_centered_cropping = fit_slicing_to_shape(c_bbox, self.baseshape, shape)
# return the bbox centered cropping
return self[bbox_centered_cropping]

delta = (np.array(shape) - np.array(self.baseshape)) / 2
low = np.floor(delta).astype(int)
high = np.ceil(delta).astype(int)
Expand Down Expand Up @@ -579,9 +595,7 @@ def fit_to_shape(self, shape, center=None, copy=True):
arr : !class
Reshaped image.
"""
if center is not None:
warnings.warn('fit_to_shape center argument no longer has any effect')
return self.reshape(shape, copy)
return self.reshape(shape, center, copy)

def conform(self, shape=None, voxsize=None, orientation=None, dtype=None, method='linear', copy=True):
"""
Expand Down

0 comments on commit c0f7824

Please sign in to comment.