Remapping an image using arbitrary transformation
In this recipe, you will learn how to transform images using per-pixel mappings. This is a piece of functionality that is very generic and is used in many computer vision applications, such as image stitching, camera frames undistortion, and many others.
Getting ready
Before you proceed with this recipe, you need to install the OpenCV version 3.3 (or greater) Python API package.
How to do it
You need to complete the following steps:
- Import the necessary modules:
import math import cv2 import numpy as np
- Load the test images:
img = cv2.imread('../data/Lena.png')
- 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])
- Remap the source image:
remapped_img = cv2.remap(img...