@preserve_shape<EOL>def grid_distortion(img, num_steps=<NUM_LIT:10>, xsteps=[], ysteps=[], interpolation=cv2.INTER_LINEAR,<EOL>border_mode=cv2.BORDER_REFLECT_101, value=None):
height, width = img.shape[:<NUM_LIT:2>]<EOL>x_step = width // num_steps<EOL>xx = np.zeros(width, np.float32)<EOL>prev = <NUM_LIT:0><EOL>for idx, x in enumerate(range(<NUM_LIT:0>, width, x_step)):<EOL><INDENT>start = x<EOL>end = x + x_step<EOL>if end > width:<EOL><INDENT>end = width<EOL>cur = width<EOL><DEDENT>else:<EOL><INDENT>cur = prev + x_step * xsteps[idx]<EOL><DEDENT>xx[start:end] = np.linspace(prev, cur, end - start)<EOL>prev = cur<EOL><DEDENT>y_step = height // num_steps<EOL>yy = np.zeros(height, np.float32)<EOL>prev = <NUM_LIT:0><EOL>for idx, y in enumerate(range(<NUM_LIT:0>, height, y_step)):<EOL><INDENT>start = y<EOL>end = y + y_step<EOL>if end > height:<EOL><INDENT>end = height<EOL>cur = height<EOL><DEDENT>else:<EOL><INDENT>cur = prev + y_step * ysteps[idx]<EOL><DEDENT>yy[start:end] = np.linspace(prev, cur, end - start)<EOL>prev = cur<EOL><DEDENT>map_x, map_y = np.meshgrid(xx, yy)<EOL>map_x = map_x.astype(np.float32)<EOL>map_y = map_y.astype(np.float32)<EOL>img = cv2.remap(img, map_x, map_y, interpolation=interpolation, borderMode=border_mode, borderValue=value)<EOL>return img<EOL>
Reference:
    http://pythology.blogspot.sg/2014/03/interpolation-on-regular-distorted-grid.html