Python Image Processing Cookbook - Sandipan Dey - E-Book

Python Image Processing Cookbook E-Book

Sandipan Dey

0,0
40,81 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.

Mehr erfahren.
Beschreibung

Explore Keras, scikit-image, open source computer vision (OpenCV), Matplotlib, and a wide range of other Python tools and frameworks to solve real-world image processing problems




Key Features



  • Discover solutions to complex image processing tasks using Python tools such as scikit-image and Keras


  • Learn popular concepts such as machine learning, deep learning, and neural networks for image processing


  • Explore common and not-so-common challenges faced in image processing



Book Description



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.




What you will learn



  • Implement supervised and unsupervised machine learning algorithms for image processing


  • Use deep neural network models for advanced image processing tasks


  • Perform image classification, object detection, and face recognition


  • Apply image segmentation and registration techniques on medical images to assist doctors


  • Use classical image processing and deep learning methods for image restoration


  • Implement text detection in images using Tesseract, the optical character recognition (OCR) engine


  • Understand image enhancement techniques such as gradient blending



Who this book is for



This book is for image processing engineers, computer vision engineers, software developers, machine learning engineers, or anyone who wants to become well-versed with image processing techniques and methods using a recipe-based approach. Although no image processing knowledge is expected, prior Python coding experience is necessary to understand key concepts covered in the book.

Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:

EPUB

Seitenzahl: 330

Veröffentlichungsjahr: 2020

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



Python Image Processing Cookbook

 

 

Over 60 recipes to help you perform complex image processing and computer vision tasks with ease

 

 

 

 

 

 

 

 

 

 

Sandipan Dey

 

 

 

 

 

 

 

 

 

 

 

BIRMINGHAM - MUMBAI

Python Image Processing Cookbook

Copyright © 2020 Packt Publishing

All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.

Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author(s), nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book.

Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.

Commissioning Editor: Pravin DhandreAcquisition Editor:Devika BattikeContent Development Editor: Athikho Sapuni RishanaSenior Editor: Sofi RogersTechnical Editor:Manikandan KurupCopy Editor: Safis EditingProject Coordinator:Aishwarya MohanProofreader: Safis EditingIndexer:Pratik ShirodkarProduction Designer: Joshua Misquitta

First published: April 2020

Production reference: 1170420

Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK.

ISBN 978-1-78953-714-7

www.packt.com

 

Packt.com

Subscribe to our online digital library for full access to over 7,000 books and videos, as well as industry leading tools to help you plan your personal development and advance your career. For more information, please visit our website.

Why subscribe?

Spend less time learning and more time coding with practical eBooks and Videos from over 4,000 industry professionals

Improve your learning with Skill Plans built especially for you

Get a free eBook or video every month

Fully searchable for easy access to vital information

Copy and paste, print, and bookmark content

Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.packt.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details.

At www.packt.com, you can also read a collection of free technical articles, sign up for a range of free newsletters, and receive exclusive discounts and offers on Packt books and eBooks. 

 
 
 
 
 
 
 
 
 
 
I dedicate this book to my beloved parents and all the teachers in my life from all the schools:Ballygunge Government High School, Kolkata, IndiaJadavpur University, Kolkata, IndiaUniversity of Maryland, Baltimore County, USAMOOC platforms like Coursera and edX

Contributors

About the author

Sandipan Dey is a data scientist with a wide range of interests, covering topics such as machine learning, deep learning, image processing, and computer vision. He has worked in numerous data science fields, working with recommender systems, predictive models for the events industry, sensor localization models, sentiment analysis, and device prognostics. He earned his master's degree in computer science from the University of Maryland, Baltimore County, and has published in a few IEEE data mining conferences and journals. He has earned certifications from 100+ MOOCs on data science, machine learning, deep learning, image processing, and related courses. He is a regular blogger (sandipanweb) and is a machine learning education enthusiast.

About the reviewers

Srinjoy Ganguly holds a master's in Artificial Intelligence from the University of Southampton, UK and has over 4 years of experience in AI. He completed his bachelor's in Electronics and Communications from GGSIPU, Delhi, and gained a thorough knowledge of signal processing algorithms and neural networks. He has actively pursued the theory and principles of machine learning, deep learning, computer vision, and reinforcement learning. His research interests include Bayesian machine learning, Bayesian deep learning, and quantum machine learning. He is also an active contributor to Julia language. He strongly believes that this book will be an extremely valuable asset into the hands of beginners in the field of AI and computer vision.

 

 

 

 

 

Shankar Jha is a software engineer who has helped a lot of beginners get to grips with Python through his articles and talks. He writes about Python, Django, tackling climate change, and poverty alleviation. He lives by the philosophy of Richard Feynman and Swami Vivekananda. His latest endeavor is to help facilitate the use of environment-friendly products. He believes this book will give you enough hands-on experience to jump start your career in computer vision.

 

 

 

 

Packt is searching for authors like you

If you're interested in becoming an author for Packt, please visit authors.packtpub.com and apply today. We have worked with thousands of developers and tech professionals, just like you, to help them share their insight with the global tech community. You can make a general application, apply for a specific hot topic that we are recruiting an author for, or submit your own idea.

Table of Contents

Title Page

Copyright and Credits

Python Image Processing Cookbook

About Packt

Why subscribe?

Dedication

Contributors

About the author

About the reviewers

Packt is searching for authors like you

Preface

Who this book is for

What this book covers

To get the most out of this book

Download the example code files

Download the color images

Conventions used

Sections

Getting ready

How to do it…

How it works…

There's more…

See also

Get in touch

Reviews

Image Manipulation and Transformation

Technical requirements

Transforming color space (RGB → Lab)

Getting ready

How to do it...

Converting RGB image into grayscale by setting the Lab space color channels to zero

Changing the brightness of the image by varying the luminosity channel

How it works...

There's more...

Applying affine transformation

Getting ready

How to do it...

How it works...

There's more...

Applying perspective transformation and homography

Getting ready

How to do it...

How it works...

There's more...

See also

Creating pencil sketches from images

Getting ready

How to do it...

How it works...

There's more...

See also

Creating cartoonish images

Getting ready

How to do it...

How it works...

There's more...

See also

Simulating light art/long exposure

Getting ready

How to do it...

How it works...

There's more...

Extended depth of field with mahotas

See also

Object detection using color in HSV

Getting ready

How to do it...

How it works...

See also

Image Enhancement

Applying filters to denoise different types of noise in an image

Getting ready

How to do it...

How it works...

There's more...

Image denoising with a denoising autoencoder

Getting ready

How to do it...

How it works...

There's more...

See also

Image denoising with PCA/DFT/DWT

Getting ready

How to do it...

How it works...

There's more...

See also

Image denoising with anisotropic diffusion

Getting ready

How to do it...

How it works...

There's more...

See also

Improving image contrast with histogram equalization

Getting ready

How to do it...

How it works...

There's more...

Implementing histogram matching

Getting ready

How to do it...

How it works...

There's more...

See also

Performing gradient blending

Getting ready

How to do it...

How it works...

Edge detection with Canny, LoG/zero-crossing, and wavelets

Getting ready

How to do it...

Canny/hysteresis thresholding

LoG/zero-crossing

Wavelets 

How it works...

There's more...

See also

Image Restoration

Restoring an image with the Wiener filter

Getting ready

How to do it...

How it works...

See also

Restoring an image with the constrained least squares filter 

Getting ready

How to do it...

How it works...

There's more...

See also

Image restoration with a Markov random field

Getting ready

How to do it...

How it works...

See also

Image inpainting

Getting ready

How to do it...

How it works...

There's more...

Image inpainting with convex optimization

See also

Image completion with inpainting using deep learning

Getting ready

How to do it...

There's more...

See also

Image restoration with dictionary learning

Getting ready

How to do it ...

There's more...

Online dictionary learning

See also

Compressing an image using wavelets

Getting ready

How to do it...

How it works...

See also

Using steganography and steganalysis

Getting ready

How to do it...

How it works...

There's more...

See also

Binary Image Processing

Applying morphological operators to a binary image

Getting ready

How to do it...

How it works...

There's more...

See also

Applying Morphological filters

Getting ready

How to do it...

Computing the Euler number, eccentricity, and center of mass with mahotas/scikit-image

Morphological image filters with mahotas

Binary image filters with SimpleITK

Dilation by reconstruction with skimage

How it works...

There's more...

See also

Morphological pattern matching

Getting ready

How to do it...

How it works...

There's more...

See also

Segmenting images with morphology

Getting ready

How to do it...

Morphological watershed

Blob detection with morphological watershed

How it works...

There's more...

Blob detection with LOG scale-space

See also

Counting objects

Getting ready

How to do it...

Blob separation and detection with erosion

Object counting with closing and opening

How it works...

There's more...

See also

Image Registration

Medical image registration with SimpleITK

Getting ready

How to do it...

How it works...

There's more

See also

Image alignment with ECC algorithm and warping 

Getting ready

How to do it...

How it works...

There is more

See also

Face alignment with dlib

Getting ready

How to do it...

How it works...

There is more

See also

Robust matching and homography with the RANSAC algorithm

Getting ready

How to do it...

How it works...

See also

Image mosaicing (panorama)

Getting ready

How to do it...

Panorama with OpenCV-Python

How it works...

There is more

See also

Face morphing

Getting ready

How to do it...

How it works

There is more

See also

Implementing an image search engine

Getting ready

How to do it...

Finding similarity between an image and a set of images with SIFT

Steps to implement a simple image search engine

There is more

See also

Image Segmentation

Thresholding with Otsu and Riddler–Calvard

Getting ready

How to do it...

How it works...

There's more...

See also

Image segmentation with self-organizing maps

Getting ready

How to do it...

How it works...

There's more...

Clustering handwritten digit images with SOM

See also

RandomWalk segmentation with scikit-image

Getting ready

How to do it...

How it works...

There's more...

See also

Human skin segmentation with the GMM-EM algorithm

Getting ready

How to do it...

How it works...

See also

Medical image segmentation

Getting ready

How to do it...

Segmentation with GMM-EM

Brain tumor segmentation using deep learning

Segmentation with watershed

How it works...

There's more...

See also

Deep semantic segmentation

Getting ready

How to do it...

Semantic segmentation with DeepLabV3

Semantic segmentation with FCN

See also

Deep instance segmentation

Getting ready

How to do it...

How it works...

See also

Image Classification

Classifying images with scikit-learn (HOG and logistic regression)

Getting ready

How to do it...

How it works...

There's more...

See also

Classifying textures with Gabor filter banks

Getting ready

How to do it...

How it works...

There's more...

See also

Classifying images with VGG19/Inception V3/MobileNet/ResNet101 (with PyTorch)

Getting ready

How to do it...

How it works...

There's more...

See also

Fine-tuning (with transfer learning) for image classification

Getting ready

How to do it...

How it works...

There's more...

See also

Classifying traffic signs using a deep learning model (with PyTorch)

Getting ready

How to do it...

How it works...

There's more...

See also

Estimating a human pose using a deep learning model

Getting ready

How to do it...

How it works...

See also

Object Detection in Images

Object detection with HOG/SVM

Getting started

How to do it...

How it works...

There's more...

See also

Object detection with Yolo V3

Getting started

How to do it...

How it works...

There's more...

See also

Object detection with Faster R-CNN

Getting started

How to do it...

How it works...

There's more...

See also

Object detection with Mask R-CNN

Getting started

How to do it...

How it works...

There's more...

See also

Multiple object tracking with Python-OpenCV

Getting started

How to do it...

How it works...

There's more...

See also

Text detection/recognition in images with EAST/Tesseract

Getting started

How to do it...

How it works...

See also

Face detection with Viola-Jones/Haar-like features

Getting ready

How to do it...

How it works...

There's more...

See also

Face Recognition, Image Captioning, and More

Face recognition using FaceNet

Getting ready

How to do it...

How it works...

See also

Age, gender, and emotion recognition using deep learning models

Getting ready

How to do it...

There's more...

See also

Image colorization with deep learning

Getting ready

How to do it...

See also

Automatic image captioning with a CNN and an LSTM

Getting ready

How to do it...

How it works...

See also

Image generation with a GAN

Getting ready

How to do it...

How it works...

There's more...

See also

Using a variational autoencoder to reconstruct and generate images

Getting ready

How to do it...

There's more...

See also

Using a restricted Boltzmann machine to reconstruct Bangla MNIST images

Getting ready

How to do it...

See also

Other Books You May Enjoy

Leave a review - let other readers know what you think

Preface

With advancements in wireless devices and mobile technology, there's an increasing demand for digital image processing skills to extract useful information from the ever-growing volume of images. This book provides comprehensive coverage of tools and algorithms, along with guiding 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 image 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-based techniques such as transfer learning and fine-tuning to solve real-world problems.

By the end of this Python book, you'll be proficient in applying image processing techniques effectively to leverage the capabilities of the Python ecosystem.

Who this book is for

This Python cookbook is for image processing engineers, computer vision engineers, software developers, machine learning engineers, or anyone who wants to become well-versed with image processing techniques and methods using a recipe-based approach. Although no image processing knowledge is expected, prior Python coding experience is necessary to understand key concepts covered in the book.

What this book covers

Chapter 1, Image Manipulation and Transformation, is where you will learn how to use different Python libraries (NumPy, SciPy, scikit-image, OpenCV, and Matplotlib) for image manipulation and transformation. You will learn how to write Python code to do point transforms (log/gamma transform, Gotham filter, colorspace transformation, and increasing brightness/contrast) and geometric transforms (swirl transform, perspective transform, and homography).

Chapter 2, Image Enhancement, is where you will learn how to use different Python libraries (NumPy, SciPy, scikit-image, OpenCV, PyWavelet, and MedPy) to denoise images (using linear/nonlinear filters, Fast Fourier transform (FFT), and autoencoders). You'll learn how to implement image enhancement techniques such as histogram equalization/matching, sketching/cartoonizing, pyramid blending/gradient blending, and edge detection with zero crossing.

Chapter 3, Image Restoration, is where you will learn how to implement image restoration (using NumPy, scikit-image, OpenCV, and scikit-learn) with deconvolution (inverse/weiner/LMS) filters. You'll learn how to implement image restoration with inpainting, variational methods, and sparse dictionary learning. You'll also learn how to implement steganography/steganalysis techniques with pysteg.

Chapter 4, Binary Image Processing, is where you will learn how to use different Python libraries (NumPy, SciPy, scikit-image, and OpenCV) for binary image processing (with mathematical morphology). You'll learn how to implement morphological operators, filters, and pattern matching and how to apply them in segmentation, fingerprint enhancement, counting objects, and blob separation. 

Chapter 5, Image Registration, is where you will learn how to use different Python libraries (NumPy, scikit-image, OpenCV, and PyStasm) for image matching/registration/stitching. You'll learn how to implement image registration techniques with warping/feature (SIFT/SURF/ORB)-based methods and the RANSAC algorithm. You'll also learn how to implement panorama image creation, and face morphing, as well as how to implement a basic image search engine.

Chapter 6, Image Segmentation, is where you will learn how to use different Python libraries (NumPy, scikit-image, OpenCV, SimpleITK, and DeepLab) for image segmentation. You'll learn how to implement image segmentation techniques with graph-based methods/clustering methods, super-pixelation, and machine learning algorithms. You'll also learn how to implement semantic segmentation with DeepLab.

Chapter 7, Image Classification, is where you will learn how to use different Python libraries (scikit-learn, OpenCV, TensorFlow, Keras, and PyTorch) for image classification. You'll learn how to implement deep learning-based techniques such as transfer learning/fine-tuning. You'll learn how to implement panorama image creation and face morphing. You'll also learn how to implement deep learning-based classification techniques for hand gestures and traffic signals.

Chapter 8, Object Detection in Images, is where you will learn how to use different Python libraries (scikit-learn, OpenCV, TensorFlow, Keras, and PyTorch) for object detection in images. You'll learn how to implement classical machine learning (HOG/SVM) techniques as well as deep learning models to detect objects. You'll also learn how to implement barcode detection and text detection from images.

Chapter 9, Face Detection and Recognition, is where you will learn how to use different Python libraries (scikit-learn, OpenCV, dlib, TensorFlow, Keras, PyTorch, DeepFace, and FaceNet) for face detection in images. You'll also learn how to implement facial keypoint recognition and facial/emotion/gender recognition with deep learning.

To get the most out of this book

Basic knowledge of Python and image processing is required to understand and run the code, along with access to a few online image datasets and the book's GitHub link.

Python 3.5+ (Python 3.7.4 was used to test the code) is needed with Anaconda preferably installed for the Windows users, along with Jupyter (to view/run notebooks).

All the code was tested on Windows 10 (Pro) with 32 GB RAM and an Intel i7-series processor. However, the code should require little/no change to be run on Linux.

You will need to install all the required Python packages using pip3.

Access to a GPU is recommended to run the recipes involving training with deep learning (that is, training that involves libraries such as TensorFlow, Keras, and PyTorch) much faster. The code that is best run with a GPU was tested on an Ubuntu 16.04 machine with an Nvidia Tesla K80 GPU (with CUDA 10.1).

A basic math background is also needed to understand the concepts in the book.

Software/hardware covered in the book

OSrequirements

Python 3.7.4.

Windows 10.

Anaconda version 2019.10 (py37_0).

Windows 10.

For the GPU, you will need an NVIDIA graphics card or access to an AWS GPU instance (https://docs.aws.amazon.com/dlami/latest/devguide/gpu.html) or Google Colab (https://colab.research.google.com/).

Windows 10/Linux (Ubuntu 16).

 

If you are using the digital version of this book, we advise you to type the code yourself or access the code via the GitHub repository (link available in the next section). Doing so will help you avoid any potential errors related to the copying and pasting of code.

To access the notebooks and images, clone the repository from this URL: https://github.com/PacktPublishing/Python-Image-Processing-Cookbook.

Install Python 3.7 and the necessary libraries as and when required. Install Anaconda/Jupyter and open the notebooks for each chapter. Run the code for each recipe. Follow the instructions for each recipe for any additional steps (for instance, you may need to download a pre-trained model or an image dataset).

Some additional exercises are provided for most of the recipes in a There's more... section to test your understanding. Perform them independently and have fun!

Download the example code files

You can download the example code files for this book from your account at www.packt.com. If you purchased this book elsewhere, you can visit www.packtpub.com/support and register to have the files emailed directly to you.

You can download the code files by following these steps:

Log in or register at

www.packt.com

.

Select the

Support

tab.

Click on

Code Downloads

.

Enter the name of the book in the

Search

box and follow the onscreen instructions.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

WinRAR/7-Zip for Windows

Zipeg/iZip/UnRarX for Mac

7-Zip/PeaZip for Linux

The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Python-Image-Processing-Cookbook. In case there's an update to the code, it will be updated on the existing GitHub repository.

We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

Download the color images

We also provide a PDF file that has color images of the screenshots/diagrams used in this book. You can download it here: https://static.packt-cdn.com/downloads/9781789537147_ColorImages.pdf.

Sections

In this book, you will find several headings that appear frequently (Getting ready, How to do it..., How it works..., There's more..., and See also).

To give clear instructions on how to complete a recipe, use these sections as follows:

Getting ready

This section tells you what to expect in the recipe and describes how to set up any software or any preliminary settings required for the recipe.

How to do it…

This section contains the steps required to follow the recipe.

How it works…

This section usually consists of a detailed explanation of what happened in the previous section.

There's more…

This section consists of additional information about the recipe in order to make you more knowledgeable about the recipe.

See also

This section provides helpful links to other useful information for the recipe.

Get in touch

Feedback from our readers is always welcome.

General feedback: If you have questions about any aspect of this book, mention the book title in the subject of your message and email us at [email protected].

Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you would report this to us. Please visit www.packtpub.com/support/errata, selecting your book, clicking on the Errata Submission Form link, and entering the details.

Piracy: If you come across any illegal copies of our works in any form on the Internet, we would be grateful if you would provide us with the location address or website name. Please contact us at [email protected] with a link to the material.

If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit authors.packtpub.com.

Reviews

Please leave a review. Once you have read and used this book, why not leave a review on the site that you purchased it from? Potential readers can then see and use your unbiased opinion to make purchase decisions, we at Packt can understand what you think about our products, and our authors can see your feedback on their book. Thank you!

For more information about Packt, please visit packt.com.

Image Manipulation and Transformation

Image transformation is the art of transforming an image. With image transformation and manipulation, we can enhance the appearance of an image. The transformation and manipulation operation can also be used as preprocessing steps for more complex image processing tasks, such as classification or segmentation, which you will get more acquainted with in later chapters. In this chapter, you are going to learn how to use different Python libraries (NumPy, SciPy, scikit-image, OpenCV-Python, Mahotas, and Matplotlib) for image manipulation and transformation. Different recipes will help you to learn how to write Python code to implement color space transformation, geometric transformations, perspective transforms/homography, and so on.

In this chapter, we will cover the following recipes:

Transforming color space (RGB → Lab)

Applying affine transformation

Applying perspective transformation and homography

Creating pencil sketches from images

Creating cartoonish images

Simulating light art/long exposure

Object detection using color in HSV

Technical requirements

To run the codes without any errors, you need to first install Python 3 (for example, 3.6) and the required libraries, if they are not already installed. If you are working on Windows, you are recommended to install the Anaconda distribution. You also need to install the jupyter library to work with the notebooks. 

All of the code files in this book are available in the GitHub repository at https://github.com/PacktPublishing/Python-Image-Processing-Cookbook. You should clone the repository (to your working directory). Corresponding to each chapter, there is a folder and each folder contains a notebook with the complete code (for all of the recipes for each chapter); a subfolder named images, which contains all the input images (and related files) required for that chapter; and (optionally) another sub-folder named models, which contains the models and related files to be used for the recipes in that chapter.

Transforming color space (RGB → Lab)

The CIELAB(abbreviated as Lab) color space consists of three color channels, expressing the color of a pixel as three tuples (L, a, b), where the L channel stands for luminosity/illumination/intensity (lightness). The a and b channels represent thegreen-redandblue-yellowcolor components, respectively. This color model separates theintensityfrom thecolorscompletely. It's device-independent and has a largegamut. In this recipe, you will see how to convert from RGB into the Lab color space and vice versa and the usefulness of this color model.

Getting ready

In this recipe, we will use a flower RGB image as the input image. Let's start by importing the required libraries with the following code block:

import numpy as npfrom skimage.io import imreadfrom skimage.color import rgb2lab, lab2rgbimport matplotlib.pylab as plt

How to do it...

In this recipe, you will see a few remarkable uses of the Lab color space and how it makes some image manipulation operations easy and elegant.

How it works...

The rgb2lab() function from the scikit-image color module was used to convert an image from RGB into the Lab color space.

The modified image in theLab color space was converted back into RGB using the lab2rgb() function from the scikit-image color module.

Since the color channels are separated in the a and b channels and in terms of intensity in the L channel by setting the color channel values to zero, we can obtain the grayscale image from a colored image in the Lab space.

The brightness of the input color image was changed by changing only the L channel values in the Lab space (unlike in the RGB color space where all the channel values need to be changed); there is no need to touch the color channels.

Applying affine transformation

An affine transformation is a geometric transformation that preserves points, straight lines, and planes. Lines that are parallel before the transform remain parallel post-application of the transform. For every pixel x in an image, the affine transformation can be represented by the mapping, x |→ Mx+b, where M is a linear transform (matrix) and b is an offset vector. 

In this recipe, we will use the scipy ndimage library function, affine_transform(), to implement such a transformation on an image.

Getting ready

First, let's import the libraries and the functions required to implement an affine transformation on a grayscale image:

import numpy as npfrom scipy import ndimage as ndifrom skimage.io import imreadfrom skimage.color import rgb2gray

How it works...

Note that, for an image, the x axis is the vertical (+ve downward) axis and theyaxis is the horizontal (+ve left-to-right) axis.

With the affine_transform() function, the pixel value at location o in the output (transformed) image is determined from the pixel value in the input image at position np.dot(matrix, o) +offset. Hence, the matrix that needs to be provided as input to the function is actually the inverse transformation matrix. 

In some of the cases, an additional matrix is used for translation, to bring the transformed image within the frame of visualization.

The preceding code snippets show how to implement different affine transformations such as reflection, scaling, rotation, and shear using the affine_transform() function. We need to provide the proper transformation matrix, M (shown in the preceding diagram) for each of these cases (homogeneous coordinates are used). 

We can use the product of all of the matrices to perform a combination of all of the affine transformations at once (for instance, if you want transformation T1 followed by T2, you need to multiply the input image by the matrix T2.T1).

If all of the transformations are applied in sequence and the transformed images are plotted one by one, you will obtain an output like the following screenshot:

There's more...

Again, in the previous example, theaffine_transform() function was applied to a grayscale image. The same effect can be obtained with a color image also, such as by applying the mapping function to each of the image channels simultaneously and independently. Also, thescikit-image library provides the AffineTransform and PiecewiseAffineTransformclasses; you may want to try them to implement affine transformation as well.

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 ProjectiveTransformfunction 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 ProjectiveTransformfrom skimage.io import imreadimport numpy as npimport matplotlib.pylab as plt

How it works...