Skip to content

Commit f25a0c2

Browse files
authored
Merge pull request #46 from jnolan14/extract_sub_image
added functionality to the FramedImage class to extract a list of sub images of a specified shape
2 parents 9608619 + d475fd3 commit f25a0c2

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

surfa/image/framed.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -816,6 +816,42 @@ def signed_distance(self):
816816
dt = lambda x: scipy.ndimage.distance_transform_edt(x, sampling=sampling)
817817
sdt = lambda x: dt(1 - x) - dt(x)
818818
return stack([self.new(sdt(self.framed_data[..., i])) for i in range(self.nframes)])
819+
820+
def extract_sub_images(self, sub_shape):
821+
"""
822+
Extract a list of sub images from the FramedImage with shape 'sub_shape'
823+
824+
Parameters
825+
----------
826+
sub_shape : array like
827+
Shape to be given to the sub images that are extracted
828+
829+
Returns
830+
-------
831+
list of FramedImages
832+
sub images
833+
"""
834+
835+
# ensure valid shape for the sub images
836+
if len(sub_shape) != self.basedim:
837+
raise ValueError('Image and sub_shape must have same dimensions')
838+
839+
img_shape = np.array(self.shape)
840+
sub_shape = np.array(sub_shape)
841+
842+
# find number of slices to take in each dim
843+
to_slice = img_shape // sub_shape
844+
845+
# get the the 'ordered pairs' of all croppings
846+
indices = [len(range(int(to_slice[d]))) for d in range(len(img_shape))]
847+
848+
sub_volumes = []
849+
# extract all the sub images
850+
for idx in np.ndindex(*indices):
851+
slices = tuple(slice(sub_shape[d] * idx[d], sub_shape[d] * (idx[d] + 1)) for d in range(len(sub_shape)))
852+
sub_volumes.append(self[slices])
853+
854+
return sub_volumes
819855

820856

821857
class Slice(FramedImage):

0 commit comments

Comments
 (0)