Removing lens distortion effects from an image
If you need to remove lens distortion effects from a whole image, you need to use dense remapping. Essentially, the undistortion algorithm warps and compresses the image in a way to compensate for lens effects, but compression leads to blank regions appearing. This recipe tells you how to undistort images and remove empty regions from the undistorted image.
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 cv2 import numpy as np
- Load the camera matrix and distortion coefficients for our camera, and a photo, that was taken by the same camera:
camera_matrix = np.load('../data/pinhole_calib/camera_mat.npy') dist_coefs = np.load('../data/pinhole_calib/dist_coefs.npy') img = cv2.imread('../data/pinhole_calib/img_00.png')
- Undistort the image with
cv2.undistort
—the empty regions will appear...