def TF_elastic_deform(img, alpha=1.0, sigma=1.0):
"""Elastic deformation of images as described in Simard 2003"""
assert len(img.shape) == 3
h, w, nc = img.shape
if nc != 1:
raise NotImplementedError("Multi-channel not implemented.")
# Generate uniformly random displacement vectors, then convolve with gaussian kernel
# and finally multiply by a magnitude coefficient alpha
dx = alpha * gaussian_filter(
(np.random.random((h, w)) * 2 - 1), sigma, mode="constant", cval=0
)
dy = alpha * gaussian_filter(
(np.random.random((h, w)) * 2 - 1), sigma, mode="constant", cval=0
)
# Map image to the deformation mesh
x, y = np.meshgrid(np.arange(h), np.arange(w), indexing='ij')
indices = np.reshape(x+dx, (-1, 1)), np.reshape(y+dy, (-1, 1))
return map_coordinates(img.reshape((h,w)), indices, order=1).reshape(h,w,nc)
评论列表
文章目录