def elastic_transform_2d(img, alpha, sigma, random_mode=True, probability=0.5):
#Taken from: https://gist.github.com/chsasank/4d8f68caf01f041a6453e67fb30f8f5a
if random_mode:
if random.random() < probability:
return img
dx = nd.gaussian_filter((random_state.rand(img.shape) * 2 - 1), sigma, mode="constant", cval=0) * alpha
dy = nd.gaussian_filter((random_state.rand(img.shape) * 2 - 1), sigma, mode="constant", cval=0) * alpha
x, y = np.meshgrid(np.arange(img.shape[0]), np.arange(img.shape[1]), indexing='ij')
indices = np.reshape(x+dx, (-1, 1)), np.reshape(y+dy, (-1, 1))
return apply_elastic(img, indices), indices
评论列表
文章目录