Book Image

Python Image Processing Cookbook

By : Sandipan Dey
Book Image

Python Image Processing Cookbook

By: Sandipan Dey

Overview of this book

With the advancements in wireless devices and mobile technology, there's increasing demand for people with digital image processing skills in order to extract useful information from the ever-growing volume of images. This book provides comprehensive coverage of the relevant tools and algorithms, and guides you through analysis and visualization for image processing. With the help of over 60 cutting-edge recipes, you'll address common challenges in image processing and learn how to perform complex tasks such as object detection, image segmentation, and image reconstruction using large hybrid datasets. Dedicated sections will also take you through implementing various image enhancement and image restoration techniques, such as cartooning, gradient blending, and sparse dictionary learning. As you advance, you'll get to grips with face morphing and image segmentation techniques. With an emphasis on practical solutions, this book will help you apply deep learning techniques such as transfer learning and fine-tuning to solve real-world problems. By the end of this book, you'll be proficient in utilizing the capabilities of the Python ecosystem to implement various image processing techniques effectively.
Table of Contents (11 chapters)

Applying perspective transformation and homography

The goal of perspective (projective) transform is to estimate homography (a matrix, H) from point correspondences between two images. Since the matrix has a Depth Of Field (DOF) of eight, you need at least four pairs of points to compute the homography matrix from two images. The following diagram shows the basic concepts required to compute the homography matrix:

Fortunately, we don't need to compute the SVD and the H matrix is computed automatically by the ProjectiveTransform function from the scikit-image transform module. In this recipe, we will use this function to implement homography.

Getting ready

We will use a humming bird's image and an image of an astronaut on the moon (taken from NASA's public domain images) as input images in this recipe. Again, let's start by importing the required libraries as usual:

from skimage.transform import ProjectiveTransform
from import imread
import numpy as np
import matplotlib.pylab as plt

How to do it...

Perform the following steps to apply a projective transformation to an image using the transform module from scikit-image:

  1. First, read the source image and create a destination image with the np.zeros() function:
im_src = (imread('images/humming2.png'))
height, width, dim = im_src.shape
im_dst = np.zeros((height, width, dim))
  1. Create an instance of the ProjectiveTransform class:
pt = ProjectiveTransform()

  1. You just need to provide four pairs of matching points between the source and destination images to estimate the homography matrix, H, automatically for you. Here, the four corners of the destination image and the four corners of the input hummingbird are provided as matching points, as shown in the following code block:
src = np.array([[ 295., 174.],
[ 540., 146. ],
[ 400., 777.],
[ 60., 422.]])
dst = np.array([[ 0., 0.],
[height-1, 0.],
[height-1, width-1],
[ 0., width-1]])
  1. Obtain the source pixel index corresponding to each pixel index in the destination:
x, y = np.mgrid[:height, :width]
dst_indices = np.hstack((x.reshape(-1, 1), y.reshape(-1,1)))
src_indices = np.round(pt.inverse(dst_indices), 0).astype(int)
valid_idx = np.where((src_indices[:,0] < height) & (src_indices[:,1] < width) &
(src_indices[:,0] >= 0) & (src_indices[:,1] >= 0))
dst_indicies_valid = dst_indices[valid_idx]
src_indicies_valid = src_indices[valid_idx]
  1. Copy pixels from the source to the destination images:
im_dst[dst_indicies_valid[:,0],dst_indicies_valid[:,1]] =       

If you run the preceding code snippets, you will get an output like the following screenshot:

The next screenshot shows the source image of an astronaut on the moon and the destination image of the canvas. Again, by providing four pairs of mapping points in between the source (corner points) and destination (corners of the canvas), the task is pretty straightforward:

The following screenshot shows the output image after the projective transform:

How it works...

In both of the preceding cases, the input image is projected onto the desired location of the output image.

A ProjectiveTransform object is needed to be created first to apply perspective transform to an image.

A set of 4-pixel positions from the source image and corresponding matching pixel positions in the destination image are needed to be passed to the estimate() function along with the object instance and this computes the homography matrix, H (and returns True if it can be computed).

The inverse() function is to be called on the object and this will give you the source pixel indices corresponding to all destination pixel indices.

There's more...

You can use the warp() function (instead of the inverse() function) to implement homography/projective transform.

See also