O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Hands-On Image Processing with Python

Book Description

Explore the mathematical computations and algorithms for image processing using popular Python tools and frameworks.

Key Features

  • Practical coverage of every image processing task with popular Python libraries
  • Includes topics such as pseudo-coloring, noise smoothing, computing image descriptors
  • Covers popular machine learning and deep learning techniques for complex image processing tasks

Book Description

Image processing plays an important role in our daily lives with various applications such as in social media (face detection), medical imaging (X-ray, CT-scan), security (fingerprint recognition) to robotics & space. This book will touch the core of image processing, from concepts to code using Python.

The book will start from the classical image processing techniques and explore the evolution of image processing algorithms up to the recent advances in image processing or computer vision with deep learning. We will learn how to use image processing libraries such as PIL, scikit-mage, and scipy ndimage in Python. This book will enable us to write code snippets in Python 3 and quickly implement complex image processing algorithms such as image enhancement, filtering, segmentation, object detection, and classification. We will be able to use machine learning models using the scikit-learn library and later explore deep CNN, such as VGG-19 with Keras, and we will also use an end-to-end deep learning model called YOLO for object detection. We will also cover a few advanced problems, such as image inpainting, gradient blending, variational denoising, seam carving, quilting, and morphing.

By the end of this book, we will have learned to implement various algorithms for efficient image processing.

What you will learn

  • Perform basic data pre-processing tasks such as image denoising and spatial filtering in Python
  • Implement Fast Fourier Transform (FFT) and Frequency domain filters (e.g., Weiner) in Python
  • Do morphological image processing and segment images with different algorithms
  • Learn techniques to extract features from images and match images
  • Write Python code to implement supervised / unsupervised machine learning algorithms for image processing
  • Use deep learning models for image classification, segmentation, object detection and style transfer

Who this book is for

This book is for Computer Vision Engineers, and machine learning developers who are good with Python programming and want to explore details and complexities of image processing. No prior knowledge of the image processing techniques is expected.

Downloading the example code for this book You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.

Table of Contents

  1. Title Page
  2. Copyright and Credits
    1. Hands-On Image Processing with Python
  3. Dedication
  4. About Packt
    1. Why subscribe?
    2. Packt.com
  5. Contributors
    1. About the author
    2. About the reviewer
    3. Packt is searching for authors like you
  6. Preface
    1. Disclaimer
    2. Who this book is for
    3. What this book covers
    4. To get the most out of this book
      1. Download the example code files
      2. Download the color images
      3. Conventions used
    5. Get in touch
      1. Reviews
  7. Getting Started with Image Processing
    1. What is image processing and some applications
      1. What is an image and how it is stored on a computer
      2. What is image processing?
      3. Some applications of image processing
    2. The image processing pipeline
    3. Setting up different image processing libraries in Python
      1. Installing pip
      2. Installing some image processing libraries in Python
      3. Installing the Anaconda distribution
      4. Installing Jupyter Notebook
    4. Image I/O and display with Python
      1. Reading, saving, and displaying an image using PIL
        1. Providing the correct path to the images on the disk
      2. Reading, saving, and displaying an image using Matplotlib
        1. Interpolating while displaying with Matplotlib imshow()
      3. Reading, saving, and displaying an image using scikit-image
        1. Using scikit-image's astronaut dataset
        2. Reading and displaying multiple images at once
      4. Reading, saving, and displaying an image using scipy misc
        1. Using scipy.misc's face dataset
    5. Dealing with different image types and file formats and performing basic image manipulations
      1. Dealing with different image types and file formats
        1. File formats
          1. Converting from one file format to another
        2. Image types (modes)
          1. Converting from one image mode into another
        3. Some color spaces (channels)
          1. Converting from one color space into another
        4. Data structures to store images
          1. Converting image data structures
      2. Basic image manipulations
        1. Image manipulations with numpy array slicing 
          1. Simple image morphing - α-blending of two images using cross-dissolving
        2. Image manipulations with PIL
          1. Cropping an image
          2. Resizing an image
          3. Negating an image
          4. Converting an image into grayscale
          5. Some gray-level transformations
          6. Some geometric transformations
          7. Changing pixel values of an image
          8. Drawing on an image
          9. Drawing text on an image
          10. Creating a thumbnail
          11. Computing the basic statistics of an image
          12. Plotting the histograms of pixel values for the RGB channels of an image
          13. Separating the RGB channels of an image 
          14. Combining multiple channels of an image
          15. α-blending two images
          16. Superimposing two images
          17. Adding two images
          18. Computing the difference between two images
          19. Subtracting two images and superimposing two image negatives
        3. Image manipulations with scikit-image
          1. Inverse warping and geometric transformation using the warp() function
          2. Applying the swirl transform
          3. Adding random Gaussian noise to images
          4. Computing the cumulative distribution function of an image 
        4. Image manipulation with Matplotlib
          1. Drawing contour lines for an image
        5. Image manipulation with the scipy.misc and scipy.ndimage modules
    6. Summary
    7. Questions
    8. Further reading
  8. Sampling, Fourier Transform, and Convolution
    1. Image formation – sampling and quantization
      1. Sampling
        1. Up-sampling
          1. Up-sampling and interpolation 
        2. Down-sampling
          1. Down-sampling and anti-aliasing
      2. Quantization
        1. Quantizing with PIL
    2. Discrete Fourier Transform
      1. Why do we need the DFT?
      2. The Fast Fourier Transform algorithm to compute the DFT
        1. The FFT with the scipy.fftpack module
          1. Plotting the frequency spectrum
        2. The FFT with the numpy.fft module
          1. Computing the magnitude and phase of a DFT
    3. Understanding convolution
      1. Why convolve an image?
      2. Convolution with SciPy signal's convolve2d
        1. Applying convolution to a grayscale image
          1. Convolution modes, pad values, and boundary conditions
        2. Applying convolution to a color (RGB) image
      3. Convolution with SciPy ndimage.convolve
      4. Correlation versus convolution
        1. Template matching with cross-correlation between the image and template
    4. Summary
    5. Questions
    6. Further reading
  9. Convolution and Frequency Domain Filtering
    1. Convolution theorem and frequency domain Gaussian blur
      1. Application of the convolution theorem
        1. Frequency domain Gaussian blur filter with numpy fft
          1. Gaussian kernel in the frequency domain
        2. Frequency domain Gaussian blur filter with scipy signal.fftconvolve()
        3. Comparing the runtimes of SciPy convolve() and fftconvolve() with the Gaussian blur kernel
    2. Filtering in the frequency domain (HPF, LPF, BPF, and notch filters)
      1. What is a filter?
      2. High-Pass Filter (HPF)
        1. How SNR changes with frequency cut-off
      3. Low-pass filter (LPF)
        1. LPF with scipy ndimage and numpy fft
          1. LPF with fourier_gaussian
        2. LPF with scipy fftpack
        3. How SNR changes with frequency cutoff
      4. Band-pass filter (BPF) with DoG
      5. Band-stop (notch) filter
        1. Using a notch filter to remove periodic noise from images
      6. Image restoration
        1. Deconvolution and inverse filtering with FFT
        2. Image deconvolution with the Wiener filter
        3. Image denoising with FFT
          1. Filter in FFT
          2. Reconstructing the final image
    3. Summary
    4. Questions
    5. Further reading
  10. Image Enhancement
    1. Point-wise intensity transformations – pixel transformation
      1. Log transform
      2. Power-law transform
      3. Contrast stretching
        1. Using PIL as a point operation
        2. Using the PIL ImageEnhance module
      4. Thresholding
        1. With a fixed threshold
        2. Half-toning
        3. Floyd-Steinberg dithering with error diffusion
    2. Histogram processing – histogram equalization and matching
      1. Contrast stretching and histogram equalization with scikit-image
      2. Histogram matching
        1. Histogram matching for an RGB image
    3. Linear noise smoothing
      1. Smoothing with PIL
        1. Smoothing with ImageFilter.BLUR
        2. Smoothing by averaging with the box blur kernel
        3. Smoothing with the Gaussian blur filter
      2. Comparing smoothing with box and Gaussian kernels using SciPy ndimage
    4. Nonlinear noise smoothing
      1. Smoothing with PIL
        1. Using the median filter
        2. Using max and min filter
      2. Smoothing (denoising) with scikit-image
        1. Using the bilateral filter
        2. Using non-local means
      3. Smoothing with scipy ndimage
    5. Summary
    6. Questions
    7. Further reading
  11. Image Enhancement Using Derivatives
    1. Image derivatives – Gradient and Laplacian
      1. Derivatives and gradients
        1. Displaying the magnitude and the gradient on the same image
      2. Laplacian
        1. Some notes about the Laplacian
      3. Effects of noise on gradient computation
    2. Sharpening and unsharp masking
      1. Sharpening with Laplacian
      2. Unsharp masking
        1. With the SciPy ndimage module
    3. Edge detection using derivatives and filters (Sobel, Canny, and so on)
      1. With gradient magnitude computed using the partial derivatives
        1. The non-maximum suppression algorithm
      2. Sobel edge detector with scikit-image
      3. Different edge detectors with scikit-image – Prewitt, Roberts, Sobel, Scharr, and Laplace
      4. The Canny edge detector with scikit-image
      5. The LoG and DoG filters
        1. The LoG filter with the SciPy ndimage module
        2. Edge detection with the LoG filter
          1. Edge detection with the Marr and Hildreth's algorithm using the zero-crossing computation
      6. Finding and enhancing edges with PIL
    4. Image pyramids (Gaussian and Laplacian) – blending images
      1. A Gaussian pyramid with scikit-image transform pyramid module
      2. A Laplacian pyramid with scikit-image transform's pyramid module
      3. Constructing the Gaussian Pyramid
      4. Reconstructing an image only from its Laplacian pyramid
      5. Blending images with pyramids
    5. Summary
    6. Questions
    7. Further reading
  12. Morphological Image Processing
    1. The scikit-image morphology module
      1. Binary operations
        1. Erosion
        2. Dilation
        3. Opening and closing
        4. Skeletonizing
        5. Computing the convex hull
        6. Removing small objects
        7. White and black top-hats
        8. Extracting the boundary 
      2. Fingerprint cleaning with opening and closing
      3. Grayscale operations
    2. The scikit-image filter.rank module
      1. Morphological contrast enhancement
      2. Noise removal with the median filter
      3. Computing the local entropy
    3. The SciPy ndimage.morphology module
      1. Filling holes in binary objects
      2. Using opening and closing to remove noise
      3. Computing the morphological Beucher gradient
      4. Computing the morphological Laplace
    4. Summary
    5. Questions
    6. Further reading
  13. Extracting Image Features and Descriptors
    1. Feature detectors versus descriptors
    2. Harris Corner Detector
      1. With scikit-image
        1. With sub-pixel accuracy
      2. An application – image matching
        1. Robust image matching using the RANSAC algorithm and Harris Corner features
    3. Blob detectors with LoG, DoG, and DoH
      1. Laplacian of Gaussian (LoG)
      2. Difference of Gaussian (DoG)
      3. Determinant of Hessian (DoH)
    4. Histogram of Oriented Gradients
      1. Algorithm to compute HOG descriptors
      2. Compute HOG descriptors with scikit-image
    5. Scale-invariant feature transform
      1. Algorithm to compute SIFT descriptors
      2. With opencv and opencv-contrib
      3. Application – matching images with BRIEF, SIFT, and ORB
        1. Matching images with BRIEF binary descriptors with scikit-image
        2. Matching with ORB feature detector and binary descriptor using scikit-image
        3. Matching with ORB features using brute-force matching with python-opencv
        4. Brute-force matching with SIFT descriptors and ratio test with OpenCV
    6. Haar-like features
      1. Haar-like feature descriptor with scikit-image
      2. Application – face detection with Haar-like features
        1. Face/eye detection with OpenCV using pre-trained classifiers with Haar-cascade features
    7. Summary
    8. Questions
    9. Further reading
  14. Image Segmentation
    1. What is image segmentation?
    2. Hough transform – detecting lines and circles
    3. Thresholding and Otsu's segmentation
    4. Edges-based/region-based segmentation
      1. Edge-based segmentation
      2. Region-based segmentation
        1. Morphological watershed algorithm
    5. Felzenszwalb, SLIC, QuickShift, and Compact Watershed algorithms 
      1. Felzenszwalb's efficient graph-based image segmentation
      2. SLIC
        1. RAG merging
      3. QuickShift
      4. Compact Watershed
      5. Region growing with SimpleITK 
    6. Active contours, morphological snakes, and GrabCut algorithms
      1. Active contours
      2. Morphological snakes
      3. GrabCut with OpenCV
    7. Summary
    8. Questions
    9. Further reading
  15. Classical Machine Learning Methods in Image Processing
    1. Supervised versus unsupervised learning
    2. Unsupervised machine learning – clustering, PCA, and eigenfaces
      1. K-means clustering for image segmentation with color quantization
      2. Spectral clustering for image segmentation
      3. PCA and eigenfaces 
        1. Dimension reduction and visualization with PCA
          1. 2D projection and visualization
        2. Eigenfaces with PCA
          1. Eigenfaces
          2. Reconstruction
          3. Eigen decomposition
    3. Supervised machine learning – image classification
      1. Downloading the MNIST (handwritten digits) dataset
      2. Visualizing the dataset
      3. Training kNN, Gaussian Bayes, and SVM models to classify MNIST 
        1. k-nearest neighbors (KNN) classifier
          1. Squared Euclidean distance
          2. Computing the nearest neighbors
          3. Evaluating the performance of the classifier
        2. Bayes classifier (Gaussian generative model)
          1. Training the generative model – computing the MLE of the Gaussian parameters
          2. Computing the posterior probabilities to make predictions on test data and model evaluation
        3. SVM classifier
    4. Supervised machine learning – object detection
      1. Face detection with Haar-like features and cascade classifiers with AdaBoost – Viola-Jones
        1. Face classification using the Haar-like feature descriptor
          1. Finding the most important Haar-like features for face classification with the random forest ensemble classifier
      2. Detecting objects with SVM using HOG features
        1. HOG training
        2. Classification with the SVM model
        3. Computing BoundingBoxes with HOG-SVM
        4. Non-max suppression
    5. Summary
    6. Questions
    7. Further reading
  16. Deep Learning in Image Processing - Image Classification
    1. Deep learning in image processing
      1. What is deep learning?
      2. Classical versus deep learning
      3. Why deep learning?
    2. CNNs
      1. Conv or pooling or FC layers – CNN architecture and how it works
        1. Convolutional layer
        2. Pooling layer
        3. Non-linearity – ReLU layer
        4. FC layer
        5. Dropout
    3. Image classification with TensorFlow or Keras
      1. Classification with TF
      2. Classification with dense FC layers with Keras
        1. Visualizing the network
        2. Visualizing the weights in the intermediate layers 
      3. CNN for classification with Keras
        1. Classifying MNIST
        2. Visualizing the intermediate layers 
    4. Some popular deep CNNs
      1. VGG-16/19
        1. Classifying cat/dog images with VGG-16 in Keras
          1. Training phase
          2. Testing (prediction) phase
      2. InceptionNet
      3. ResNet
    5. Summary
    6. Questions
    7. Further reading
  17. Deep Learning in Image Processing - Object Detection, and more
    1. Introducing YOLO v2 
      1. Classifying and localizing images and detecting objects
      2. Proposing and detecting objects using CNNs
      3. Using YOLO v2 
        1. Using a pre-trained YOLO model for object detection
    2. Deep semantic segmentation with DeepLab V3+
      1. Semantic segmentation
      2. DeepLab V3+
        1. DeepLab v3 architecture
        2. Steps you must follow to use DeepLab V3+ model for semantic segmentation
    3. Transfer learning – what it is, and when to use it
      1. Transfer learning with Keras
    4. Neural style transfers with cv2 using a pre-trained torch model
      1. Understanding the NST algorithm
      2. Implementation of NST with transfer learning
        1. Ensuring NST with content loss
        2. Computing the style cost
        3. Computing the overall loss
      3. Neural style transfer with Python and OpenCV
    5. Summary
    6. Questions
    7. Further reading
  18. Additional Problems in Image Processing
    1. Seam carving
      1. Content-aware image resizing with seam carving
      2. Object removal with seam carving
    2. Seamless cloning and Poisson image editing
    3. Image inpainting
    4. Variational image processing
      1. Total Variation Denoising
      2. Creating flat-texture cartoonish images with total variation denoising
    5. Image quilting
      1. Texture synthesis
      2. Texture transfer
    6. Face morphing
    7. Summary
    8. Questions
    9. Further reading
  19. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think