def distort_elastic_cv2(image, alpha=80, sigma=20, random_state=None):
"""Elastic deformation of images as per [Simard2003].
"""
if random_state is None:
random_state = np.random.RandomState(None)
shape_size = image.shape[:2]
# Downscaling the random grid and then upsizing post filter
# improves performance. Approx 3x for scale of 4, diminishing returns after.
grid_scale = 4
alpha //= grid_scale # Does scaling these make sense? seems to provide
sigma //= grid_scale # more similar end result when scaling grid used.
grid_shape = (shape_size[0]//grid_scale, shape_size[1]//grid_scale)
blur_size = int(4 * sigma) | 1
rand_x = cv2.GaussianBlur(
(random_state.rand(*grid_shape) * 2 - 1).astype(np.float32),
ksize=(blur_size, blur_size), sigmaX=sigma) * alpha
rand_y = cv2.GaussianBlur(
(random_state.rand(*grid_shape) * 2 - 1).astype(np.float32),
ksize=(blur_size, blur_size), sigmaX=sigma) * alpha
if grid_scale > 1:
rand_x = cv2.resize(rand_x, shape_size[::-1])
rand_y = cv2.resize(rand_y, shape_size[::-1])
grid_x, grid_y = np.meshgrid(np.arange(shape_size[1]), np.arange(shape_size[0]))
grid_x = (grid_x + rand_x).astype(np.float32)
grid_y = (grid_y + rand_y).astype(np.float32)
distorted_img = cv2.remap(image, grid_x, grid_y,
borderMode=cv2.BORDER_REFLECT_101, interpolation=cv2.INTER_LINEAR)
return distorted_img
image_processing_common.py 文件源码
python
阅读 24
收藏 0
点赞 0
评论 0
评论列表
文章目录