Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying Python Image Processing Cookbook
  • Table Of Contents Toc
Python Image Processing Cookbook

Python Image Processing Cookbook

By : Sandipan Dey
2 (2)
close
close
Python Image Processing Cookbook

Python Image Processing Cookbook

2 (2)
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)
close
close

Implementing histogram matching

Histogram matching is an image processing task where an image is altered in such a way that its histogram matches the histogram of another reference (template) image's histogram. The algorithm is described as follows:

  1. Compute the cumulative histogram for each image.
  2. For any given pixel value, xi, in the input image, find the corresponding pixel value, xj, in the output image by matching the input image's histogram with the template image's histogram (G(xi)=H(xj), as shown in the following diagram.
  3. Replace pixel xi in the input with xj as shown in the following diagram:

In this recipe, we will implement histogram matching for colored images on our own.

Getting ready

As usual, let's start by importing the required libraries:

from skimage.exposure import cumulative_distribution
from skimage.color import rgb2gray
import matplotlib.pylab as plt
import numpy as np

How to do it...

Perform the following steps to implement histogram matching:

  1. Let's implement the histogram-matching algorithm with the hist_matching() function, which accepts the original and the template image's cdf along with the original image:
def hist_matching(c, c_t, im):
b = np.interp(c, c_t, np.arange(256))
# find closest matches to b_t
pix_repl = {i:b[i] for i in range(256)}
# dictionary to replace the pixels
mp = np.arange(0,256)
for (k, v) in pix_repl.items():
mp[k] = v
s = im.shape
im = np.reshape(mp[im.ravel()], im.shape)
im = np.reshape(im, s)
return im
  1. Compute cdf of an image with the following function:
def cdf(im):
c, b = cumulative_distribution(im)
for i in range(b[0]):
c = np.insert(c, 0, 0)
for i in range(b[-1]+1, 256):
c = np.append(c, 1)
return c

  1. Finally, read the input and template images, compute their cdfs, and create the output image with the hist_matching() function. Plot the input, template, and output images by running the following code:
im = imread('images/goddess.png').astype(np.uint8)
im_t = imread('images/leaves.png')

im1 = np.zeros(im.shape).astype(np.uint8)
for i in range(3):
c = cdf(im[...,i])
c_t = cdf(im_t[...,i])
im1[...,i] = hist_matching(c, c_t, im[...,i])

plt.figure(figsize=(20,17))
plt.subplots_adjust(left=0, top=0.95, right=1, bottom=0, \
wspace=0.05, hspace=0.05)
plt.subplot(221), plt.imshow(im), plt.axis('off'), \
plt.title('Input Image', size=25)
plt.subplot(222), plt.imshow(im_t), plt.axis('off'), \
plt.title('Template Image', size=25)
plt.subplot(223), plt.imshow(im1[...,:3]), plt.axis('off'), \
plt.title('Output Image', size=25)
plt.show()

How it works...

The cumulative_distribution() function from the scikit-image.exposure module was used to compute the cdf of an image.

Note that the cumulative_distribution() function returns the bins in increasing order of consecutive pixel values, starting from the minimum pixel value present in the image up to the maximum pixel value present.

Since we needed all the pixel values starting from 0 to 255, we used np.insert() with an appropriate cdf value (0 for all the smaller pixel values and 1 for higher pixel values not present).

For each of the color channels, cdf was computed separately for the input and the template images using the cdf() function, and then the hist_matching() function was invoked with these values along with the input image to construct the corresponding color channel in the output image.

The following diagram shows the output image generated for these given input and template images:

There's more...

You can use histogram matching to change an image taken in daylight to a night-vision image using an appropriate template image. The next example shows a similar fun application of histogram matching:

See also

CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
Python Image Processing Cookbook
notes
bookmark Notes and Bookmarks search Search in title playlist Add to playlist download Download options font-size Font size

Change the font size

margin-width Margin width

Change margin width

day-mode Day/Sepia/Night Modes

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY

Submit Your Feedback

Modal Close icon
Modal Close icon
Modal Close icon