def apply_transform(transform, source, target):
"""Applies the transformation ``transform`` to ``source``.
The output image will have the same shape as ``target``.
Args:
transform: A scikit-image ``SimilarityTransform`` object.
source (numpy array): A 2D numpy array of the source image to be
transformed.
target (numpy array): A 2D numpy array of the target image. Only used
to set the output image shape.
Return:
A numpy 2D array of the transformed source. If source is a masked array
the returned image will also be a masked array with outside pixels set
to True.
"""
from skimage.transform import warp
aligned_image = warp(source, inverse_map=transform.inverse,
output_shape=target.shape, order=3, mode='constant',
cval=_np.median(source), clip=False,
preserve_range=False)
if isinstance(source, _np.ma.MaskedArray):
# it could be that source's mask is just set to False
if isinstance(source.mask, _np.ndarray):
aligned_image_mask = warp(source.mask.astype('float32'),
inverse_map=transform.inverse,
output_shape=target.shape,
cval=1.0)
aligned_image_mask = aligned_image_mask > 0.4
aligned_image = _np.ma.array(aligned_image,
mask=aligned_image_mask)
else:
# If source is masked array with mask set to false, we
# return the same
aligned_image = _np.ma.array(aligned_image)
return aligned_image
评论列表
文章目录