def align_major(images, angles, reshape=True):
"""
Rotates images based on the angles passed in
:param images: Either a single image or a list of them. Must be at least 3d
numpy arrays, ordered as TCZYX
:param angles: The tuple returned by get_align_angles. Tells the function how to rotate the images
:param reshape: boolean. If True, the output will be resized to ensure that no data
from img is lost. If False, the output will be the same size as the input, with potential to
lose data that lies outside of the input shape after rotation. Default is True
:return: If a single image was passed in, it will will return a rotated copy of that image. If a list was
passed in, it will return a list of rotated images in the same order that they were passed in
"""
if isinstance(images, (list, tuple)):
return_list = True
image_list = images
else:
# turn it into a single element list for easier code
return_list = False
image_list = [images]
if not all(getattr(img, "ndim", 0) >= 3 for img in image_list):
raise ValueError("All images must be at least 3d")
rotate_axes = ((-3, -2), (-3, -1), (-2, -1))
# output list
out_list = []
for img in image_list:
out = img.copy()
for axis, angle in angles:
out = rotate(out, angle, reshape=reshape, order=1, axes=rotate_axes[axis], cval=(np.nan if reshape else 0))
out_list.append(out)
if reshape:
# cropping necessary as each resize makes the image bigger
# np.nan used as fill value when reshaping in order to make cropping easy
for i in range(len(out_list)):
out_list[i] = crop(out_list[i], np.nan)
out_list[i][np.isnan(out_list[i])] = 0
if return_list:
return out_list
else:
# turn it back from a single element list to a single image
return out_list[0]
评论列表
文章目录