How to do it

You need to complete the following steps:

  1. Import the necessary modules:
import mathimport cv2import numpy as np
  1. Load the test images:
img = cv2.imread('../data/Lena.png')
  1. Prepare the per-pixel transformation maps:
xmap = np.zeros((img.shape[1], img.shape[0]), np.float32)ymap = np.zeros((img.shape[1], img.shape[0]), np.float32)for y in range(img.shape[0]):    for x in range(img.shape[1]):        xmap[y,x] = x + 30 * math.cos(20 * x / img.shape[0])        ymap[y,x] = y + 30 * math.sin(20 * y / img.shape[1])
  1. Remap the source image:
remapped_img = cv2.remap(img, xmap, ymap, cv2.INTER_LINEAR, None, cv2.BORDER_REPLICATE)
  1. Visualize the results:
plt.figure(0)plt.axis('off')plt.imshow(remapped_img[:,:,[2,1,0]])plt.show()

Get OpenCV 3 Computer Vision with Python Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.