0% found this document useful (0 votes)
13 views262 pages

Introduction to Python in Earth Science Data Analysis

The document is a review copy of 'Image Processing Recipes in MATLAB', which contains 30 practical recipes for image processing, designed for researchers and practitioners. Each recipe includes step-by-step instructions, source code, and visual examples, making it a user-friendly guide for implementing image processing workflows. The book is authored by Oge Marques and Gustavo Benvenutti Borba, and serves as a comprehensive resource for both novices and experienced professionals in the field of image processing.

Uploaded by

idamtlab
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views262 pages

Introduction to Python in Earth Science Data Analysis

The document is a review copy of 'Image Processing Recipes in MATLAB', which contains 30 practical recipes for image processing, designed for researchers and practitioners. Each recipe includes step-by-step instructions, source code, and visual examples, making it a user-friendly guide for implementing image processing workflows. The book is authored by Oge Marques and Gustavo Benvenutti Borba, and serves as a comprehensive resource for both novices and experienced professionals in the field of image processing.

Uploaded by

idamtlab
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 262

Image Processing Recipes in

Copyright Material - Provided by Taylor & Francis


Review Copy - Not for Redistribution
®
MATLAB
Leveraging the latest developments in MATLAB and its image processing toolbox,
this ‘cookbook’ is a collection of 30 practical recipes for image processing, rang-
ing from foundational techniques to recently published algorithms. Presented in a
clear and meaningful sequence, these recipes are prepared with the reader in mind,
allowing one to focus on particular topics or read as a whole from cover to cover.
Key Features:
• A practical, user-friendly guide that equips researchers and practitioners with
the tools to implement efficient image processing workflows in MATLAB.
• Each recipe is presented through clear, step-by-step instructions and rich visual
examples.
• Each recipe contains its own source code, explanations, and figures, making
the book an excellent standalone resource for quick reference.
• Strategically structured to aid sequential learning, yet with self-contained
chapters for those seeking solutions to specific image processing challenges.

The book serves as a concise and readable practical reference to deploy image pro-
cessing pipelines in MATLAB quickly and efficiently. With its accessible and practical
approach, the book is a valuable guide for those who navigate this evolving area,
including researchers, students, developers, and practitioners in the fields of image
processing, computer vision, and image analysis.
Oge Marques, PhD is a Professor of Computer Science and Engineering in the College
of Engineering and Computer Science, a Professor of Biomedical Science (Secondary)
in the Charles E. Schmidt College of Medicine, and a Professor of Information Tech-
nology (by courtesy), in the College of Business at Florida Atlantic University (FAU)
(Boca Raton, FL).
He is the author of 12 technical books, one patent, and more than 130 refereed scien-
tific articles on image processing, medical image analysis, computer vision, artificial
intelligence, and machine learning.
Dr. Marques is a Senior Member of both the IEEE (Institute of Electrical and Elec-
tronics Engineers) and the ACM (Association for Computing Machinery), Fellow of
the NIH AIM-AHEAD Consortium, Fellow of the Leshner Leadership Institute of the
American Association for the Advancement of Science (AAAS), Tau Beta Pi Eminent
Engineer, and member of the honor societies of Sigma Xi, Phi Kappa Phi, and Upsilon
Pi Epsilon.
Gustavo Benvenutti Borba, PhD is an Associate Professor in the Department of Elec-
tronics and the Graduate School on Biomedical Engineering at the Federal University
of Technology-Paraná (UTFPR) (Curitiba, Brazil).
He obtained his PhD in Electrical Engineering from UTFPR. He is the author of
Copyright Material - Provided by Taylor & Francis
more than 30 refereed scientific articles on image processing, image retrieval, and
related topics.Review Copy - Not for Redistribution
Chapman & Hall/CRC Computer Science and Engineering
Recipes
®
ImageCopyright
Processing Recipes in MATLAB
Material - Provided by Taylor & Francis
Oge Marques, Gustavo Benvenutti Borba
Review Copy - Not for Redistribution

For more information on this series please visit: https://www.routledge.com


/Chapman- -HallCRC-Computer-Science-and-Engineering-Recipes-Series/
book-series
Copyright Material - Provided by Taylor & Francis
Review Copy - Not for Redistribution
Image Processing
Copyright Material - Provided by Taylor & Francis
Review Copy - Not for Redistribution

Recipes in MATLAB®

Oge Marques
Gustavo Benvenutti Borba
Designed cover image: © Shutterstock

MATLAB® and Simulink® are trademarks of The MathWorks, Inc. and are used with permission.
The MathWorks does not warrant the accuracy of the text or exercises in this book. This book’s use
Copyright Material - Provided by Taylor & Francis
or discussion of MATLAB® or Simulink® software or related products does not constitute endorse-
ment or sponsorship by The MathWorks of a particular pedagogical approach or particular use of the
Review Copy - Not for Redistribution
MATLAB® and Simulink® software.

First edition published 2024


by CRC Press
2385 NW Executive Center Drive, Suite 320, Boca Raton, FL 33431

and by CRC Press


4 Park Square, Milton Park, Abingdon, Oxon, OX14 4RN

CRC Press is an imprint of Taylor & Francis Group, LLC

© 2024 Oge Marques and Gustavo Benvenutti Borba

Reasonable efforts have been made to publish reliable data and information, but the author and pub-
lisher cannot assume responsibility for the validity of all materials or the consequences of their use.
The authors and publishers have attempted to trace the copyright holders of all material reproduced
in this publication and apologize to copyright holders if permission to publish in this form has not
been obtained. If any copyright material has not been acknowledged please write and let us know so
we may rectify in any future reprint.

Except as permitted under U.S. Copyright Law, no part of this book may be reprinted, reproduced,
transmitted, or utilized in any form by any electronic, mechanical, or other means, now known or here-
after invented, including photocopying, microfilming, and recording, or in any information storage or
retrieval system, without written permission from the publishers.

For permission to photocopy or use material electronically from this work, access www.copyright.com
or contact the Copyright Clearance Center, Inc. (CCC), 222 Rosewood Drive, Danvers, MA 01923, 978-
750-8400. For works that are not available on CCC please contact [email protected]

Trademark notice: Product or corporate names may be trademarks or registered trademarks and are used
only for identification and explanation without intent to infringe.

ISBN: 9780367771973 (hbk)


ISBN: 9780367767136 (pbk)
ISBN: 9781003170198 (ebk)

DOI: 10.1201/9781003170198

Typeset in Palatino
by Newgen Publishing UK
Copyright Material - Provided by Taylor & Francis
Review Copy - Not for Redistribution

To Ingrid and Nicholas, with love


– OM

With love, to Maristela and Tito, who have filled me with motivation and
encouragement
– GB

In loving memory of our dear friend and colleague Hugo Vieira Neto.
Copyright Material - Provided by Taylor & Francis
Review Copy - Not for Redistribution
Contents

Copyright
Preface . . . . . . . . . .Material
. . . . . . . . . .-. Provided
. . . . . . . . . . .by
. . . Taylor
. . . . . . . .&
. . .Francis
.............. xi
Review Copy - Not for Redistribution

Part I Basic image processing: Acquisition and


visualization

Recipe 1: Loading, displaying, and saving images . . . . . . . . . . . . . . . . . . 3


Recipe 2: Image conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Recipe 3: Image acquisition using a webcam . . . . . . . . . . . . . . . . . . . . . . . 14
Recipe 4: Browsing through images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20

Part II Geometric operations

Recipe 5: Geometric transformations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29


Recipe 6: Image warping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38

Part III Histograms

Recipe 7: Histograms and statistics of grayscale images . . . . . . . . . . . . . 47


Recipe 8: Histogram equalization and histogram matching . . . . . . . . . 52
Recipe 9: Individual channel histograms of color images . . . . . . . . . . . 58
Recipe 10: Combined color histograms and dominant colors in an
image . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63

Part IV Point transformations

Recipe 11: Intensity transformation functions . . . . . . . . . . . . . . . . . . . . . . . 73


Recipe 12: Custom point transformation functions . . . . . . . . . . . . . . . . . . 81
Recipe 13: Gamma correction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
Recipe 14: Leveling non-uniform illumination . . . . . . . . . . . . . . . . . . . . . . 94

Part V Spatial filtering and special effects

Recipe 15: Smoothing filters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101


Recipe 16: Sharpening filters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
Recipe 17: Other image filters and special effects . . . . . . . . . . . . . . . . . . . . 114

ix
x Contents

Part VI Image segmentation

Recipe 18: Image binarization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123


Copyright
Recipe Material
19: Region-based - Provided .by
segmentation . . . Taylor
. . . . . . . .&. . .Francis
.............. 131
Recipe 20: Image
Review Copy - Not for Redistribution . . . . . . . . . . . .
segmentation using k-means clustering 136
Recipe 21: Superpixel oversegmentation using SLIC . . . . . . . . . . . . . . . . 143
Recipe 22: Graph-based segmentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148

Part VII Binary image analysis

Recipe 23: Finding, counting, and accessing connected components


in binary images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
Recipe 24: Basic morphological operations . . . . . . . . . . . . . . . . . . . . . . . . . . 166
Recipe 25: Computing connected components’ features . . . . . . . . . . . . . . 175

Part VIII Color image processing

Recipe 26: Converting among different color spaces . . . . . . . . . . . . . . . . . 191


Recipe 27: Color image adjustments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205
Recipe 28: Image pseudocoloring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212

Part IX Batch processing and handling large images

Recipe 29: Processing very large images . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221


Recipe 30: Batch processing a set of images . . . . . . . . . . . . . . . . . . . . . . . . . 229

Bibliography . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243
Preface

This isCopyright
a cookbookMaterial - Provided
containing 30 recipes by
thatTaylor & Francis
showcase classic and modern
image processing
Review Copy - Not for Redistribution aims to provide a
techniques using MATLAB. This book
concise and easily understandable reference for deploying image process-
ing pipelines quickly and efficiently in MATLAB. The recipes cover the
latest developments in MATLAB and relevant toolboxes, including a wide
range of image processing methods. These methods range from foundational
techniques found in textbooks to popular contemporary algorithms.
This book is intended for researchers, students, developers, and practition-
ers in the area of visual media processing, such as computer vision, image
processing, image and video analysis, image retrieval, and multimedia.
The recipes are organized into Parts and presented in a meaningful se-
quence to facilitate reading from cover to cover. Each recipe is self-contained
and includes step-by-step instructions, source code, figures, brief discus-
sions, and references for further learning.
This cookbook serves as both an introductory guide for novices in image
processing and a succinct reference for seasoned practitioners. We’re confi-
dent that its unique format makes it a worthy addition to your collection,
either as an independent resource or alongside comprehensive texts in the
field.

Recipes format
Figure 0.1 shows the general format of the recipes in the book. After the num-
ber and name of the recipe, you will find its goal and, eventually, meaningful
information about that topic.
All the resources required to prepare the recipe are then listed in the You
will need (Ingredients) section, including the version of the MATLAB and
the Image Processing Toolbox (IPT), and image files. Most recipes use MAT-
LAB’s built-in images to streamline the preparation, while giving you the
option to experiment with your images. When external images (available on
GitHub: https://github.com/ip-recipes-matlab-book, including
all code listings) are necessary, their filenames are indicated.
The core of the recipe is presented in the Steps (Preparation) section. It
begins by objectively outlining the complete procedure to achieve the goal
through a concise list of step-by-step instructions. Following the list, you will
find detailed explanations of how to perform that image processing proce-
dure, including annotated codes and figures. Depending on the recipe, this
section is broken into subsections for better organization and flow.

xi
xii Preface

Recipe 19 Region-based Segmentation


This recipe teaches you how to perform... Goal
Copyright Material - Provided by Taylor & Francis
You will need (Ingredients)
Review Copy
MATLAB R2013a -or Not
later for Redistribution
Bullet list with the
required items
MATLAB Image Processing Toolbox (IPT) version...

Steps (Preparation) Numbered list with


1. Load the image into the workspace. the complete
procedure,
2. Specify an initial contour surrounding the...
followed by
detailed
Listing 19.1 illustrates how to perform segmentation... explanations

Discussion (Recipe notes)


Conclusion and
In this recipe, you learned how to...
additional insights

Learn more about it


Useful MATLAB functions List of the most
activecontour... relevant functions

MATLAB documentation, demos, and examples


Getting Started with Image Segmenter App Supplementary
Image Segmentation and... materials and links

FIGURE 0.1
Format of the recipes throughout the book.

In the Discussion (Recipe notes) section, you will find a summary of the
key points covered in the recipe, accompanied by insightful comments high-
lighting the most relevant theoretical and/or practical aspects of the image
processing procedure.
The final section of each recipe is the Learn more about it, where you will
discover a list containing the essential functions utilized in the recipe, along
with other related functions worth exploring. Additionally, there is a curated
list of related MATLAB official materials, including documentation, demos,
and examples, all conveniently linked for further reference.

A note on MATLAB prerequisites and documentation


This book uses MATLAB and some of its toolboxes and apps to demonstrate
practical approaches to image processing, encapsulated as 30 self-contained
recipes.
If you need a MATLAB primer or refresher, we strongly recommend taking
the following MATLAB short courses. These are free, interactive, browser-
based, self-paced training programs that will guide you through the basics
of MATLAB and some common Image Processing and Computer Vision
procedures:
Preface xiii

• MATLAB Onramp
https://matlabacademy.mathworks.com/details/matlab-onramp/getti
ngstarted
• Copyright Material
Image Processing - Provided
Onramp by Taylor & Francis
Review Copy - Not for Redistribution
https://matlabacademy.mathworks.com/details/image-processing-
onramp/imageprocessing

You might also want to bookmark the following resources, which should
be useful for your explorations in MATLAB’s image processing:

• Image Processing Toolbox “home”


www.mathworks.com/help/images
• Comprehensive list of the Image Processing Toolbox functions
www.mathworks.com/help/images/referencelist.html

Additionally, throughout the book, you will see references to valuable


online resources from the MathWorks websites – including documentation,
examples, and demos – that expand the discussion of the topics covered by
each recipe and offer opportunities for further exploration on your own.

We wish you a pleasant reading and hope you will enjoy both the classic
and spicier recipes that we have carefully prepared! Feel free to join our
kitchen by sending us feedback and suggestions for additional titles in
this series.

Acknowledgments
We are immensely grateful to many colleagues in the image processing
community for their encouragement and valuable lessons throughout the
years.
We are grateful to our current and former students who have provided
valuable inspiration throughout many insightful conversations over the
years, particularly Mikhail Anatholy Koslowski and Maiko Min Ian Lie.
A very special thank-you to our dear colleague and mentor Humberto
Remigio Gamba, for his gentle leadership, unwavering support, continued
encouragement, and friendship.
xiv Preface

Special thanks to Christian Garbin, Matthew Acs and Richard Acs for their
comments and suggestions during the preparation of this manuscript.
Many thanks to the MathWorks Book Program for their continued support
over the years. Material - Provided by Taylor & Francis
Copyright
A very special
ReviewnoteCopy
of gratitude
- Not to
forRandi (Cohen) Slack and her team at
Redistribution
CRC Press / Taylor & Francis for their support throughout this project.
Copyright Material - Provided by Taylor & Francis
Review Copy - Not for Redistribution

Part I

Basic image processing:


Acquisition and
visualization
Part I – Basic image processing: Acquisition and visualization
This Part contains recipes to get you started using MATLAB for image
processing tasks and projects.
Recipe 1 shows how to read images from disk, display them, and save
images to disk.
Recipe 2 teaches how to perform different types of image conversion.
Recipe 3 shows how to use a webcam to acquire your own images.
Recipe 4 introduces the Image Browser App and shows how it can be used
to browse through image folders.
Following these recipes will give you the fundamental skills to acquire,
read, write, browse, and visualize images using MATLAB.

DOI: 10.1201/9781003170198-1 1
Copyright Material - Provided by Taylor & Francis
Review Copy - Not for Redistribution
1
Recipe 1: Loading,
Copyright displaying,
Material - Provided by Taylorand
& Francis
saving images
Review Copy - Not for Redistribution

This recipe teaches you how to load image contents from a file, display an
image, and save the image contents back to disk.

You will need (Ingredients)


• MATLAB R2016b or later
• MATLAB Image Processing Toolbox (IPT) version R2016b or later
• (OPTIONAL1 ) One or more of your images

Steps (Preparation)
(OPTIONAL) Displaying information about an image file
MATLAB has a built-in function to display information about image files
(without opening them and storing their contents in the workspace),
imfinfo.

1. The code below shows how to read information about a built-in image
file, pout.tif.
imfinfo(’pout.tif’)

The resulting structure (stored in variable ans) will contain the


following information2 :
Filename: ’/.../pout.tif’
FileModDate: ’13-Apr-2015 13:23:13’
FileSize: 69296
Format: ’tif’
Width: 240
Height: 291
BitDepth: 8

DOI: 10.1201/9781003170198-2 3
4 Image processing recipes in MATLAB®

ColorType: ’grayscale’
FormatSignature: [73 73 42 0]
ByteOrder: ’little-endian’
NewSubFileType: 0
Copyright Material - Provided8 by Taylor & Francis
BitsPerSample:
Review Copy - Not for Redistribution
Compression: ’PackBits’
PhotometricInterpretation: ’BlackIsZero’
StripOffsets: [9x1 double]
SamplesPerPixel: 1
RowsPerStrip: 34
StripByteCounts: [9x1 double]
XResolution: 72
YResolution: 72
ResolutionUnit: ’Inch’
PlanarConfiguration: ’Chunky’
Orientation: 1
FillOrder: 1
GrayResponseUnit: 0.0100
MaxSampleValue: 255
MinSampleValue: 0
Thresholding: 1
Offset: 69004
ImageDescription: Copyright The MathWorks, Inc.

Many of these fields are too technical, and some are file format-
dependent. Nonetheless, you should still be able to locate information
about the image size (240×291 pixels), the file size (69296 bytes), the
type of image (grayscale), the number of bits per pixel (8), and its
minimum and maximum values (0 and 255).
2. (OPTIONAL) Repeat the process for other built-in images.
3. Type the MATLAB code below and save it in a script.
LISTING 1.1
Displaying MATLAB demo images using the Image Browser App.
1 % Display MATLAB demo images using the Image Browser app
2 folder = fullfile(matlabroot, ’/toolbox/images/imdata’);
3 if exist(folder, ’dir’)
4 fprintf(’Demo Images Folder is %s.\n’, folder);
5 else
6 fprintf(’Folder %s does not exist.\n’, folder);
7 end
8 dir(folder)
9 imageBrowser(folder)

The script in Listing 1.1 will display the demo images’ file names
in the command window and open the MATLAB Image Browser App3 ,
which allows you to browse the images and obtain additional informa-
tion about them – file name, image size (rows × columns × number of
color channels), and data class (Figure 1.1).
4. (OPTIONAL) Repeat the process for your image folders.
Recipe 1: Loading, displaying, and saving images 5

Copyright Material - Provided by Taylor & Francis


Review Copy - Not for Redistribution

FIGURE 1.1
Using the Image Browser App to browse through demo images and obtain additional information
about the trailer.jpg image.

Reading an image file (Loading an image from disk)


MATLAB has a built-in function to open and read the contents of image files
in the most popular formats (e.g., TIFF, JPEG, BMP, GIF, and PNG), imread.
The imread function allows you to read image files of almost any type, in
virtually any format, located anywhere4 . This saves you from a (potentially
large) number of problems associated with file headers, memory allocation,
file format conventions, etc., and allows you to focus on what you want to do
to the image once it has been read and stored into a variable in the MATLAB
workspace.
When using imread, it is essential to know the image type (binary,
grayscale, true color, indexed color, etc.) and assign the image’s contents
(after the file is read and decompressed) to the proper variable(s).

1. The code below shows how to read a built-in grayscale image –


pout.tif – and assign its contents to a workspace variable, img_1.
img_1 = imread(’pout.tif’);

In this case, a single variable, img_1, on the left-hand side, is ap-


propriate. Inspecting the size and numerical contents of img_1, you
should see that it is a 2D array of size 291×240, whose values are
unsigned integers within the [0, 255] range.
6 Image processing recipes in MATLAB®

2. The code below shows how to read a built-in indexed color image
– trees.tif – and assign its contents to two workspace variables:
img_2 (containing the index) and map_img_2 (containing the associ-
ated colormap).
Copyright Material - Provided by Taylor & Francis
Review Copy
[img_2,map_img_2] - Not for Redistribution
= imread(’trees.tif’);

3. The code below shows how to read a built-in true color image – pep-
pers.png – and assign its contents to a variable img_3 whose size
is 𝑀 × 𝑁 × 3, indicating the pixel values for each pixel in each of the
three (R, G, and B) channels, where 𝑀 is the number of rows and 𝑁 is
the number of columns.
img_3 = imread(’peppers.png’);

4. (OPTIONAL) Repeat the process for other built-in images.


5. (OPTIONAL) Repeat the process for your images.

Displaying the contents of an image


MATLAB has several functions for displaying images:

• image: displays an image using the current colormap5 .


• imagesc: scales image data to the full range of the current colormap
and displays the image.
• imshow: displays an image and contains several optimizations and
optional parameters for property settings associated with the image
display.
• imtool: calls the Image Viewer App tool from the command line. The
Image Viewer App offers an integrated environment for displaying im-
ages, accessing several tools for navigating and exploring images and
performing common image processing tasks.

Listing 1.2 shows how to open an image file and display it using different
imshow options:
LISTING 1.2
Opening and displaying an image file.
1 % Open and display an image file
2 img_1 = imread(’pout.tif’);
3 imshow(img_1)
4 figure, imshow(img_1,[])
5 figure, imshow(img_1,[100 160])

The first call to imshow (line 3) displays the image in its original state. The
code in line 4 opens a new figure and displays a scaled (for display purposes)
version of the same image. The code in line 5 specifies a range of gray levels,
such that all values between 100 and 160 will map to values between 0 and
2556 . The three results are shown side-by-side in Figure 1.2.
Recipe 1: Loading, displaying, and saving images 7

Copyright Material - Provided by Taylor & Francis


Review Copy - Not for Redistribution

FIGURE 1.2
Displaying an image: (a) without scaling; (b) scaling for display purposes; (c) emphasizing pixels
within a specified range. Original image: courtesy of The MathWorks.

Writing the resulting image onto a file


MATLAB has a built-in function, imwrite, to write the contents of an image
to disk using any of the most popular graphic file formats (such as JPEG,
PNG, or TIFF) as well as several legacy formats, e.g., PCX, PGM, and BMP.
If the output file format uses lossy compression (e.g., JPEG), imwrite al-
lows the specification of a quality parameter, used as a trade-off between the
resulting image’s subjective quality and file size.
Listing 1.3 shows how to read an image from a PNG file and save it to a
JPG file using three different quality parameters: 75 (default), 5 (poor quality,
small size), and 95 (better quality, larger size).
LISTING 1.3
Image conversion at different quality settings.
1 % Image conversion (PNG->JPG) at different quality settings
2 img_4 = imread(’peppers.png’);
3 imwrite(img_4, ’pep75.jpg’);
4 imwrite(img_4, ’pep05.jpg’, ’quality’, 5);
5 imwrite(img_4, ’pep95.jpg’, ’quality’, 95);

The results are displayed in Figure 1.3. The image in part (c) of the figure
is clearly of lower visual quality than the ones in parts (b) and (d). On the
other hand, the differences between the images in parts (b) and (d) are barely
noticeable.

Discussion (Recipe notes)


In this recipe, you learned the basics of reading images from disk, displaying,
and writing them (presumably in a different format, if you so desire) back to
disk.
8 Image processing recipes in MATLAB®

Copyright Material - Provided by Taylor & Francis


Review Copy - Not for Redistribution

FIGURE 1.3
Reading and writing images: (a) original image (PNG); (b) compressed image (JPG, q = 75, file
size = 24 KB); (c) compressed image (JPG, q = 5, file size = 8 KB); (d) compressed image (JPG,
q = 95, file size = 60 KB). Original image: courtesy of The MathWorks.

One of the best features of MATLAB and its support for image processing is
its ability to handle images of various formats; once the image is loaded into
the workspace, it becomes a variable (matrix) that can be processed using
many useful library functions.
Special attention must be given, however, to the fact that you must have a
basic understanding of image types and their representation in MATLAB to
assign images to the correct variables and process the pixel values in a way
that is consistent with their range.

Learn more about it


Useful MATLAB functions
Type the function name in the search field at www.mathworks.com/help/
matlab/
Recipe 1: Loading, displaying, and saving images 9

· image 7 · imagesc · imfinfo · imread · imshow · imwrite ·

MATLAB documentation, demos, and examples


Copyright Material - Provided by Taylor & Francis
• Add Color Bar to Displayed Grayscale Image
Review Copy - Not for Redistribution
www.mathworks.com/help/images/add-colorbar-to-displayed-image.
html
• Display Different Image Types
www.mathworks.com/help/images/display-different-image-types.html
• Displaying Image Data
www.mathworks.com/help/matlab/creating_plots/displaying-image-
data.html
• Image Browser App
www.mathworks.com/help/images/ref/imagebrowser-app.html
• Image Types in the Toolbox
www.mathworks.com/help/images/image-types-in-the-toolbox.html
• Image Viewer App
www.mathworks.com/help/images/ref/imageviewer-app.html

Notes
1 The recipe has been prepared using MATLAB’s built-in images.
2 All empty fields have been omitted for space reasons.
3 You will learn more about the Image Browser App in Recipe 4.
4 MATLAB also contains specialized functions for reading DICOM (Digital Imaging
and Communications in Medicine) files (dicomread), NITF (National Imagery
Transmission Format) files (nitfread), and HDR (high dynamic range) files
(hdrread). They are beyond the scope of this recipe.
5 The colormap array is an M-by-3 matrix of class double, where each element is a
floating-point value in the range [0, 1]. Each row in the colormap represents the R
(red), G (green), and B (blue) values for that particular row.
6 This means that pixel values below 100 in the original image will become black
pixels in the displayed image and pixel intensities above 160 in the original image
will become white pixels in the displayed image.
7 There are several functions with the same name in MATLAB. In this case, we are
interested in the one at: www.mathworks.com/help/matlab/ref/image.html.
2
Recipe 2: Image
Copyright Materialconversion
- Provided by Taylor & Francis
Review Copy - Not for Redistribution

This recipe teaches you how to perform three types of image conversions:
(1) convert an image file to a different file format; (2) convert an image to a
different type; and (3) convert an image to a different class.

You will need (Ingredients)


• MATLAB R2020b or later
• MATLAB Image Processing Toolbox (IPT) version R2020b or later
• (OPTIONAL1 ) One or more of your images

Steps (Preparation)
Part 1: File format conversion
MATLAB has no library function to perform (batch) file format conversion.
Consequently, you have to follow these steps:

1. Specify the original and desired file formats.


2. Loop through the contents of the folder where the images reside.
3. For each image, read the file in the original format from disk and save
it in the desired file format.

An image can be read from disk using imread and saved back to disk
using imwrite2 .

Part 2: Conversion to a different image type


Conversion between different image types, such as RGB (truecolor), bi-
nary, grayscale, and indexed images, can be achieved using built-in library
functions, such as those listed in Table 2.1.

10 DOI: 10.1201/9781003170198-3
Recipe 2: Image conversion 11

TABLE 2.1
MATLAB functions to perform image data class conversion.
Name Description
Copyright
cmap2gray
Material - Provided by Taylor & Francis
Convert RGB colormap to grayscale colormap
Review Copy
gray2ind Convert-grayscale
Not foror Redistribution
binary image to indexed image
im2gray Convert RGB image to grayscale
ind2gray Convert indexed image to grayscale image
mat2gray Convert matrix to grayscale image
rgb2gray Convert RGB image or colormap to grayscale
rgb2lightness Convert RGB color values to lightness values
rgb2ind Convert RGB image to indexed image
ind2rgb Convert indexed image to RGB image
label2rgb Convert label matrix into RGB image
demosaic Convert Bayer pattern encoded image to truecolor image
imsplit Split multichannel image into its individual channels

Part 3: Conversion to a different image class


Once the contents of an image have been read and stored into one or more
variables, you are encouraged to inspect the data class of these variables
and their range of values to understand how the pixel contents are repre-
sented and what is their allowed range of values. You should ensure that
the variable’s data class is compatible with the input data class expected by
the MATLAB functions that will be applied to that variable. If you are writing
your own functions and scripts, you must also ensure data class compatibility
or perform the necessary conversions.
The most common data classes for images in MATLAB are:

• uint8: 1 byte (8 bits) per pixel per channel, where each pixel value is
represented within the [0, 255] range.
• uint16: 2 bytes (16 bits) per pixel per channel, where each pixel value
is represented within the [0, 65535] range.
• int16: 2 bytes (16 bits) per pixel per channel, where each pixel value
is represented within the [-32768, 32767] range.
• single: 4 bytes (32 bits) per pixel per channel, i.e., single-precision
floating-point values, usually in the [0.0, 1.0] range.
• double (the default numeric data type (class) in MATLAB): 8 bytes (64
bits) per pixel per channel, i.e., double-precision floating-point values,
usually in the [0.0, 1.0] range.
• logical: 1 byte per pixel, representing its value as true (1 or white) or
false (0 or black).

To convert an image (or an arbitrary array, for that matter) to a data class
and range suitable for image processing, you are encouraged to use one of
the specialized functions listed in Table 2.2.
12 Image processing recipes in MATLAB®

TABLE 2.2
IPT functions to perform image data class conversion.
Name Description
Copyright Material - Provided
im2double
by Taylor & Francis
Convert image to double precision
Review Copy - Convert
im2int16 Not for Redistribution
image to 16-bit signed integers
im2single Convert image to single precision
im2uint16 Convert image to 16-bit unsigned integers
im2uint8 Convert image to 8-bit unsigned integers

Note that functions such as im2double and im2single do not rescale the
output to the [0.0, 1.0] range when the input image has single or double
data type. If your input image is of data type single or double with pixel
values outside this range, you can use the rescale function to scale pixel
values to the expected range.

Discussion (Recipe notes)


In this recipe, you learned three types of conversions that might be needed
when dealing with images in MATLAB:

1. File format conversions, essentially accomplished using (a series of)


imread and imwrite function calls.
2. Image type conversions, for which there are several convenient built-
in functions (Table 2.1).
3. Image class (data type) conversions, for which there are several built-
in functions (Table 2.2).

As mentioned in Recipe 1, one of the best features of MATLAB is its ability


to handle images of various formats and classes. In this recipe, we have ex-
panded upon the need to have a basic understanding of data types (classes)
in MATLAB in order to ensure that pixel values are represented using the ap-
propriate numerical precision and within an expected range of values. This is
particularly crucial in the case of images of type single or double – where
the values are typically within the [0.0, 1.0] range, whereas the correspond-
ing data classes are capable of representing much larger and smaller (i.e.,
negative) values.

Learn more about it


Useful MATLAB functions
Type the function name in the search field at www.mathworks.com/help/
matlab/
Recipe 2: Image conversion 13

· cmap2gray · demosaic · gray2ind · im2double · im2gray


· im2single · imread · imsplit · imwrite · ind2gray · ind2rgb
· label2rgb · mat2gray · rescale · rgb2gray · rgb2ind ·
rgb2lightness ·
Copyright Material - Provided by Taylor & Francis
Review Copy - Not for Redistribution
MATLAB documentation, demos, and examples
• Convert Between Image Types
www.mathworks.com/help/images/convert-between-image-types.html
• Convert Image Data Between Data Types
www.mathworks.com/help/images/convert-image-data-between-classes.
html
• Display Separated Color Channels of RGB Image
www.mathworks.com/help/images/display-separated-color-channels-
of-rgb-image.html
• Image Types in the Toolbox
www.mathworks.com/help/images/image-types-in-the-toolbox.html

Notes
1 The recipe has been prepared using MATLAB’s built-in images.
2 See Recipe 1 for additional details.
3
Recipe 3: Image
Copyright Materialacquisition using
- Provided by Taylor a
& Francis
webcamReview Copy - Not for Redistribution

This recipe teaches you different ways to acquire images from a computer’s
built-in webcam or from a webcam attached to the USB (Universal Serial Bus)
port.

USB Video Class (UVC) devices, such as integrated or external USB web-
cams, can serve as valuable tools for image processing tasks. These webcams,
commonly found in most computer setups, offer sufficient quality for a wide
array of applications and are conveniently accessible via MATLAB. Even
when your project demands the use of more advanced cameras, possibly
even requiring specific optical or lighting configurations, leveraging readily
available resources to conduct quick and simple experiments can be bene-
ficial. This approach not only enables you to preview the potential image
processing strategies for a particular application but also to gain insights into
the ideal lighting setup or the desirable characteristics of an alternative image
capturing device.

You will need (Ingredients)


• MATLAB R2019b or later
• MATLAB Image Processing Toolbox (IPT) version R2019b or later
• A built-in webcam or a webcam attached to a USB port
• (OPTIONAL) MATLAB Image Acquisition Toolbox (IAT) version
R2019b or later

Steps (Preparation)
1. Install the Hardware Support Package USB webcams.
2. Get a list of the available webcams in the system.

14 DOI: 10.1201/9781003170198-4
Recipe 3: Image acquisition using a webcam 15

3. Preview, acquire, store in the workspace, and display images from the
desired webcam.
4. Connect, get, and set parameters of the desired webcam.
Copyright Material - Provided by Taylor & Francis
The Hardware Support
Review Package
Copy for for
- Not USBRedistribution
webcams allows MATLAB to com-
municate with the webcams in your system. Starting from MATLAB 2014a,
several Support Packages are not included in MATLAB installation but are
available for download and installation through the Add-Ons menu of the
desktop instead. To download and install the Hardware Support Package for
USB webcams, proceed as follows:

1. Click on HOME tab → Add-Ons → Get Hardware Support Packages.


2. In the Hardware Support Packages window, search for webcam. The top
result should be MATLAB Support Package for USB Webcams.
3. (If the support package isn’t installed yet) Click on the Install button and
follow the instructions until the installation is finished.

Examples
Now you should be able to interface with the available webcams and acquire
images, using the code in Listing 3.11 .

LISTING 3.1
Webcam – basic setup.
1 % Webcam - basic setup
2 myWcams = webcamlist
3 wcam1 = webcam(1)
4 % wcam2 = webcam(2)
5 img_1 = snapshot(wcam1);
6 % img_2 = snapshot(wcam2);
7 image(img_1)
8 % figure, image(img_2)
9 clear wcam1
10 % clear wcam2

The webcamlist function lists the system’s webcams that MATLAB can
access:

myWcams =
’Integrated Webcam’
’Logitech HD Webcam C270’

The webcam function creates webcam objects, into variables wcam1 and
wcam2. In the workspace, note that their class is webcam. These are the most
important variables in our code since they represent the connection between
16 Image processing recipes in MATLAB®

MATLAB and webcams. The wcam1 and wcam2 variables contain properties
and their respective values for each webcam. The listed properties and values
are device-specific, for example:
Copyright Material - Provided by Taylor & Francis
wcam1 =
Review
webcam with Copy
properties: - Not for Redistribution
Name: ’Integrated Webcam’
Resolution: ’640x480’
AvailableResolutions: {’640x480’ ’160x120’ ’320x180’
’320x240’ ’424x240’ ’640x360’}
Hue: 0
Exposure: -5
Contrast: 0
Saturation: 64
WhiteBalanceMode: ’auto’
Brightness: 0
Sharpness: 2
Gamma: 100
WhiteBalance: 4600
BacklightCompensation: 1
ExposureMode: ’auto’

wcam2 =
webcam with properties:

Name: ’Logitech HD Webcam C270’


Resolution: ’640x480’
AvailableResolutions: {1x19 cell}
Exposure: -4
Contrast: 32
Saturation: 32
Brightness: 128
Gain: 192
Sharpness: 24
WhiteBalance: 5690
BacklightCompensation: 0
ExposureMode: ’auto’

This confirms that the communication between MATLAB and the web-
cams is operational. MATLAB has been granted access to the live video from
the webcam, i.e., a sequence of frames captured by the webcam at a specific,
device-dependent timing between the frames, expressed in frames per second
(FPS). The acquisition of a single image means we are selecting one of those
frames as our still image. This is obtained with snapshot, which triggers
storing a single frame in the workspace. The snapshot function receives
a webcam object as an input parameter and outputs an image. It is a good
practice to clear the webcam object variables once the connection is no longer
needed to make the associated webcam available for further connections.
Recipe 3: Image acquisition using a webcam 17

In Listing 3.2 (which uses Integrated Webcam), we improve the usabil-


ity of Listing 3.1. The preview function opens the MATLAB Video Preview
window, which shows the live video provided by the webcam. The pause
function stops theMaterial
Copyright script execution and waits
- Provided for the &
by Taylor user to press any key.
Francis
After a key isReview
pressed, Copy
the snapshot function is executed,
- Not for Redistribution triggering a single
image acquisition and making it available in the workspace.
LISTING 3.2
Webcam – improved setup.
1 % Webcam - improved setup
2 wcam = webcam(1);
3 preview(wcam)
4 disp(’Press any key to trigger acquisiton’)
5 pause
6 img_1 = snapshot(wcam);
7 imshow(img_1)
8 clear wcam

Listing 3.3 shows how to acquire an image sequence by placing the snap-
shot function in a loop. In this case, four RGB true color (24 bits per pixel)
images are stored in a four-dimensional (4D) array, where the fourth di-
mension is used to address each image2 . See Figure 3.1 for examples of
results.
The code in Listing 3.3 can be useful in applications that require the anal-
ysis of a sequence of images from a given dynamic event. This strategy can
also be used to allow further selection of the best image among the 𝑛 available
candidates or in situations in which it is difficult to trigger the acquisition of
the single desired image manually.
LISTING 3.3
Webcam – image sequence acquisition.
1 % Webcam - image sequence acquisition
2 wcam = webcam(1);
3 preview(wcam)
4 disp(’Press any key to trigger acquisiton’)
5 pause
6 img_array = [];
7 for n = 1:4
8 img_array = cat(4,img_array,snapshot(wcam));
9 end
10 montage(img_array), title(’Four acquired images’)
11 img_3 = img_array(:,:,:,3);
12 figure, imshow(img_3), title(’The third acquired image’)
13 clear wcam
18 Image processing recipes in MATLAB®

Copyright Material - Provided by Taylor & Francis


Review Copy - Not for Redistribution

FIGURE 3.1
Examples of using the snapshot function in a loop to acquire a sequence of images. Part (a)
shows the four images obtained inside the loop; it is possible to observe, from the sequence of
images (left to right, top to bottom), that the disks are sliding down the monkey’s tail. Part (b)
shows a specific image (lines 11–12 in Listing 3.3).

Discussion (Recipe notes)


In this recipe, you learned how to acquire images using a webcam and the
basic functionality provided by MATLAB to identify, connect, and control the
webcams available in your system.
The Image Acquisition Toolbox (IAT) extends MATLAB’s image acqui-
sition functionalities, including interfacing with several industry-standard
devices, such as optical, depth, infrared, and 3D cameras; frame grabbers;
X-ray detectors; and 3D laser line profile scanners.

Learn more about it


Useful MATLAB functions
Type the function name in the search field at www.mathworks.com/help/
matlab/
· closePreview · preview · snapshot · webcam · webcamlist ·

MATLAB documentation, demos, and examples


• Acquiring a Single Image in a Loop Using getsnapshot
https://www.mathworks.com/help/imaq/acquire- single- image- in-
loop-using-getsnapshot.html
Recipe 3: Image acquisition using a webcam 19

• Configuring Callback Properties


www.mathworks.com/help/imaq/configuring-callback-properties.html
• Determining the Rate of Acquisition
Copyright Material - Provided by Taylor & Francis
www.mathworks.com/help/imaq/determining-the-rate-of-acquisition.
html Review Copy - Not for Redistribution
• Event Types (Supported by the Image Acquisition Toolbox)
www.mathworks.com/help/imaq/event-types.html
• Image Acquisition Toolbox Properties
www.mathworks.com/help/imaq/image-acquisition-toolbox-properties.
html
• Logging Data at Constant Intervals
www.mathworks.com/help/imaq/logging-data-at-constant-intervals.
html
• MATLAB Support Package for USB Webcams
www.mathworks.com/help/supportpkg/usbwebcams/index.html
• Retrieving Event Information
www.mathworks.com/help/imaq/retrieving-event-information.html
• Set Properties for Webcam Acquisition
www.mathworks.com/help/supportpkg/usbwebcams/ug/set-properties-
for-webcam-acquisition.html

Notes
1 If your computer has two webcams, such as the one used in this example,
uncomment lines 4, 6, 8, and 10.
2 Other approaches could be used, such as storing each image in a cell of a cell array
or in a field of an array of structures. However, in this example, the option for the
4D array was chosen in anticipation of using the montage function, which expects
this type of data structure.
4
Recipe 4: Browsing
Copyright through
Material - Provided images
by Taylor & Francis
Review Copy - Not for Redistribution

This recipe teaches you how to browse through images in a folder and sub-
folders, preview images, and automatically get information about the image
files to access them.

In Recipe 1, we briefly introduced the Image Browser App. We indicated how


it can be helpful to visualize images and obtain additional information about
them, such as file name, image size, and data class. This recipe will expand
our discussion and introduce the ImageDatastore object.
We will use the Image Browser App to explore Caltech 101, a well-known
image dataset created in September 2003 to advance research in object recog-
nition that consists of a total of 9144 color images with a resolution of 300×200
pixels, organized into 101 object categories, such as animals, vehicles, and
people.

You will need (Ingredients)


• MATLAB R2016b or later
• MATLAB Image Processing Toolbox (IPT) version R2016b or later
• Caltech 101 image dataset

Steps (Preparation)
1. Download and extract the Caltech 101 image dataset.
2. Open the Image Browser App, and use it to browse and preview the
downloaded image dataset.
3. Export an image and an ImageDatastore object from the Image
Browser to the workspace.
4. Create an ImageDatastore object using the imageDatastore
function.

20 DOI: 10.1201/9781003170198-5
Recipe 4: Browsing through images 21

Copyright Material - Provided by Taylor & Francis


Review Copy - Not for Redistribution

FIGURE 4.1
Image Browser: loading images. Three options are available under Load. The option Load folder,
include subfolders was selected.

To get started, go to the Download section of the Caltech 101 image dataset
website1 , download the caltech-101.zip file and extract it, locate the
101_ObjectCategories.tar.gz file, and extract it at a path of your pref-
erence. The required disk space is ≈150 MB. In MATLAB, open the Image
Browser App, either by selecting it from the APPS tab at the MATLAB desktop
or by typing imageBrowser in the Command Window.
Figure 4.1 shows the GUI and the available options when you click the Load
button:

• Load folder of images: Image Browser loads the images of a single folder.
It does not include subfolders.
• Load folder, include subfolders: Image Browser loads the images of the
selected folder and its subfolders.
• Load image datastore from workspace: Image Browser loads a ImageData-
store object which contains, among other information, images’ file
names and locations.

Select the Load folder, include subfolders option, and browse your computer
using the Select a folder with images window to indicate the Caltech 101 “root
folder”, 101_ObjectCategories, as depicted in Figure 4.22 . Finish by
clicking on the Select Folder button. All 9144 images can now be browsed
using the Image Browser.
You can visualize the images’ thumbnails (Figure 4.3(a)) and adjust their
sizes using the Thumbnail Size slider (Figure 4.3(b)).
You can inspect the image by double-clicking on it or clicking on Preview
(see Figure 4.4).
With a right-click on the image in the browsing or preview areas, it is pos-
sible to select the Export image to workspace option, and a dialog box allows
you to type the variable name. The selected image becomes available in the
workspace after clicking OK at the dialog box.
22 Image processing recipes in MATLAB®

Copyright Material - Provided by Taylor & Francis


Review Copy - Not for Redistribution

FIGURE 4.2
Image Browser: after selecting the Load folder, include subfolders option, a window to browse the
computer and select the desired folder is provided.

FIGURE 4.3
Image Browser: thumbnails. After selecting a folder, it is possible to browse through images (a)
and adjust thumbnails’ sizes at the Thumbnail Size slider (b).

The Image Browser App also provides the Export All option that gener-
ates a MATLAB object of the ImageDatastore class. Clicking on Export
All, a dialog box allows you to type the name of the variable of the class
ImageDatastore to be exported to the workspace. In this case, the name
x101_ObjectCategories is suggested. Click on the OK button to finish.
Recipe 4: Browsing through images 23

Copyright Material - Provided by Taylor & Francis


Review Copy - Not for Redistribution

FIGURE 4.4
Image Browser: preview. The selected image and its name, size, and class are presented in the
Preview area.

Typing x101_ObjectCategories in the Command Window to get the


contents of the variable, we have:

x101_ObjectCategories =
ImageDatastore with properties:

Files: {
’C:\101_ObjectCategories\BACKGROUND_Google\image_0001.jpg’;
’C:\101_ObjectCategories\BACKGROUND_Google\image_0002.jpg’;
’C:\101_ObjectCategories\BACKGROUND_Google\image_0003.jpg’
... and 9141 more
}
ReadSize: 1
Labels: {}
ReadFcn: @iptui.internal.imageBrowser.readAllIPTFormats

The field Files is an array of cells that provides the location and file name
(complete path) of each of the 9144 images of the dataset since we loaded
the entire dataset into the Image Browser. To generate an ImageDatastore
object of a single object category, for instance, water lily flower, follow the steps
in Figure 4.1, but now load into Image Browser the subfolder water_lilly,
and proceed with a new Export All. Accepting the suggested variable name of
the dialog box, a new ImageDatastore is exported to the workspace, with
the name water_lilly. Typing water_lilly in the Command Window
to get the content of the variable, you should see the complete paths of the
37 images:
24 Image processing recipes in MATLAB®

water_lilly =
ImageDatastore with properties:

Files: {
Copyright ’C:\101_ObjectCategories\water_lilly\image_0001.jpg’;
Material - Provided by Taylor & Francis
’C:\101_ObjectCategories\water_lilly\image_0002.jpg’;
Review Copy - Not for Redistribution
’C:\101_ObjectCategories\water_lilly\image_0003.jpg’
... and 34 more
}
ReadSize: 1
Labels: {}
ReadFcn: @iptui.internal.imageBrowser.readAllIPTFormats
An image can now be read, loaded into the workspace, and displayed with
the following commands:

filename = water_lilly.Files{30};
img_1 = imread(filename);
figure, imshow(img_1)

The same operation can be performed using the readimage function,


which accepts an ImageDatastore object and a number indicating the
index of the complete path in the Files field:

[img_2,fileinfo] = readimage(water_lilly,30);
figure, imshow(img_2)

Listing 4.1 shows how to create ImageDatastore objects programmati-


cally using the imageDatastore function. In this case, we get information
about the entire dataset and select only those from the water_lilly sub-
folder using the Labels field, whose values were filled automatically, pass-
ing the name-value pair arguments ’LabelSource’,’foldernames’ to the
imageDatastore function. This strategy helps to improve the organization
of the image datastore and makes it easier to access images of a given label.

LISTING 4.1
ImageDatastore creation and processing.
1 % ImageDatastore creation and processing
2 imds_path = ’101_ObjectCategories’;
3 imds = imageDatastore(imds_path,’IncludeSubfolders’,true,... ’LabelSource’,’foldernames’);
4 idx = find(imds.Labels == ’water_lilly’);
5 WLpaths = imds.Files(idx);
6 filename = WLpaths{30};
7 img_3 = imread(filename);
8 figure, imshow(img_3)
Recipe 4: Browsing through images 25

The automatically created labels can now be inspected as follows:

autoLabels = imds.Labels;

Copyright Material - Provided by Taylor & Francis


The autoLabels array is of class categorical3 . To get a list of the labels,
Review Copy - Not for Redistribution
we can use the unique function:

uLabels = unique(imds.Labels)

The ImageDatastore object is a valuable and versatile resource, par-


ticularly when dealing with many image files. In this recipe, we used the
readimage function provided by the ImageDatastore object, but there
are other functions that you might want to explore. For example, coun-
tEachLabel provides the number of files per label in a variable of the class
table:

tableLabels = countEachLabel(imds)

tableLabels =
102x2 table
Label Count
_________________ ______
BACKGROUND_Google 467
Faces 435
Faces_easy 435
Leopards 200
Motorbikes 798
accordion 55
airplanes 800
anchor 42
... ...
windsor_chair 56
wrench 39
yin_yang 60

Discussion (Recipe notes)


In this recipe, you learned how to use the Image Browser App to browse
and preview images from a folder and its subfolders. You also learned
how to create an object of the ImageDatastore class in two different
ways: (i) using the Export All option of the Image Browser; and (ii) using the
imageDatastore function.
We encourage you to explore further the Image Browser App and the
ImageDatastore class using some of the resources listed next.
26 Image processing recipes in MATLAB®

Learn more about it


UsefulCopyright Material
MATLAB functions - Provided by Taylor & Francis
Review
Type the function Copy
name in the- search
Not for Redistribution
field at www.mathworks.com/help/
matlab/
· countEachLabel · imageDatastore · readimage ·

MATLAB documentation, demos, and examples


• Class (data type) categorical
www.mathworks.com/help/matlab/ref/categorical.html
• Class (data type) table
www.mathworks.com/help/matlab/ref/table.html
• Image Browser
www.mathworks.com/help/images/ref/imagebrowser-app.html
• Image file formats supported by MATLAB
www.mathworks.com/help/matlab/ref/imformats.html
• View and Edit Collection of Images in Folder or Datastore
www.mathworks.com/help/images/view- thumbnails- of- images- in-
folder-or-datastore.html

Notes
1 https://data.caltech.edu/records/mzrjq-6wc02
2 Note that each subfolder of the image dataset is named with the category of the
object. Thus, we should expect 101 subfolders within 101_ObjectCategories,
but there are 102, as we will see later. The BACKGROUND_Google subfolder
was created by the authors for evaluation of the proposed object recognition
algorithm [17], and it is not considered an object category.
3 For more information about the categorical data type, check the Learn more
about it section at the end of this recipe.
Copyright Material - Provided by Taylor & Francis
Review Copy - Not for Redistribution

Part II

Geometric operations

Part II – Geometric operations


This Part contains recipes that focus on image processing techniques that
modify the geometric properties of an image, i.e., the spatial relationships
between groups of pixels.
Recipe 5 shows how to perform basic geometric operations on images, such
as cropping, flipping, resizing, and rotating.
Recipe 6 teaches how to create affine transformations specifying arbitrary
geometric operations and apply them to images.

DOI: 10.1201/9781003170198-6 27
Copyright Material - Provided by Taylor & Francis
Review Copy - Not for Redistribution
5
Recipe 5: Geometric
Copyright transformations
Material - Provided by Taylor & Francis
Review Copy - Not for Redistribution

This recipe teaches you how to perform basic geometric operations on


images.

Geometric operations modify the geometry of an image by repositioning pix-


els in a constrained way. In other words, rather than changing the pixel values
of an image (as most techniques presented in the rest of this book do), they
modify the spatial relationships between groups of pixels.

You will need (Ingredients)


• MATLAB R2014a or later
• MATLAB Image Processing Toolbox (IPT) version R2014a or later
• (OPTIONAL1 ) One or more of your images

Steps (Preparation)
1. Select and open the input image.
2. Apply desired geometric transformation to the image using a MAT-
LAB function from Table 5.1.
3. (OPTIONAL) Display original and transformed images.
4. (OPTIONAL) Save the transformed image in any file format sup-
ported by MATLAB.

DOI: 10.1201/9781003170198-7 29
30 Image processing recipes in MATLAB®

TABLE 5.1
Selected geometric operations available
in MATLAB.
Copyright Material
MATLAB-function
ProvidedOperation
by Taylor & Francis
Review Copy - Not for Redistribution
imresize resizing/scaling
imrotate rotation
imtranslate translation
imcrop cropping

Examples
Part 1: Image resizing
Listing 5.1 shows an example of image resizing. In this particular case, the
original image has been resized to 80% of its original size, from 972×1296×3
to 778×1037×3. The results are shown in Figure 5.1.

LISTING 5.1
Image resizing.
1 % Image resizing
2 img_1 = imread(’flamingos.jpg’);
3 img_2 = imresize(img_1, 0.8, ”Method”, ”bicubic”, ”Antialiasing”,true);
4 imshowpair(img_1, img_2, ’montage’)
5 sz_img_1 = size(img_1);
6 sz_img_2 = size(img_2);

A common mistake that you should try to avoid when resizing an image
is to specify a desired target size – in pixels – whose aspect ratio is different

FIGURE 5.1
Image resizing example. Original image: courtesy of The MathWorks.
Recipe 5: Geometric transformations 31

Copyright Material - Provided by Taylor & Francis


Review Copy - Not for Redistribution

FIGURE 5.2
Image resizing with incorrect specification of target image size. Original image: courtesy of The
MathWorks.

FIGURE 5.3
Image resizing with correct specification of target image size. Original image: courtesy of The
MathWorks.

from the original image. See Listing 5.2 and Figure 5.2 for an example of what
not to do.

LISTING 5.2
Image resizing with wrong aspect ratio.

1 % Image resizing (wrong aspect ratio!)


2 img_1 = imread(’flamingos.jpg’);
3 img_2 = imresize(img_1, [1040 480]);
4 imshowpair(img_1, img_2, ’montage’)
5 sz_img_1 = size(img_1);
6 sz_img_2 = size(img_2);

Fortunately, MATLAB provides an elegant solution to resize an image


specifying either the number of rows or columns of the desired target size
and leaving the other dimension as NaN, which preserves the aspect ratio of
the original image. See Listing 5.3 and Figure 5.3.
32 Image processing recipes in MATLAB®

Copyright Material - Provided by Taylor & Francis


Review Copy - Not for Redistribution

FIGURE 5.4
Image rotation example. Original image: courtesy of The MathWorks.

LISTING 5.3
Image resizing while keeping the aspect ratio.
1 % Image resizing (preserving the correct aspect ratio)
2 img_1 = imread(’flamingos.jpg’);
3 img_2 = imresize(img_1, [NaN 480]); % We want 480 columns
4 imshowpair(img_1, img_2, ’montage’)
5 sz_img_1 = size(img_1); % 972-by-1296 pixels
6 sz_img_2 = size(img_2); % 360-by-480 pixels

Part 2: Image rotation


Listing 5.4 shows an example of image rotation. In this particular case, the
image is rotated counterclockwise by 35∘ . The results are shown in Figure 5.4.

LISTING 5.4
Image rotation.
1 % Image rotation
2 img_1 = imread(’car1.jpg’);
3 img_2 = imrotate(img_1, 35, ”bicubic”);
4 imshowpair(img_1, img_2, ’montage’)
5 sz_img_1 = size(img_1);
6 sz_img_2 = size(img_2);

Note that when you rotate an image by an angle that is not a multiple of 90∘ ,
the resulting image will usually change size2 to accommodate all the original
pixels; the remaining pixels in the output image will be set to 0 by default. If
you want the output image to be the same size as the input image (with the
associated loss of contents), you can use the ’crop’ option. See Listing 5.5
and Figure 5.5.
Recipe 5: Geometric transformations 33

Copyright Material - Provided by Taylor & Francis


Review Copy - Not for Redistribution

FIGURE 5.5
Image rotation with cropped output. Original image: courtesy of The MathWorks.

LISTING 5.5
Image rotation with cropped output.
1 % Image rotation (with cropped output)
2 img_1 = imread(’car1.jpg’);
3 img_2 = imrotate(img_1, 35, ”bicubic”,”crop”);
4 imshowpair(img_1, img_2, ’montage’)
5 sz_img_1 = size(img_1);
6 sz_img_2 = size(img_2);

Part 3: Image translation


Listing 5.6 shows an example of image translation by 750 pixels down and 400
pixels to the right of the original coordinates. Note that when you translate
an image, the resulting image will change size3 to accommodate all the orig-
inal pixels; the remaining pixels in the output image will be set to the value
specified under the ’FillValues’ option, which in this example was set to
255 (white). The results are shown in Figure 5.6.

LISTING 5.6
Image translation.
1 % Image translation
2 img_1 = imread(’car2.jpg’);
3 img_2 = imtranslate(img_1,[400, 750],’FillValues’,255,’OutputView’,’full’);
4 figure, imshow(img_1), title(’Original Image’), ...
5 set(gca,’Visible’,’on’);
6 figure, imshow(img_2), title(’Full Translated Image’), ...
7 set(gca,’Visible’,’on’);
8 sz_img_1 = size(img_1);
9 sz_img_2 = size(img_2);

If you want the output image to be of the same size as the input image
(with the associated loss of contents), you can set the ’OutputView’ option
to ’Same’. See Listing 5.7 and Figure 5.7.
34 Image processing recipes in MATLAB®

Original Image Full Translated Image

500
500

Copyright Material - Provided by


1000 Taylor & Francis

1000
Review Copy - Not for Redistribution
1500

1500 2000

2500
2000

3000
500 1000 1500 2000 2500 3000 3500 500 1000 1500 2000 2500 3000 3500

(a) (b)

FIGURE 5.6
Image translation example. Original image: courtesy of The MathWorks.

Original Image Full Translated Image

500 500

1000 1000

1500 1500

2000 2000

500 1000 1500 2000 2500 3000 3500 500 1000 1500 2000 2500 3000 3500

(a) (b)

FIGURE 5.7
Image translation with cropped output. Original image: courtesy of The MathWorks.

LISTING 5.7
Image translation with cropped output.

1 % Image translation (with cropped output)


2 img_1 = imread(’car2.jpg’);
3 img_2 = imtranslate(img_1,[400, 750],’FillValues’,255,’OutputView’,’same’);
4 figure, imshow(img_1), title(’Original Image’), ...
5 set(gca,’Visible’,’on’);
6 figure, imshow(img_2), title(’Full Translated Image’), ...
7 set(gca,’Visible’,’on’);
8 sz_img_1 = size(img_1);
9 sz_img_2 = size(img_2);
Recipe 5: Geometric transformations 35

Copyright Material - Provided by Taylor & Francis


Review Copy - Not for Redistribution

FIGURE 5.8
Image cropping example. Original image: courtesy of The MathWorks.

Part 4: Image cropping


Listing 5.8 shows an example of image cropping, where the input image is
cropped according to the position and dimensions specified in the crop rect-
angle ’rect’. The results are shown in Figure 5.8. The size of the original
image is 391×518×3 whereas the size of the cropped image is 181×101×3.

LISTING 5.8
Image cropping (programmatic).
1 % Image cropping (programmatic)
2 img_1 = imread(’coloredChips.png’);
3 sz_img_1 = size(img_1);
4 rect = [10 200 100 180];
5 img_2 = imcrop(img_1, rect);
6 imshowpair(img_1, img_2, ’montage’)
7 sz_img_2 = size(img_2);

Image cropping is often done interactively. In the example below


(Figure 5.9 and Listing 5.9), we selected a small area containing the red cir-
cle closest to the top-left corner of the original image, resulting in a rectangle
’rect’ whose values are: [82.5100 5.5100 90.9800 76.9800]. Note
that between lines 4 and 5, MATLAB enters the interactive mode where
the program execution is temporarily stopped, and the user is expected to
use the mouse to define the cropping rectangle. Once a region has been
selected, the remaining lines in the script are then executed. The size of
the original image is 391×518×3 whereas the size of the cropped image is
77×91×3.
36 Image processing recipes in MATLAB®

Copyright Material - Provided by Taylor & Francis


Review Copy - Not for Redistribution

FIGURE 5.9
Image cropping example: (a) input image; (b) interactive selection of desired cropped area;
(c) resulting (cropped) image. Original image: courtesy of The MathWorks.

LISTING 5.9
Image cropping (interactive).
1 % Image cropping (interactive)
2 img_1 = imread(’coloredChips.png’);
3 sz_img_1 = size(img_1);
4 [img_2, rect] = imcrop(img_1); %interactive cropping
5 imwrite(img_2,’cropped_image.png’);
6 imshowpair(img_1, img_2, ’montage’)
7 sz_img_2 = size(img_2);

Discussion (Recipe notes)


In this recipe, you learned how to apply fundamental geometric transforma-
tions to images using MATLAB built-in functions (Table 5.1).
The syntax used for each example illustrates one of many possible uses of
each associated library function. You are encouraged to expand upon each
example in this recipe, particularly:

• For imresize: try other magnification factors, interpolation methods,


and options.
• For imrotate: try other rotation angles, interpolation methods, and
options.
• For imtranslate: try other fill values, interpolation methods, and
options.
• For imcrop: try other image types (including indexed color images)
and options.
Recipe 5: Geometric transformations 37

In Recipe 6, we will expand upon this topic and teach you how to perform
image warping and other geometric operations.

Copyright Material - Provided by Taylor & Francis


Review Copy - Not for Redistribution
Learn more about it
Useful MATLAB functions
Type the function name in the search field at www.mathworks.com/help/
matlab/
· imcrop · imresize · imrotate · imtranslate ·

MATLAB documentation, demos, and examples


• Common Geometric Transformations
www.mathworks.com/help/images/geometric-transformations.html
• Crop an Image
www.mathworks.com/help/images/crop-an-image.html
• Resize an Image
www.mathworks.com/help/images/resize-an-image.html
• Resize Image and Preserve Aspect Ratio
www.mathworks.com/help/images/resize-image-and-preserve-aspect-
ratio.html
• Rotate an Image
www.mathworks.com/help/images/rotate-an-image.html
• Translate an Image Using imtranslate Function
www.mathworks.com/help/images/translate-an-image.html

Notes
1 The recipe has been prepared using MATLAB’s built-in images.
2 In this particular case, the image size will change from 2336×3504×3 to
3924×4211×3.
3 In this particular case, the image size will change from 2336×3504×3 to
3086×3904×3.
6
Recipe 6: Image
Copyright Materialwarping
- Provided by Taylor & Francis
Review Copy - Not for Redistribution

This recipe teaches you how to create affine transformations specifying


arbitrary geometric operations and apply them to images.

In Recipe 5, you learned how to perform some of the most frequently used
geometric transformations to 2D images using MATLAB’s built-in functions
for image resizing, cropping, rotation, and translation. In this recipe, you’ll
learn how to apply any 2D affine geometric transformation using MATLAB.
To better understand what affine transformations are, here is a summary of
the theory.
A geometric operation can be described mathematically as the process of
transforming an input image 𝑓(𝑥, 𝑦) into a new image 𝑔(𝑥′ , 𝑦′ ) by modifying
the coordinates of image pixels:

𝑓(𝑥, 𝑦) → 𝑔(𝑥′ , 𝑦′ ) (6.1)

i.e., the pixel value originally located at coordinates (𝑥, 𝑦) will be relocated to
coordinates (𝑥′ , 𝑦′ ) in the output image.
To model this process, a mapping function is needed. The mapping function
specifies the new coordinates (in the output image) for each pixel in the input
image:
(𝑥′ , 𝑦′ ) = 𝑇 (𝑥, 𝑦) (6.2)
This mapping function is an arbitrary 2D function. It is often specified as
two separate functions, one for each dimension:

𝑥′ = 𝑇𝑥 (𝑥, 𝑦) (6.3)
and
𝑦′ = 𝑇𝑦 (𝑥, 𝑦). (6.4)
where 𝑇𝑥 and 𝑇𝑦 are usually expressed as polynomials in 𝑥 and 𝑦. The
case where 𝑇𝑥 and 𝑇𝑦 are linear combinations of 𝑥 and 𝑦 is called affine
transformation (or affine mapping):

38 DOI: 10.1201/9781003170198-8
Recipe 6: Image warping 39

TABLE 6.1
Summary of transformation coefficients for selected affine transformations.
Transformation a0 a1 a2 b0 b1 b2
Copyright Material - Provided
Translation by Δ𝑥 , Δ𝑦 1
by0 Taylor
Δ𝑥
& Francis
0 1 Δ𝑦
Review
Scaling by a factor [𝑠𝑥 , 𝑠𝑦 ] Copy - Not𝑠𝑥for Redistribution
0 0 0 𝑠𝑦 0
Counterclockwise rotation by angle 𝜃 cos 𝜃 sin 𝜃 0 − sin 𝜃 cos 𝜃 0
Shear by a factor [𝑠ℎ𝑥 , 𝑠ℎ𝑦 ] 1 𝑠ℎ𝑥 0 𝑠ℎ𝑦 1 0

𝑥′ = 𝑎 0 𝑥 + 𝑎 1 𝑦 + 𝑎 2 , (6.5)

𝑦 ′ = 𝑏 0 𝑥 + 𝑏1 𝑦 + 𝑏2 . (6.6)
Equations 6.5 and 6.6 can also be expressed in matrix form as:

𝑥′ 𝑎0 𝑎1 𝑎2 𝑥
⎡ 𝑦′ ⎤ = ⎡ 𝑏 𝑏1 𝑏2 ⎤ ⎡ ⎤
⎢ ⎥ ⎢ 0 ⎥⎢ 𝑦 ⎥ (6.7)
⎣ 1 ⎦ ⎣ 0 0 1 ⎦⎣ 1 ⎦
Affine mapping transforms straight lines to straight lines, triangles to tri-
angles, and rectangles to parallelograms. Parallel lines remain parallel, and
the distance ratio between points on a straight line does not change. Four of
the most common geometric operations – translation, scaling (resizing), ro-
tation, and shearing – are all special cases of Equation 6.7, as summarized in
Table 6.1.
In MATLAB, you will use affinetform2d to store information about
a 2D affine geometric transformation and imwarp to apply the selected
transformation to an image.

You will need (Ingredients)


• MATLAB R2022b or later
• MATLAB Image Processing Toolbox (IPT) version R2022b or later
• (OPTIONAL1 ) One or more of your images

Steps (Preparation)
1. Select and open the input image.
2. Specify desired 2D affine transformation using affinetform2d.
3. Apply desired geometric transformation to the image using imwarp.
40 Image processing recipes in MATLAB®

4. (OPTIONAL) Display original and transformed images.


5. (OPTIONAL) Save the transformed image in any file format sup-
ported by MATLAB.
Copyright Material - Provided by Taylor & Francis
Review Copy - Not for Redistribution

Examples
Part 1: Image resizing
Listing 6.1 shows an example of image resizing (to 80% of its original size,
from 972×1296×3 to 778×1037×3). The results are shown in Figure 6.1 and
are identical to the ones in Figure 5.1 (Recipe 5).

LISTING 6.1
Image scaling using 2D affine transformation.
1 % Image scaling using 2D affine transformation
2 img_1 = imread(’flamingos.jpg’);
3 sz_img_1 = size(img_1);
4 affine_transform_1 = affinetform2d([.8 0 0; 0 .8 0; 0 0 1]);
5 img_2 = imwarp(img_1,affine_transform_1);
6 imshowpair(img_1, img_2, ’montage’)
7 sz_img_2 = size(img_2);

Part 2: Image rotation


Listing 6.2 shows an example of image rotation (counterclockwise, by 35∘ ).
The results are shown in Figure 6.2 and are identical to the ones in Figure 5.4
(Recipe 5).

FIGURE 6.1
Image resizing example. Original image: courtesy of The MathWorks.
Recipe 6: Image warping 41

Copyright Material - Provided by Taylor & Francis


Review Copy - Not for Redistribution

FIGURE 6.2
Image rotation example. Original image: courtesy of The MathWorks.

LISTING 6.2
Image rotation using 2D affine transformation.
1 % Image rotation using 2D affine transformation
2 img_1 = imread(’car1.jpg’);
3 sz_img_1 = size(img_1);
4 rotation_angle = 35; % in degrees
5 sin_ra = sind(rotation_angle);
6 cos_ra = cosd(rotation_angle);
7 affine_transform_2 = affinetform2d([cos_ra sin_ra 0; -sin_ra cos_ra 0; 0 0 1]);
8 img_2 = imwarp(img_1,affine_transform_2);
9 imshowpair(img_1, img_2, ’montage’)
10 sz_img_2 = size(img_2);

Part 3: Image shearing


The greater flexibility of using 2D affine transforms with arbitrary coefficients
allows the use of geometric transformations (distortions) that could not be
implemented (with simpler syntax and fewer steps) using the built-in func-
tions from Recipe 5. One example of such distortions is the shearing operation,
which shifts one part of an image in a specific direction and the other in the
opposite direction.
Listing 6.3 shows an example of image shearing. The results are shown in
Figure 6.3. Note that the resulting image will change size (in this case, from
2336×3504×3 to 2336×4672×3) to accommodate all the original pixels; the
remaining pixels in the output image will be set to 0 (black).
42 Image processing recipes in MATLAB®

Copyright Material - Provided by Taylor & Francis


Review Copy - Not for Redistribution

FIGURE 6.3
Image shearing example. Original image: courtesy of The MathWorks.

LISTING 6.3
Image shearing using 2D affine transformation.
1 % Image shearing using 2D affine transformation
2 img_1 = imread(’car2.jpg’);
3 % Define 2D affine transform
4 coefficients = [1 0.5 0; 0 1 0; 0 0 1];
5 affine_transform_3 = affinetform2d(coefficients);
6 % Apply transform
7 img_2 = imwarp(img_1,affine_transform_3);
8 % View results
9 imshowpair(img_1, img_2, ’montage’)
10 sz_img_1 = size(img_1);
11 sz_img_2 = size(img_2);

Part 4: Combining multiple distortions


Affine transformations can be encapsulated using the randomAffine2d,
which can be helpful in the context of image data augmentation for deep
learning solutions in computer vision.
Listing 6.4 shows an example of using randomAffine2d with fixed val-
ues for Scale, XShear, YShear, and Rotation. The results are shown in
Figure 6.4. As expected, the resulting image is larger, sheared, and rotated.
In case you’re curious, behind the scenes randomAffine2d created a 3×3
matrix (see Equation 6.7) whose coefficients are: 𝑎0 = 0.4823, 𝑏0 = -1.8000,
𝑎1 = 1.8000, and 𝑏1 = -0.3174. Since no translation is involved, both 𝑎2 and 𝑏2
are equal to zero.
LISTING 6.4
Combining 2D affine transformations.
1 % Combining several 2D affine transformations
2 img_1 = imread(’football.jpg’);
3 % Define 2D affine transform
4 affine_transform_4 = randomAffine2d(’Scale’,[1.8 1.8], ...
5 ’XShear’,[10 10], ’YShear’, [15 15], ...
6 ’Rotation’,[90 90]);
7 % Apply transform
8 img_2 = imwarp(img_1,affine_transform_4);
9 % View results
10 imshowpair(img_1, img_2, ’montage’)
11 sz_img_1 = size(img_1);
12 sz_img_2 = size(img_2);
Recipe 6: Image warping 43

Copyright Material - Provided by Taylor & Francis


Review Copy - Not for Redistribution

FIGURE 6.4
Combining multiple distortions in one pass. Original image: courtesy of The MathWorks.

Discussion (Recipe notes)


In this recipe, you learned how to apply geometric transformations to images
using MATLAB built-in functions affinetform2d and imwarp. You also
learned to combine multiple geometric transformations in one line of code
using randomaffine2d.
Admittedly, we have just scratched the surface of the vast area of designing
and applying geometric distortions to images. You are encouraged to modify
and expand the examples to your specific needs. See the links below for ideas.

Learn more about it


Useful MATLAB functions
Type the function name in the search field at www.mathworks.com/help/
matlab/
· affinetform2d · imwarp · randomaffine2d ·

MATLAB documentation, demos, and examples


• 2-D and 3-D Geometric Transformation Process Overview
www.mathworks.com/help/images/2-d-and-3-d-geometric-transforma
tion-process-overview.html
44 Image processing recipes in MATLAB®

• Create Composite 2-D Affine Transformations


www.mathworks.com/help/images/create-composite-2d-affine-transf
ormation.html
• Copyright Material
Create a Gallery - Provided
of Transformed by Taylor
Images & Francis
Review Copy - Not for Redistribution
www.mathworks.com/help/images/creating-a-gallery-of-transformed
-images.html
• Matrix Representation of Geometric Transformations
www.mathworks.com/help/images/matrix-representation-of-geometri
c-transformations.html
• Perform Simple 2-D Translation Transformation
www.mathworks.com/help/images/perform-a-2-d-translation-transf
ormation.html

Note
1 The recipe has been prepared using MATLAB’s built-in images.
Copyright Material - Provided by Taylor & Francis
Review Copy - Not for Redistribution

Part III

Histograms

Part III – Histograms


This Part contains recipes that focus on histogram-based image processing
techniques. Histograms are a convenient way to summarize the distribution
of an image’s intensity (or color) values. You will learn how to compute, dis-
play, interpret, modify, and explore the histograms of grayscale and color
images.
Recipe 7 shows how to compute and display histograms and statistics of
grayscale images.
Recipe 8 helps you expand upon that knowledge and perform contrast and
intensity adjustments on images using histogram-based techniques.
Recipe 9 shifts our attention from grayscale to color images and teaches
you how to compute and display the histograms of individual color channels
of color images.
Recipe 10 teaches you how to quantize the colors in an image, compute
and display the resulting histogram, and obtain the dominant colors of the
image.

DOI: 10.1201/9781003170198-9 45
Copyright Material - Provided by Taylor & Francis
Review Copy - Not for Redistribution
7
Recipe 7: Histograms
Copyright andbystatistics
Material - Provided of
Taylor & Francis
grayscale images
Review Copy - Not for Redistribution

This recipe teaches you how to compute and display histograms and statistics
of grayscale images.

You will need (Ingredients)


• MATLAB R2019b or later
• MATLAB Image Processing Toolbox (IPT) version R2019b or later
• (OPTIONAL1 ) One or more of your own images

Steps (Preparation)
1. Select and open the input image.
2. Compute and display the corresponding histogram.
3. (OPTIONAL) Compute and display relevant summary statistics about
the image’s pixel contents.
4. (OPTIONAL) Save histogram plot in any file format supported by
MATLAB.

Part 1: Basic histogram display


Listing 7.1 uses the imhist function to compute and display the histogram
of an image (see results in Figure 7.1).
LISTING 7.1
Histogram of a grayscale image.
1 % Histograms - grayscale images
2 img_1 = imread(’circuit.tif’);
3 figure
4 subplot(2,1,1), imshow(img_1), title(’Image’)
5 subplot(2,1,2), imhist(img_1,256), title(’Histogram’)

DOI: 10.1201/9781003170198-10 47
48 Image processing recipes in MATLAB®

Image Histogram

1000

800
Copyright Material - Provided by Taylor & Francis
600
Review Copy - Not for Redistribution
400

200

0 50 100 150 200 250

FIGURE 7.1
An image and its histogram. Original image: courtesy of The MathWorks.

Part 2: Improved histogram display


The default options for displaying a histogram using imhist are convenient
for a quick display of the image’s histogram. A closer inspection, however,
reveals that there are at least four aspects over which you might want to have
greater control:

1. Use a different number of bins.


2. Adjust the height of the bars to prevent any bars whose height is
greater than the vertical dimension of the plot from being clipped.
3. Use more flexible plot options (e.g., colors and markers).
4. Store the histogram counts for further processing.

Listing 7.2 addresses the first issue by specifying different values for the
number of bins as a second parameter to imhist. Histograms tend to become
less useful when the number of bins is lower than a certain threshold (which
varies from one case to the next), as you can see in Figure 7.2.

LISTING 7.2
Histograms with different numbers of bins.
1 % Histograms with different numbers of bins
2 img_1 = imread(’circuit.tif’);
3 figure
4 subplot(3,1,1), imhist(img_1,256), title(’Histogram with 256 bins’)
5 subplot(3,1,2), imhist(img_1,64), title(’Histogram with 64 bins’)
6 subplot(3,1,3), imhist(img_1,16), title(’Histogram with 16 bins’)
Recipe 7: Histograms and statistics of grayscale images 49

Histogram with 256 bins


1000

500 Copyright Material - Provided by Taylor & Francis


Review Copy - Not for Redistribution
0
0 50 100 150 200 250

Histogram with 64 bins


4000

3000

2000

1000

0
0 50 100 150 200 250

Histogram with 16 bins


15000

10000

5000

0
0 50 100 150 200 250

FIGURE 7.2
Varying the number of bins used to display the histogram of the image at the left of Figure 7.1.

Listing 7.3 addresses the three remaining issues. It illustrates how to store
the histogram counts for further processing (e.g., converting absolute counts
into percentages) and uses the stem function for greater flexibility regard-
ing the histogram’s visual aspects2 . We also introduce the tiledlayout
function and use it to create a 2-by-1 TiledChartLayout object that
manages the display of the image and its histogram into the figure
(Figure 7.3). Lines 8–10 will produce the following messages on the command
window:

The tallest peak (at gray level 73) corresponds to 5.78 %


of the pixels in the image

Pixel percentages add up to 1.000000


50 Image processing recipes in MATLAB®

Image

Copyright Material - Provided by Taylor & Francis


Review Copy - Not for Redistribution

Histogram

0.05 X 73
Y 0.0577778
0.04

0.03

0.02

0.01

0
0 50 100 150 200 250

FIGURE 7.3
Using the stem function to display the histogram of an image with greater control over the
appearance of the histogram. Original image: courtesy of The MathWorks.

LISTING 7.3
Histograms using stem.
1 % Histograms using stem
2 img_1 = imread(’coins.png’);
3
4 [pixel_counts, gray_levels] = imhist(img_1,256);
5 pixel_percentages = pixel_counts / numel(img_1);
6 [max_percent, idx_max_percent] = max(pixel_percentages);
7
8 fprintf([’The tallest peak (at gray level %d) corresponds to %.2f %%’ ...
9 ’ of the pixels in the image \n’], idx_max_percent-1, 100*max_percent);
10 fprintf(’Pixel percentages add up to %f’, sum(pixel_percentages));
11
12 figure
13 t = tiledlayout(2,1);
14 t.TileSpacing = ’tight’; t.Padding = ’compact’;
15 nexttile, imshow(img_1), title(’Image’)
16 nexttile, stem(0:255, pixel_percentages, ’Marker’,’none’, ...
17 ’Color’,’#CC33BB’, ’LineWidth’,1)
18 set(gca,’ylim’,[0 max_percent],’xlim’,[0 255]), title(’Histogram’)
Recipe 7: Histograms and statistics of grayscale images 51

Discussion (Recipe notes)


In thisCopyright Material
recipe you learned - Provided
how bydisplay,
to compute, Taylorand
& explore
Francisthe contents
of histograms of grayscale images.
Review Copy - Not for Redistribution
You are encouraged to expand upon each example in this recipe,
particularly:

• Explore additional options associated with the stem function and


other plotting capabilities in MATLAB.
• Play with the histogram function and see whether its functionality
overlaps with stem and imhist.
• Explore histogram values (counts and associated percentages) further,
e.g., computing useful summary statistics (mean, median, max, min,
standard deviation, etc.) for each image, i.e., treating a histogram as a
probability mass function (PMF) of gray levels (which can be thought
of as discrete random variables whose values lie between 0 and 255) in
the image.

Learn more about it


Useful MATLAB functions
Type the function name in the search field at www.mathworks.com/help/
matlab/
· histogram · imhist · stem ·

MATLAB documentation, demos, and examples


• Create Image Histogram
www.mathworks.com/help/images/create-image-histogram.html
• Working with Probability Distributions
www.mathworks.com/help/stats/working-with-probability-distribu
tions.html

Notes
1 The recipe has been prepared using MATLAB’s built-in images.
2 Note how we had to reconcile MATLAB’s 1-based array indexing with the fact that
gray levels vary from 0 to 255 (line 9 in the code).
8
Recipe 8: Histogram
Copyright equalization
Material - Provided and
by Taylor & Francis
histogram
Reviewmatching
Copy - Not for Redistribution

This recipe teaches you how to perform contrast and intensity adjustments
on images using two popular histogram-based techniques: histogram equal-
ization and histogram matching.

You will need (Ingredients)


• MATLAB R2012b or later
• MATLAB Image Processing Toolbox (IPT) version R2012b or later
• (OPTIONAL1 ) One or more of your images

Steps (Preparation)
1. Select and open the input image.
2. (OPTIONAL) Compute and display its histogram.
3. Perform histogram equalization (or matching) using the appropriate
library function(s).
4. Display the processed image (and associated histogram) and assess
the results.
5. (OPTIONAL) Save/export resulting figures and images using any file
format supported by MATLAB.

Part 1: Histogram equalization


Histogram equalization is a technique that rearranges the distribution of
pixel values in a grayscale image to produce a uniform histogram, in which
(ideally2 ) the percentage of pixels of every gray level is the same. The basic
histogram equalization technique (available in MATLAB as histeq) can be

52 DOI: 10.1201/9781003170198-11
Recipe 8: Histogram equalization and histogram matching 53

Original Image Original Histogram


4000

3000
Copyright Material - Provided by Taylor & Francis
Review Copy - Not 2000
for Redistribution
1000

0
0 50 100 150 200 250

Equalized Image Equalized Histogram


4000

3000

2000

1000

0
0 50 100 150 200 250

FIGURE 8.1
An image and its histogram, before and after histogram equalization. Original image: courtesy
of The MathWorks.

used as a contrast enhancement algorithm when the original image’s gray


levels are grouped in a narrow range of gray levels.
See Figure 8.1 to compare the results before and after the histogram equal-
ization operation using the code in Listing 8.1. In this particular case, you will
probably agree that the image quality was enhanced as a result of applying
this technique.

LISTING 8.1
Histogram equalization.
1 % Histogram equalization
2 img_1 = imread(’pout.tif’);
3 img_1_eq = histeq(img_1,256);
4 figure
5 subplot(2,2,1), imshow(img_1), title(’Original Image’)
6 subplot(2,2,2), imhist(img_1), ylim(’auto’), title(’Original Histogram’)
7 subplot(2,2,3), imshow(img_1_eq), title(’Equalized Image’)
8 subplot(2,2,4), imhist(img_1_eq), ylim(’auto’), title(’Equalized Histogram’)
54 Image processing recipes in MATLAB®

The basic histogram equalization algorithm implemented by his-


teq has inherent limitations. An improved version of histeq, called
adapthisteq, implements the Contrast-Limited Adaptive Histogram
Equalization
Copyright(CLAHE) algorithm
Material [26]. CLAHE
- Provided by Tayloroperates on small regions
& Francis
in the image,Review
called tiles, rather than the entire
Copy - Not for Redistribution image. The adapthisteq
function calculates the contrast transform function for each tile individually,
and neighboring tiles are combined using bilinear interpolation to eliminate
artificially induced boundaries.
See Figure 8.2 to compare the results before and after the histogram equal-
ization operation for both histeq (middle row) and adapthisteq (bottom
row) using the code in Listing 8.2. You will probably agree that – for this
particular image – the results obtained using CLAHE are far superior.

Original Image 104 Original Histogram


2

0
0 50 100 150 200 250

Equalized Image 104 Equalized Histogram


2

0
0 50 100 150 200 250
Histogram after CLAHE
Equalized Image using CLAHE
6000

4000

2000

0
0 50 100 150 200 250

FIGURE 8.2
Adaptive histogram equalization. Original image: courtesy of The MathWorks.
Recipe 8: Histogram equalization and histogram matching 55

LISTING 8.2
Histogram equalization using CLAHE.
1 % Histogram equalization using CLAHE
2 img_1 = imread(’eight.tif’);
3 Copyright Material - Provided by Taylor & Francis
img_1_eq = histeq(img_1,256);
4
5
Review Copy - Not for Redistribution
img_1_clahe_eq = adapthisteq(img_1);
subplot(3,2,1), imshow(img_1), title(’Original Image’)
6 subplot(3,2,2), imhist(img_1), ylim(’auto’), title(’Original Histogram’)
7 subplot(3,2,3), imshow(img_1_eq), title(’Equalized Image’)
8 subplot(3,2,4), imhist(img_1_eq), ylim(’auto’), ...
9 title(’Equalized Histogram’)
10 subplot(3,2,5), imshow(img_1_clahe_eq), ...
11 title(’Equalized Image using CLAHE’)
12 subplot(3,2,6), imhist(img_1_clahe_eq), ylim(’auto’), ...
13 title(’Histogram after CLAHE’)

Part 2: Histogram matching


Despite its usefulness in contrast enhancement, histogram equalization has
some limitations. For example, there are situations in which you want to be
able to perform specific changes on the original histogram, e.g., to make it
match – as closely as possible – another image’s histogram. In these situa-
tions, a helpful technique is the direct histogram specification, also known as
histogram matching.
In MATLAB, histogram matching can be implemented using the
imhistmatch function. Listing 8.3 shows how it can be used to adjust the
histogram of a 2D image to match the histogram of a reference image. Results
are shown in Figure 8.3. Note how the histogram on the bottom-right portion
of the figure is much closer to the histogram of the reference image (middle
row) than the histogram of the original image (top row).

LISTING 8.3
Histogram matching.
1 % Histogram matching
2 img_1 = imread(’pout.tif’); % Original image
3 ref_img = imread(’coins.png’); % Reference image
4 matched_img = imhistmatch(img_1, ref_img);
5

6 subplot(3,2,1), imshow(img_1), title(’Original Image’)


7 subplot(3,2,2), imhist(img_1), ylim(’auto’), ...
8 title(’Histogram of Original Image’)
9 subplot(3,2,3), imshow(ref_img), title(’Reference Image’)
10 subplot(3,2,4), imhist(ref_img), ylim(’auto’), ...
11 title(’ Histogram of Reference Image’)
12 subplot(3,2,5), imshow(matched_img), ...
13 title(’Histogram matched image’)
14 subplot(3,2,6), imhist(matched_img), ylim(’auto’), ...
15 title(’Modified Histogram’)
56 Image processing recipes in MATLAB®

Original Image Histogram of Original Image


4000

2000
Copyright Material - Provided by Taylor & Francis
Review Copy - Not for Redistribution
0
0 50 100 150 200 250

Reference Image Histogram of Reference Image


4000

2000

0
0 50 100 150 200 250

Histogram matched image Modified Histogram


15000

10000

5000

0
0 50 100 150 200 250

FIGURE 8.3
Histogram matching. Original image: courtesy of The MathWorks.

Discussion (Recipe notes)


In this recipe, you learned how to perform histogram equalization and
histogram matching. Our examples used grayscale images for the sake of
simplicity.
You are encouraged to expand upon each example in this recipe,
particularly:

• Extend the examples to include color images and image stacks (e.g.,
MRI slices).
• Explore additional options associated with the imhistmatch func-
tion. Check the official documentation for ideas.
Recipe 8: Histogram equalization and histogram matching 57

Learn more about it


UsefulCopyright Material
MATLAB functions - Provided by Taylor & Francis
Review
Type the function Copy
name in the- search
Not for Redistribution
field at www.mathworks.com/help/
matlab/
· adapthisteq · histeq · imhistmatch · stretchlim ·

MATLAB documentation, demos, and examples


• Adaptive Histogram Equalization
www.mathworks.com/help/images/adaptive-histogram-equalization.
html
• Adjust Image Contrast Using Histogram Equalization
www.mathworks.com/help/images/histogram-equalization.html
• Contrast Enhancement Techniques
www.mathworks.com/help/images/contrast-enhancement-techniques.
html

Notes
1 The recipe has been prepared using MATLAB’s built-in images.
2 Since we are dealing with digital images, this is usually not possible and we have
to settle for a resulting histogram that is “as close to a uniform distribution as it
can get.”
9
Recipe 9: Individual
Copyright channel
Material - Provided histograms
by Taylor & Francis
of colorReview
imagesCopy - Not for Redistribution

This recipe teaches you how to compute and display the histograms of
individual color channels of color images.

You will need (Ingredients)


• MATLAB R2019b or later
• MATLAB Image Processing Toolbox (IPT) version R2019b or later
• (OPTIONAL1 ) One or more of your images

Steps (Preparation)
1. Load input image.
2. (OPTIONAL) Convert the image to another color model.
3. Select the channel(s) of interest – e.g., for RGB images, you’ll probably
want the individual color channels (R, G, and B), whereas, for images
in the YIQ color space, you’ll probably choose the luminance (Y) of the
image.
4. Compute the histogram of the desired channels using imhist.
5. Display the histogram(s) using imhist or stem.

Part 1: Histograms of individual color channels of an RGB image


Color images can be represented in different color models, also named color
spaces2 . In the RGB color space, each pixel is expressed as a combination of
the three primary (component) colors, one per channel – red (R), green (G),
and blue (B). RGB color images are usually encoded using 24 bits per pixel,

58 DOI: 10.1201/9781003170198-12
Recipe 9: Individual channel histograms of color images 59

i.e., 8 bits per pixel in each channel, and are called true color images. Consid-
ering the available combinations of R, G, and B, each in the range [0, 255], a
true color image allows for the representation of 256×256×256 = 16,777,216
distinct colors. Material - Provided by Taylor & Francis
Copyright
Since histograms
Reviewcomprising
Copy - Notthisfor
large number of colors would not be
Redistribution
practical (or meaningful), a possible approach is to show each color channel
individually.
The code in Listing 9.1 displays, in a single MATLAB figure, the input
color image, its R, G, and B channels as intensity images, and the respective
histograms. The input is a true color image, assigned to the RGB multidi-
mensional array 𝑀 × 𝑁 × 3, where 𝑀 is the number of rows and 𝑁 is the
number of columns. The commands R = RGB(:,:,1); G = RGB(:,:,2);
B = RGB(:,:,3); decompose the image into individual color channels
and their respective histograms are computed with the imhist function.
The countMax variable stores the maximum pixel count among the three
histograms to adjust the 𝑦 axis of the histograms and plot them at the same
scale. The tiledlayout function creates a 3-by-3 TiledChartLayout ob-
ject that manages the display of the images and histograms into the figure.
The nexttile([3 1]) function spans one display region to 3-by-1, which
is used to show the input image. To plot the histogram, we use the stem
function, a versatile option to present images’ histograms3 .

LISTING 9.1
Histograms of individual color channels of RGB images.
1 % Histograms of individual color channels of RGB images
2 img = imread(’lighthouse.png’);
3 %img = imread(’fabric.png’);
4
5 img_r = img(:,:,1); img_g = img(:,:,2); img_b = img(:,:,3);
6 hist_r = imhist(img_r); hist_g = imhist(img_g); hist_b = imhist(img_b);
7 count_max = max([hist_r; hist_g; img_bh]);
8
9 figure
10 t = tiledlayout(3,3);
11 t.TileSpacing = ’tight’; t.Padding = ’compact’;
12
13 nexttile([3 1]), imshow(img)
14 nexttile, imshow(img_r)
15 nexttile, stem(0:255,hist_r,’Marker’,’none’,’Color’,’r’,’LineWidth’,1)
16 set(gca,’ylim’,[0 count_max],’xlim’,[0 255],’xticklabels’,[])
17 nexttile, imshow(img_g)
18 nexttile, stem(0:255,hist_g,’Marker’,’none’,’Color’,’g’,’LineWidth’,1)
19 set(gca,’ylim’,[0 count_max],’xlim’,[0 255],’xticklabels’,[])
20 nexttile, imshow(img_b)
21 nexttile, stem(0:255,hist_b,’Marker’,’none’,’Color’,’b’,’LineWidth’,1)
22 set(gca,’ylim’,[0 count_max],’xlim’,[0 255])

Figure 9.1 presents the output of the code for the fabric.png and
lighthouse.png built-in images.
60 Image processing recipes in MATLAB®

10000

5000

Copyright Material - Provided by Taylor & Francis


0
Review Copy - Not for Redistribution
10000

5000

10000

5000

0
0 100 200
(a)

10000

5000

10000

5000

10000

5000

0
0 100 200

(b)

FIGURE 9.1
Results of decomposing a true color RGB image into its individual channels and computing
the respective histograms (see Listing 9.1). The input image is shown at the left, and the R, G,
and B channels, from top to bottom, as intensity images. (a) The histogram of the R channel
presents a large bin at the 255 value, demonstrating that many pixels of the image have saturated
R values. (b) The histogram of the B channel presents a peak between 200 and 225 due to the
bluish pixels of the sky in the image and many of the whitish pixels. Original images: courtesy
of The MathWorks.
Recipe 9: Individual channel histograms of color images 61

Part 2: Histogram of the luminance component of a color image


Another possibility is to compute the histogram of the “grayscale version” of
the color image. This can be accomplished using a conversion from RGB to
Copyright
a color Material the
space that separates - Provided by Taylor(color
chroma components & Francis
information) from
the luminanceReview Copy
(achromatic - Not for of
information) Redistribution
the image. The YIQ color space, for
example, employs this method – Y is the luminance, while I and Q encode
the chromaticity of the image.
In Listing 9.2, built-in images office_5.jpg and office_6.jpg are
converted from RGB to YIQ by the rgb2ntsc function, then the respective Y
channels are isolated, and their histograms are computed and displayed by
imhist function. Figure 9.2 shows the results.

LISTING 9.2
Histogram of the luminance component of a color image.
1 % Histogram of the luminance component of a color image
2 img_1 = imread(’office_5.jpg’);
3 img_2 = imread(’office_6.jpg’);
4
5 img_1_yiq = rgb2ntsc(img_1); img_2_yiq = rgb2ntsc(img_2);
6 img_1_y = im2uint8(img_1_yiq(:,:,1)); img_2_y = im2uint8(img_2_yiq(:,:,1));
7
8 figure
9 subplot(2,3,1), imshow(img_1), title(’Image 1’)
10 subplot(2,3,2), imshow(img_1_y), title(’Y’)
11 subplot(2,3,3), imhist(img_1_y), axis tight
12 subplot(2,3,4), imshow(img_2), title(’Image 2’)
13 subplot(2,3,5), imshow(img_2_y), title(’Y’)
14 subplot(2,3,6), imhist(img_2_y), axis tight

FIGURE 9.2
Output of the code to display the histogram of the luminance of two color images. From left
to right: the original color images, their Y (luminance) channels obtained using an RGB to YIQ
color space conversion, and the histograms. Histograms show that Image 2 is considerably more
saturated with light pixels than Image 1. Original images: courtesy of The MathWorks.
62 Image processing recipes in MATLAB®

Discussion (Recipe notes)


In thisCopyright
recipe, youMaterial - Provided
learned how byand
to compute Taylor & Francis
display individual channel
histograms of color images.
Review Copy - Not for Redistribution
Although individual channel histograms of color images may convey help-
ful information, as demonstrated in the examples in this recipe, they do not
summarize the combination of the channels into an image. You will learn
how to compute and display combined color histograms in Recipe 10.

Learn more about it


Useful MATLAB functions
Type the function name in the search field at www.mathworks.com/help/
matlab/
· imhist · rgb2ntsc · stem · tiledlayout ·

MATLAB documentation, demos, and examples


• Understanding Color Spaces and Color Space Conversion
www.mathworks.com/help/images/understanding-color-spaces-and-
color-space-conversion.html

Notes
1 The recipe has been prepared using MATLAB’s built-in images.
2 You will learn more about color spaces in Recipe 26.
3 See Recipe 7.
10
Recipe 10: Material
Copyright Combined color
- Provided histograms
by Taylor & Francis
and dominant colors
Review Copy - Not in an image
for Redistribution

This recipe teaches you how to reduce the number of colors in an image (a
process known as quantization), compute and display the resultant histogram,
and identify the dominant colors within the image.

You will need (Ingredients)


• MATLAB R2016b or later
• MATLAB Image Processing Toolbox (IPT) version R2016b or later
• (OPTIONAL1 ) One or more of your images

Steps (Preparation)
1. Load input image.
2. Reduce the number of colors of the image using a quantization, with
rgb2ind.
3. Compute the histogram of the color quantized image using imhist.
4. Display the histogram(s) using bar.

Each pixel of a true color RGB image is expressed as a combination of the


three primary (component) colors, one per channel – red (R), green (G), and
blue (B). RGB color images are usually encoded using 24 bits per pixel, i.e.,
8 bits per pixel in each channel, representing 256×256×256 = 16,777,216 dis-
tinct colors. Even though a color image typically does not contain pixels with
every color combination, it is common to find thousands of unique colors in
a true color image.
As an example, we can find the unique colors of a built-in image by typing
the following commands:

DOI: 10.1201/9781003170198-13 63
64 Image processing recipes in MATLAB®

img = imread(’flamingos.jpg’);
[img_idx, img_cmap] = cmunique(img);
nuc = size(img_cmap,1)

TheCopyright function converts


cmunique Material the true
- Provided by color input
Taylor image into a MAT-
& Francis
LAB indexedReview
image, eliminating the duplicated colors
Copy - Not for Redistribution present in the image.
The outputs are a matrix img_idx of the same width and height of the input
image and a colormap img_cmap containing the unique colors of the input
image. Each pixel of img_idx contains the index of the row of img_cmap,
which stores the pixel’s 𝑅, 𝐺, and 𝐵 values. In MATLAB, a colormap is a 𝑁 ×3
matrix, where 𝑁 is the number of colors and the three columns consist of the
values of 𝑅, 𝐺, 𝐵, in the [0, 1.0] range. The number of rows of the colormap
in variable nuc corresponds to the number of unique colors of the images:
nuc =
194001

We can use the opportunity to inspect an indexed color image using


MATLAB’s Pixel Region tool:
imshow(img_idx, img_cmap)
impixelregion

The outputs of these commands are shown in Figure 10.1. The


impixelregion function displays a rectangle in the figure created by
imshow (center of the cross in Figure 10.1(a)), and a new corresponding
figure window, named Pixel Region (Figure 10.1(b)). You can change the
position and the dimensions of the rectangle that inspects the image interac-
tively using the mouse. Moreover, when the mouse cursor is positioned on
the Pixel Region window, the coordinates and value of the pixel are presented
at the Pixel info region, as shown in Figure 10.1(c).
To conclude our inspection of the elements of an indexed color image, type
the following commands to access the image matrix img_idx, the colormap
img_cmap, and confirm the values obtained with the Pixel Region tool, shown
in Figure 10.1(c):
p = img_idx(486,648)
cmapRow = img_cmap(p,:)

As expected, we obtained the following:


p =
81569
cmapRow =
0.9609 0.4902 0.2861

Note that the Pixel Region tool specifies the pixel coordinates in (𝑋, 𝑌 )
notation (Figure 10.1(b)), and to access it the (𝑟𝑜𝑤, 𝑐𝑜𝑙𝑢𝑚𝑛) notation is re-
quired. Thus, in the code above, access to the pixel 𝑋 = 648 and 𝑌 = 486
(Figure 10.1(c)) is specified as (486, 648).
Recipe 10: Combined color histograms and dominant colors in an image 65

Copyright Material - Provided by Taylor & Francis


Review Copy - Not for Redistribution

FIGURE 10.1
Inspection of a color image with the Pixel Region tool. (a) Image with the original colors. (b) The
region corresponding to the small rectangle in (a); < 𝑝 > is the value into the image matrix and
𝑅, 𝐺, 𝐵 the color into the colormap at row < 𝑝 >. (c) Mouse pointer’s Pixel info, in the bottom
of the window. Original image: courtesy of The MathWorks.

It is possible to visualize all the colors present in an image using a point


cloud representation, where each color is expressed as a point in the specified
color space. In MATLAB, the colorcloud function provides a Panel object
that shows the 3D color space and the distribution of the image’s colors.
The following commands show MATLAB’s color cloud for the built-in
image flamingos.jpg in the RGB color space. Results are presented in
Figure 10.2, where part (b) shows the distribution of the colors in the image
(a) in the RGB color space, in the original view presented by colorcloud
function, whereas part (c) presents a rotated2 view, allowing the visualization
of the colors mapping the green vegetation in the image.
img_1 = imread(’flamingos.jpg’);
colorcloud(img_1)

The code in Listing 10.1 shows MATLAB’s color cloud for the built-in
image lighthouse.png for both RGB and HSV color spaces. Figure 10.3
shows the results for the cube-shaped RGB color space (part (b)) and the HSV
color space (part (c)), represented by an inverted cone.
66 Image processing recipes in MATLAB®

Copyright Material - Provided by Taylor & Francis


Review Copy - Not for Redistribution

FIGURE 10.2
Visualization of the colors in an image with the colorcloud function. Original image: courtesy
of The MathWorks.

FIGURE 10.3
Visualization of the colors in an image with the colorcloud function. Original image: courtesy
of The MathWorks.

LISTING 10.1
Visualization of the colors in different color spaces (RGB and HSV).
1 % Visualization of the colors in different color spaces
2 img_2 = imread(’lighthouse.png’);
3 figure, imshow(img_2)
4 colorcloud(img_2)
5 colorcloud(img_2,’hsv’)

Color clouds provide a helpful way to visualize all the colors present in an
image, but they cannot summarize the image’s color contents. Conversely, a
combined color histogram with thousands of colors – in this example, 194,001
different colors – is impractical. Thus, a reduction of the number of colors is
required. This is obtained using color quantization.
The most straightforward color quantization method is the uniform quan-
tization, in which each color channel is quantized using fixed-size ranges.
Considering the RGB color space, performing uniform quantization results
in partitioning the RGB cube into smaller cubes of the same size, and the
obtained quantized colors are those of the center of the cubes. In this type of
Recipe 10: Combined color histograms and dominant colors in an image 67

quantization, the actual colors present in the image are not taken into account
to determine the partitions.
Color quantization methods that factor in the colors in the image are likely
to produce better Material
Copyright results. In- MATLAB,
Providedsuch quantization
by Taylor can be achieved
& Francis
using the rgb2ind function, which uses a method
Review Copy - Not for Redistribution called minimum variance
quantization. In the context of the RGB color space, the results of this quanti-
zation process don’t always yield uniform cubes; instead, they depend on the
specific colors present in the image. The resulting quantized colors represent
the center of these cubes.
Listing 10.2 shows code for performing color quantization using rgb2ind,
followed by the computation and displaying of the respective histogram. The
number of quantized colors is assigned to the variable n, and the rgb2ind
function performs a minimum variance quantization. The third input pa-
rameter to the rgb2ind function – ’nodither’ – specifies the behavior
of the function regarding dithering process3 which is not recommended for
this type of application4 . The histogram of the quantized indexed image x_q
with a colormap cmap_q is obtained with the imhist function, and lines 11–
12 sort the histogram in descending order to present the dominant colors of
the image from left to right. The bar function is used to plot the sorted his-
togram, and line 14 sets the colors of the bars with the image’s quantized
colors themselves so that we obtain our combined color histogram of the
input image.
LISTING 10.2
Color quantization and dominant colors.
1 % Color quantization and dominant colors
2 img = imread(’flamingos.jpg’);
3 % img = imread(’lighthouse.png’);
4 figure, imshow(img)
5

6 n = 8;
7 [img_q,cmap_q] = rgb2ind(img,n,’nodither’);
8 figure, imshow(img_q,cmap_q)
9

10 hx_q = imhist(img_q,cmap_q);
11 [hx_qs,idx] = sort(hx_q,’descend’);
12 cmap_qs = cmap_q(idx,:);
13 figure, b = bar(hx_qs,’FaceColor’,’flat’);
14 b.CData = cmap_qs;
15 if n == 1
16 xticks(1), xticklabels(0)
17 else
18 xticks([1 n]), xticklabels([0 n-1])
19 end

Figure 10.4 shows the results for two different images (parts (a) and (d))
for eight quantized colors. The quantized images are shown in parts (b) and
(e), and the respective histograms are displayed in parts (c) and (f). We can
see that the pinkish flamingo colors are the fourth dominant color in the
68 Image processing recipes in MATLAB®

Copyright Material - Provided by Taylor & Francis


Review Copy - Not for Redistribution

FIGURE 10.4
Reduction of the number of colors using color quantization with the rgb2ind function, and the
respective combined color histograms. Original images: courtesy of The MathWorks.

top image, whereas, for the lighthouse image, the first two dominant colors
correspond to the sky.
The number of quantized colors, denoted as 𝑛, is determined based on
the following rationale: a histogram incorporating many colors might under-
mine the effectiveness of summarizing the image’s color palette. Conversely,
selecting too small a number could lead to a quantization process that clusters
significantly distinct colors together.
An example is shown in Figure 10.5: for 𝑛 = 4, the reddish and greenish
colors of the input image (part (a)) were grouped in the quantized image (part
(b)). The minimum variance quantization algorithm considers the actual dis-
tribution of colors in the image, allocating more quantized colors to those that
appear more frequently in the image. In the example, two quantized colors
were allocated to the bluish portion of the input image (colors 0 and 3 in the
histogram in part (c)), and only one quantized color was allocated to the red-
dish and greenish portions of the input image (color 1 in the histogram in
part (c)).
Recipe 10: Combined color histograms and dominant colors in an image 69

Copyright Material - Provided by Taylor & Francis


Review Copy - Not for Redistribution

FIGURE 10.5
Color quantization (b) and histogram (c) of the image lighthouse.png (a), for 𝑛 = 4. Original
image: courtesy of The MathWorks.

Discussion (Recipe notes)


In this recipe, you learned how to compute and display combined color his-
tograms and dominant colors of an image. We used the rgb2ind function to
reduce the number of colors with the minimum variance quantization method.
We encourage you to explore additional color quantization options (see links
below for resources).

Learn more about it


Useful MATLAB functions
Type the function name in the search field at www.mathworks.com/help/
matlab/
· bar 5 · cmunique · colorcloud · imapprox · impixelregion
· rgb2ind ·

MATLAB documentation, demos, and examples


• Reduce the Number of Colors in an Image
www.mathworks.com/help/images/reduce-the-number-of-colors-in-
an-image.html

Notes
1 The recipe has been prepared using MATLAB’s built-in images.
2 Rotation was performed interactively, using the mouse.
70 Image processing recipes in MATLAB®

3 Dithering is a technique used in image processing to create the illusion of color


depth and detail in images with a limited color palette. It involves intention-
ally applying noise or fine patterns to reduce the appearance of color banding or
quantization errors.
4
Copyright Material - Provided by Taylor & Francis
Dithering can make the image look more detailed, but the original color informa-
Review
tion is potentially Copy
lost. If the -goal
Notis for Redistribution
to determine the dominant colors, dithering
can confuse the issue by spreading the color errors around and making it difficult
to identify the primary colors in the image.
5 There are several functions with the same name in MATLAB. In this case, we are
interested in the one at: www.mathworks.com/help/matlab/ref/bar.html.
Copyright Material - Provided by Taylor & Francis
Review Copy - Not for Redistribution

Part IV

Point transformations

Part IV – Point transformations


The recipes in Part IV focus on point operations whose common goal is to
enhance an image. The enhancement results are sometimes targeted at a hu-
man viewer (e.g., contrast adjustment or gamma correction). In contrast, in
other instances, the results may be more suitable for subsequent process-
ing stages in a machine vision system (e.g., compensating for non-uniform
illumination).

Recipe 11 teaches how to implement point transformation techniques


using MATLAB’s built-in functions.
Recipe 12 introduces look-up tables (LUTs) and shows how they can be
used to create custom point transformation functions.
Recipe 13 teaches how to perform gamma correction programmatically
using MATLAB.
Recipe 14 addresses the problem of leveling non-uniform illumination and
offers two methods to solve it.

DOI: 10.1201/9781003170198-14 71
Copyright Material - Provided by Taylor & Francis
Review Copy - Not for Redistribution
11
Recipe 11: Material
Copyright Intensity transformation
- Provided by Taylor & Francis
functions
Review Copy - Not for Redistribution

This recipe teaches you how to modify the pixel contents of monochrome
images using point transformation techniques available as library functions
in MATLAB.

Point operations apply the same mathematical function – often called trans-
formation function – to all pixels, regardless of their location in the image or
the values of their neighbors.
Transformation functions in the spatial domain can be expressed as:

𝑔(𝑥, 𝑦) = 𝑇 [𝑓(𝑥, 𝑦)] (11.1)

where 𝑔(𝑥, 𝑦) is the processed image, 𝑓(𝑥, 𝑦) is the original image, and 𝑇 is
an operator on 𝑓(𝑥, 𝑦).
Since the actual coordinates do not play any role in the way the transfor-
mation function processes the original image, a shorthand notation can be
used:

𝑠 = 𝑇 [𝑟] (11.2)

where 𝑟 is the original gray level, and 𝑠 is the resulting gray level after
processing.
Point transformations may be linear (e.g., negative), piecewise-linear (e.g.,
gray level slicing), or non-linear (e.g., gamma correction). Figure 11.1 shows
examples of basic linear (identity and negative) and non-linear (log, inverse log,
power, and 𝑛𝑡ℎ root) transformation functions.
Point operations are usually treated as simple mapping operations
whereby the new pixel value at a specific location depends only on the orig-
inal pixel value at that same location and the mapping function. In other
words, the resulting image does not exhibit any change in size, geometry,
or local structure if compared with the original image.

DOI: 10.1201/9781003170198-15 73
74 Image processing recipes in MATLAB®

Copyright Material - Provided by Taylor & Francis


Review Copy - Not for Redistribution

FIGURE 11.1
Basic intensity transformation functions.

You will need (Ingredients)


• MATLAB R2012b or later
• MATLAB Image Processing Toolbox (IPT)
• (OPTIONAL1 ) One or more of your images

Steps (Preparation)
This recipe focuses on point transformation techniques available as library
functions in MATLAB, particularly imadjust (for contrast adjustment) and
imcomplement (for computing the negative of an image). The process
consists of these steps:

1. Open input image.


2. Select a MATLAB library function that performs the desired transfor-
mation.
3. Apply the transformation function to the input image.
4. (OPTIONAL) Display before and after images and other relevant plots
(e.g., transformation function) and values.
Recipe 11: Intensity transformation functions 75

Copyright Material - Provided by Taylor & Francis


Review Copy - Not for Redistribution

FIGURE 11.2
Examples of gray level transformations for contrast enhancement. Redrawn from [12].

Examples

Part 1: Contrast adjustment


One of the most common applications of point transformation functions
is contrast manipulation (also known by many other names, such as con-
trast stretching, gray level stretching, contrast adjustment, and amplitude scaling).
These functions often exhibit a curve that resembles a sigmoid function
(Figure 11.2(a)): pixel values of 𝑟 < 𝑚 are compressed towards darker values
in the output image, whereas values of 𝑟 > 𝑚 are mapped to brighter pixel
values in the resulting image. The slope of the curve indicates how dramatic
the contrast changes will be; in its most extreme case, a contrast manipulation
function degenerates into a binary thresholding2 function (Figure 11.2(b)),
where pixels in the input image whose value is 𝑟 < 𝑚 become black and
pixels whose value is 𝑟 > 𝑚 are converted to white.
One of the most valuable variants of contrast adjustment functions is the
automatic contrast adjustment (or simply auto-contrast), a point transformation
that – for images of class uint8 in MATLAB – maps the darkest pixel value
in the input image to 0, the brightest pixel value to 255, and redistributes the
intermediate values linearly (Figure 11.3).
The autocontrast function can be described as:

𝐿−1
𝑠= ⋅ (𝑟 − 𝑟𝑚𝑖𝑛 ) (11.3)
𝑟𝑚𝑎𝑥 − 𝑟𝑚𝑖𝑛

where: 𝑟 is the pixel value in the original image (in the [0, 255] range), 𝑟𝑚𝑎𝑥
and 𝑟𝑚𝑖𝑛 are the values of its brightest and darkest pixels, 𝑠 is the resulting
76 Image processing recipes in MATLAB®

Copyright Material - Provided by Taylor & Francis


Review Copy - Not for Redistribution

FIGURE 11.3
Auto-contrast operation. Redrawn from [10].

FIGURE 11.4
Contrast adjustment. Original image: courtesy of The MathWorks.

pixel value, and 𝐿 − 1 is the highest gray value that can be encoded in the
input image (usually 𝐿 = 256).
Listing 11.1 shows how to apply the auto-contrast transformation function
to an input image with poor contrast. It uses imadjust with the simplest
possible syntax (i.e., default options for input and output gray level range).
According to the documentation, in this case, “imadjust saturates the bot-
tom 1% and the top 1% of all pixel values.” The overall result is an increase
in the contrast of the image. Figure 11.4 shows the images before and after
processing.
Recipe 11: Intensity transformation functions 77

Copyright Material - Provided by Taylor & Francis


Review Copy - Not for Redistribution

FIGURE 11.5
Image adjustment with a customized range of gray levels of interest. Original image: courtesy
of The MathWorks.

LISTING 11.1
Auto-contrast.
1 % Auto-contrast
2 img_1 = imread(’pout.tif’);
3 img_2 = imadjust(img_1);
4 montage({img_1, img_2})

Listing 11.2 uses imadjust with customized options for input and output
gray level range (𝑟𝑚𝑖𝑛 , 𝑟𝑚𝑎𝑥 , 𝑠𝑚𝑖𝑛 , and 𝑠𝑚𝑎𝑥 ) selected to provide a more
dramatic contrast effect on the image. Note that these values are normal-
ized to the [0, 1.0] range (lines 2–3) to comply with the imadjust function.
Figure 11.5 shows the images before and after processing.

LISTING 11.2
Auto-contrast with specified range.
1 % Auto-contrast with specified range
2 r_min = 100/255; r_max = 140/255;
3 s_min = 5/255; s_max = 250/255;
4 img_1 = imread(’pout.tif’);
5 img_2 = imadjust(img_1,[r_min r_max],[s_min s_max]);
6 montage({img_1, img_2})

Contrast adjustment can also be applied to color images but should be used
carefully since it might result in hue changes in the output image. Listing 11.3
uses imadjust with an input RGB color image. Figure 11.6 shows the images
before and after processing.
78 Image processing recipes in MATLAB®

Copyright Material - Provided by Taylor & Francis


Review Copy - Not for Redistribution

FIGURE 11.6
Image adjustment applied to a color image. Original image: courtesy of The MathWorks.

LISTING 11.3
Auto-contrast for a color image.
1 % Auto-contrast for color image
2 img_1 = imread(’football.jpg’);
3 img_2 = imadjust(img_1,[.2 .3 0; .6 .7 1],[]);
4 montage({img_1, img_2})

Part 2: Negative
Listing 11.4 shows how to apply the negative transformation function to an
image using imcomplement. The negative point transformation function is
used whenever it makes the output more suitable for the task at hand (e.g., by
making it easier to notice interesting details in the image or making the con-
vention of background and foreground pixels in binary images3 consistent
with subsequent steps in the pipeline). Figure 11.7 shows the images before
and after processing for binary, grayscale, and color images.
LISTING 11.4
Negative transformation function.
1 % Negative transformation function
2

3 % Binary image
4 img_1 = imread(’text.png’);
5 img_2 = imcomplement(img_1);
6 montage({img_1, img_2})
7

8 % Grayscale image
9 img_1 = imread(’cameraman.tif’);
10 img_2 = imcomplement(img_1);
11 montage({img_1, img_2})
12

13 % Color image
14 img_1 = imread(’football.jpg’);
15 img_2 = imcomplement(img_1);
16 montage({img_1, img_2})
Recipe 11: Intensity transformation functions 79

Copyright Material - Provided by Taylor & Francis


Review Copy - Not for Redistribution

FIGURE 11.7
Negative of an image for binary (top), grayscale (center), and color (bottom) images. Original
images: courtesy of The MathWorks.
80 Image processing recipes in MATLAB®

Discussion (Recipe notes)


In thisCopyright
recipe, youMaterial - Provided
have learned by Taylor
how to apply simple&point
Francis
transformation
functions to Review
enhance Copy
the contrast of an image, including
- Not for Redistribution cases where you
specify the input and output ranges for the stretching operation. MATLAB
has a function, stretchlim, that can assist you in finding the best upper
and lower limits for contrast stretching and includes an option where you
can specify the fraction of the image to saturate at low and high pixel values.
You have also learned how to compute the negative of an image using
imcomplement.
You are encouraged to expand upon each example in this recipe, for ex-
ample, by performing contrast adjustment interactively, using the MATLAB
Adjust Contrast tool.

Learn more about it


Useful MATLAB functions
Type the function name in the search field at www.mathworks.com/help/
matlab/
· imadjust · imcomplement · stretchlim ·

MATLAB documentation, demos, and examples


• Adjust Contrast Tool
www.mathworks.com/help/images/ref/imcontrast.html
• Adjust Image Intensity Values to Specified Range
www.mathworks.com/help/images/adjust-image-intensity-values-to-
specified-range.html
• Contrast Enhancement Techniques
www.mathworks.com/help/images/contrast-enhancement-techniques.
html

Notes
1 The recipe has been prepared using MATLAB’s built-in images.
2 See Recipe 18.
3 See Part VII.
12
Recipe 12: Material
Copyright Custom point by
- Provided transformation
Taylor & Francis
functions
Review Copy - Not for Redistribution

This recipe teaches you how to implement any type of point transformation
function using look-up tables (LUTs).

You will need (Ingredients)


• MATLAB R2020b or later
• MATLAB Image Processing Toolbox (IPT)
• Image file: vpfig.png
• (OPTIONAL1 ) One or more of your images

Steps (Preparation)
The process consists of these steps:

1. Load input image.


2. Create a point transformation function expressed as a look-up table
(LUT).
3. Apply the transformation function to the input image.
4. (OPTIONAL) Display before and after images and other relevant plots
(e.g., transformation function) and values.

Part 1: Linear functions


Listing 12.1 shows how to create an identity transformation function, which
simply maps each pixel value to the same value. Figure 12.1 part (a) shows a
plot of the transformation function whereas parts (b) and (c) display the input

DOI: 10.1201/9781003170198-16 81
82 Image processing recipes in MATLAB®

Copyright Material - Provided by Taylor & Francis


Review Copy - Not for Redistribution

FIGURE 12.1
Identity transformation. (a) Plot of the identity transformation function. (b) Input image. (c)
Output image. Original image: courtesy of The MathWorks.

and output images – which, in this case, have identical contents (which has
been confirmed using isequal in the code).

LISTING 12.1
Identity point transformation function.

1 % Identity point transformation function


2 identity_function = uint8(0:255);
3 plot(identity_function); xlim([0 255]); ylim([0 255]);
4

5 img_1 = imread(’moon.tif’);
6 img_2 = intlut(img_1, identity_function);
7 figure, montage({img_1, img_2})
8

9 if isequal(img_1, img_2)
10 disp(”The two images are identical.”)
11 end

Listing 12.2 shows how to create the negative transformation function (also
known as contrast reverse [21]), which generates the negative of an image.
The negative point transformation function is used whenever it makes the
output more suitable for the task at hand (e.g., by making it easier to notice
interesting details in the image). Figure 12.2 part (a) shows a plot of the trans-
formation function whereas part (b) displays the input and output images –
which, in this case, are the negative of each other (which has been confirmed
using isequal and the library function imcomplement2 in the code).
Recipe 12: Custom point transformation functions 83

Copyright Material - Provided by Taylor & Francis


Review Copy - Not for Redistribution

FIGURE 12.2
Negative transformation. (a) Plot of the negative transformation function. (b) Left: input image;
right: output image. Original image: courtesy of The MathWorks.

LISTING 12.2
Negative point transformation function.
1 % Negative point transformation function
2 negative_function = uint8(255:-1:0);
3 plot(negative_function); xlim([0 255]); ylim([0 255]);
4

5 img_1 = imread(’moon.tif’);
6 img_2 = intlut(img_1, negative_function);
7 figure, montage({img_1, img_2})
8

9 if isequal(img_2, imcomplement(img_1))
10 disp(”The two images are the negative of each other.”)
11 end

Part 2: Piecewise-linear functions


Piecewise-linear transformations can be described by two or more linear
equations, one for each interval of gray level values in the input image.
Listing 12.3 shows how the piecewise-linear transformation function spec-
ified by Eq. (12.1) can be implemented using a lookup table (LUT) (and
the intlut function) in MATLAB. Figure 12.3 part (a) shows a plot of the
transformation function, whereas part (b) displays the input and output
images.

⎧ 2⋅𝑓 for 0 < 𝑟 ≤ 64


{
𝑠 = ⎨ 128 for 64 < 𝑟 ≤ 128 (12.1)
{
⎩ 𝑓 for 𝑟 > 128
84 Image processing recipes in MATLAB®

Copyright Material - Provided by Taylor & Francis


Review Copy - Not for Redistribution

FIGURE 12.3
Piecewise-linear transformation. (a) Plot of the piecewise-linear transformation function spec-
ified by Eq. (12.1). (b) Left: input image; right: output image. Original image: courtesy of The
MathWorks.

LISTING 12.3
Piecewise-linear point transformation function.
1 % Piecewise-linear point transformation function
2

3 my_lut = uint8(zeros([1 256]));


4 my_lut(1:65) = 2*(0:64);
5 my_lut(66:129) = 128;
6 my_lut(130:256) = (130:256)-1;
7

8 plot(my_lut), axis tight, axis square


9

10 img_1 = imread(’coins.png’);
11 img_2 = intlut(img_1,my_lut);
12

13 figure, montage({img_1, img_2})

Part 3: Non-linear functions


There are many useful non-linear point transformation functions in image
processing, including logarithmic, power law, and sigmoid functions. Essen-
tially, if you know how to express the function mathematically, you should
be able to implement it using the same recipe as before.
Listing 12.4 shows how to create logarithmic transformation functions,
which can be used to compress the dynamic range of an image to bring out
features that were not originally as clear.
Log transformations can be mathematically described as:

𝑠 = 𝑐 ⋅ log(1 + 𝑟) (12.2)
Recipe 12: Custom point transformation functions 85

Copyright Material - Provided by Taylor & Francis


Review Copy - Not for Redistribution

FIGURE 12.4
Logarithmic transformation. (a) Plot of the log transformation function specified by Eq. (12.2).
(b) Left: input image; right: output image. Original image: courtesy of The MathWorks.

where: 𝑟 is the original pixel value, 𝑠 is the resulting pixel value, and 𝑐 is a
constant.
LISTING 12.4
Logarithmic transformation function.
1 % Logarithmic function
2

3 x = 0:255; c = 255 / log(256);


4 log_function = uint8(c * log(x + 1));
5 plot(log_function); xlim([0 255]); ylim([0 255]);
6

7 img_1 = imread(’moon.tif’);
8 img_2 = intlut(img_1, log_function);
9 figure, montage({img_1, img_2})

Figure 12.4 part (a) shows a plot of the transformation function, whereas
part (b) displays the input and output images.
The log transformation and its inverse are non-linear transformations
used, respectively, when we want to compress or expand the dynamic range
of pixel values in an image.
The inverse of the log function is as follows.
𝑠 = exp(𝑟/𝑐) − 1 (12.3)
where: 𝑟 is the original pixel value, 𝑠 is the resulting pixel value, and 𝑐 is a
constant.
Listing 12.5 shows how to apply the inverse logarithmic transformation
to “undo” the changes made by the log transformation to the image and
computes the absolute difference between the initial image and the final im-
age (after having applied the log transformation followed by the inverse
86 Image processing recipes in MATLAB®

log transformation). Figure 12.5 part (a) shows a plot of the transforma-
tion function, whereas part (b) displays the input, intermediate, and output
images.
Copyright Material - Provided by Taylor & Francis
LISTING 12.5
Review
Inverse logarithmic Copy function.
transformation - Not for
Redistribution
1 % Inverse log function
2

3 x = 0:255; c = 255 / log(256);


4 inverse_log_function = uint8(exp(x/c) - 1);
5 plot(inverse_log_function); xlim([0 255]); ylim([0 255]);
6

7 img_1 = imread(’moon.tif’);
8 img_2 = intlut(img_1, log_function);
9 img_3 = intlut(img_2, inverse_log_function);
10 figure, montage({img_1, img_2, img_3})
11

12 img_diff = imabsdiff(img_1, img_3);


13

14 figure, imshow(img_diff,[])
15 num_non_zero_pixels = nnz(img_diff);
16 total_num_pixels = numel(img_diff);
17 percent_non_zero_pixels = 100 * num_non_zero_pixels/total_num_pixels;
18 brightest_pixel_value = max(img_diff(:));
19

20 sprintf(”There are %d non-zero pixels in the difference image” + ...


21 ” (corresponding to %.1f percent of the total image)”, ...
22 num_non_zero_pixels, percent_non_zero_pixels)
23

24 sprintf(”The brightest pixels value in img_diff is ” + ...


25 ”%d (in a [0..255] range)”, brightest_pixel_value)

FIGURE 12.5
Logarithmic and inverse logarithmic transformations. (a) Plot of the inverse log transformation
function specified by Eq. (12.3). (b) Left: input image; center: image after log transformation;
right: output image obtained by applying the inverse log transformation. Original image: cour-
tesy of The MathWorks.
Recipe 12: Custom point transformation functions 87

Copyright Material - Provided by Taylor & Francis


Review Copy - Not for Redistribution

FIGURE 12.6
Difference image after log transformation followed by inverse log transformation. Original
image: courtesy of The MathWorks.

Note that, at first glance, the output image looks identical to the input im-
age. This is not entirely true: a closer inspection shows minor differences
in some pixel values, caused by the need to round to the nearest uint8
value when building the LUTs for both log and inverse log transformations.
Figure 12.6 shows the differences, emphasized for display purposes. The
code in Listing 12.5 will also produce descriptive messages indicating that
“There are 39677 non-zero pixels in the difference image (corresponding to
20.6 percent of the total image)” and “The brightest pixels in the final image
have a gray value of 3 (in a [0..255] range).”
For another example of a useful non-linear transformation function, let us
create a sigmoid transformation described by:

1
𝑠= (12.4)
1 + exp(−𝑎(𝑟 − 𝑏))

where: 𝑟 is the original pixel value, 𝑠 is the resulting pixel value, 𝑎 is a param-
eter that defines the slope of the function, and 𝑏 is a parameter that defines the
inflection point of the function where 𝑠 = 0.5.
The sigmoid transformation function can be used to increase the contrast of
an image to bring out features that were not initially as clear or de-emphasize
distracting aspects of the image.
Listing 12.6 shows how to create the sigmoid transformation function and
apply it to an image to “clean up” the results of scanning a figure from a
textbook. Figure 12.7 part (a) shows a plot of the transformation function,
whereas part (b) displays the input and output images.
88 Image processing recipes in MATLAB®

Copyright Material - Provided by Taylor & Francis


Review Copy - Not for Redistribution

FIGURE 12.7
Sigmoid transformation. (a) Plot of the sigmoid transformation function specified by Eq. (12.4).
(b) Left: input image; right: output image. Original image: courtesy of The MathWorks.

LISTING 12.6
Sigmoid transformation function.
1 % Sigmoid transformation function
2

3 x = 0:255; slope = 0.1; inflec = 127;


4 sigmoid_function = uint8(255./(1 + exp(-slope*(x - inflec))));
5 plot(sigmoid_function); xlim([0 255]); ylim([0 255]); grid on
6 title(’Sigmoid function’)
7

8 img_1 = imread(’vpfig.png’);
9 img_2 = intlut(img_1, sigmoid_function);
10 figure, montage({img_1, img_2})

To conclude this recipe, a few words about the computational efficiency


gains obtained by using LUTs instead of a “naive” approach that computes
the result of applying a function to each pixel in the input image.
Listing 12.7
√ shows how to implement the non-linear transformation func-
tion 𝑠 = 𝑐 𝑟, where 𝑐 = 5 and apply it to a large image (5000×5358 pixels)
using both approaches. It uses MATLAB’s tic and toc functions to mea-
sure the execution time for the transformation step in each case. The results
obtained by the authors3 verify that the LUT approach is noticeably faster (in
this case, by a factor of 3) than the pixel-by-pixel computation.

LISTING 12.7
Computational cost comparison: LUT versus “naive” approach.
1 % Computational cost of LUT vs. naive approach
2

3 img = imread(’tumor_091R.tif’);
4

5 % Direct (naive) method


6 img_1 = double(im2gray(img));
7 tic
8 img_2 = uint8(5 * sqrt(img_1));
Recipe 12: Custom point transformation functions 89

9 toc
10

11 % LUT-based method
12 my_lut = uint8(zeros([1 256]));
13 Copyright
my_lut(1:256) Material
= uint8(5 - Provided by Taylor & Francis
* sqrt(0:255));
14
Review Copy - Not for Redistribution
img_3 = im2gray(img);
15

16 tic
17 img_4 = intlut(img_3, my_lut);
18 toc

Discussion (Recipe notes)


In this recipe, you have learned how to use lookup tables (and the intlut
function) to create any custom point transformation function allowing you to,
essentially, modify the pixel contents of an image any way you want, with di-
rect control over the specification of the transformation function. Moreover,
by implementing these functions as lookup tables (instead of naively com-
puting the values for each pixel in the image), you have also learned how to
do it in the most computationally effective way.

Learn more about it


Useful MATLAB functions
Type the function name in the search field at www.mathworks.com/help/
matlab/
· intlut · isequal · montage ·

Notes
1 The recipe has been prepared using MATLAB’s built-in images and some of the
authors’ images, which should be available on the website for the book.
2 See Recipe 11.
3 In one of the runs, we recorded 0.27 seconds for the direct method and 0.09 sec-
onds for the LUT-based method using a specific combination of hardware, OS, and
MATLAB version. These numbers (and their ratio) may vary significantly from
one setup to the next.
13
Recipe 13: Material
Copyright Gamma correction
- Provided by Taylor & Francis
Review Copy - Not for Redistribution

This recipe teaches you how to perform gamma correction on an image.

Gamma correction is a method initially devised to tackle the discrepan-


cies between sensors and display units in analog television systems be-
cause light intensity, whether captured by the camera or reproduced on the
screen, doesn’t correspond linearly with voltage levels. In addition to pre-
compensating for the non-linearity of the display, gamma correction has the
added benefit of encoding the luminance information into a perceptually
uniform space, thus compensating for the non-linear characteristics of the
human visual system. Moreover, the gamma-corrected signal also becomes
less sensitive to noise.
Outside of TV systems, gamma correction can be applied to individual
images as a type of non-linear intensity transformation function (also known
as power-law function), described mathematically as:

𝑠 = 𝑐 ⋅ 𝑟𝛾 (13.1)
where: 𝑟 is the original pixel value, 𝑠 is the resulting pixel value, 𝑐 is a scaling
constant, and 𝛾 is a positive value. Figure 13.1 shows a plot of Equation (13.1)
for several values of 𝛾.
The imadjust function in MATLAB can be used to perform gamma cor-
rection with the syntax: g = imadjust(f,[],[],gamma). You can limit
the application of the power-law transformation to values within a range
specified using the syntax:

J = imadjust(I,[low_in; high_in],[low_out; high_out], gamma)

In this case, any values below low_in and above high_in are clipped or
simply mapped to low_out and high_out, respectively. Only values in
between these limits are affected by the curve.

90 DOI: 10.1201/9781003170198-17
Recipe 13: Gamma correction 91

Copyright Material - Provided by Taylor & Francis


Review Copy - Not for Redistribution

FIGURE 13.1
Examples of power-law transformations for different values of 𝛾.

You will need (Ingredients)


• MATLAB R2012b or later
• MATLAB Image Processing Toolbox (IPT)
• (OPTIONAL1 ) One or more of your images

Steps (Preparation)
The process consists of these steps:

1. Load input image.


2. Apply the power-law transformation function to the input image
using a suitable value for gamma.
3. (OPTIONAL) Display before and after images and other relevant plots
(e.g., transformation function) and values.

Listing 13.1 shows how to apply gamma correction to grayscale and color
images. Figures 13.2 and 13.3 show the results for three different values of
gamma. Notice how the results with 𝛾 = 1 look identical to the input image
(as expected), whereas those with 𝛾 > 1 and 𝛾 < 1 produce darker and
brighter versions of the input image, respectively.
92 Image processing recipes in MATLAB®

Copyright Material - Provided by Taylor & Francis


Review Copy - Not for Redistribution

FIGURE 13.2
Gamma correction for grayscale images with different values of gamma. (a) Input image. (b)
Output image for 𝛾 < 1. (c) Output image for 𝛾 = 1. (d) Output image for 𝛾 > 1. Original
image: courtesy of The MathWorks.

FIGURE 13.3
Gamma correction for color images with different values of gamma. (a) Input image. (b) Output
image for 𝛾 < 1. (c) Output image for 𝛾 = 1. (d) Output image for 𝛾 > 1. Original image:
courtesy of The MathWorks.

LISTING 13.1
Gamma correction.
1 % Gamma correction
2

3 % Grayscale image
4 img_1 = imread(’pout.tif’);
5 imshow(img_1), title(”Original image”)
6 for gamma = 0.5:0.5:1.5
7 img_2 = imadjust(img_1, [], [], gamma);
8 figure, imshow(img_2), title(sprintf(”Gamma = %.1f”,gamma))
9 imwrite(img_2,sprintf(”result_gamma_gray_%.1f.png”, gamma));
10 end
11

12 % Color image
13 img_1 = imread(’football.jpg’);
14 figure, imshow(img_1), title(”Original image”)
15 for gamma = 0.5:0.5:1.5
16 img_2 = imadjust(img_1, [], [], gamma);
17 figure, imshow(img_2), title(sprintf(”Gamma = %.1f”,gamma))
18 imwrite(img_2,sprintf(”result_gamma_color_%.1f.png”, gamma));
19 end
Recipe 13: Gamma correction 93

Discussion (Recipe notes)


In thisCopyright
recipe, youMaterial - Provided
learned how bygamma
to perform Taylorcorrection
& Francis (a non-linear
transformation function used for brightness and
Review Copy - Not for Redistribution contrast adjustment) using
the built-in MATLAB function, imadjust (initially introduced in Recipe 11).
You might want to try implementing gamma correction “from scratch” using
a LUT (see Recipe 12) and compare the results.
You might also want to explore the lin2rgb function and experiment with
its rich options to control the output color gamut and make it comply with
standards, such as sRGB, Adobe RGB (1998), or ProPhoto (ROMM RGB).

Learn more about it


Useful MATLAB functions
Type the function name in the search field at www.mathworks.com/help/
matlab/
· imadjust · intlut · lin2rgb ·

Note
1 The recipe has been prepared using MATLAB’s built-in images and some of the
authors’ images, which should be available on the website for the book.
14
Recipe 14: Material
Copyright Leveling non-uniform
- Provided by Taylor & Francis
illumination
Review Copy - Not for Redistribution

This recipe teaches you how to correct for non-uniform illumination when
binarizing a grayscale image.

The process of image binarization (described in detail in Recipe 18) using


thresholding is highly sensitive to the (background) illumination of a scene.
Even “easy-to-binarize” images (which usually show a bimodal histogram
corresponding to concentrations of foreground and background pixels) pose
a much more complex challenge if the illumination pattern changes from
constant (uniform) to gradual.
In this recipe, you will learn how to successfully binarize images to
compensate for non-uniform illumination using two different methods: (1)
adaptive thresholding; and (2) morphological image processing operations.

You will need (Ingredients)


• MATLAB R2016a or later
• MATLAB Image Processing Toolbox (IPT)
• (OPTIONAL1 ) One or more of your images

Steps (Preparation)
Method 1: Adaptive thresholding
For background illumination correction using adaptive thresholding, the
main steps are:

1. Load a grayscale image into the workspace.


2. Compute the adaptive threshold, which represents an estimate of
average background illumination.

94 DOI: 10.1201/9781003170198-18
Recipe 14: Leveling non-uniform illumination 95

Copyright Material - Provided by Taylor & Francis


Review Copy - Not for Redistribution

FIGURE 14.1
Background illumination correction using adaptive thresholding. Original image: courtesy of
The MathWorks.

3. Binarize the grayscale image using the computed threshold.


4. (OPTIONAL) Display before and after images and other relevant plots
(e.g., original image’s histogram), images, and numerical values.

The code in Listing 14.1 illustrates how to perform adaptive thresholding


in MATLAB. Note that the adaptthresh function takes a second param-
eter that represents the sensitivity (a scalar in the range [0 1.0]) and a third
parameter ’ForegroundPolarity’, used to determine which pixels are
considered foreground pixels2 .

LISTING 14.1
Handling non-uniform illumination (Method 1: adaptive thresholding).
1 % Handling non-uniform illumination
2 % Method 1: adaptive thresholding
3

4 img_1 = imread(’printedtext.png’);
5 img_2 = adaptthresh(img_1,0.4,’ForegroundPolarity’,’dark’);
6 img_3 = imbinarize(img_1,img_2);
7 figure, montage({img_1, img_2,img_3})

Figure 14.1 shows the original image (a), the extracted background illumi-
nation pattern (b), and the resulting binarized image (c).

Method 2: Morphological image processing


For background illumination correction using morphological operations, the
main steps are:

1. Load a grayscale image into the workspace.


2. Define an appropriately shaped structuring element with a size com-
mensurate with the size of the foreground objects in the image.
3. Perform morphological opening using the structuring element from
the previous step to obtain an estimate of the background illumination
pattern.
96 Image processing recipes in MATLAB®

4. Subtract the background approximation image from the original im-


age.
5. Adjust the contrast of the resulting image.
6.Copyright
Binarize theMaterial
grayscale-image.
Provided by Taylor & Francis
ReviewDisplay
7. (OPTIONAL) Copy -before
Notand
for after
Redistribution
images and other relevant plots
(e.g., original image’s histogram), images, and numerical values.

Listing 14.2 illustrates the steps above.

LISTING 14.2
Handling non-uniform illumination (Method 2: morphological image processing).
1 % Handling non-uniform illumination
2 % Method 2: morphological image processing
3

4 img_1 = imread(’rice.png’);
5 se = strel(’disk’,15);
6 background = imopen(img_1,se); % estimate of background
7 img_3 = img_1 - background;
8 img_4 = imadjust(img_3);
9 img_5 = imbinarize(img_4);
10 figure, montage({img_1, background, img_3, img_5})

Figure 14.2 shows the original image (a), the extracted background illumi-
nation pattern (b), the result of subtracting the background from the original
image (c), and the resulting binarized image (d).

Discussion (Recipe notes)


In this recipe, you learned how to compensate for non-uniform illumination
when performing image binarization using two different approaches. How-
ever, as you likely observed, neither method serves as a universal solution
applicable to every image.
The first method uses the built-in MATLAB function adaptthresh and
provides an elegant solution independent of the actual size and shape of the
foreground objects of interest. Despite its elegance, finding the best value for
the sensitivity parameter passed to adaptthresh might require some trial
and error.
The second method uses a clever combination of morphological operations
and image subtraction before applying a conventional (i.e., non-adaptive)
binarization function, imbinarize. In our example, the selected structuring
element shape (disk) and size (radius of 15 pixels) were particularly conve-
nient for this specific image (where the foreground objects were rice grains
whose main axis is less than 30 pixels long).
Recipe 14: Leveling non-uniform illumination 97

Copyright Material - Provided by Taylor & Francis


Review Copy - Not for Redistribution

FIGURE 14.2
Background illumination correction using morphological image processing. Original image:
courtesy of The MathWorks.

Learn more about it


Useful MATLAB functions
Type the function name in the search field at www.mathworks.com/help/
matlab/
· adaptthresh · imadjust · imbinarize · imopen · imopen
· montage ·
98 Image processing recipes in MATLAB®

Notes
1 TheCopyright Material
recipe has been - Provided
prepared by Taylor
using MATLAB’s & Francis
built-in images.
2 In this case, since the image consists of dark text on a bright background, we have
Review Copy - Not for Redistribution
to explicitly indicate so.
Copyright Material - Provided by Taylor & Francis
Review Copy - Not for Redistribution

Part V

Spatial filtering and


special effects

Part V – Spatial filtering and special effects


The recipes in Part V focus on image filtering operations, whose common goal
is to modify the pixel values of an input image to enhance its appearance and
make it more attractive to a human viewer.

Recipe 15 teaches how to implement smoothing filters using MATLAB.


Recipe 16 shows how to create sharpening filters.
Recipe 17 provides the algorithmic version of common artistic filters used
in image manipulation and image-sharing apps.

DOI: 10.1201/9781003170198-19 99
Copyright Material - Provided by Taylor & Francis
Review Copy - Not for Redistribution
15
Recipe 15: Material
Copyright Smoothing filters
- Provided by Taylor & Francis
Review Copy - Not for Redistribution

This recipe teaches you how to perform linear and non-linear filtering for
image smoothing based on neighborhood-oriented operations.

Smoothing filters are designed to preserve an image’s coarser details and ho-
mogeneous areas while reducing some of its fine details. Smoothing filters
can be used to: (i) reduce the amount of noise in the image (see the median fil-
ter example); (ii) blur the image contents (see the mean filter example); or (iii)
soften the image in a visually pleasing way (see the Gaussian blur example).

You will need (Ingredients)


• MATLAB R2015a or later
• MATLAB Image Processing Toolbox (IPT) version R2015a or later
• (OPTIONAL1 ) One or more of your images

Steps (Preparation)
The process of image smoothing in MATLAB usually follows these steps and
illustrates them for four different options of filters:

1. Load input image.


2. Design convolution mask (filter, kernel) either manually or using
fspecial.
3. Apply the filter using imfilter.
4. Display and/or save output image.

DOI: 10.1201/9781003170198-20 101


102 Image processing recipes in MATLAB®

Copyright Material - Provided by Taylor & Francis


Review Copy - Not for Redistribution

FIGURE 15.1
Smoothing an image with a uniform averaging mask of 3×3 size. (a) Original image; (b) filtered
image. Original image: courtesy of The MathWorks.

Option 1: Mean filter


Listing 15.1 illustrates how to perform image smoothing using a uniform av-
eraging 3×3 mask in MATLAB. The input and output images are shown in
Figure 15.1. You might want to repeat the process for different mask sizes and
compare the results – the larger the size of the mask, the greater the blurring
effect.

LISTING 15.1
Mean filter.
1 % Mean filter
2 img_1 = imread(’football.jpg’); % load image
3 h_ave = fspecial(’average’,3); % create mean mask of size 3 by 3
4 img_1_ave = imfilter(img_1,h_ave); % filter the image
5 figure % initialize figure
6 subplot(1,2,1), imshow(img_1), title(’Original Image’);
7 subplot(1,2,2), imshow(img_1_ave), title(’Smoothed Image’);

Option 2: Non-uniform averaging filter


The mean filter we just implemented is known as box filter, and it is a uniform
filter – all mask coefficients have the same value (e.g., 1/9 for a 3×3 mask).
The non-uniform version of the mean filter gives the center of the mask
(the pixel in question) a higher weighted value, while all other coefficients
are weighted by their distance from the center2 :

1 2 1
1 ⎡
ℎ(𝑥, 𝑦) = ⎢ 2 4 2 ⎤
⎥ (15.1)
16
⎣ 1 2 1 ⎦
Recipe 15: Smoothing filters 103

Copyright Material - Provided by Taylor & Francis


Review Copy - Not for Redistribution

FIGURE 15.2
Smoothing an image with a non-uniform averaging mask. (a) Original image; (b) filtered image.
Original image: courtesy of The MathWorks.

Listing 15.2 illustrates how to perform non-uniform average filtering in


MATLAB. Note that fspecial does not provide this type of mask, so we
must create it ourselves, following Equation 15.1. The result is shown in
Figure 15.2.

LISTING 15.2
Non-uniform averaging filter.
1 % Nonuniform averaging filter
2 img_1 = imread(’football.jpg’); % load image
3 h_non = (1/16).*[1 2 1; 2 4 2; 1 2 1]; % 3 by 3 nonuniform averaging mask
4 img_1_non = imfilter(img_1,h_non); % filter the image
5 figure % initialize figure
6 subplot(1,2,1), imshow(img_1), title(’Original Image’);
7 subplot(1,2,2), imshow(img_1_non), title(’Smoothed Image’);

Option 3: Gaussian filter


The Gaussian blur filter is the best-known example of a distanced-based non-
uniform smoothing mask. The coefficients for this mask are specified by a 2D
Gaussian function:

1 −(𝑥2 + 𝑦2 )
ℎ(𝑥, 𝑦) = exp [ ] (15.2)
2𝜋𝜎2 2𝜎2

It is important to note that the value of the mask at a given position 𝑥, 𝑦 is


determined by two factors:

1. The Euclidean distance between a given point and the center of the
mask.
104 Image processing recipes in MATLAB®

Copyright Material - Provided by Taylor & Francis


Review Copy - Not for Redistribution

FIGURE 15.3
Gaussian masks of different sizes and 𝜎 as 3D bar plots: (a) 𝜎 = 0.5 and 𝑠 = 3; (b) 𝜎 = 2 and
𝑠 = 3; (c) 𝜎 = 2 and 𝑠 = 9.

2. The value of 𝜎, a parameter known as standard deviation. A larger 𝜎 will


produce a more gradual (i.e., smoother) falloff from the center to the
periphery.

Note that the larger the 𝜎, the larger the size of the square mask necessary
to enclose and preserve the shape of the Gaussian completely. We can use
the fspecial function to try different combinations of 𝜎 and mask sizes.
Listing 15.3 plots the coefficients of the filters as 3D bars (bar3 function).
Results are shown in Figure 15.3. Plot (a) shows a Gaussian with 𝜎 = 0.5
into a 3×3 mask. In plot (b), one can see the effect of a Gaussian that is not
completely enclosed in the mask – the shape of the Gaussian of 𝜎 = 2 is not
preserved into the 3×3 mask and, in this case, the filter approximates to a
box filter. Plot (c), on the other hand, shows an adequate mask size (9×9) to
enclose the Gaussian of 𝜎 = 2. Note that coefficients fall off near zero at the
periphery of the mask, and the shape of the Gaussian bell-shaped curve is
preserved.

LISTING 15.3
Plotting Gaussian bars.
1 % Plot Gaussian bars
2 h_1 = fspecial(’gaussian’,3,0.5); % 3x3, sigma = 0.5
3 h_2 = fspecial(’gaussian’,3,2); % wrong: 3x3, sigma = 2
4 h_3 = fspecial(’gaussian’,9,2); % 9x9, sigma = 2
5 figure; % initialize figure
6 subplot(1,3,1), bar3(h_1), title(’Gaussian mask h\_1’);
7 subplot(1,3,2), bar3(h_2), title(’Gaussian mask h\_2’);
8 subplot(1,3,3), bar3(h_3), title(’Gaussian mask h\_3’);

Listing 15.4 illustrates how to perform Gaussian filtering in MATLAB us-


ing the imgaussfilt function, which requires specifying only the value of
𝜎. In this case, the size 𝑠 of the square mask is computed internally by the
equation:
Recipe 15: Smoothing filters 105

Copyright Material - Provided by Taylor & Francis


Review Copy - Not for Redistribution

FIGURE 15.4
Blurring an image with a Gaussian mask of 𝜎 = 0.5 and 3×3 size. (a) Original image; (b) filtered
image. Original image: courtesy of The MathWorks.

𝑠 = 2 ⌈2𝜎⌉ + 1 (15.3)

In our example, given 𝜎 = 0.5, the size of the square mask computed using
Equation 15.3 is 𝑠 = 3, i.e., 3×3. The result is shown in Figure 15.4.

LISTING 15.4
Gaussian filter.
1 % Gaussian filter
2 img_1 = imread(’football.jpg’); % load image
3 % Filter the image with a Gaussian of sigma = 0.5
4 img_1_gauss = imgaussfilt(img_1,0.5);
5 figure; % initialize figure
6 subplot(1,2,1), imshow(img_1), title(’Original Image’);
7 subplot(1,2,2), imshow(img_1_gauss), title(’Smoothed Image’);

After running this example, you might want to change the mask size and
see what happens to the output image – the larger the size of 𝜎, the greater the
smoothing effect. You will find that changing the mask size for a Gaussian
filter has a less prominent blurring effect than doing so with the averaging
filter.

Option 4: Median filter


The median filter is the most famous example of an order statistic filter. This
filter simply sorts all values within a window, finds the median value, and
replaces the original pixel value with the median value. It is commonly used
to remove salt-and-pepper noise from images. Because of its popularity, the
median filter has its own function (medfilt2) in MATLAB.
106 Image processing recipes in MATLAB®

Copyright Material - Provided by Taylor & Francis


Review Copy - Not for Redistribution

FIGURE 15.5
Removing salt-and-pepper noise of a grayscale image with a median filter. (a) Original image;
(b) noisy image; (c) filtered Image. Original image: courtesy of The MathWorks.

Listing 15.5 illustrates how to perform a 3 × 3 median filtering on a


grayscale image degraded with salt-and-pepper noise. The result is shown
in Figure 15.5.

LISTING 15.5
Median filter applied to grayscale image.
1 % Median filter gray
2 img_1 = imread(’pout.tif’); % load image
3 img_1_n = imnoise(img_1,’salt & pepper’); % add noise to image
4 % Filter the image with a median filter of size 3 x 3
5 img_1_n_med = medfilt2(img_1_n,[3 3]);
6 figure % initialize figure
7 subplot(1,3,1), imshow(img_1), title(’Original Image’)
8 subplot(1,3,2), imshow(img_1_n), title(’Noisy Image’)
9 subplot(1,3,3), imshow(img_1_n_med), title(’Filtered Image’)

You might want to repeat the process for different mask sizes and com-
pare the results – the larger the size of the mask, the greater the smoothing
effect.

Discussion (Recipe notes)


In this recipe, you learned how to smooth an image using different techniques
– with the linear filters mean (also named box filter), non-uniform averaging, and
Gaussian, and with the non-linear filter median.
The convolution masks for the linear filters can be either created manually
by the user or using the function fspecial, which provides a variety of
filters for distinct applications. For example, ‘laplacian’ is typically used
Recipe 15: Smoothing filters 107

for image sharpening, ‘log’, ‘prewitt’, and ‘sobel’ for edge detection,
and ’motion’ to simulate the effect of camera motion on image acquisition.
The filters are applied to images using imfilter function. Among the
options provided by
Copyright imfilter,
Material you can find:
- Provided by Taylor & Francis
Review Copy - Not for Redistribution
• Handling image borders – Since the convolution mask is not com-
pletely inside the image matrix at the borders of the image, some of its
coefficients have no corresponding image pixels to compute the multi-
plication. Thus, it is necessary to specify how to perform the padding
of these pixels. imfilter allows for the following options.

• Zero-padding: this is the default option. Other constant values (dif-


ferent from zero) can also be specified.
• ‘symmetric’: image borders are mirror-reflected.
• ‘replicate’: image borders are replicated.
• ‘circular’: the image is considered a periodic signal; that is, it
repeats in the 𝑥 and 𝑦 axis.

• Size of the output image:

• ‘same’: this is the default option. The output image is the same size
as the input image.
• ‘full’: output image is larger than the input image because the
values computed at the padding are also presented.

The Gaussian filter provides a “gentler” and more visually pleasant


smoothing effect than the box and non-uniform averaging filters. In MAT-
LAB, imgaussfilt function is preferred to imfilter to perform the
Gaussian filtering.
The median filter, implemented by the medfilt2 function, is a non-linear
filter. It can remove outlier pixels from an image, such as those of the salt-
and-pepper noise. While imfilter and imgaussfilt accept grayscale and
color images as input, as shown in our examples, medfilt2 accepts only
grayscale images.

Learn more about it


Useful MATLAB functions
Type the function name in the search field at www.mathworks.com/help/
matlab/
· fspecial · imfilter · imgaussfilt · imnoise · medfilt2 ·
108 Image processing recipes in MATLAB®

MATLAB documentation, demos, and examples


• Filter Grayscale and Truecolor (RGB) Images using imfilter Func-
tion
Copyright Material - Provided by Taylor & Francis
www.mathworks.com/help/images/filter-images-using-imfilter.html
Review
• ROI-Based Copy - Not for Redistribution
Processing
www.mathworks.com/help/images/roi-based-processing.html

Notes
1 The recipe has been prepared using MATLAB’s built-in images.
2 The further away from the center, the smaller the weight.
16
Recipe 16: Material
Copyright Sharpening filters
- Provided by Taylor & Francis
Review Copy - Not for Redistribution

This recipe teaches you how to perform image filtering to sharpen the image
contents.

Sharpening filters are designed to emphasize an image’s finer edges and de-
tails. This recipe presents two approaches for image sharpening: (1) unsharp
masking; (2) sharpening using a Laplacian mask.

You will need (Ingredients)


• MATLAB R2013a or later
• MATLAB Image Processing Toolbox (IPT) version 2013a or later
• (OPTIONAL1 ) One or more of your images

Steps (Preparation)
1. Load input image.
2. Apply the image sharpening filter of your choice (see two options
below).
3. Display and/or save output image.

Option 1: Unsharp masking


MATLAB includes the imsharpen function, which implements the unsharp
masking technique. Unsharp masking consists of the following steps: (i) sub-
tract a blurred image from its original to generate the “mask” – an image that
mainly contains fine edges and details. (ii) Add the “mask” to the original
image to reinforce the fine edges and details.

DOI: 10.1201/9781003170198-21 109


110 Image processing recipes in MATLAB®

Copyright Material - Provided by Taylor & Francis


Review Copy - Not for Redistribution

FIGURE 16.1
Sharpening using unsharp masking technique with the imsharpen function: (a) original image;
(b) ’Radius’ = 1 and ’Amount’ = 0.8; (c) ’Radius’ = 2 and ’Amount’ = 1.2. Original
image: courtesy of The MathWorks.

Listing 16.1 presents an example of image sharpening using the


imsharpen function. The ’Radius’ parameter specifies the 𝜎 of the Gaus-
sian applied internally to blur the image, whereas ’Amount’ determines the
“strength” of the sharpening. The default values are ’Radius’ = 1 and
’Amount’ = 0.8. Results for these values are shown in Figure 16.1, part
(b). Figure 16.1, part (c) shows a more intense sharpening, obtained with
’Radius’ = 2 and ’Amount’ = 1.2.

LISTING 16.1
Unsharp masking.

1 % Unsharp Masking
2 img_1 = imread(’moon.tif’); % load image
3 img_1_um1 = imsharpen(img_1); % default parameters Radius=1, Amount=0.8
4 img_1_um2 = imsharpen(img_1,’Radius’,2,’Amount’,1.2); % Radius=2, Amount=1
5 figure % initialize figure
6 subplot(1,3,1), imshow(img_1), title(’Original Image’);
7 subplot(1,3,2), imshow(img_1_um1), title(’Sharpened Image 1’);
8 subplot(1,3,3), imshow(img_1_um2), title(’Sharpened Image 2’);

Option 2: Sharpening using a Laplacian mask


The Laplacian is an operator that performs a second-order derivative on an
image. Although one can find variations of the Laplacian mask, the following
mask is ubiquitous:
Recipe 16: Sharpening filters 111

Copyright Material - Provided by Taylor & Francis


Review Copy - Not for Redistribution

FIGURE 16.2
Filtering an image with a Laplacian mask: (a) original image; (b) filtered image. Original image:
courtesy of The MathWorks.

0 1 0
ℎ(𝑥, 𝑦) = ⎡
⎢ 1 −4 1 ⎤
⎥ (16.1)
⎣ 0 1 0 ⎦

By convolving the image with the Laplacian, we can detect local intensity
transitions, mainly due to fine edges and details, as shown in Listing 16.2.
Note that we must convert the image to double because a Laplacian-filtered
image can result in negative values. If we were to keep the image as class
uint8, all negative values would be truncated and, therefore, would not ac-
curately reflect the result of applying a Laplacian mask. By converting the
image to double, all negative values will remain intact. The result is shown
in Figure 16.2.

LISTING 16.2
Laplacian filter.
1 % Laplacian filter
2 img_1 = imread(’moon.tif’); % load image
3 img_1_d = im2double(img_1); % convert to double (in the range [0...1])
4 h_lap = fspecial(’laplacian’,0); % create Laplacian mask
5 img_1_d_lap = imfilter(img_1_d,h_lap); % filter the image
6 figure % initialize figure
7 subplot(1,2,1), imshow(img_1), title(’Original Image’);
8 subplot(1,2,2), imshow(img_1_d_lap,[]), title(’Laplacian output’);

To actually sharpen the image, the Laplacian output and the original im-
age have to be combined using subtraction since the central element of the
Laplacian mask in Equation 16.1 is negative. Additionally, a constant 𝑎 can
be used to determine the proportion of the Laplacian output that is combined
112 Image processing recipes in MATLAB®

Copyright Material - Provided by Taylor & Francis


Review Copy - Not for Redistribution

FIGURE 16.3
Sharpening using a Laplacian mask: (a) original image; (b) 𝑎 = 1.0; (c) 𝑎 = 1.5. Original image:
courtesy of The MathWorks.

with the original image. The larger the value of 𝑎, the stronger the sharpening
effect.
The code in Listing 16.3 performs image sharpening for 𝑎 = 1.0 and
𝑎 = 1.5. Note that we convert the sharpened images to class uint8 before
displaying, with the im2uint8 function. Thus, pixels smaller than 0 and
larger than 1 after the subtraction are truncated to 0 and 255, respectively.
Figure 16.3 shows the results.

LISTING 16.3
Image sharpening using a Laplacian mask.
1 % Sharpening using a Laplacian mask
2 img_1 = im2double(imread(’moon.tif’));
3 h_lap = fspecial(’laplacian’,0); % create Laplacian mask
4 img_1_lap = imfilter(img_1,h_lap); % filter the image
5 img_1_sha1 = img_1 - img_1_lap; % original-Laplacian, a=1.0
6 img_1_sha2 = img_1 - 1.5.*img_1_lap; % original-Laplacian, a=1.5
7 img_1_sha1 = im2uint8(img_1_sha1); % convert to uint8 (truncate)
8 img_1_sha2 = im2uint8(img_1_sha2); % convert to uint8 (truncate)
9 figure % initialize figure
10 subplot(1,3,1), imshow(img_1), title(’Original Image’);
11 subplot(1,3,2), imshow(img_1_sha1), title(’Sharpened Image, a=1.0’);
12 subplot(1,3,3), imshow(img_1_sha2), title(’Sharpened Image, a=1.5’);
Recipe 16: Sharpening filters 113

Discussion (Recipe notes)


In thisCopyright
recipe, youMaterial - Provided
learned how by an
to sharpen Taylor
image&using
Francis
different tech-
niques. You are encouraged to try to use the imsharpen
Review Copy - Not for Redistribution function with color
images and evaluate the quality of the result.

Learn more about it


Useful MATLAB functions
Type the function name in the search field at www.mathworks.com/help/
matlab/
· fspecial · im2uint8 · imfilter · imsharpen ·

Note
1 The recipe has been prepared using MATLAB’s built-in images.
17
Recipe 17: Material
Copyright Other -image
Providedfilters and
by Taylor special
& Francis
effects Review Copy - Not for Redistribution

This recipe teaches you how to algorithmically perform other types of image
filters and special effects.

Image editors and sharing apps provide several filters designed to change the
appearance of images to obtain an artistic or creative version of the original.
Some of these filters do not require the user’s interaction and are based on
simple image processing operations. The examples in this recipe present the
MATLAB implementations of the following special effects filter: (1) emboss;
(2) sepia; (3) vignette; (4) posterization.

You will need (Ingredients)


• MATLAB R2016a or later
• MATLAB Image Processing Toolbox (IPT) version R2016a or later
• (OPTIONAL1 ) One or more of your images

Steps (Preparation)
1. Load input image.
2. Apply the special effect algorithm of your choice (see four options
below) with the desired parameterization.
3. Display and/or save output image.

1: Emboss
The emboss effect is obtained using a convolution mask designed to reinforce
the delicate edges of the image, following the same principle that we used to

114 DOI: 10.1201/9781003170198-22


Recipe 17: Other image filters and special effects 115

Copyright Material - Provided by Taylor & Francis


Review Copy - Not for Redistribution

FIGURE 17.1
Emboss special effect. (a) Original image; (b) after embossing. Original image: courtesy of The
MathWorks.

sharpen an image in Recipe 16, i.e., detecting local intensity transitions. An


example of a convolution mask that provides a visible emboss effect is shown
in Equation 17.1.

−2 −1 0
ℎ(𝑥, 𝑦) = ⎡
⎢ −1 1 1 ⎤
⎥ (17.1)
⎣ 0 1 2 ⎦
In Listing 17.1, we create the mask and use the imfilter function to apply
it to the input image. Figure 17.1 shows the result.

LISTING 17.1
Emboss effect.
1 % Emboss
2 img_1 = imread(’lighthouse.png’); % load image
3 h_emb = [-2 -1 0; -1 1 1; 0 1 2]; % create emboss mask
4 img_1_emb = imfilter(img_1,h_emb);% filter the image
5 figure % initialize figure
6 subplot(1,2,1), imshow(img_1), title(’Original image’)
7 subplot(1,2,2), imshow(img_1_emb), title(’Emboss’)

2: Sepia
The sepia is a well-known effect that gives an image the appearance of an
“old photo” changing the original colors to brownish/yellowish tones. It can
be implemented using a transformation matrix that linearly changes the val-
ues of the 𝑅, 𝐺, and 𝐵 color channels of the pixels into new 𝑅𝑠 , 𝐺𝑠 , and 𝐵𝑠
(Equation 17.2).
116 Image processing recipes in MATLAB®

Copyright Material - Provided by Taylor & Francis


Review Copy - Not for Redistribution

FIGURE 17.2
Sepia special effect. (a) Original image; (b) after sepia. Original image: courtesy of The Math-
Works.

𝑅𝑠 0.393 0.769 0.189 𝑅


⎡ 𝐺 ⎤ = ⎡ 0.349 0.686 0.168 ⎤ ⎡ 𝐺 ⎤ (17.2)
⎢ 𝑠 ⎥ ⎢ ⎥⎢ ⎥
⎣ 𝐵𝑠 ⎦ ⎣ 0.272 0.534 0.131 ⎦ ⎣ 𝐵 ⎦
Listing 17.2 shows how to decompose an image’s color channels using im-
split, apply the transformation matrix to each channel, and recompose the
image using the cat function. Note that the multiplications are performed
on double, and the results are converted back to uint8 integers. Figure 17.2
shows the result.

LISTING 17.2
Sepia effect.
1 % Sepia
2 img_1 = imread(’lighthouse.png’);
3 [img_1_r,img_1_g,img_1_b] = imsplit(im2double(img_1));
4 img_1_r_sep = im2uint8(0.393*img_1_r + 0.769*img_1_g + 0.189*img_1_b);
5 img_1_g_sep = im2uint8(0.349*img_1_r + 0.686*img_1_g + 0.168*img_1_b);
6 img_1_b_sep = im2uint8(0.272*img_1_r + 0.534*img_1_g + 0.131*img_1_b);
7 img_1_sep = cat(3, img_1_r_sep, img_1_g_sep, img_1_b_sep);
8 figure
9 subplot(1,2,1), imshow(img_1), title(’Original image’)
10 subplot(1,2,2), imshow(img_1_sep), title(’Sepia’)

3: Vignette
The idea behind the vignette effect is to “hide” some scene elements and show
only the desired ones to draw the viewer’s attention. In Listing 17.3, we ex-
emplify the concept of a vignette effect by multiplying the input by another
Recipe 17: Other image filters and special effects 117

Copyright Material - Provided by Taylor & Francis


Review Copy - Not for Redistribution

FIGURE 17.3
Vignette special effect. (a) Original image; (b) Gaussian function of 𝜎 = 350; (c) after vignette.
Original image: courtesy of The MathWorks.

image of the same dimensions containing a Gaussian function. In this im-


plementation, the central region of the original image, corresponding to the
highest values of the Gaussian, is preserved. As the Gaussian falloff nears
zero, the corresponding pixels in the image fade to black. The standard devia-
tion 𝜎 of the Gaussian (sigma variable) specifies the diameter of the vignette
effect. Note that the multiplications are performed on double, and the re-
sults are converted back to uint8 integers. The mat2gray function rescales
the Gaussian into the range [0, 1.0]. Figure 17.3, part (b) shows the applied
Gaussian function, and part (c) shows the multiplication result.

LISTING 17.3
Vignette effect.
1 % Vignette
2 img_1 = im2double(imread(’kobi.png’));
3 [r, c] = size(img_1,1,2);
4 sigma = 350; % standard deviation of the Gaussian
5 img_g = mat2gray(fspecial(’gaussian’,[r c],sigma));
6 img_1_vig = im2uint8(img_1.*img_g);
7 figure
8 subplot(1,3,1), imshow(img_1), title(’Original image’)
9 subplot(1,3,2), imshow(img_g), title(’Gaussian’)
10 subplot(1,3,3), imshow(img_1_vig), title(’Vignette’)

4: Posterization
The technique known as posterization was initially employed as a pre-printing
process for color photographs, with the objective of minimizing the required
ink colors by reducing the overall number of colors in the image [4]. In Recipe
10, we saw that the number of colors of an image could be reduced using color
quantization, with the function rgb2ind. This recipe uses rgb2ind as part
of the posterization process.
In Listing 17.4, besides reducing the number of colors to 𝑛 = 8, we apply a
median filter on the quantized image and a boundary demarcation between
118 Image processing recipes in MATLAB®

the different colors to perform a customized “artistic posterization.” The me-


dian filter increases the homogeneity of the regions with the same color,
reducing or even eliminating sparse pixels of different colors at the bound-
aries between
Copyright them, where the
Material tones in the
- Provided byoriginal
Taylorimage change subtly. In
& Francis
this example,Review
a medianCopy
of mask 𝑠 = 7
- Not for Redistribution color channel of
size is applied at each
the quantized image. The detection of the boundaries between the different
colors is obtained with the function boundarymask applied to an arbitrary
color channel of the image. The output of boundarymask is a binary image
(class logical), where the pixels labeled as logical “true” correspond to the
boundaries. This image is an input parameter for the imoverlay function,
which “stamps” the boundaries on the desired image. Figure 17.4 shows the
results of the intermediary steps and the final artistic posterization.

LISTING 17.4
Posterization effect.
1 % Posterization
2 img_1 = imread(’lighthouse.png’);
3
4 n = 8; % number of colors
5 [X_q,cmap_q] = rgb2ind(img_1,n,’nodither’);
6 img_1_q = ind2rgb(X_q,cmap_q);
7
8 s = 7; % size of the median filter
9 img_1_q_m(:,:,1) = medfilt2(img_1_q(:,:,1),[s s]);
10 img_1_q_m(:,:,2) = medfilt2(img_1_q(:,:,2),[s s]);
11 img_1_q_m(:,:,3) = medfilt2(img_1_q(:,:,3),[s s]);
12
13 bw = boundarymask(img_1_q_m(:,:,1));
14 img_1_q_m_artpos = imoverlay(img_1_q_m,bw,’black’);
15
16 figure
17 subplot(1,4,1), imshow(img_1), title(’Original image’)
18 subplot(1,4,2), imshow(img_1_q), title(’Color quantized image’)
19 subplot(1,4,3), imshow(img_1_q_m), title(’Median’)
20 subplot(1,4,4), imshow(img_1_q_m_artpos), title(’Artistic posterization’)

Discussion (Recipe notes)


In this recipe, you learned how to implement different image filters to add
special effects to images. Note that we did not employ specific functions
to create these special effects – they were based mainly on classic image
processing techniques and MATLAB functions that we saw in former recipes.
It is worth mentioning that special effects filters are, by design, dependent
on the programmer’s creativity and, naturally, oriented to experimentation.
You are encouraged to try your own variations of the filters presented in this
recipe and design entirely new special effects filters yourself!
Recipe 17: Other image filters and special effects 119

Copyright Material - Provided by Taylor & Francis


Review Copy - Not for Redistribution

FIGURE 17.4
Posterization special effect, with customized operations, for an artistic result. (a) Original image;
(b) quantized image with 𝑛 = 8 colors; (c) after median filter of mask size 𝑠 = 7. (d) artistic
posterization. Original image: courtesy of The MathWorks.

Learn more about it


Useful MATLAB functions
Type the function name in the search field at www.mathworks.com/help/
matlab/
· im2uint8 · im2double · imfilter · imoverlay · ind2rgb
· mat2gray · medfilt2 · rgb2ind ·

Note
1 The recipe has been prepared using MATLAB’s built-in images.
Copyright Material - Provided by Taylor & Francis
Review Copy - Not for Redistribution
Copyright Material - Provided by Taylor & Francis
Review Copy - Not for Redistribution

Part VI

Image segmentation

Part VI – Image segmentation


Image segmentation is the process of partitioning an image into a set of non-
overlapping regions whose union is the entire image. These regions should
ideally correspond to objects and their meaningful parts, and background.
Image segmentation plays a vital role in computer vision and image analy-
sis, enabling a wide range of applications, such as object detection, tracking,
recognition, and classification. Image segmentation is a challenging task, as
it requires identifying the boundaries of the objects in the image and sepa-
rating them from the background or other objects. Therefore, many image
segmentation techniques have been developed over the years, each with its
own strengths and weaknesses.
Image segmentation techniques can vary widely according to the type of
image (e.g., binary, gray, color), choice of mathematical framework (e.g.,
morphology, image statistics, graph theory), type of features (e.g., intensity,
color, texture, motion), and approach (e.g., top-down, bottom-up, graph-
based).

The recipes in Part VI cover some of the most popular and useful segmen-
tation strategies.
Recipe 18 shows how to binarize a grayscale image using thresholding tech-
niques, thereby segmenting foreground objects from the (brighter or darker)
background.

DOI: 10.1201/9781003170198-23 121


122 Image processing recipes in MATLAB®

Recipe 19 shows how to use a region-based segmentation algorithm (active


contour) to produce high-quality segmentation results.
Recipe 20 teaches how to segment an image by grouping similar pixels
usingCopyright
the k-means Material
clustering algorithm.
- Provided by Taylor & Francis
Recipe 21Review
illustrates the use
Copy - Not of superpixel oversegmentation strategies
for Redistribution
using the simple linear iterative clustering (SLIC) algorithm.
Recipe 22 introduces the lazy snapping algorithm, one of the most popular
graph-based segmentation methods in the literature.
18
Recipe 18: Material
Copyright Image- binarization
Provided by Taylor & Francis
Review Copy - Not for Redistribution

This recipe teaches you how to binarize a grayscale image using thresholding
techniques.

The fundamental problem of thresholding is the conversion of an image with


several gray levels into another image with only two gray levels (a binary im-
age), usually corresponding to the notion of foreground (objects of interest)
and a (lighter or darker) background. This operation is also called binarization
in the literature. In its simplest variant (known as global thresholding), this
conversion is performed by comparing each pixel intensity against a refer-
ence value (threshold, hence the name) and replacing the original pixel value
with a new value that means ‘white’ or ‘black’ depending on the outcome of
the comparison1 . The most popular approach under this category was pro-
posed by Otsu in 1979 [19] and implemented as the graythresh function in
MATLAB. Otsu’s method chooses a threshold that minimizes the intraclass
variance of the thresholded black and white pixels.
Thresholding an image is a common preprocessing step in machine vision
tasks in which there are relatively few objects of interest whose shape (silhou-
ette) is more important than surface properties (such as texture) and whose
average brightness is somewhat higher or lower than the other elements in
the image. Hence, it can be seen as a simple segmentation technique that seg-
ments the foreground objects from the background. Once an image has been
binarized, it can be processed using techniques like the ones described in Part
VII of this book.
Global thresholding works well for images whose gray-level distribution
(histogram) has two distinct modes, such as the one in Figure 18.1, where
the narrowest and most prominent mode (on the left) corresponds to back-
ground pixels, whereas the broadest mode (on the right) reflects the intensity
distribution of pixels corresponding to the coins. For more complex cases,
such as the one in Figure 18.2, a more sophisticated technique, called adaptive
thresholding is used2 .

DOI: 10.1201/9781003170198-24 123


124 Image processing recipes in MATLAB®

Copyright Material - Provided by Taylor & Francis


Review Copy - Not for Redistribution

FIGURE 18.1
A grayscale image (coins) (a) and its histogram (b). Original image: courtesy of The MathWorks.

FIGURE 18.2
A grayscale image (rice) (a) and its histogram (b). Original image: courtesy of The MathWorks.

You will need (Ingredients)


• MATLAB R2016a or later
• MATLAB Image Processing Toolbox (IPT)
• (OPTIONAL3 ) One or more of your images

Steps (Preparation)
For binarization using a global image threshold, the main steps are:
Recipe 18: Image binarization 125

1. Load grayscale image into the workspace.


2. Compute the threshold.
3. Binarize the grayscale image using the computed threshold.
Copyright Material - Provided by Taylor & Francis
4. (OPTIONAL) Display before and after images and other relevant plots
Reviewimage’s
(e.g., original Copy histogram)
- Not for Redistribution
and values (e.g., the optimal thresh-
old computed by the graythresh function).

Listing 18.1 illustrates how to perform global thresholding in MATLAB.


Note that graythresh computes the optimal threshold value based on the
image’s histogram, whereas imbinarize uses that value as a parameter to
perform the binarization. In this case, the optimal threshold value is 𝑇 =
0.4941, and the resulting image is shown in Figure 18.3.

LISTING 18.1
Global thresholding.
1 % Global thresholding
2 img_1 = imread(’coins.png’);
3 level = graythresh(img_1);
4 img_2 = imbinarize(img_1,level);
5 montage({img_1,img_2})

The steps for binarization using adaptive thresholding are essentially the
same, except that now you will use the adaptthresh function.
Listing 18.2 illustrates how to perform adaptive thresholding in MATLAB.
Note that the adaptthresh function takes a second parameter that repre-
sents the sensitivity (a scalar in the range [0, 1.0]): lower values will result
in more pixels being labeled as background. Figure 18.4 shows results for
three different sensitivity values.

FIGURE 18.3
Binarized version of the grayscale image in Figure 18.1(a). Original image: courtesy of The Math-
Works.
126 Image processing recipes in MATLAB®

Copyright Material - Provided by Taylor & Francis


Review Copy - Not for Redistribution

FIGURE 18.4
A grayscale image (a) and the results of image binarization using different values of sensitivity:
0.1 (b), 0.4 (c), and 0.8 (d). Original image: courtesy of The MathWorks.

LISTING 18.2
Adaptive thresholding.
1 % Adaptive thresholding
2

3 img_1 = imread(’rice.png’);
4 sensitivity = 0.4;
5 img_2 = adaptthresh(img_1, sensitivity);
6 img_3 = imbinarize(img_1,img_2);
7 imshowpair(img_1, img_3, ’montage’)

Lastly, it is worth mentioning that MATLAB also includes a function


called otsuthresh that computes a global threshold T from histogram counts,
using Otsu’s method [19]. When called using the syntax [T,EM] = ot-
suthresh(counts), the function returns the computed threshold T as well
Recipe 18: Image binarization 127

as an effectiveness metric EM, a numeric scalar in the range [0, 1.0],


which indicates the effectiveness of the thresholding (an effectiveness of 1
is ideal). See Listing 18.3. For the coins image (whose histogram appears in
FigureCopyright
18.1(b)), theMaterial
value of EM is 0.9168 whereas
- Provided for the
by Taylor & rice image (whose
Francis
histogram appears in Figure 18.2(b)), the value
Review Copy - Not for Redistribution of EM is 0.7356.
LISTING 18.3
Otsu thresholding.
1 % Otsu thresholding
2

3 img_1 = imread(’rice.png’);
4 [counts, gray_level] = imhist(img_1,16);
5 stem(gray_level,counts)
6 [T, EM] = otsuthresh(counts);
7 img_2 = imbinarize(img_1,T);
8 figure, imshow(img_2)

Discussion (Recipe notes)


In this recipe, you learned how to binarize an image using built-in MATLAB
functions adaptthresh, graythresh, imbinarize, otsuthresh.
You certainly have noticed that, despite their usefulness and straightfor-
ward syntax, none of the thresholding techniques are guaranteed to work
“automatically” for any input image. There often is a fair amount of trial-and-
error involved in getting some parameters right (such as the best value for
the sensitivity for adaptthresh, for example). Moreover, all results are de-
pendent on the specific image (and its grayscale distribution, i.e., histogram),
and sometimes a manually chosen threshold (for global thresholding) might
work better than the one computed by graythresh (see Figure 18.5)4 .
Last but certainly not least, we have seen that illumination and reflectance
patterns play a critical role in thresholding. Even an easy input image (such
as the coins image), which could be successfully segmented using global
thresholding, poses a much harder challenge if the illumination pattern
changes from constant (uniform) to gradual (Figure 18.6). The resulting im-
age (Figure 18.7(a)) is significantly darker overall, and the corresponding
histogram (Figure 18.7(b)) shows an expected shift to the left. Consequently,
using the same value of threshold that produced very good results before
(Figure 18.5(b)) will lead to an unacceptable binarized image (Figure 18.7(c)).
Noise can also have a significant impact on thresholding, as illustrated in
Figure 18.7(d)–(f). In this case, a Gaussian noise of mean zero and variance
0.03 has been applied to the image, resulting in the image on Figure 18.7(d),
whose histogram, shown in Figure 18.7(e), has lost its original bimodal shape.
The result of segmenting the image using 𝑇 = 0.25 is shown in Figure 18.7(f).
Although not as bad as one could expect, it would need post-processing
(noise reduction) to be truly useful.
128 Image processing recipes in MATLAB®

Copyright Material - Provided by Taylor & Francis


Review Copy - Not for Redistribution

FIGURE 18.5
Image thresholding results for the coins image: threshold computed by Otsu’s method (𝑇 =
0.4941) (a) versus a manually selected threshold (𝑇 = 0.25) (b). Original image: courtesy of The
MathWorks.

FIGURE 18.6
An example of uneven illumination pattern used to generate the image in Figure 18.7(a). Original
image: courtesy of The MathWorks.

In summary, the images changed significantly in both cases, their his-


tograms lost their bimodal shape, and the initially chosen value for the global
threshold (𝑇 = 0.25) was no longer adequate. Additionally, no other value
could be easily chosen just by inspecting the histogram and following the
trial-and-error procedure suggested earlier.
Solutions to leveling non-uniform illumination and denoising the image
(before binarization) are discussed in Recipes 14 and 15, respectively.

Learn more about it


Useful MATLAB functions
Type the function name in the search field at www.mathworks.com/help/
matlab/
· adaptthresh · graythresh · imbinarize · otsuthresh ·
Recipe 18: Image binarization 129

Copyright Material - Provided by Taylor & Francis


Review Copy - Not for Redistribution

FIGURE 18.7
Effect of illumination (left) and noise (right) on thresholding. Original image: courtesy of The
MathWorks.

MATLAB documentation, demos, and examples


• Getting Started with Image Segmenter
https://www.mathworks.com/help/images/image-segmentation-using-
the-image-segmenter-app.html
130 Image processing recipes in MATLAB®

• Segment Image Using Thresholding in Image Segmenter


https://www.mathworks.com/help/images/Segment-an-Image-Using-
Thresholding.html
Copyright Material - Provided by Taylor & Francis
Review Copy - Not for Redistribution

Notes
1 Note that the convention for ‘white’ or ‘black’ pixels being represented as 1 or 0 is
not universal.
2 For the rice image, an uneven illumination pattern made an otherwise easy-to-
binarize image (bright rice grains against a darker background) become harder to
threshold; notice the three modes in its associated histogram. See Recipe 14 for a
solution to this problem.
3 The recipe has been prepared using MATLAB’s built-in images.
4 In MATLAB’s Image Segmenter App (see references in the Learn more about it
section), under the Threshold menu, you can find a friendly resource for interactive
image thresholding.
19
Recipe 19: Material
Copyright Region-based segmentation
- Provided by Taylor & Francis
Review Copy - Not for Redistribution

This recipe teaches you how to perform region-based image segmentation


using the active contours technique.

Region-based image segmentation algorithms are used to partition an image


into regions based on similarities such as color, texture, and intensity. One ap-
proach to region-based segmentation is active contours, also known as snakes1 ,
which are flexible curves that can adapt to the boundaries of objects in an im-
age. The active contour algorithm iteratively adjusts the position of the snake
until it conforms to the boundaries of the object to be segmented.
In MATLAB, this is implemented by the activecontour function, which
segments an image into the foreground (object) and background regions us-
ing a mask – a binary image that specifies the initial state of the active contour.
The mask can be specified programmatically or interactively.

You will need (Ingredients)


• MATLAB R2013a or later
• MATLAB Image Processing Toolbox (IPT) version R2013a or later
• Image file: bean_coffee_08.png

Steps (Preparation)
1. Load the image into the workspace.
2. Specify an initial contour surrounding the objects of interest to serve
as a mask, i.e., the region of interest (ROI).
3. Perform the segmentation operation, specifying the original image,
the ROI, and the number of iterations.
4. Display before and after images and (visually) evaluate the quality of
the results.

DOI: 10.1201/9781003170198-25 131


132 Image processing recipes in MATLAB®

Listing 19.1 illustrates how to perform segmentation for three differ-


ent images of different types and degrees of complexity. The first image
(coins.png) is a relatively easy-to-segment grayscale image2 . The second
imageCopyright
(rice.png)Material
is significantly harder to
- Provided bysegment
Taylordue to the uneven light-
& Francis
ing pattern3 .Review
Lastly, the coffee beans image is a
Copy - Not for Redistributionreasonably easy image to
segment, which has been included in this recipe to remind you that the ac-
tivecontour function can also be used for color images. The results appear
in Figure 19.1.
Note that the code in Listing 19.1 uses active contours in “unsupervised
mode,” i.e., programmatically specifying an ROI that covers most of the
image (lines 6–7, 18–19, and 30–32). The activecontour function also sup-
ports region-growing segmentation in a supervised way, i.e., interactively
drawing rectangular masks surrounding the regions of interest or specifying
points of interest using the mouse, to assist the segmentation algorithm4 .
The simplicity and straightforwardness of the code in Listing 19.1 hide two
potentially tricky aspects that have an impact on the quality of the results:

1. The size of the ROI matters. Our code uses the entire image minus a
few pixels at the border as a mask. Changing the values used to spec-
ify the border (lines 7, 19, and 32) might significantly impact the final
result.
2. Getting the (maximum) number of iterations right is a trial-and-error
process. The default value is 100, which leads to very poor results for
any of these images. You might want to change the code in lines 11, 23,
and 36 and see it yourself!
Recipe 19: Region-based segmentation 133

Copyright Material - Provided by Taylor & Francis


Review Copy - Not for Redistribution

FIGURE 19.1
Examples of segmentation with activecontour for three test images.
134 Image processing recipes in MATLAB®

LISTING 19.1
Segmentation using active contours.
1 % Active contours
2
3 Copyright Material - Provided by Taylor & Francis
%% Image 1: coins
4
5
Review Copy - Not for Redistribution
img_1 = imread(’coins.png’);
% Specify initial contour surrounding the objects of interest
6 mask_1 = zeros(size(img_1));
7 mask_1(25:end-25,25:end-25) = 1;
8 % Display the contour
9 figure, imshow(mask_1)
10 % Segment the image by using the activecontour function (<= 300 iterations)
11 img_1_bw = activecontour(img_1, mask_1, 300);
12 figure, imshow(img_1_bw)
13 figure, montage({img_1,img_1_bw})
14
15 %% Image 2: rice
16 img_2 = imread(’rice.png’);
17 % Specify initial contour surrounding the objects of interest
18 mask_2 = zeros(size(img_2));
19 mask_2(15:end-15,15:end-15) = 1;
20 % Display the contour
21 figure, imshow(mask_2)
22 % Segment the image by using the activecontour function (<= 900 iterations)
23 img_2_bw = activecontour(img_2, mask_2, 900);
24 figure, imshow(img_2_bw)
25 figure, montage({img_2,img_2_bw})
26
27 %% Image 3: coffee beans
28 img_3 = imread(’bean_coffee_08.png’);
29 % Specify initial contour surrounding the objects of interest
30 sz_img_3 = size(img_3);
31 mask_3 = zeros(sz_img_3(1),sz_img_3(2));
32 mask_3(45:end-45,45:end-45) = 1;
33 % Display the contour
34 figure, imshow(mask_3)
35 % Segment the image by using the activecontour function (<= 700 iterations)
36 img_3_bw = activecontour(img_3, mask_3, 700);
37 figure, imshow(img_3_bw)
38 figure, montage({img_3,img_3_bw})

Discussion (Recipe notes)


In this recipe, you learned how to use the activecontour function to per-
form region-based segmentation on 2D grayscale and color images. This
method can also be used for 3D images. Moreover, it has been integrated
with the Image Segmenter App. See references in the Learn more about it
section.
Beware that the active contours algorithm has many limitations, e.g., it pro-
duces unacceptable results for images with complex backgrounds. Recipe 22
will show how to use graph-based algorithms for such cases.
Recipe 19: Region-based segmentation 135

Learn more about it


UsefulCopyright Material
MATLAB functions - Provided by Taylor & Francis
Review
Type the function Copy
name in the- search
Not for Redistribution
field at www.mathworks.com/help/
matlab/
· activecontour ·

MATLAB documentation, demos, and examples


• Getting Started with Image Segmenter App
www.mathworks.com/help/images/image- segmentation- using- the-
image-segmenter-app.html
• Image Segmentation and Analysis
www.mathworks.com/help/images/image-analysis.html
• Segment Image Using Active Contours in Image Segmenter
www.mathworks.com/help/images/use- active- contours- to- refine-
the-segmentation.html

Notes
1 Active contours is the term that designates the technique in general. There are sev-
eral models of active contours [23]. Depending on the context, you can find the
term snakes referring to that specific model of active contours, introduced by [15].
2 See Recipe 18 for attempts to segment this image using thresholding techniques
and additional insights.
3 See Recipes 14 and 18 for related discussions.
4 See links to the official documentation at the end of the recipe, for examples.
20
Recipe 20: Material
Copyright Image- segmentation
Provided by Taylor using
& Francis
k-means clustering
Review Copy - Not for Redistribution

This recipe teaches you how to perform image segmentation using the
k-means clustering algorithm.

The main rationale behind the use of the clustering in the context of image
segmentation is to group similar pixels into clusters, provided that – in ad-
dition to satisfying a criterion for similarity – the pixels in question are also
adjacent, i.e., together they comprise a connected region of relatively uni-
form color. In MATLAB, the imsegkmeans function can be used to quantize
gray levels or colors using k-means clustering without regard for the pixel
locations1 . Additional preprocessing can be incorporated, depending on the
characteristics of the input image, to use imsegkmeans to implement more
effective segmentation2 .

You will need (Ingredients)


• MATLAB R2018b or later
• MATLAB Image Processing Toolbox (IPT) version R2018b or later
• Image file: rug.png

Steps (Preparation)
For image quantization using k-means clustering, the main steps are:

1. Load an image into the workspace.


2. Select the value of 𝐾 that best represents the number of distinct color
regions in the image.
3. Perform k-means clustering using MATLAB’s imsegkmeans func-
tion.

136 DOI: 10.1201/9781003170198-26


Recipe 20: Image segmentation using k-means clustering 137

4. (OPTIONAL) Convert the result into an RGB image for easier visual-
ization.
5. Display before and after images and (visually) evaluate the quality of
Copyright
the results. Material - Provided by Taylor & Francis
Review Copy - Not for Redistribution
Part 1: Image quantization
Listing 20.1 illustrates how to perform quantization using k-means clustering
in MATLAB for different values of 𝐾, varying from 2 to 6. Note that the choice
of 𝐾 is crucial to determine the quality (and interpret the meaning!) of the
results.
LISTING 20.1
Image segmentation using k-means clustering.
1 % Image segmentation using k-means clustering
2
3 img_1 = imread(’coloredChips.png’);
4 imshow(img_1), title(”Original image”)
5 for n_colors = 2:6
6 pixel_labels = imsegkmeans(img_1,n_colors);
7 pixel_labels_rgb = label2rgb(pixel_labels,’hsv’);
8 figure, imshow(pixel_labels,[]),title(sprintf(”%d colors”,n_colors))
9 imwrite(pixel_labels_rgb,sprintf(”result_%d_colors.png”, n_colors));
10 end

Figure 20.1 shows the original image (a) and the segmentation results for
𝐾 = 2 (b), 𝐾 = 3 (c), 𝐾 = 4 (d), 𝐾 = 5 (e), and 𝐾 = 6 (f). From a hu-
man vision perspective, the image contains five distinct foreground colors
(red/orange, blue, green, and yellow chips, plus the black marker) and a rela-
tively distinct and uncomplicated background. This means a choice of 𝐾 = 6
should be appropriate. Inspecting the results, though, we can see that:

• The “cleanest” result was obtained for 𝐾 = 3 (Figure 20.1 part (c)),
which assumed that yellow chips belong to the background and con-
flated the green and blue chips and the black marker into the same
cluster.
• All results for 𝐾 > 3 (bottom row of Figure 20.1) show that the uneven
illumination (the right half of the image is brighter than the left half)
played a role and “forced” the algorithm to treat the background as
two distinct regions3 .
• The result for part (f) (𝐾 = 6) is mostly correct, but – due to the al-
gorithm’s decision to split the background into two color clusters –
the black marker is segmented as if it has the same colors as the green
chips4 .

An elegant solution to the impact of the uneven illumination consists


of converting the RGB image into another color space that separates the
luminance from chrominance information, e.g., L*a*b*, and applying the
imsegkmeans function to the color components only.
138 Image processing recipes in MATLAB®

Copyright Material - Provided by Taylor & Francis


Review Copy - Not for Redistribution

FIGURE 20.1
Image segmentation using k-means clustering, for different values of 𝐾. (a) Original image;
(b) 𝐾 = 2; (c) 𝐾 = 3; (d) 𝐾 = 4; (e) 𝐾 = 5; (f) 𝐾 = 6. Original image: courtesy of The
MathWorks.

Listing 20.2 shows how to perform quantization using k-means clustering


on a color image converted to the L*a*b* color space for different values of 𝐾,
varying from 2 to 6. Figure 20.2 shows the original image (a) and the segmen-
tation results for 𝐾 = 2 (b), 𝐾 = 3 (c), 𝐾 = 4 (d), 𝐾 = 5 (e), and 𝐾 = 6 (f).
The results are in general much “cleaner” than the ones in Figure 20.1, but are
far from perfect. You might find it surprising that, for most cases, the black
marker “blends in” with the background. However, thanks to the separation
between luminance and chrominance, the uneven illumination problem only
affects the results for 𝐾 = 6.

LISTING 20.2
Image segmentation using k-means clustering and the L*a*b* color model.

1 % Image segmentation using k-means clustering and the L*a*b* color model
2
3 img_1 = imread(’coloredChips.png’);
4 imshow(img_1), title(”Original image”)
5 img_1_lab = rgb2lab(img_1);
6 ab = img_1_lab(:,:,2:3); % a* and b* color components
7 ab = im2single(ab); % imsegkmeans requires floating point in single precis.
8 for n_colors = 2:6
9 pixel_labels = imsegkmeans(ab, n_colors);
10 pixel_labels_rgb = label2rgb(pixel_labels,’hsv’);
11 figure, imshow(pixel_labels,[]), title(sprintf(”%d colors”,n_colors))
12 imwrite(pixel_labels_rgb,sprintf(”result_%d_colors_lab.png”, n_colors));
13 end
Recipe 20: Image segmentation using k-means clustering 139

Copyright Material - Provided by Taylor & Francis


Review Copy - Not for Redistribution

FIGURE 20.2
Image segmentation using k-means clustering in the L*a*b* color space for different values of
𝐾. (a) Original image; (b) 𝐾 = 2; (c) 𝐾 = 3; (d) 𝐾 = 4; (e) 𝐾 = 5; (f) 𝐾 = 6. Original image:
courtesy of The MathWorks.

Part 2: Image segmentation


In some cases, it is necessary to do additional processing to encode the spatial
information of the image before applying the imsegkmeans function.
The code in Listing 20.3 illustrates the problem. It shows a naive attempt to
segment the foreground object (in this case, a cardboard box) from the com-
plex background (rug with similar colors) using the imsegkmeans function.
As you can see (Figure 20.3), the results are not what you would want: the
dark/bright spots on the floor are clustered together with the darker/lighter
pixels belonging to the box, without any regard for the spatial location of the
pixels.

LISTING 20.3
Segmentation of a more complex image using k-means clustering: first attempt.
1 % Image segmentation using k-means clustering
2 % First attempt
3 img_1 = imread(’rug.png’);
4 % Attempt to segment the image into two regions -- foreground (box)
5 % and background -- using k-means clustering.
6 labels = imsegkmeans(img_1,2);
7 labeled_image = labeloverlay(img_1,labels);
8 montage({img_1, labeled_image})
140 Image processing recipes in MATLAB®

Copyright Material - Provided by Taylor & Francis


Review Copy - Not for Redistribution

FIGURE 20.3
Failed attempt at image segmentation using k-means clustering and default options: (a) original
image; (b) output image.

LISTING 20.4
Segmentation of a more complex image using k-means clustering: second attempt.
1 % Image segmentation using k-means clustering with
2 % texture and spatial information
3 img_1 = imread(’rug.png’);
4 % Resize the image
5 img_1 = imresize(img_1,0.5);
6 % Convert the image to grayscale.
7 img_1_gray = im2gray(im2single(img_1));
8
9 %---------------------------
10 % Supplement the image with information about the texture in the
11 % neighborhood of each pixel. To obtain the texture information, filter a
12 % grayscale version of the image with a set of 24 Gabor filters, covering
13 % 6 wavelengths and 4 orientations.
14 wavelength = 2.^(0:5) * 3;
15 orientation = 0:45:135;
16 g = gabor(wavelength,orientation);
17 % Filter the grayscale image using the Gabor filters.
18 gabor_mag = imgaborfilt(img_1_gray,g);
19 % Smooth each filtered image to remove local variations.
20 for i = 1:length(g)
21 sigma = 0.65*g(i).Wavelength;
22 gabor_mag(:,:,i) = imgaussfilt(gabor_mag(:,:,i),3.5*sigma);
23 end
24
25 %---------------------------
26 % Supplement the information about each pixel with spatial location
27 % information. This additional information allows the k-means clustering
28 % algorithm to prefer groupings that are close together spatially.
29 % Get the x and y coordinates of all pixels in the input image.
30 n_rows = size(img_1,1);
31 n_cols = size(img_1,2);
Recipe 20: Image segmentation using k-means clustering 141

32 [x,y] = meshgrid(1:n_cols,1:n_rows);
33
34 %---------------------------
35 % Concatenate intensity, texture, and spatial information about each pixel.
36
37
Copyright Material - Provided by Taylor & Francis
featureSet = cat(3,img_1_gray,gabor_mag,x,y);
% Segment the image into two regions using k-means clustering with
38 Review Copy - Not for Redistribution
% the supplemented feature set.
39 labels = imsegkmeans(featureSet,2,’NormalizeInput’,true);
40 labeled_image = labeloverlay(img_1,labels);
41 imshow(labeled_image)
42 title(’Labeled Image with Additional Pixel Information’)
43
44 montage({img_1, labeled_image})

Listing 20.4 shows a clever solution to the problem. It illustrates how to


combine intensity (converting the image from RGB to gray), local texture
(via Gabor filters), and location information (using a mesh grid) to suc-
cessfully segment the foreground object from the background5 . The result
(Figure 20.4(b)) is quite good if compared to the first attempt (Listing 20.3
and Figure 20.3).

Discussion (Recipe notes)


In this recipe, you learned how to use the k-means clustering algorithm to
quantize pixels according to their colors or intensities. In its simplest form,

FIGURE 20.4
Successful attempt at image segmentation using k-means clustering augmented with intensity,
texture, and spatial location information: (a) original image; (b) segmented image.
142 Image processing recipes in MATLAB®

and for images with “easy” objects and uncomplicated texture, this can pro-
vide a quick way to group regions of similar color (e.g., for visualization
purposes).
We Copyright
also learnedMaterial
that in order to use the
- Provided byimsegkmeans MATLAB func-
Taylor & Francis
tion to perform proper segmentation, additional
Review Copy - Not for Redistribution preprocessing is needed to
convey supplementary information (features), such as intensity, texture, and
spatial information.
At this point, if you think there should be better segmentation algorithms
out there, you are correct. We will see some of them in upcoming recipes.

Learn more about it


Useful MATLAB functions
Type the function name in the search field at www.mathworks.com/help/
matlab/
· imsegkmeans · label2rgb · labeloverlay · rgb2lab ·

MATLAB documentation, demos, and examples


• Color-Based Segmentation Using K-Means Clustering
www.mathworks.com/help/images/color-based-segmentation-using-k-
means-clustering.html
• Texture Segmentation Using Gabor Filters
www.mathworks.com/help/images/texture-segmentation-using-gabor-
filters.html

Notes
1 This can be useful in some cases, as you will see in Part 1 of this recipe.
2 You will learn how to do it in Part 2 of this recipe.
3 The attentive reader will probably remember that Recipe 14 taught some ideas
on how to solve the uneven illumination problem that might help in these cases.
Since we are dealing with color images, a more elegant solution will be to decouple
luminance from chrominance, as you will see later in this recipe.
4 Making K = 7 will partially solve the problem by grouping the black pixels belong-
ing to the marker into their own cluster, separate from any other cluster that maps
to chip colors. Try it!
5 This example was adapted from the official documentation page for imsegk-
means (listed in the Learn more about it section). Please check the source for
additional details.
21
Recipe 21: Material
Copyright Superpixel oversegmentation
- Provided by Taylor & Francis
using SLIC
Review Copy - Not for Redistribution

This recipe teaches you to perform superpixel image oversegmentation using


the popular SLIC (Simple Linear Iterative Clustering) technique.

Superpixel oversegmentation is a technique that groups pixels in an im-


age into compact and perceptually meaningful regions. This technique can
help reduce the complexity of an image by representing it with a smaller
set of regions while preserving the essential features. It is often used as an
intermediate approach in the segmentation process1 .
One approach to superpixel oversegmentation is SLIC, which stands for
Simple Linear Iterative Clustering. The SLIC algorithm uses k-means clus-
tering2 to group pixels in the image based on color similarity and spatial
proximity. The algorithm takes as input the desired number of superpixels
and iteratively adjusts the position and color of the cluster centers until con-
vergence. The SLIC algorithm is computationally efficient, and the resulting
superpixels have compact shapes, well-defined boundaries, and consistent
size.

You will need (Ingredients)


• MATLAB R2016a or later
• MATLAB Image Processing Toolbox (IPT) version R2016a or later
• (OPTIONAL3 ) One or more of your images

DOI: 10.1201/9781003170198-27 143


144 Image processing recipes in MATLAB®

Steps (Preparation)
1.Copyright
Load a colorMaterial - Provided
image into by Taylor & Francis
the workspace.
Review Copy - Not for Redistribution
2. Compute the superpixels of the image using the superpixels func-
tion.
3. Display the superpixel boundaries overlaid on the original image and
(visually) evaluate the quality of the results.

LISTING 21.1
Superpixel oversegmentation using SLIC.
1 % Superpixel oversegmentation using SLIC
2

3 img = imread(’lighthouse.png’);
4

5 % Compute superpixels of the image for 4 different values


6 for number_superpixels = [100 200 500 1000]
7 [label_matrix, ~] = superpixels(img, number_superpixels);
8 % Show superpixels overlaid on image
9 boundary_mask = boundarymask(label_matrix);
10 figure, imshow(imoverlay(img,boundary_mask,’yellow’))
11 end
12

13 % Play with ’compactness’ argument


14 number_superpixels = 500;
15 for compactness_value = [1 10 15 20]
16 [label_matrix, ~] = superpixels(img, number_superpixels, ...
17 Method=”slic”, Compactness=compactness_value);
18 % Show superpixels overlaid on image
19 boundary_mask = boundarymask(label_matrix);
20 figure, imshow(imoverlay(img,boundary_mask,’yellow’))
21 end

Listing 21.1 illustrates how to perform superpixel oversegmentation using


SLIC in MATLAB. It explores two aspects that might have an impact on the
quality of the results:

1. The desired number of superpixels4 . The loop in lines 6–11 calls


the superpixels function with four different values for the
number_superpixels argument (see Figure 21.1).
2. The Compactness argument. This parameter controls the shape of su-
perpixels: higher values make superpixels more regularly shaped, i.e.,
square-like; lower values make superpixels adhere to boundaries bet-
ter, making them irregularly shaped5 . The loop in lines 15–21 calls the
superpixels function with four different values (for a fixed number
of 500 desired superpixels), whose results appear in Figure 21.2.
Recipe 21: Superpixel oversegmentation using SLIC 145

Copyright Material - Provided by Taylor & Francis


Review Copy - Not for Redistribution

FIGURE 21.1
Results of applying superpixel oversegmentation using SLIC to the lighthouse image speci-
fying different values for the desired number of superpixels: (a) 100; (b) 200; (c) 500; (d) 1000.

Discussion (Recipe notes)


In this recipe, you learned how to perform superpixel oversegmentation us-
ing SLIC in MATLAB using the superpixels function and experiment with
some of its arguments. This method can also be used for 3D images. See
useful references next.
146 Image processing recipes in MATLAB®

Copyright Material - Provided by Taylor & Francis


Review Copy - Not for Redistribution

FIGURE 21.2
Results of applying superpixel oversegmentation using SLIC to the lighthouse image with
a fixed number of 500 desired superpixels and specifying different values for Compactness
parameter: (a) 1; (b) 10; (c) 15; (d) 20.

Learn more about it


Useful MATLAB functions
Type the function name in the search field at www.mathworks.com/help/
matlab/
Recipe 21: Superpixel oversegmentation using SLIC 147

· superpixels · superpixels3 ·

MATLAB documentation, demos, and examples


Copyright Material - Provided by Taylor & Francis
• ImageReview
Segmentation
Copy - Not for Redistribution
www.mathworks.com/help/images/image-segmentation.html
• Image Segmentation and Analysis
www.mathworks.com/help/images/image-analysis.html
• Plot Land Classification with Color Features and Superpixels
www.mathworks.com/help/images/land-classification-with-color-
features-and-superpixels.html

Notes
1 See Recipe 22.
2 See Recipe 20.
3 The recipe has been prepared using MATLAB’s built-in images.
4 Note that the superpixels function returns two variables: a label matrix and
the actual number of superpixels computed. In Listing 21.1, we have no use for
the latter and replaced it with a ∼. You might want to assign it to a variable and
inspect its value to see how close it gets to the desired number of regions passed
as an argument to the function.
5 Note that to keep Compactness constant during clustering, the Method argu-
ment has to be set to slic instead of the default, slic0 (see lines 16–17).
22
Recipe 22: Material
Copyright Graph-based
- Provided segmentation
by Taylor & Francis
Review Copy - Not for Redistribution

This recipe teaches you how to perform graph-based image segmentation in


MATLAB.

Graph-based image segmentation techniques approach the problem of divid-


ing an image into multiple segments or regions from the perspective of graph
theory: the pixels in an image are represented as nodes, and the relationships
between them are represented as edges. The goal of graph-based image seg-
mentation is to find the optimal partitioning of an image into segments such
that the pixels within each segment are as similar as possible and the pixels
in different segments are as dissimilar as possible. This can be achieved by
finding a minimum cut in the graph.
There are several graph-based algorithms in the literature. This recipe
focuses on two of the most popular methods: GrabCut and lazy snapping.

• GrabCut combines interactive and automatic techniques to achieve


high-quality image segmentation. It is an iterative process that starts
with an initial estimation of the foreground and background regions
and then uses graph-based optimization to refine this estimation. The
algorithm is called “GrabCut” because it is designed to allow the
user to “grab” the desired foreground object by providing a rough
bounding box around it.
• Lazy snapping provides an interactive and efficient way of partition-
ing an image into multiple segments. The algorithm is called “lazy”
because it does not require a full computation of the graph and instead
only computes the necessary portions of the graph during the interac-
tive process. In lazy snapping, the user provides seed points or scribbles
to indicate the desired partition of the image, which will serve as the
initial constraints for the segmentation process.

These algorithms are implemented by the grabcut and lazysnapping


functions, respectively, available in MATLAB’s Image Processing Toolbox.

148 DOI: 10.1201/9781003170198-28


Recipe 22: Graph-based segmentation 149

You will need (Ingredients)


• Copyright Material
MATLAB R2018a - Provided by Taylor & Francis
or later
Review Copy - NotToolbox
• MATLAB Image Processing for Redistribution
(IPT) version R2018a or later
• Image file: pinkphone.png

Steps (Preparation)
Option 1: GrabCut
1. Load the image into the workspace.
2. Generate label matrix using the superpixels function1
3. Specify a region of interest (ROI) (enclosing the foreground objects),
e.g., using drawpolygon and poly2mask.
4. Perform the GrabCut operation using the grabcut function, specify-
ing the original image, the label matrix, and the ROI mask.
5. Display before and after images and (visually) evaluate the quality of
the results.

Listing 22.1 illustrates how to segment the foreground object (a smart-


phone in a pink cover) from the background2 using the grabcut function.
The SLIC superpixel oversegmentation using the superpixels function
(lines 5–6) is an intermediate step, and the resulting label_matrix is used
as one of the parameters expected by the grabcut function (line 28).
Figure 22.1 shows intermediate images and final results. Note that there
are some errors in both directions, i.e., some background (BG) pixels have
been mistakenly labeled as foreground (FG) and vice-versa, which can be
seen clearly in parts (c) and (d) of the figure. In Figure 22.2, when we take a
closer look at some of the errors (zooming into the bottom-left portion of the
phone case), the impact of the SLIC step becomes apparent: the segmenta-
tion errors are aligned with the superpixels’ boundaries. In other words, this
algorithm doesn’t label individual pixels as either FG or BG but entire pixel
regions (i.e., superpixels) instead.
150 Image processing recipes in MATLAB®

LISTING 22.1
Segmentation using GrabCut.
1 % Segmentation using GrabCut
2 img = imread(’pinkphone.png’); % Read image
3 Copyright Material - Provided by Taylor & Francis
4
Review Copy - Not for Redistribution
% Compute superpixels of the image
5 number_superpixels = 500;
6 [label_matrix, number_labels] = superpixels(img, number_superpixels);
7
8 % Show superpixels overlaid on image
9 boundary_mask = boundarymask(label_matrix);
10 figure, imshow(imoverlay(img,boundary_mask,’yellow’))
11
12 % Specify a region of interest and create a mask image.
13 figure, imshow(img)
14
15 % Mode 1: interactive
16 % user will click and select vertices
17 % roi = drawpolygon(”Color”,’yellow’);
18
19 % Mode 2: programmatic
20 % using coordinates of previously selected vertices
21 roi = drawpolygon(’Position’,[318,87; 1053,118; 1039,1828; 205,1804]);
22
23 roi_vertices = roi.Position;
24 roi_mask = poly2mask(roi_vertices(:,1),roi_vertices(:,2), ...
25 size(label_matrix,1),size(label_matrix,2));
26
27 % Apply GrabCut to image passing superpixels and ROI mask as arguments
28 mask = grabcut(img,label_matrix,roi_mask);
29 figure, imshow(mask)
30
31 % Create and display masked image
32 masked_image = img;
33 masked_image(repmat(~mask,[1 1 3])) = 0;
34 figure, imshow(masked_image)
35
36 % Create and display overlay image
37 overlay_image = labeloverlay(img, mask, ’Colormap’,[0 1 0]);
38 figure, imshow(overlay_image)
39
40 % Create and display overlay image
41 overlay_image = img;
42 overlay_image(~mask) = 0.2*img(~mask);
43 figure, imshow(overlay_image)

The code in Listing 22.1 uses the coordinates of a polygon whose vertices
have been programmatically selected (line 21) but has provision for being
used in interactive mode using the drawpolygon function: you just need to
uncomment line 17 and comment out line 21.

Option 2: Lazy snapping


1. Load the image into the workspace.
Recipe 22: Graph-based segmentation 151

Copyright Material - Provided by Taylor & Francis


Review Copy - Not for Redistribution

FIGURE 22.1
Image segmentation using GrabCut: (a) superpixel boundaries (yellow) overlaid on the input
image; (b) selected polygon; (c) masked image; (d) overlay image.

2. Generate label matrix using the superpixels function.


3. Specify one or more rectangular ROIs denoting the foreground, using
drawrectangle and createmask.
4. Specify one or more rectangular ROIs denoting the background, using
drawrectangle and createmask.
152 Image processing recipes in MATLAB®

Copyright Material - Provided by Taylor & Francis


Review Copy - Not for Redistribution

FIGURE 22.2
A closer look at some errors: (a) superpixel boundaries (cyan) overlaid on the input image;
(b) overlay image.

5. Perform the lazy snapping operation using the lazysnapping func-


tion, specifying the original image, the label matrix, and the FG and
BG ROIs.
6. Display before and after images and (visually) evaluate the quality of
the results.

Listing 22.2 illustrates how to segment the foreground object from the back-
ground using the lazysnapping function and the same test image from
before.
Once again, the SLIC superpixel oversegmentation using the superpix-
els function (lines 5–6) appears as an intermediate step, and the resulting
label_matrix is used as one of the parameters expected by the lazys-
napping function (line 32).
In Listing 22.2, we also illustrate that the lazysnapping function sup-
ports multiple disjoint ROIs for specifying the background3 . In this example,
these ROIs are created programmatically as three separate rectangles (lines
20–25), eventually combined into a single BG mask (lines 27–29).
Figure 22.3 shows intermediate images and final results. The FG and BG
masks appear as green and red rectangles, respectively, in part (b) of the
figure. Once again, there are some errors in both directions, i.e., some back-
ground pixels have been mistakenly labeled as foreground and vice-versa,
which can be seen clearly in parts (c) and (d) of the figure. The errors
are more pronounced than the ones obtained using the GrabCut algorithm
(Figure 22.1) for the same image. This might be due to the number of super-
pixel regions passed as an argument to the superpixels function (line 5)
as well the number, shape, location, and size of the FG and BG masks4 .
Recipe 22: Graph-based segmentation 153

Copyright Material - Provided by Taylor & Francis


Review Copy - Not for Redistribution

FIGURE 22.3
Image segmentation using lazy snapping: (a) superpixel boundaries (yellow) overlaid on the
input image; (b) selected regions of interest (ROIs) for FG (green) and BG (red); (c) masked image;
(d) overlay image.
154 Image processing recipes in MATLAB®

LISTING 22.2
Segmentation using lazy snapping.
1 % Segmentation using lazy snapping
img = imread(’pinkphone.png’); % Read image
2
3
Copyright Material - Provided by Taylor & Francis
4 Review Copy - Not for Redistribution
% Compute superpixels of the image
5 number_superpixels = 300;
6 [label_matrix, number_labels] = superpixels(img, number_superpixels);
7
8 % Show superpixels overlaid on image
9 boundary_mask = boundarymask(label_matrix);
10 figure, imshow(imoverlay(img,boundary_mask,’yellow’))
11
12 % Specify foreground region of interest (ROI) and create foreground mask
13 figure, imshow(img)
14
15 foreground_roi = drawrectangle(gca,’Position’,[400 420 500 1200], ...
16 ’Color’,’g’);
17 foreground_mask = createMask(foreground_roi,img);
18
19 % Specify 1+ background ROIs and create corresponding masks
20 background_roi_1 = drawrectangle(gca,’Position’,[1100 600 350 500], ...
21 ’Color’,’r’); % right side of the image
22 background_roi_2 = drawrectangle(gca,’Position’,[10 600 100 700], ...
23 ’Color’,’r’); % left side of the image
24 background_roi_3 = drawrectangle(gca,’Position’,[300 1800 700 150], ...
25 ’Color’,’r’); % bottom of the image
26
27 background_mask = createMask(background_roi_1,img) + ...
28 createMask(background_roi_2,img) + ...
29 createMask(background_roi_3,img);
30
31 % Apply lazy snapping to image passing superpixels and masks as arguments
32 mask = lazysnapping(img, label_matrix, foreground_mask, background_mask);
33 figure, imshow(mask)
34
35 % Create and display masked image
36 masked_image = img;
37 masked_image(repmat(~mask,[1 1 3])) = 0;
38 figure, imshow(masked_image)
39
40 % Create and display overlay image
41 overlay_image = labeloverlay(img, mask, ’Colormap’,[0 1 0]);
42 figure, imshow(overlay_image)

Discussion (Recipe notes)


In this recipe, you learned how to perform graph-based image segmentation
in MATLAB, using the lazysnapping and grabcut functions available in
the Image Processing Toolbox. Both methods can be used for 3D images (see
MATLAB documentation for examples).
Recipe 22: Graph-based segmentation 155

You are encouraged to modify the examples in this recipe to investigate


the impact of several parameters on the final result, such as the number of
superpixels and the size and shape of the foreground and background ROIs.
Graph-based
Copyrightsegmentation can also be by
Material - Provided performed
Taylor interactively
& Francis using the
Image SegmenterReview Copy - Not for Redistribution algorithm’s under-
App, which allows users to refine the
standing of which pixels belong to foreground or background by using
scribbles and interactively watching the results improve. We encourage you
to give it a try!

Learn more about it


Useful MATLAB functions
Type the function name in the search field at www.mathworks.com/help/
matlab/
· boundarymask · createMask · drawpolygon · drawrectangle
· grabcut · labeloverlay · lazysnapping · poly2mask · superpixels ·

MATLAB documentation, demos, and examples


• Getting Started with Image Segmenter App
www.mathworks.com/help/images/image- segmentation- using- the-
image-segmenter-app.html
• Image Segmentation and Analysis
www.mathworks.com/help/images/image-analysis.html
• Segment Image Using Local Graph Cut (Grabcut) in Image Segmenter
www.mathworks.com/help/images/segment-image-using-local-graph-
cut-grab-cut.html

Notes
1 See Recipe 21 for more details on superpixel oversegmentation using SLIC.
2 Note that the image has a textured background that would make earlier ap-
proaches, such as active contour and k-means clustering produce unacceptable
results.
3 The grabcut function also supports multiple ROIs to indicate FG and BG masks.
4 You might want to change the code in Listing 22.2 to change the value in line 5
and make the BG masks bigger, especially the one at the bottom of the image,
to see if it leads to improved results. The ‘Position’ name-value argument of
drawrectangle function requires the rectangle position and size as [𝑥 𝑦 𝑤 ℎ],
where 𝑥 and 𝑦 specify the upper left corner, and 𝑤 and ℎ specify the width and
height.
Copyright Material - Provided by Taylor & Francis
Review Copy - Not for Redistribution
Copyright Material - Provided by Taylor & Francis
Review Copy - Not for Redistribution

Part VII

Binary image analysis

Part VII – Binary image analysis


A typical image processing workflow often requires the manipulation of bi-
nary images, either as a means to an end (e.g., producing binary masks for
ROI-based image processing) or as an end by itself (e.g., counting, measuring,
and identifying foreground objects and their properties).

The recipes in Part VII cover some of the most useful techniques for
processing and analyzing binary images.
Recipe 23 teaches how to identify and manage individual connected
components in a binary image. It guides you through the process of find-
ing, counting, and accessing these components for subsequent analysis or
examination.
Recipe 24 introduces the most popular morphological operations for bi-
nary images, and shows examples of their usage.
Recipe 25 teaches you how to compute region-based features for each con-
nected component in a binary image. This information can be used to feed a
machine learning algorithm to perform image classification.

DOI: 10.1201/9781003170198-29 157


Copyright Material - Provided by Taylor & Francis
Review Copy - Not for Redistribution
23
Recipe 23: Material
Copyright Finding, counting,
- Provided and
by Taylor & Francis
accessing connected
Review Copy - Notcomponents
for Redistributionin binary
images

This recipe teaches how to identify and manage individual connected com-
ponents in a binary image. It walks you through the process of finding,
counting, and accessing these components for subsequent analysis or exam-
ination.

You will need (Ingredients)


• MATLAB R2009a or later
• MATLAB Image Processing Toolbox (IPT) version R2009a or later
• Image file: bean_coffee_08.png

Steps (Preparation)
1. Load a binary input image, or binarize it using one of the techniques
in Recipe 18.
2. Use bwconncomp to locate, count, and access the connected compo-
nents.
3. Use labelmatrix and label2rgb to show the identified connected
components.

Connected components in a binary image are clusters of pixels that often


correspond to objects or regions of interest within the image. Listing 23.1
demonstrates the successful separation of the background and the coffee
beans, which are the objects of interest, using global thresholding with Otsu’s
method, originally introduced in Recipe 18.

DOI: 10.1201/9781003170198-30 159


160 Image processing recipes in MATLAB®

Copyright Material - Provided by Taylor & Francis


Review Copy - Not for Redistribution

FIGURE 23.1
Binarization of image to obtain the connected components corresponding to the coffee beans. (a)
Original image; (b) grayscale version; (c) after binarization; (d) complement of binarized image.

LISTING 23.1
Foreground-background segmentation using global thresholding.
1 % Segmentation using global thresholding
2 img_1 = imread (’bean_coffee_08.png’);
3 figure, imshow(img_1), title(’Input’)
4 img_1_g = rgb2gray(img_1);
5 level = graythresh(img_1_g);
6 img_1_bw = imbinarize(img_1_g, level);
7 figure, montage ({img_1_g, img_1_bw}), title(’Grayscale, binary’)
8 img_1_bw = ~img_1_bw;
9 figure, imshow(img_1_bw), title(’Connected components’)
10 imwrite(img_1_bw, ’bean_coffee_08_bw.png’) %save 1 bit per pixel image

Figure 23.1 shows the most relevant results. Note that the coffee beans are
dark objects over a light background, so we complement the binary image
to obtain a final representation, shown in part (d), where the white (logical
one or “true”) pixels correspond to the desired connected components and
the black (logical zero or “false”) to the background.
Once a binary image containing one or more connected components of in-
terest has been produced, we can use the MATLAB function bwconncomp to
take the binary image as input and output a structure (stored in variable CC
in Listing 23.2) that contains the following fields:

• Connectivity: this parameter is the neighborhood that specifies


whether a pixel is part of the same connected component. For 2D im-
ages, it can be 4 (the four adjacent neighbors of a pixel) or 8 (the eight
neighbors of a pixel). The default value is 8.
• ImageSize: size of the input binary image.
• NumObjects: the number of connected components detected by the
function.
• PixelIdxList: a cell array containing the connected components
themselves, organized as follows: each element of the array stores a
vector with the linear indices of the pixels belonging to that connected
component.
Recipe 23: Finding, counting, and accessing connected components 161

In Listing 23.2, we use bwconncomp to find and count the coffee beans in
the image. As expected, the obtained NumObjects is 18.

LISTING 23.2
Copyright Material - Provided by Taylor & Francis
Finding and counting connected components.
1 % Find and
Review Copy - Not for Redistribution
count connected components
2 img_1_bw = imread (’bean_coffee_08_bw.png’);
3 figure, imshow(img_1_bw), title(’Input’)
4 CC = bwconncomp(img_1_bw);
5 disp([’Number of connected components: ’ num2str(CC.NumObjects)])
6 img_1_bw(CC.PixelIdxList{7}) = 0;
7 figure, imshow(img_1_bw), title(’Excluding connected component 7’)

Typing CC in the Command Window to inspect its content, you should get:

CC =
struct with fields:
Connectivity: 8
ImageSize: [500 500]
NumObjects: 18
PixelIdxList: {1x18 cell}

To illustrate how to access the PixelIdxList field, the code in line 6 of


Listing 23.2 excludes an arbitrarily chosen connected component (number 7,
in this case) from the image. The result is shown in Figure 23.2.
The conventional method for distinguishing connected components in im-
age processing is commonly known as region labeling. When provided with
a binary image as input, a labeling algorithm generates an output image of
the same dimensions, assigning a consistent label to each pixel belonging
to a connected component. The labels are numbered from 1 to 𝑛, where 𝑛
represents the total count of connected components.

FIGURE 23.2
Excluding a connected component from the binary image. (a) Original binarized image; (b) out-
put image, with one of the 18 connected components removed.
162 Image processing recipes in MATLAB®

We can use MATLAB functions labelmatrix and label2rgb to visu-


alize the connected components and their respective identifiers (labels) in an
image, as demonstrated in Listing 23.3.
Copyright Material - Provided by Taylor & Francis
LISTING 23.3
Review
Labeling connected Copy
- Not for Redistribution
components.
1 % Label connnected components
2 img_1_bw = imread (’bean_coffee_08_bw.png’);
3 figure, imshow(img_1_bw), title(’Input’)
4 CC = bwconncomp(img_1_bw);
5 disp([’Number of connected components: ’ num2str(CC.NumObjects)])
6 img_1_bw_L = labelmatrix(CC);
7 figure, h = imshow(img_1_bw_L, []); title(’Labeled connected comp.’)
8 img_1_bw_L_rgb = label2rgb(img_1_bw_L);
9 figure, imshow(img_1_bw_L_rgb), title(’Labeled connected comp. pseudoc.’)

The labelmatrix function takes the CC structure as input and labels ev-
ery pixel of each connected component with its respective index, obtained
from the cell array stored in the PixelIdxList field. With label2rgb,
it is possible to pseudocolor the labeled image for clearer visualization
(Figure 23.3).
Moreover, we can inspect the labels into the image with Pixel Region tool,
typing:
impixelregion(h)

Figure 23.4 illustrates the process. It shows the pixel values (i.e., the labels
for each connected component) for the first and last connected components.
Figure 23.5 shows all connected components with their respective labels (in
red text) overlaid.
When connected components touch the borders of the image, they might
represent incomplete objects of interest in the original image. If necessary, you
can exclude them from the image using the imclearborder function, as
shown in the code in Listing 23.4, whose results appear in Figure 23.6.

FIGURE 23.3
Connected components with their respective labels. (a) Input image; (b) grayscale image con-
taining the labels; and (c) the final, pseudocolored version.
Recipe 23: Finding, counting, and accessing connected components 163

Copyright Material - Provided by Taylor & Francis


Review Copy - Not for Redistribution

FIGURE 23.4
Inspecting labels using the impixelregion function: (a) and (c) first connected component
(labeled as 1); (b) and (d) last connected component (labeled as 18).

FIGURE 23.5
Labeled connected components.

The number of connected components for the original image, its central
patch – extracted from the input image with the upper left corner at coordi-
nates 𝑥 and 𝑦 = 100 and the bottom right corner at coordinates 𝑥 and 𝑦 = 400
164 Image processing recipes in MATLAB®

Copyright Material - Provided by Taylor & Francis


Review Copy - Not for Redistribution

FIGURE 23.6
Excluding connected components linked to the borders of the image. (a) Original image; (b)
central patch; (c) result of applying imclearborder to (b).

– and the same central patch after applying the imclearborder function
are shown below:

Number of con. comp. Original: 18


Number of con. comp. Central patch: 14
Number of con. comp. Central patch borders cleared: 6

LISTING 23.4
Using imclearborder to remove connected components that touch the borders of the image.
1 % Clear borders
2 img_1_bw = imread (’bean_coffee_08_bw.png’);
3 CC = bwconncomp(img_1_bw);
4 figure, imshow(img_1_bw), title(’Original’)
5
6 img_1_bw_p = img_1_bw(100:400,100:400);
7 CCp = bwconncomp(img_1_bw_p);
8 figure, imshow(img_1_bw_p), title(’Central patch’)
9 img_1_bw_pc = imclearborder(img_1_bw_p);
10 figure, imshow(img_1_bw_pc), title(’Central patch borders cleared’)
11 CCpc = bwconncomp(img_1_bw_pc);
12
13 disp([’Number of con. comp. Original: ’ num2str(CC.NumObjects)])
14 disp([’Number of con. comp. Central patch: ’ num2str(CCp.NumObjects)])
15 disp([’Number of con. comp. Central patch borders cleared: ’...
16 num2str(CCpc.NumObjects)])

Discussion (Recipe notes)


In this recipe, you learned how to locate, enumerate, and analyze individ-
ual connected components in a binary image for subsequent processing or
modification.
Recipe 23: Finding, counting, and accessing connected components 165

In our examples, the bwconncomp function outputs the connected com-


ponents as sets of linear coordinates rather than an image representation.
To generate an image with labeled components, the labelmatrix function
can be utilized inMaterial
Copyright conjunction with bwconncomp.
- Provided by Taylor &Additionally,
Francis for im-
proved visualization of the connected components,
Review Copy - Not for Redistribution label2rgb can assign
pseudocolors to the labeled image.
In MATLAB, you can also perform connected component labeling using
the bwlabel function, which accepts the binary image as an input. However,
in this recipe, we adopted labelmatrix due to its lower memory usage.

Learn more about it


Useful MATLAB functions
Type the function name in the search field at www.mathworks.com/help/
matlab/
· bwconncomp · bwlabel · imclearborder · impixelregion ·
label2rgb · labelmatrix ·

MATLAB documentation, demos, and examples


• Label and Measure Connected Components in a Binary Image
www.mathworks.com/help/images/label-and-measure-objects-in-a-
binary-image.html
• Pixel Connectivity
www.mathworks.com/help/images/pixel-connectivity.html
24
Recipe 24: Material
Copyright Basic morphological operations
- Provided by Taylor & Francis
Review Copy - Not for Redistribution

This recipe teaches you the definitions and interpretations of morphological


operations such as dilation, erosion, opening, closing for binary images, along
with methods for their application.

The term morphology is related to the form and structure of objects. In bi-
nary image processing, morphological operations can be used to analyze and
manipulate the shapes of connected components (objects).
The idea is to probe every image pixel using a well-defined structuring el-
ement (SE), in which the morphological operation is applied. The shape and
size of the SE can present any desired configuration, depending on the ap-
plication, although regular shapes such as squares, diamonds, and disks are
the most employed. The reference point of the SE is referred to origin, cen-
tral point, or center. However, since it does not necessarily correspond to the
geometric center, we adopt the term hot spot here.
When applying morphological operations to binary images, it is important
to remember that – by convention – pixels belonging to foreground objects
will have a value of 1 (white pixels), whereas background pixels will have
a value of 0 (black pixels). The same convention is adopted for the SE, i.e.,
an active point has a value of 1, and an inactive (irrelevant, or “don’t care”)
point has a value of 0.
The mathematical formulation of morphological operations predomi-
nantly uses set notation and can be found in various references, such as [18,
12]. For the sake of simplicity, in this recipe, we will present the definition of
operations in a textual format.

You will need (Ingredients)


• MATLAB R2006a or later
• MATLAB Image Processing Toolbox (IPT) version R2006a or later
• Image files: 18x12_bw_1.png, 250x250_bw_3cb.png,
250x250_bw_3cb2_n.png

166 DOI: 10.1201/9781003170198-31


Recipe 24: Basic morphological operations 167

Steps (Preparation)
Copyright
Morphological Materialin-MATLAB
operations Provided by Taylor
usually follow&these
Francis
steps:
Review Copy - Not for Redistribution
1. Load a binary input image, or binarize it using one of the techniques
in Part VI.
2. Specify desired SE to actuate on the morphological operation(s) using
strel.
3. Apply desired morphological operation(s) to the image using imdi-
late, imerode, imopen, imclose.
4. Display before and after images and interpret the results.

1: Primary operations: dilation and erosion


Dilation and erosion, as their names suggest, aim to add pixels to an image
and remove pixels from an image, respectively. The SE’s size, geometry, and
position of the hot spot establish the effect of the dilation and erosion over
the input image.
To define the operations, let’s say that a binary input image 𝐴 is submitted
to dilation 𝐷 or erosion 𝐸 by a SE 𝑠𝑒. The output image 𝐷 or 𝐸 is initialized
with 𝐴. The SE 𝑠𝑒 visits every pixel of input 𝐴, and every time that the hot
spot ℎ𝑠 of 𝑠𝑒 finds a 1 (white) pixel:

• Dilation of 𝐴 by 𝑠𝑒, denoted by 𝐷 = 𝐴 ⊕ 𝑠𝑒: the entire 𝑠𝑒 is inserted in


output image 𝐷.
• Erosion of 𝐴 by 𝑠𝑒, denoted by 𝐸 = 𝐴 ⊖ 𝑠𝑒: the ℎ𝑠 of 𝑠𝑒 is eliminated
from output image 𝐸 if 𝑠𝑒 does not fit entirely into the object.

Figure 24.1 presents examples of dilation (part (a)) and erosion (part (b))
applied to a simple 18×12 binary image 𝐴, for easier inspection of the oper-
ations for every pixel. The SE 𝑠𝑒 is an “inverted T”, with hot spot ℎ𝑠 at the
horizontal line’s center (identified by a black circle). The bold line at the out-
puts 𝐷 and 𝐸 represents the boundary of the connected components (objects)
in the original image 𝐴.
In MATLAB, these morphological operations are implemented using
imdilate and imerode. They work in association with the strel function,
which creates the SE (an object of class strel).
Listing 24.11 reproduces the examples in Figure 24.1. To specify the arbi-
trary “inverted T” SE, the function strel receives a matrix with 1’s and 0’s
(variable nhood), where 1 is an active, and 0 is an inactive (irrelevant, or
“don’t care”) point of the SE. To position the hot spot of the SE at the center
of the horizontal line of the “inverted T,” an additional row of 0’s is required
168 Image processing recipes in MATLAB®

Copyright Material - Provided by Taylor & Francis


Review Copy - Not for Redistribution

FIGURE 24.1
Example of morphological operations: (a) dilation of image 𝐴 by structuring element 𝑠𝑒 result-
ing in image 𝐷; (b) erosion of image 𝐴 by the same SE resulting in image 𝐸.

(line 8) because the hot spot’s position is given by floor((size(nhood)


+ 1)/2), such that for a 𝑀 × 𝑁 matrix, with 𝑀 and 𝑁 odd numbers, the
hot spot’s position matches the geometrical center. Thus, for a 3 × 3 matrix
nhood, the hot spot lies at position (2, 2). Lines 10 to 12 of the code were
included to illustrate the position of the hot spot with a black circle.

LISTING 24.1
Morphological dilation and erosion.
1 % Morphological Dilation and Erosion
2 bw = imread(’18x12_bw_1.png’);
3 figure, subplot(4,1,1)
4 imshow(bw), title(’Input’)
5 h_bw = impixelregion; set(h_bw,’Name’,’Input’)
6 nhood = [ 0 1 0 % structuring element specification
7 1 1 1
8 0 0 0];
9 se = strel(’arbitrary’,nhood); % create the structuring element
10 se_hs = floor((size(nhood)+1)/2);
11 subplot(4,1,2), imshow(se.Neighborhood), title(’SE’)
12 hold on, plot(se_hs(1),se_hs(2),’ko’,’MarkerFaceColor’,’k’), hold off
13 % Dilation
14 bw_d = imdilate(bw,se);
15 subplot(4,1,3), imshow(bw_d), title(’Dilation’)
16 h_bw_d = impixelregion; set(h_bw_d,’Name’,’Dilation’)
17 % Erosion
18 bw_e = imerode(bw,se);
19 subplot(4,1,4), imshow(bw_e), title(’Erosion’)
20 h_bw_e = impixelregion; set(h_bw_e,’Name’,’Erosion’)
Recipe 24: Basic morphological operations 169

Copyright Material - Provided by Taylor & Francis


Review Copy - Not for Redistribution

FIGURE 24.2
Example of morphological operations (a) opening of image 𝐴 by SE 𝑠𝑒 resulting in image 𝑂;
(b) closing of image 𝐴 by the same SE 𝑠𝑒 resulting in image 𝐶.

2: Compound operations: opening and closing


Opening and closing are compound morphological operations derived from
simple combinations of dilation and erosion and defined as follows:

• Opening of 𝐴 by 𝑠𝑒, denoted by 𝑂 = 𝐴 ∘ 𝑠𝑒: 𝑂 = (𝐴 ⊖ 𝑠𝑒) ⊕ 𝑠𝑒, i.e., an


erosion followed by dilation with the same SE, 𝑠𝑒.
• Closing of 𝐴 by 𝑠𝑒, denoted by 𝐶 = 𝐴 • 𝑠𝑒: 𝐶 = (𝐴 ⊕ 𝑠𝑒) ⊖ 𝑠𝑒, i.e., a
dilation followed by erosion with the same SE, 𝑠𝑒.

Figure 24.2 presents examples of opening (part (a)) and closing (part (b))
applied to the same image and using the same SE as in Figure 24.1 (which
illustrates dilation and erosion). To reproduce this example in MATLAB you
can start from the code in Listing 24.1 as a template and use imopen and
imclose instead of imdilate and imerode.
These operations behave as follows: opening breaks narrow connections
and eliminates thin protrusions; closing connects narrow breaks and fills
small holes and gaps in contours. It is worth reinforcing that, to obtain
such effects, the SE’s size must be compatible with the mentioned elements’
(connections, protrusions, breaks, gaps).
The code in Listing 24.2 applies these operations to a 250×250 pixel image
containing binary objects corresponding to coffee beans. The original image
is submitted to opening and closing with a predefined SE of ’disk’ shape,
of radii 𝑟 = 3, 4, and 5. To inspect a SE, after running the code, open the
variable se to view its Neighborhood field, or type se.Neighborhood in
the command line.
170 Image processing recipes in MATLAB®

LISTING 24.2
Morphological opening and closing.
1 % Morphological Opening and Closing
2 bw = imread(’250x250_bw_3cb.png’);
3
Copyright Material - Provided by Taylor & Francis
h(1) = figure;
4 Review
subplot(1,4,1), Copy - title(’Input’)
imshow(bw), Not for Redistribution
5 h(2) = figure;
6 subplot(1,4,1), imshow(bw), title(’Input’)
7 p = 2;
8 for r = 3:5
9 se = strel(’disk’,r); % create pre-defined structuring element
10 bw_o = imopen(bw,se); % opening
11 figure(h(1)), subplot(1,4,p), imshow(bw_o),...
12 title([’Opening r=’,num2str(r)])
13 bw_c = imclose(bw,se); % closing
14 figure(h(2)), subplot(1,4,p), imshow(bw_c),...
15 title([’Closing r=’,num2str(r)])
16 p = p+1;
17 end

Figure 24.3 shows the results for opening and closing, where the leftmost
image is the input (original), and the following are the outputs for 𝑟 = 3,
𝑟 = 4, 𝑟 = 5. In the opening, an SE of 𝑟 = 3 and 𝑟 = 4 could eliminate the
protrusion but not break the narrow connection, whereas an SE with 𝑟 = 5
did. In closing, one can note that five of the six holes were filled with a SE

FIGURE 24.3
Example of morphological operations (a) opening and (b) closing, with a SE of disk shape and
increasing radii of 𝑟 = 3, 𝑟 = 4, 𝑟 = 5.
Recipe 24: Basic morphological operations 171

Copyright Material - Provided by Taylor & Francis


Review Copy - Not for Redistribution

FIGURE 24.4
Example of applying morphological erosion and dilation with SEs of 𝑟 = 3 and 𝑟 = 5.

of 𝑟 ⩾ 3. The larger hole was partially filled with 𝑟 = 3 and 𝑟 = 4, and


completely filled with 𝑟 = 5. The small gap in the top contour was gradually
filled with the radius increasing. Finally, an 𝑟 = 5 connected two objects that
were apart.
Figure 24.4 presents the results of single erosion and dilation operations
with SEs of 𝑟 = 3 and 𝑟 = 5. Compared with opening and closing, which
tend to act on connections, protrusions, breaks, and gaps, one can note that
erosion “retracts” entire objects, whereas dilation “grows” entire objects.

3: Open-close filter
Binary images often contain both the targeted objects of interest and un-
wanted connected components. These superfluous components can arise
due to problems during image capture or the binarization process. Typically
viewed as noise, these undesired elements often require removal. However,
even the desired objects may have flaws, such as tiny holes or irregular edges.
To refine images with these traits, we can apply a sequence of opening and
closing operations. The opening operation effectively eliminates connected
components smaller than the structuring element (SE), while the closing
operation aids in bridging small gaps and smoothing out contours.
This morphological operation is known as open-close filter (a type of mor-
phological filter). The open-close filtering of an input image 𝐴 using a SE 𝑠𝑒,
denoted by 𝑂𝐶, is 𝑂𝐶 = (𝐴 ∘ 𝑠𝑒) • 𝑠𝑒, i.e., an opening followed by closing
with the same SE, 𝑠𝑒.
The code in Listing 24.3 implements open-close filtering applied to a
250×250 pixel input image containing binary regions corresponding to cof-
fee beans over a noisy background due to the presence of dirt particles. The
SE 𝑠𝑒 is a disk with radius 𝑟 = 5. Results are shown in Figure 24.5. During the
opening step, the noisy background was cleaned up, and after closing, small
holes were filled.
172 Image processing recipes in MATLAB®

Copyright Material - Provided by Taylor & Francis


Review Copy - Not for Redistribution

FIGURE 24.5
Open-close filtering using a SE of disk shape with radius 𝑟 = 5. (a) Input image; (b) opening;
(c) subsequent closing.

LISTING 24.3
Morphological open-close filter.
1 % Mophological filtering (open-close filter)
2 bw = imread(’250x250_bw_3cb2_n.png’);
3 se = strel(’disk’, 5);
4 bw_o = imopen(bw,se); % opening of input image
5 bw_o_c = imclose(bw_o,se); % closing of opened image
6 subplot(1,3,1), imshow(bw), title(’Input’)
7 subplot(1,3,2), imshow(bw_o), title(’open(Input)’)
8 subplot(1,3,3), imshow(bw_o_c), title(’close(open(Input))’)

4: Designing your structuring elements


The strel function allows the creation of any desired SE. The user can man-
ually define arbitrary SEs or ask for pre-defined shapes: diamond, octagon,
disk, line, square, rectangle. Figure 24.6 presents examples of these SEs ob-
tained with strel function. The code snippets in the figure show that the
logical matrices containing the SEs per se are stored in se.Neighborhood.

5: Going further with bwmorph


Several other morphological processing techniques can be found in MAT-
LAB. Listing 24.4 shows how to use the bwmorph function to implement
skeletonization, a process that gradually wears away structures until they are
only 1 pixel thick, without breaking them apart, and thickening to thicken
objects without connecting previously unconnected ones.
The imfill function performs region filling and is useful to fill holes into
the objects. The bwperim function extracts the perimeter of objects (boundary).
Figure 24.7 presents the results. The original image (a) is submitted to a hole
filling (b). Then, the filled image (b) is processed with skeletonization (c),
thickening (d), and perimeter extraction (e).
Recipe 24: Basic morphological operations 173

Copyright Material - Provided by Taylor & Francis


Review Copy - Not for Redistribution

FIGURE 24.6
Examples of pre-defined SEs obtained with strel function.

FIGURE 24.7
Morphological techniques obtained with imfill, bwmorph, and bwperim functions. (a) Origi-
nal image; (b) original image with filled holes; (c) skeleton of (b); (d) thickening of (b); (e) perime-
ter extraction of (b).

LISTING 24.4
Additional morphological techniques.
1 % Other morphological techniques
2 bw = imread(’250x250_bw_3cb.png’);
3 subplot(2,4,1), imshow(bw), title(’Original’);
4 bw_f = imfill(bw,’holes’); % holes filling
5 subplot(2,4,2), imshow(bw_f), title(’Filled’);
6 bw_f_s = bwmorph(bw_f,’skel’,Inf); % skeletonization
7 subplot(2,4,6), imshow(bw_f_s), title(’Skeleton of Filled’);
8 bw_f_t = bwmorph(bw_f,’thicken’,5); % thickening
9 subplot(2,4,7), imshow(bw_f_t), title(’Thickening of Filled’);
10 bw_f_p = bwperim(bw_f); % perimeter extracting
11 subplot(2,4,8), imshow(bw_f_p), title(’Perimeter of Filled’);
174 Image processing recipes in MATLAB®

Discussion (Recipe notes)


In thisCopyright
recipe, youMaterial - Provided
learned the definitionsbyand
Taylor
how to& interpret
Francis and apply
the basic (dilation, erosion) and compound (opening,
Review Copy - Not for Redistribution closing) morphological
operations, including the open-close filter.
Morphological image processing is a vast topic with extensive support
in MATLAB. The bwmorph function alone provides many other operations
worth exploring.

Learn more about it


Useful MATLAB functions
Type the function name in the search field at www.mathworks.com/help/
matlab/
· bwmorph · bwperim · imclose · imdilate · imerode · imfill
· imopen · strel ·

MATLAB documentation, demos, and examples


• Binary Morphology in Image Processing (MathWorks Teaching Re-
sources)
www.mathworks.com/matlabcentral/fileexchange/94590-binary-morp
hology-in-image-processing
• Morphological Operations
www.mathworks.com/help/images/morphological-filtering.html
• Structuring Elements
www.mathworks.com/help/images/structuring-elements.html
• Types of Morphological Operations
www.mathworks.com/help/images/morphological-dilation-and-
erosion.html

Note
1 You are encouraged to run the code and confirm the results!
25
Recipe 25: Material
Copyright Computing connected
- Provided by Taylor & Francis
components’ features
Review Copy - Not for Redistribution

This recipe teaches you how to compute region-based features of the con-
nected components in a binary image using the regionprops function
available in the MATLAB Image Processing Toolbox.

When processing binary images, one key concept is the identification and
analysis of “connected components” within those images. At its core, a con-
nected component is a set of neighboring pixels in a binary image that share
the same value, typically ‘1’ or ‘true’, indicating they belong to the same ob-
ject or region. These components provide an essential means to separate and
identify individual objects within an image, especially when multiple objects
are present.
Once connected components are identified, MATLAB’s regionprops
function becomes a valuable tool, allowing users to compute a myriad of
properties associated with each component. These properties range from ba-
sic ones like ’Area’, indicating the number of pixels in the component, to
more complex descriptors like ’Centroid’, specifying the center of mass
of the component, or ’BoundingBox’, which provides the smallest rectan-
gle encompassing the component. Other properties such as ’Perimeter’,
’Eccentricity’, and ’Orientation’ can offer insights into the shape
and orientation of the object. From these properties, one can glean significant
information about the objects.
The following example illustrates the application of connected compo-
nents’ features into the usual “segmentation → feature extraction → classifi-
cation” image processing pipeline. Figure 25.1(a), top part, shows an image
containing three types of balls – toy, American football, and tennis, – on a reg-
ular background. Figure 25.1(a), bottom part, shows the image segmented
with MATLAB’s Image Segmenter App1 . Using the segmented image, we can
extract features from the corresponding connected components and use them
for the subsequent classification of the objects. Figure 25.1(b) shows the val-
ues of the selected features – eccentricity (between 0 and 1) and area (in pixels)
– to discriminate the three categories of sports balls, using elementary rules
as shown in Figure 25.1(c). The eccentricity values can be used to distinguish

DOI: 10.1201/9781003170198-32 175


176 Image processing recipes in MATLAB®

Copyright Material - Provided by Taylor & Francis


Review Copy - Not for Redistribution

FIGURE 25.1
A simple example of the usual pipeline “segmentation → feature extraction → classification”
in image processing. (a) Top: input image with three types of balls – toy, tennis, and American
football, bottom: segmented image; (b) values of eccentricity and area features of the connected
components corresponding to the balls; (c) simple rules based on the selected features to classify
the objects.

spherical balls from the American football one, whereas the area is adequate
to distinguish the toy ball from the tennis ball in the image.
Eccentricity and area are just two of the many features that can be used to
describe the properties of connected components in binary images. In this
recipe, we use MATLAB’s regionprops function to explore many other
region-based features that describe connected components.

You will need (Ingredients)


• MATLAB R2019a or later
• MATLAB Image Processing Toolbox (IPT) version R2019a or later
• Image files: lego_23_c500.png and sball_4_matseglgc3b.png

Steps (Preparation)
1. Load a binary input image, or binarize a color/grayscale image using
one of the techniques in Part VI.
2. Specify desired connected components’ features to be computed.
3. Compute the features using regionprops.
4. Visualize features’ values in table format or superimpose them on the
image.
Recipe 25: Computing connected components’ features 177

Listing 25.1 explores the input/output options of the regionprops func-


tion, using the example image and features of Figure 25.1. The input can be
a binary image, a CC struct (see Recipe 23), or a labeled image, i.e., an image
with the labels of Material
Copyright the connected components
- Provided (see Recipe
by Taylor 23). The output is
& Francis
2
stored in a variable of class table or into an
Review Copy - Not for Redistributionarray of structures. In addition
to eccentricity and area, we also compute the centroid of the objects (line 16)
which will be used to position (line 21) the identifying labels overlaid on the
original image (with “1” over the American football, “2” over the toy ball,
and “3” over the tennis ball). The 𝑛𝑡ℎ element of the array of structures f_s
stores the features of the object 𝑛. Thus, the toy ball’s features, for example,
are accessed with f_s(2).

LISTING 25.1
Experimenting with some of the input/output options of the regionprops function.
1 % Function regionprops input/output
2 bw = imread(’sball_4_matseglgc3b.png’);
3
4 % Input: binary image, Output: table
5 f_t_1 = regionprops(”table”,bw,”Eccentricity”,”Area”);
6 % Input: CC struct, Output: table
7 cc = bwconncomp(bw);
8 f_t_2 = regionprops(”table”,cc,”Eccentricity”,”Area”);
9 % Input: labeled image, Output: table
10 bw_L = labelmatrix(cc);
11 f_t_3 = regionprops(”table”,bw_L,”Eccentricity”,”Area”);
12
13 disp([’Equal outputs: ’ num2str(isequal(f_t_1,f_t_2,f_t_3))])
14
15 % Input: binary image, Output: structure array
16 f_s = regionprops(bw,”Eccentricity”,”Area”,”Centroid”);
17 figure, imshow(bw)
18 % Show the label (number) of the object on the image
19 hold on
20 for n=1:length(f_s) % stats_s(n) is the nth object
21 text(f_s(n).Centroid(1),f_s(n).Centroid(2),num2str(n),’Color’,’red’)
22 end
23 hold off

Typing f_t_1 and f_s in the command window, you can view the outputs
of types table and array of structures, respectively:

f_t_1 =
3×2 table
Area Eccentricity
_____ ____________
29659 0.79528
29807 0.27479
3355 0.29585

f_s =
3×1 struct array with fields:
Area
Centroid
Eccentricity
178 Image processing recipes in MATLAB®

The regionprops function can measure several features from the con-
nected components. It is worth mentioning that MATLAB’s regionprops
documentation employs the term property to refer to a feature, and region
to refer to a connected
Copyright component.
Material - ProvidedIn the
byfollowing
Taylor &sections,
Francisfeatures (in-
put parameter properties for the regionprops
Review Copy - Not for Redistribution function) are grouped
according to different criteria for a better organization.

1: Features of scalar type


In this group of features, we present those that output a scalar (single
number).

• ”Area”: area of the region in pixels.


• ”Circularity”: or roundness given by

4𝜋𝐴 0.5 2
𝐶= (1 − )
𝑃2 𝑟
where 𝐴 is the area, 𝑃 the perimeter and 𝑟 = 𝑃 /2𝜋 + 0.5.
• ”ConvexArea”: area in pixels of ”ConvexImage” feature3 .
• ”Eccentricity”: or elongation, defined by 𝐸 = ratio of the distance
between the foci of an ellipse (that has the same second moments as
the region) and its major axis length. 0 ⩽ 𝐸 ⩽ 1; 𝐸 = 0 for a circle;
𝐸 = 1 for a line.
• ”EquivDiameter”: diameter of a circle that has the same area as the
region, calculated by
4𝐴
𝐸𝑑 = √
𝜋
• ”EulerNumber”: 1 minus the number of holes within the region.
• ”Extent”: quotient between the region’s area and its bounding box’s
area.
• ”FilledArea”: area in pixels of ”FilledImage” feature3 .
• ”MajorAxisLength”: major axis length of an ellipse, where the
ellipse’s normalized second central moments is the same as the region4 .
• ”MinorAxisLength”: minor axis length of an ellipse, where the
ellipse’s normalized second central moments is the same as the region5 .
• ”Orientation”: angle 𝜃 in degrees (−90∘ ⩽ 𝜃 ⩽ 90∘ ) between the
horizontal axis (0∘ ) and the major axis of an ellipse, where the ellipse’s
second moments is the same as the region6 . By convention, positive
angles indicate counterclockwise orientation, whereas negative angles
mean clockwise.
• ”Perimeter”: length of the outer border of the region.
Recipe 25: Computing connected components’ features 179

• ”Solidity”: quotient between the region’s area and its convex hull’s7
area, obtained by ”Area”/”ConvexArea”.

In Listing 25.2 we compute the above features for a color input image
Copyright Material - Provided by Taylor & Francis
containing four Lego pieces.
The imageReview Copyfollowing
is segmented - Not for Redistribution
these steps (lines 4–9):

(i) Color to grayscale conversion (Recipe 26);


(ii) Gaussian filtering with 𝜎 = 2 to smooth internal noise to the ob-
jects, mainly significantly brighter spots due to the illumination, and
eventual noise in the background (Recipe 15);
(iii) Global automatic thresholding using Otsu’s method (Recipe 18);
(iv) Inverting the resulting binary image to produce an output in which
white pixels correspond to objects and black pixels to the background
(Recipe 23).

The resulting binary image with the segmented objects is saved into the
lego_23_c500_bw.png file (line 29).
Figure 25.2 presents the results, including the values of the computed
features.
LISTING 25.2
Computing features of scalar type with regionprops function.
1 % Function regionprops, features of scalar type
2 img = imread(’lego_23_c500.png’);
3
4 % Objects segmentation in 4 steps:
5 img_g = im2gray(img); % 1) convert from color to grayscale
6 % 2) Gaussian sigma=2 to smooth background and internal object’s details
7 img_g_g = imgaussfilt(img_g,2);
8 % 3) Global auto-threshold and 4) invert output
9 img_g_g_bw = ~imbinarize(img_g_g,’global’);
10
11 % Compute features of scalar type (also Centroid to show labels in image)
12 f_t = regionprops(”table”,img_g_g_bw,”Centroid”,...
13 ”Area”,”Circularity”,”ConvexArea”,”Eccentricity”,”EquivDiameter”,...
14 ”EulerNumber”,”Extent”,”FilledArea”,”MajorAxisLength”,...
15 ”MinorAxisLength”,”Orientation”,”Perimeter”,”Solidity”);
16
17 t = tiledlayout(2,2);
18 nexttile, imshow(img), title(’Original’)
19 nexttile, imshow(img_g), title(’Grayscale’)
20 nexttile, imshow(img_g_g), title(’Smoothed’)
21 nexttile, imshow(img_g_g_bw), title(’Segmented’)
22 % Show the label (number) of the object on the image
23 hold on
24 for n=1:size(f_t,1) % n is the nth object
25 text(f_t.Centroid(n,1),f_t.Centroid(n,2),num2str(n),’Color’,’red’)
26 end
27 hold off
28
29 imwrite(img_g_g_bw,’lego_23_c500_bw.png’) % save segmented bw image
180 Image processing recipes in MATLAB®

Copyright Material - Provided by Taylor & Francis


Review Copy - Not for Redistribution

FIGURE 25.2
Segmentation and computation of scalar type features using regionprops function. Images
from left to right, top to bottom: original color image, grayscale image, smoothed image, and
the result of the segmentation with the labels 1 to 4 of each connected component (object). On
the right: the list of features and their corresponding values for each object.

2: Features of coordinate type


In this group of features, we highlight those features that output the co-
ordinates (𝑥, 𝑦), i.e., (col,row) of relevant properties of each connected
component in the image.

• ”BoundingBox”: smallest box that encloses the region. Outputs a vec-


tor [xmin ymin width height], where xmin ymin are the 𝑥 and 𝑦
coordinates of the top left corner, and width height are the lengths
of the box along 𝑥-axis and 𝑦-axis.
• ”Centroid”: center of mass of the region. Outputs a vector [x y],
where x y are the 𝑥 and 𝑦 coordinates of the centroid.
• ”ConvexHull”: smallest convex polygon that encloses the region.
Outputs a 𝑛 × 2 matrix 𝑉 , where 𝑛 is the number of vertices of the
polygon. V(n,1) V(n,2) are the 𝑥 and 𝑦 coordinates of the 𝑛𝑡ℎ
vertex.
• ”Extrema”: a set of coordinates indicating the extrema of the region.
Outputs a 8 × 2 matrix 𝐸, where 8 is the number of possible extrema
(some points may be the same). E(e,1) E(e,2) are the 𝑥 and 𝑦
coordinates of the 𝑒𝑡ℎ extrema, organized as follows: 𝑒 = 1 to 8 –
top-left, top-right, right-top, right-bottom, bottom-right, bottom-left,
left-bottom, left-top.
Recipe 25: Computing connected components’ features 181

Copyright Material - Provided by Taylor & Francis


Review Copy - Not for Redistribution

FIGURE 25.3
Features of coordinate type computed with regionprops function. (a) ”Centroid”;
(b) ”BoundingBox”; (c) ”ConvexHull”; (d) ”Extrema”.

In Listing 25.3 we compute the above features for image lego_23_c500_


bw.png, created in Listing 25.2. Figure 25.3 presents the results superim-
posed on the input image.

LISTING 25.3
Computing features of coordinate type with regionprops function.
1 % Function regionprops, features of coordinate type
2 img = imread(’lego_23_c500_bw.png’);
3

4 % Compute features of coordinate type


5 f_t = regionprops(”table”,img,”Centroid”,...
6 ”BoundingBox”,”ConvexHull”,”Extrema”);
7

8 tiledlayout(2,2)
9 ax1 = nexttile; imshow(img), title(’Centroid’)
10 ax2 = nexttile; imshow(img), title(’BoundingBox’)
11 ax3 = nexttile; imshow(img), title(’ConvexHull’)
12 ax4 = nexttile; imshow(img), title(’Extrema’)
13 % Show Centroid, BoundingBox, ConvexHull, Extrema
14 hold(ax1,’on’), hold(ax2,’on’), hold(ax3,’on’), hold(ax4,’on’)
15 for n=1:size(f_t,1) % n is the nth object
16 plot(ax1,f_t.Centroid(n,1),f_t.Centroid(n,2),...
17 ’r+’,’LineWidth’,1)
18 rectangle(ax2,’Position’,f_t.BoundingBox(n,:),...
19 ’EdgeColor’,’r’,’LineWidth’,1);
20 line(ax3,f_t.ConvexHull{n}(:,1),f_t.ConvexHull{n}(:,2),...
21 ’Color’,’r’,’LineWidth’,1);
22 plot(ax4,f_t.Extrema{n}(:,1),f_t.Extrema{n}(:,2),...
23 ’r+’,’LineWidth’,1)
24 end
25 hold(ax1,’off’), hold(ax2,’off’), hold(ax3,’off’), hold(ax4,’off’)

3: Features of image type


In this group of features, we present those features that output an image
based on the properties of each connected component.
182 Image processing recipes in MATLAB®

Copyright Material - Provided by Taylor & Francis


Review Copy - Not for Redistribution

FIGURE 25.4
Features of image type computed with regionprops function. Left: input image. Right: each
row shows an image type feature of the objects in the input image.

• ”ConvexImage”: smallest convex polygon that encloses the region.


Outputs a binary image of the same size as the region’s bounding box.
• ”FilledImage”: the original region without eventual internal holes.
Outputs a binary image of the same size as the region’s bounding box.
• ”Image”: the original region itself. Outputs a binary image of the same
size as the region’s bounding box.

In Listing 25.4 we compute the above features for image lego_23_c500


_bw.png, created on Listing 25.2. Figure 25.4 presents the results.

LISTING 25.4
Computing features of image type with regionprops function.
1 % Function regionprops, features of image type
2 img_bw = imread(’lego_23_c500_bw.png’);
3 figure, imshow(img_bw)
4
5 % Compute features of image type
6 f_i = regionprops(”table”,img_bw,”ConvexImage”,”FilledImage”,”Image”);
7
8 % Show ConvexImage, FilledImage, Image
9 figure, t = tiledlayout(3,size(f_i,1));
10 t.TileSpacing = ’tight’; t.Padding = ’tight’;
11 for n=1:size(f_i,1) % n is the nth object
12 nexttile, imshow(f_i.ConvexImage{n}),title([’ConvexImage ’ num2str(n)])
13 end
14 for n=1:size(f_i,1) % n is the nth object
15 nexttile, imshow(f_i.FilledImage{n}),title([’FilledImage ’ num2str(n)])
16 end
17 for n=1:size(f_i,1) % n is the nth object
18 nexttile, imshow(f_i.Image{n}),title([’Image ’ num2str(n)])
19 end
Recipe 25: Computing connected components’ features 183

4: Feret features
The maximum and minimum Feret diameters determine an object’s largest
and smallest widths.
Copyright Material - Provided by Taylor & Francis
Review Copy - Notreturns:
• ”MaxFeretProperties” for Redistribution
MaxFeretDiameter
MaxFeretAngle
MaxFeretCoordinates
• ”MinFeretProperties” returns:
MinFeretDiameter
MinFeretAngle
MinFeretCoordinates

These properties contain the following fields:

• Diameter: scalar representing the size of the line segment.


• Angle: in degrees, between the horizontal axis (0∘ ) and the Diame-
ter line segment. A positive angle is clockwise, and a negative is
counterclockwise.
• Coordinates: of the endpoints of the Diameter line segment, in the form
of a 2 × 2 matrix, where each row contains the 𝑥 and 𝑦 coordinates of
an endpoint.

In Listing 25.5 we compute the above features for image lego_23_c500_


bw.png, created on Listing 25.2. Figure 25.5 presents the results superim-
posed on the input image.

LISTING 25.5
Computing Feret features with regionprops function.
1 % Function regionprops, Feret features
2 img = imread(’lego_23_c500_bw.png’);
3
4 % Compute Feret features
5 f_t = regionprops(”table”,img,...
6 ”MaxFeretProperties”,”MinFeretProperties”);
7
8 figure, imshow(img), title(’MaxFeretProperties’), ax1 = gca;
9 figure, imshow(img), title(’MinFeretProperties’), ax2 = gca;
10 % Show MaxFeretProperties, MinFeretProperties
11 hold(ax1,’on’), hold(ax2,’on’)
12 for n=1:size(f_t,1) % n is the nth object
13 imdistline(ax1,f_t.MaxFeretCoordinates{n}(:,1),...
14 f_t.MaxFeretCoordinates{n}(:,2));
15 angle_pos_max = mean(f_t.MaxFeretCoordinates{n})+20;
16 text(ax1,angle_pos_max(1),angle_pos_max(2),...
17 [num2str(round(f_t.MaxFeretAngle(n))) ’^{\circ}’],’Color’,’red’)
18 imdistline(ax2,f_t.MinFeretCoordinates{n}(:,1),...
184 Image processing recipes in MATLAB®

Copyright Material - Provided by Taylor & Francis


Review Copy - Not for Redistribution

FIGURE 25.5
Feret features computed with regionprops function. Values in black are the Diameter and in
red the Angle. (a) ”MaxFeretFeatures”; (b) ”MinFeretFeatures”.

19 f_t.MinFeretCoordinates{n}(:,2));
20 angle_pos_min = mean(f_t.MinFeretCoordinates{n})+20;
21 text(ax2,angle_pos_min(1),angle_pos_min(2),...
22 [num2str(round(f_t.MinFeretAngle(n))) ’^{\circ}’],’Color’,’red’)
23 end
24 hold(ax1,’off’), hold(ax2,’off’)

5: Features from grayscale pixel value


These features use the connected components (regions) as masks to operate
on the pixels of the original (color or grayscale) image.

• ”MaxIntensity”: highest pixel value within the region.


• ”MeanIntensity”: mean value of all pixels within the region.
• ”MinIntensity”: lowest pixel value within the region.
• ”PixelValues”: the pixels within the region, encoded as a 𝑝 × 1
vector, where 𝑝 is the number of pixels.
• ”WeightedCentroid”: center of region based on location and in-
tensity value. Outputs a vector [x y], where x y are the 𝑥 and 𝑦
coordinates of the weighted centroid.

In Listing 25.6 we compute the above features for image lego_23_c500


_bw.png, created on Listing 25.2. Note that, besides the binary image, the
grayscale is also an input parameter of regionprops function. Figure 25.6
presents the results.
Recipe 25: Computing connected components’ features 185

Copyright Material - Provided by Taylor & Francis


Review Copy - Not for Redistribution

FIGURE 25.6
Features from grayscale pixel value computed with regionprops function. From left to right:
the boundaries of the binary regions are superimposed on the grayscale image; ”Weighted-
Centroid” feature; list of the other grayscale pixel value features and the corresponding values
for each region.

LISTING 25.6
Computing features from grayscale pixel value with regionprops function.
1 % Function regionprops, features of grayscale pixel type
2 img = imread(’lego_23_c500.png’);
3 img_bw = imread(’lego_23_c500_bw.png’);
4 img_g = im2gray(img);
5 img_bw_b = boundarymask (img_bw);
6 img_g_r = imoverlay(img_g,img_bw_b);
7

8 % Compute features of grayscale pixel type


9 f_t = regionprops(”table”,img_bw,img_g,...
10 ”MaxIntensity”,”MeanIntensity”,”MinIntensity”,...
11 ”PixelValues”,”WeightedCentroid”);
12

13 tiledlayout(1,2)
14 nexttile; imshow(img_g_r), title(’Grayscale pixels’)
15 nexttile; imshow(img_bw), title(’WeightedCentroid’)
16 % Show WeightedCentroid
17 hold on
18 for n=1:size(f_t,1) % n is the nth object
19 plot(f_t.WeightedCentroid(n,1),f_t.WeightedCentroid(n,2),...
20 ’r+’,’LineWidth’,1)
21 text(f_t.WeightedCentroid(n,1)+10,...
22 f_t.WeightedCentroid(n,2)+10,num2str(n),’Color’,’red’)
23 end
24 hold off

Discussion (Recipe notes)


In this recipe, you learned how to extract and visualize several region-based
features from connected components into an image.
To access the connected components, besides the features of image type
presented here, you can also use ”PixelIdxList” (see Recipe 23), ”Pix-
elList”, and ”SubarrayIdx”.
186 Image processing recipes in MATLAB®

Most of our example code in this recipe uses MATLAB’s table class
(data type) to store the features, but you can also use an array of structures
according to your preference, as we did in line 21 of Listing 25.1.
Features of scalar
Copyright type can- also
Material be extracted
Provided and visualized
by Taylor & Francis interactively
using the Image Region Analyzer App.
Review Copy - Not for Redistribution
In the Learn more about it section you can find additional useful resources
to deal with connected components, including the bwpropfilt function to
facilitate the extraction of objects based on its features, and the bwbound-
aries and bwtraceboundary functions, which trace the boundaries of the
objects.

Learn more about it


Useful MATLAB functions
Type the function name in the search field at www.mathworks.com/help/
matlab/
· compactitem · bwboundaries · bwconncomp · bwconvhull · bweuler
· bwferet · bwperim · bwpropfilt · bwselect · bwtraceboundary
· grayconnected · imdistline · line · rectangle · roipoly
· roicolor · table · text ·

MATLAB documentation, demos, and examples


• Classify Pixels That Are Partially Enclosed by ROI
www.mathworks.com/help/images/classify-pixels-that-are-partiall
y-enclosed-by-roi.html
• Create ROI Shapes
www.mathworks.com/help/images/roi-creation-overview.html
• Detect and Measure Circular Objects in an Image
www.mathworks.com/help/images/detect-and-measure-circular-obje
cts-in-an-image.html
• Identifying Round Objects
www.mathworks.com/help/images/identifying-round-objects.html
• Image Region Analyzer
www.mathworks.com/help/images/ref/imageregionanalyzer-app.html
• Measuring Regions in Grayscale Images
www.mathworks.com/help/images/measuring-regions-in-grayscale-
images.html
• Region and Image Properties
www.mathworks.com/help/images/pixel-values-and-image-statistics
.html
Recipe 25: Computing connected components’ features 187

Other sources
• Feret Diameter: Introduction
https://blogs.mathworks.com/steve/2017/09/29/feret- diameter-
Copyright Material - Provided by Taylor & Francis
introduction/
Review Copy
• Feret Properties - NotUp
– Wrapping for Redistribution
https://blogs.mathworks.com/steve/2018/04/17/feret-properties-
wrapping-up/
• Visualizing regionprops Ellipse Measurements
https://blogs.mathworks.com/steve/2010/07/30/visualizing-region
props-ellipse-measurements/

Notes
1 We used the Local Graph Cut method with a rectangular region of interest (ROI) that
embraces the three objects, and Subregion Density parameter at maximum (slider at
the rightest position). Except for some imperfections in the contours, especially for
the toy ball, which is particularly difficult to segment due to many visual details,
the results are quite good.
2 The table format is particularly recommended for visualization in the MATLAB
Command Window.
3 See Section 3. Features of image type.
4 See also the MaxFeretDiameter field in ”MaxFeretProperties”.
5 See also the MinFeretDiameter field in ”MinFeretProperties”.
6 See also the MaxFeretAngle field in ”MaxFeretProperties”.
7 See Section 2. Features of coordinate type.
Copyright Material - Provided by Taylor & Francis
Review Copy - Not for Redistribution
Copyright Material - Provided by Taylor & Francis
Review Copy - Not for Redistribution

Part VIII

Color image processing

Part VIII – Color image processing


This Part is dedicated to some aspects of image processing that are specific to
color images, such as the different ways by which color image contents can be
encoded, as well as the differences between full-color image processing and
pseudocolor image processing – and the practical usefulness of the latter.

Recipe 26 demonstrates the main characteristics of the color spaces avail-


able in MATLAB and teaches you how to perform conversions among
them.
Recipe 27 shows how to adjust color images to make them more inter-
pretable or visually pleasant, using different color spaces.
Recipe 28 teaches you how and when to use pseudocolor image processing
techniques.

DOI: 10.1201/9781003170198-33 189


Copyright Material - Provided by Taylor & Francis
Review Copy - Not for Redistribution
26
Recipe 26: Material
Copyright Converting among
- Provided different
by Taylor & Francis
color spaces
Review Copy - Not for Redistribution

This recipe teaches the main characteristics of the color spaces available in
MATLAB and how to perform conversions among them.

Color perception is a psychophysiological experience that only occurs in


systems equipped with sensors capable of differentiating wavelengths in
the visible spectrum. In terms of human vision, these sensors are the cone
photoreceptors found in the retina.
There are three types of cone cells, with peaks of responsiveness in the L
(long), M (medium), and S (short) wavelengths, corresponding to what we
call primary colors of light, i.e., red, green, and blue, respectively.
A large study with subjects concluded in 1931 was conducted by the CIE
(Commission Internationale de l’Eclairage) [2] to obtain the amount of L, M, and
S wavelengths required to be perceived as particular color by human ob-
servers. That study was the basis for creating the CIE XYZ (also known as
CIE 1931) color space.
Manufacturers of color image acquisition equipment (like cameras and
scanners) and reproduction devices (such as displays and printers) utilize
a standardized conversion from the CIE XYZ color space to sRGB. This stan-
dardization facilitates dealing with the familiar R, G, and B components in
both hardware and software settings. “Several standard image formats, in-
cluding JPEG and PNG are based on sRGB color data, which makes sRGB
the de facto standard for digital still cameras, color printers, and other imag-
ing devices at the customer level […] Thus, in practice, working with any
RGB color data almost always means dealing with sRGB [10].”
In addition to CIE XYZ and RGB, several other color spaces have been
designed to be more convenient and efficient for various applications. This
recipe introduces different color spaces and their conversions, supported by
MATLAB and its Image Processing Toolbox (IPT).

DOI: 10.1201/9781003170198-34 191


192 Image processing recipes in MATLAB®

You will need (Ingredients)


• Copyright Material
MATLAB R2020b - Provided by Taylor & Francis
or later
Review Copy - NotToolbox
• MATLAB Image Processing for Redistribution
(IPT) version R2020b or later
• Image files: allColors32k.png, fabric_pp_09.png

Steps (Preparation)
1. Load an image file, a multidimensional array with the image in a
specific color space, or create synthetic images directly specifying the
pixels’ values in the desired color space.
2. Check Figure 26.1 to see if a MATLAB function for direct conversion
is available. Otherwise, use an intermediate conversion between the
input and the desired output color space.
3. Apply the conversion using the selected dedicated functions or color
transformation structures.
4. (OPTIONAL) Inspect the numerical values of the converted pixels and
display the individual color channels in the form of intensity images1 .

Figure 26.1 presents the color spaces and conversions supported by MAT-
LAB and the IPT. Conversions can be performed with either a dedicated
function or a color transformation structure. A dedicated function follows the
name format in2out, where in is the input (original) color space, and out
is the output (converted) color space. A color transformation structure can
be used to store the parameters for color conversion. Color transformation
structures are created with the makecform function and applied with the
applycform function. The main syntax of makecform requires the type of
conversion specified by character vectors following the same name format
’in2out’ of a dedicated function.
Most conversions are obtained exclusively with either dedicated func-
tions or color transformation structures. The following conversions can be
done using both types of methods, but dedicated functions are preferred: in-
stead of ’lab2srgb’ and ’srgb2lab’, you should prefer lab2rgb and
rgb2lab; instead of ’xyz2srgb’ and ’srgb2xyz’, you should prefer
xyz2rgb and rgb2xyz; instead of ’lab2xyz’ and ’xyz2lab’, you should
prefer lab2xyz and xyz2lab.
Recipe 26: Converting among different color spaces 193

Copyright Material - Provided by Taylor & Francis


Review Copy - Not for Redistribution

FIGURE 26.1
Color spaces and conversions supported by MATLAB’s IPT. in2out is the name of a conversion-
dedicated function. ’in2out’ (with single quotes) is the character vector that identifies the
conversion, to be used as an input parameter of makecform function.

The color spaces in Figure 26.1 are briefly explained next.

1: CIE XYZ and CIE chromaticity diagrams colors spaces


The CIE XYZ color space was designed to map the relationship between the
physical wavelengths of visible light to the corresponding perceived colors
by the human visual system. The traditional horseshoe-shaped CIE chro-
maticity diagrams, which aim to present all visible colors, are directly derived
from CIE XYZ.

• CIE XYZ (xyz): Also known as CIE 1931 or XYZ. Y represents the
luminance (achromatic information). CIE XYZ was obtained from an
extensive experiment with volunteers to standardize the color percep-
tion of an average human observer. CIE XYZ is considered the root of
all tristimulus color spaces.
• xyY (xyl): Results from a procedure that normalizes the XYZ val-
ues in order to describe the chromaticity of color in terms of x and y.
𝑥 = 𝑋/(𝑋 + 𝑌 + 𝑍); 𝑦 = 𝑌 /(𝑋 + 𝑌 + 𝑍); Y is the luminance of the
CIE XYZ color space. Typically used to obtain the well-known CIE x,y
chromaticity diagram, presenting all “pure” visible colors, i.e., with no
luminance information.
194 Image processing recipes in MATLAB®

0.8 0.6

0.5
0.6
Copyright Material - Provided by Taylor & Francis
0.4
Review Copy - Not for Redistribution

v'
y

0.4 0.3

0.2
0.2
0.1

0 0
0 0.2 0.4 0.6 0.8 0 0.2 0.4 0.6
x u'

FIGURE 26.2
Chromaticity diagrams obtained with plotChromaticity function, presenting the visible
spectrum inside the horseshoe-shaped contour. (a) CIE x,y chromaticity diagram. (b) CIE u’,v’
chromaticity diagram.

• uvY2 (uvl): The 𝑢 and 𝑣 components, also known as CIE 1960 𝑢 and
𝑣, aim to deliver a more perceptually uniform3 chromaticity diagram
than the one obtained with 𝑥 and 𝑦 [22]. Y is the luminance of the CIE
XYZ color space.
• u’v’Y2 (upvpvl): The 𝑢′ and 𝑣′ components, also known as CIE 1976
𝑢′ and 𝑣′ , are related to 𝑢 and 𝑣 as 𝑢′ = 𝑢 and 𝑣′ = 1.5𝑣 [22, 13],
and provide an improved chromaticity diagram in terms of percep-
tual uniformity, if compared to that obtained with 𝑢 and 𝑣 [20]. Y is
the luminance of the CIE XYZ color space.

The plotChromaticity function generates CIE chromaticity diagrams.


For a diagram based on x and y components, use plotChromaticity
(no input is required); and for a diagram based on u’ and v’ components,
use plotChromaticity(”ColorSpace”,”uv”). Results are shown in
Figure 26.2.

2: RGB color spaces


RGB color spaces are primarily designed for hardware-oriented purposes,
serving as the internal color representation used by electronic devices such
as monitors and displays. It may not be intuitive for users to specify colors
in the RGB space.
Recipe 26: Converting among different color spaces 195

Blue Cyan
Copyright Material - Provided by Taylor & Francis
Magenta
Review Copy - Not for Redistribution
White

Black G
Green

R Red Yellow

FIGURE 26.3
(a) The RGB cube and the locations of the primary colors, secondary colors, black, white, and
gray levels. (b) Color cloud into the RGB color space of a synthetic image containing 32,768
equally spaced colors.

• RGB (rgb): Represented by a cube with red (R), green (G), and blue
(B) axes. Figure 26.3(a) shows the RGB cube and the locations of the
primary (red, green, blue) colors, secondary colors (mixture of two pri-
maries at a time), black, white, and gray levels. The gray levels lie on
the main diagonal of the cube, i.e., they are encoded with equal values
of R, G, and B. Figure 26.3(b) shows the distribution of 32,768 equally
spaced colors from the synthetic image allColors32k.png, in the
RGB color cube, using a color cloud. To reproduce the Figure, load the
image into a variable, for instance, img, and ask for the color cloud
using colorcloud(img,’rgb’).
• sRGB (srgb): Standard RGB. sRGB has become the widely accepted
standard for various imaging devices, such as digital still cameras and
displays, as evidenced by its adoption in popular image formats like
JPEG and PNG. Consequently, handling RGB color data generated by
most devices means we are working with sRGB [10].
• Wide-gamut RGB (rgbwide): the color gamut of a display represents
the full range of colors it can accurately reproduce. Advancements in
display technology aim to expand this gamut, among other improve-
ments [8]. Color spaces such as wide-gamut RGB are designed to meet
this growing demand.
196 Image processing recipes in MATLAB®

Green V Yellow

H Red
Cyan
Copyright Material
White - Provided
S by Taylor & Francis
Review Copy
Blue - Not for Redistribution
Magenta

Black

FIGURE 26.4
(a) The HSV inverted cone and the locations of the primary colors, secondary colors, black, white,
and gray levels. (b) Color cloud into the HSV color space of a synthetic image containing 32,768
equally spaced colors.

3: Hue-oriented color spaces


In contrast to hardware-oriented color spaces, hue-oriented ones are pri-
marily designed for intuitive specification and selection of colors. MATLAB
adopts the HSV (hue, saturation, value) color space4 .

• HSV (hsv): Represented by an inverted cone, where each dimension


is an intuitive attribute of color. Figure 26.4(a) shows the HSV inverted
cone and the locations of the primary colors, secondary colors, black,
white, and gray levels5 .
Hue (H) – represents the “type” of color, often associated with the
concept of “basic color” such as blue, red, or yellow. Related to the
physical quantity dominant wavelength. In the HSV inverted cone, the
H dimension is the angle.
Saturation (S) – this characteristic is linked to the “purity” or “vivid-
ness” of a color. Higher saturation means greater purity and vividness
while reducing saturation creates the impression of diluting the color
with white. In the HSV inverted cone, the S dimension is the distance
from the center to the edge (horizontal distance from the V axis).
Value (V) – the achromatic component of the color. Related to the per-
ceptual quantity brightness. In the HSV inverted cone, V is the height.
A V value of 0 represents black, regardless of H and S. If S is set to
0, ranging V from 0 to 100%, we have gray levels, no matter the H.
Figure 26.4(b) shows the distribution of 32,768 equally spaced colors
Recipe 26: Converting among different color spaces 197

from the synthetic image allColors32k.png, in the HSV inverted


cone, using a color cloud.

Copyright
In Listing Material
26.1, we - Provided
generate byand
the primary Taylor & Francis
secondary colors of light, as
well as blackReview Copy
and white, - Notcoordinates
in RGB for Redistribution
and perform a conversion to
HSV. Primary and secondary colors follow the sequence defined by the HSV
inverted cone in Figure 26.4, as the H angle increases from 0∘ : Red, Yellow,
Green, Cyan, Blue, Magenta.

LISTING 26.1
Converting from RGB to HSV color space.
1 % RGB to HSV color space conversion
2 % Specify in RGB: Black, Red, Yellow, Green, Cyan, Blue, Magenta, White
3 % K R Y G C B M W
4 r = [0 1 1 0 0 0 1 1];
5 g = [0 0 1 1 1 0 0 1];
6 b = [0 0 0 0 1 1 1 1];
7
8 img_rgb = cat(3, r, g, b); % 1x8 image with the specified colors
9 imshow(img_rgb,’InitialMagnification’,’fit’);
10
11 img_hsv = rgb2hsv(img_rgb); % convert image from RGB to HSV
12
13 % Build a table to inspect colors’ values in RGB and HSV
14 values = [reshape(img_rgb,[],3) reshape(img_hsv,[],3)];
15 T = array2table(values);
16 T.Properties.VariableNames = {’R’,’G’,’B’,’H’,’S’,’V’};
17 T.Properties.RowNames = {’Black’,’Red’,’Yellow’,’Green’,...
18 ’Cyan’,’Blue’,’Magenta’,’White’};
19 disp(T)

The resulting table T is presented next. In MATLAB, H, S, and V are


represented with normalized values (range 0 to 1). Thus, the values for
H are Red=0, Yellow= 16 =0.167, Green= 26 =0.333, Cyan= 36 =0.5, Blue= 46 =0.667,
Magenta= 56 =0.833. Since the primary and secondary colors are positioned at
the edge of the top (the “lid”6 ) of the inverted cone, S and V are equal to 1.

R G B H S V
_ _ _ _______ _ _
Black 0 0 0 0 0 0
Red 1 0 0 0 1 1
Yellow 1 1 0 0.16667 1 1
Green 0 1 0 0.33333 1 1
Cyan 0 1 1 0.5 1 1
Blue 0 0 1 0.66667 1 1
Magenta 1 0 1 0.83333 1 1
White 1 1 1 0 0 1

To exemplify color specifications in HSV, we will obtain an orange, a pink,


and a brown tone, derived from their intuitive descriptions:
198 Image processing recipes in MATLAB®

FIGURECopyright
26.5 Material - Provided by Taylor & Francis
1
Color specification in HSV. Orange: 𝐻 = 12 , 𝑆 = 1, 𝑉 = 1; pink: 𝐻 = 0, 𝑆 = 0.5, 𝑉 = 1;
1 Review Copy - Not for Redistribution
brown: 𝐻 = 12 , 𝑆 = 1, 𝑉 = 0.5.

Orange can be described as located between red and yellow. Given that
𝐻 = 0 is red and 𝐻 = 16 is yellow, 𝐻 = 12 1
, with 100% of S and V
(𝑆 = 1, 𝑉 = 1) produce a tone of orange.
Pink can be described as a whitish red. Given that reducing saturation
creates the impression of diluting the color with white, a red (𝐻 = 0)
with S of 50% (𝑆 = 0.5) and value of 100% (𝑉 = 1) produces a tone of
pink.
Brown can be described as a dark orange. Given that the V dimension
1
represents the achromatic component of the color, an orange (𝐻 = 12 ,
𝑆 = 1) with 50% of V (𝑉 = 0.5) produces a tone of brown.

Listing 26.2 implements these specified tones of orange, pink, and brown.
An HSV to RGB conversion is applied since imshow requires images in RGB.
Figure 26.5 shows the result.

LISTING 26.2
Specifying colors in HSV.
1 % Color specification in HSV
2 % Specify in HSV: Orange, Pink, Brown
3 % O P Br
4 h = [1/12 0 1/12];
5 s = [ 1 0.5 1 ];
6 v = [ 1 1 0.5];
7

8 img_hsv = cat(3, h, s, v); % 1x3 image with the specified colors


9 img_rgb = hsv2rgb(img_hsv); % convert from HSV to RGB
10 imshow(img_rgb,’InitialMagnification’,’fit’);

4: Perceptually uniform color spaces


Perceptually uniform color spaces are designed to reproduce the human
perception of differences between colors. Thus, equal distances in the color
space are reasonable approximations of equal perceived differences in color
appearance.

• L*a*b* (lab): Also known as CIE LAB or CIE 1976 L*a*b*. L* rep-
resents the luminance (achromatic), a* represents the magenta-green,
and b* represents the yellow-blue content of color. Gray levels are
represented by a*=b*=0 and L* ranging from 0 (black) to 100 (white).
Recipe 26: Converting among different color spaces 199

Positive values of a* represent magenta tones, while negative values


of a* represent green tones. Positive values of b* represent yellow
tones, while negative values of b* represent blue tones. a* and b* are
not explicitly
Copyright constrained
Material and typically
- Provided assume
by Taylor & values
Francis from -128 to
127. The distance between colors 𝐶1 and 𝐶2 in L*a*b* is computed as
∗ Review Copy - Not for∗ Redistribution
Δ𝐸𝑎𝑏 = [(𝐿∗𝑐1 − 𝐿∗𝑐2 )2 + (𝑎∗𝑐1 − 𝑏𝑐2 )2 + (𝑏𝑐1
∗ ∗ 2 1/2
− 𝑏𝑐2 ) ] , and is available
in MATLAB’s IPT deltaE function.

We can use Listing 26.1 as a template to convert primary colors, sec-


ondary colors, black and white, from RGB to L*a*b*. All that is needed is
to change the function in line 11 to rgb2lab, and change the names of
the rows in T.Properties.RowNames, and rename all necessary variables
accordingly.
The resulting table T is presented next. Since a* represents magenta(positive)-
green(negative), the larger positive value in this example occurs at Magenta
(98.234), and the larger negative value at Green (-86.183). Similarly, since b*
represents yellow(positive)-blue(negative), the larger positive value in this
example occurs at Yellow (94.478), and the larger negative value at Blue
(-107.86).

R G B L* a* b*
_ _ _ ______ _______ _______
Black 0 0 0 0 0 0
Red 1 0 0 53.241 80.092 67.203
Yellow 1 1 0 97.139 -21.554 94.478
Green 0 1 0 87.735 -86.183 83.179
Cyan 0 1 1 91.113 -48.088 -14.131
Blue 0 0 1 32.297 79.188 -107.86
Magenta 1 0 1 60.324 98.234 -60.825
White 1 1 1 100 0 0

• L*ch (lch): Also know as LCH or L*C*h𝑎𝑏 . It is derived from the L*a*b*
color space, obtained by means of a rectangular to polar transforma-
tion on a* and b* values. The h dimension represents the hue, and c is
called chroma. L*ch is used in the CIE 1994 color difference metric [22],
available in MATLAB’s IPT imcolordiff function. L*ch also finds
applications, for example, in the design of color palettes for statistical
data visualization [25, 24].

5: TV and video color spaces


The TV and video color spaces discussed here are based on two key prin-
ciples rooted in historical reasons. Firstly, they decouple luminance from
chromaticity, enabling compatibility with older black-and-white systems.
Secondly, these spaces represent colors by utilizing color differences rather
than their absolute values, which in turn reduces the bandwidth required for
signal transmission.
200 Image processing recipes in MATLAB®

• YIQ (ntsc): The Y component represents the luminance (achromatic),


while I and Q components encode the color information, based on
color differences. I stands for in-phase and Q stands for quadrature.
These termsMaterial
Copyright are related to the modulation
- Provided by Taylorscheme employed in the
& Francis
transmission of analog TV signals, as specified
Review Copy - Not for Redistribution by the National Tele-
vision System Committee (NTSC) standard. In YIQ color space, Y is
derived from the RGB components using the equation 𝑌 = 0.2989𝑅 +
0.5870𝐺 + 0.1140𝐵. Y can range from 0 (black) to 1 (white) and is
commonly regarded as the grayscale representation of an RGB image.
In MATLAB, this RGB to grayscale conversion is available with the
rgb2gray function7 .

Another possible method to convert an image from RGB to grayscale is


averaging the R, G, and B components. However, it’s not always the most
accurate representation of perceived luminance due to the way human vision
works.
The human visual system does not perceive the three colors (red, green,
and blue) with equal sensitivity. Green is perceived as being the bright-
est, followed by red and then blue. As a result, a more common for-
mula for converting RGB to grayscale that considers human perception is:
Grayscale=0.299×R+0.587×G+0.114×B. This weighted sum gives more
prominence to the green channel, reflecting its higher perceived brightness.
So, while averaging is acceptable and will give you a grayscale image, the
weighted sum approach is often preferred for a more perceptually accurate
result. This becomes particularly visible if we use saturated colors to compare
both conversion methods, as Listing 26.3 exemplifies. Results are shown in
Figure 26.6, where the bar’s saturated red, green, and blue in part (b) become
indistinguishable for the average method.

LISTING 26.3
Converting from RGB to grayscale using two different approaches.
1 % RGB to grayscale conversion using two methods
2 b = bar(1:3,’FaceColor’,’flat’); % an arbitrary bar plot with 3 bars
3 xticks([]), yticks([])
4 % [R G B] values specifying 1st,2nd,3rd bars colors as red,green,blue
5 b.CData(1,:) = [1 0 0]; b.CData(2,:) = [0 1 0]; b.CData(3,:) = [0 0 1];
6 % Store the plot as a raster image using the screen resolution
7 img = print(’-RGBImage’,’-r0’);
8
9 % Method 1: grayscale = (R+G+B)/3
10 img_gray_1 = imlincomb(1/3,img(:,:,1),1/3,img(:,:,2),1/3,img(:,:,3));
11 % Method 2: grayscale = Y = 0.2989*R + 0.5870*G + 0.1140*B
12 img_gray_2 = rgb2gray(img);
13
14 figure, imshowpair (img_gray_1, img_gray_2, ’montage’)
Recipe 26: Converting among different color spaces 201

Copyright Material - Provided by Taylor & Francis


Review Copy - Not for Redistribution

FIGURE 26.6
RGB to grayscale convertion. (a) Original image. (b) Converted image using the average of R, G,
and B. (c) Converted image using rgb2gray function.

• YCbCr (ycbcr): The Y component represents the luminance (achro-


matic), while Cb and Cr components encode the color information as
differences between blue and a reference value, and red and a refer-
ence value. YCbCr is adopted in image compression (e.g., JPEG) and
digital video (e.g., H.264) standards.
Beware that the 𝑌 component resulting from an RGB to YCbCr con-
version (rgb2ycbcr function) does not occupy the full range8 of the
image’s class (data type) and, therefore, it is not an adequate grayscale
representation of an RGB image, such as the 𝑌 component in the YIQ
model.

6: Printing color spaces


The above color models in this recipe are based on the additive mixture of lights.
In contrast, a printing color space is based on the additive mixture of pigments,
also called subtractive mixture of lights. A primary color of pigment absorbs
a primary color of light and reflects the other two. Thus, cyan (absorbs red,
reflects blue and green), magenta (absorbs green, reflects red and blue), and
yellow (absorbs blue, reflects red and green) are commonly adopted as the
primary colors of pigments for use in color printing devices.

• CMYK (cmyk): C, M, and Y stand for the primaries cyan, magenta,


and yellow. In practice, the mixture of these three pigments results in a
faded black, so the K (black) pigment is added as a fourth component
of the color space. In its simplest form, the primaries C, M, Y, and K
can be obtained from R, G, and B (in the range 0…1) as 𝐶 = 1 − 𝑅;
𝑀 = 1 − 𝐺; 𝑌 = 1 − 𝐵; 𝐾 = 𝑚𝑖𝑛(𝐶, 𝑀 , 𝑌 ).

Listing 26.4 shows a conversion from RGB to CMYK of an image of fabrics


with colors predominantly cyan, magenta, yellow, and black. The conversion
is performed with makecform and applycform functions. Line 9 makes a
row-wise concatenation of the converted image’s C, M, Y, and K components.
202 Image processing recipes in MATLAB®

Copyright Material - Provided by Taylor & Francis


Review Copy - Not for Redistribution

FIGURE 26.7
RGB to CMYK conversion: (a) is the original RGB image and (b) are the C, M, Y, and K compo-
nents of the converted CMYK image, presented as intensity images. The largest C, M, Y, and K
values occur at the corresponding regions with predominant cyan, magenta, yellow, and black
colors in the original image.

Results are shown in Figure 26.7. Note that the predominant color appears
more intensely in its correspondent channel.

LISTING 26.4
Converting from RGB to CMYK color space.
1 % RGB to CMYK color space conversion
2 img = imread(’fabric_pp_09.png’);
3 img = rot90(img); % rotate image 90 deg counterclockwise
4 imshow(img), title(’Original’)
5

6 c = makecform(’srgb2cmyk’); % c is a structure to specify...


7 img_cmyk = applycform(img,c); % ...the conversion
8

9 img_cmyk_2d = img_cmyk(:,:); % MxNx4 to row-wise MxN*4


10 figure, imshow(img_cmyk_2d), title(’C,M,Y,K components’)

Discussion (Recipe notes)


In this recipe, you learned the main characteristics of several color spaces
and explored how to perform conversions among them, using either dedicated
functions (format in2out) or color transformation structures (makecform and
applycform functions).
Color perception and color spaces are immensely vast topics, encompass-
ing a myriad of aspects, from the physics of light to the intricacies of human
vision and data encoding, among others. You are encouraged to explore these
topics further, starting from the references in the Learn more about it section
below.
Recipe 26: Converting among different color spaces 203

Learn more about it


UsefulCopyright Material
MATLAB functions - Provided by Taylor & Francis
Review
Type the function Copy
name in the- search
Not for Redistribution
field at www.mathworks.com/help/
matlab/
· applycform · colorcloud · deltaE · hsv2rgb · im2gray
· imcolordiff · imlincomb · lab2rgb · lab2xyz · ntsc2rgb · rgb2hsv
· rgb2lab · rgb2ntsc · rgb2xyz · hsv2rgb · lab2rgb · lab2xyz
· measureColor · ntsc2rgb · plotChromaticity · rgb2gray · rgb2hsv
· rgb2lab · rgb2ntsc · rgb2xyz · lab2rgb · rgb2ycbcr · rgbwide2xyz
· · rgbwide2ycbcr · xyz2lab · xyz2rgb · xyz2rgbwide · ycbcr2rgb
· ycbcr2rgbwide ·

MATLAB documentation, demos, and examples


• Calculate CIE94 Color Difference of Colors on Test Chart
www.mathworks.com/help/images/calculate-cie94-color-differences
-of-colors-measured-on-test-chart.html
• Color Space Conversions, Support for International Color Consortium
(ICC) Profiles
www.mathworks.com/help/images/color.html
• Device-Independent Color Spaces
www.mathworks.com/help/images/device-independent-color-spaces.
html
• Implement Digital Camera Processing Pipeline
www.mathworks.com/help/images/end- to- end- implementation- of-
digital-camera-processing-pipeline.html
• Understanding Color Spaces and Color Space Conversion
www.mathworks.com/help/images/understanding-color-spaces-and-
color-space-conversion.html

Notes
1 Remember that imshow and similar functions expect an RGB image as input.
2 Do not mistake for the TV-oriented YUV color space [10], or for the perceptually
uniform CIE LUV (also known as L*u*v*) color space. YUV and CIE LUV are not
available in MATLAB’s IPT.
3 Perceptually uniform color spaces will be discussed later in this recipe.
4 HSB (hue, saturation, brightness) is generally accepted to denote the same color
space. There are variations of the HSV, such as HSI (hue, saturation, intensity)
and HLS (hue, luminance, saturation). It is important to mention that different
204 Image processing recipes in MATLAB®

interpretations of these acronyms can occur. Thus, it is important to verify the


current set of equations employed to convert from RGB to one of these color spaces
(see [5] for details).
5 To reproduce Figure 26.4(b), load the image into a variable, for instance, img, and
Copyright Material - Provided by Taylor & Francis
ask for the color cloud using colorcloud(img,’hsv’).
6 The “lid” Review
of the HSVCopy - Not
inverted conefor Redistribution
originates what is commonly called a color
wheel.
7 Note that starting with R2020b, MATLAB also includes the im2gray function that
spares the user from having to ensure that the image is indeed RGB before calling
the rgb2gray function.
8 According to the documentation: “The range of numeric values depends on the
data type of the image. YCbCr does not use the full range of the image data type
so that the video stream can include additional (non-image) information.”
27
Recipe 27: Material
Copyright Color -image
Providedadjustments
by Taylor & Francis
Review Copy - Not for Redistribution

This recipe teaches you how to adjust color images to make them more
interpretable or visually pleasant, using different color spaces.

Previously1 , we have used MATLAB to adjust the brightness, contrast, and


overall appearance of grayscale images. Extending these operations to color
images might lead to additional challenges and unexpected results, such as
the appearance of spurious colors in the resulting image.
In this recipe, depending on the image processing operation, we adopt one
of these strategies to ensure correct results:

1. Separate the achromatic (brightness) and chromatic (color) elements


of the original image and:
• Modify only the achromatic component while preserving the chro-
matic components.
• Modify the chromatic components while monitoring the resulting
output to prevent unwanted alterations in the perceived colors.
2. Apply the same adjustments to the red, green, and blue components
of the RGB color space to ensure minimal changes in the perceived
colors.

You will need (Ingredients)


• MATLAB R2019b or later
• MATLAB Image Processing Toolbox (IPT) version R2019b or later
• (OPTIONAL2 ) One or more of your images

Steps (Preparation)
1: Contrast improvement using histogram equalization
1. Load the image into the workspace.

DOI: 10.1201/9781003170198-35 205


206 Image processing recipes in MATLAB®

2. Extract the luminance channel using the rgb2lab function.


3. Apply histogram equalization to the luminance channel.
4. Recover the adjusted image with the lab2rgb function.
Copyright Material - Provided by Taylor & Francis
5. Display before and after images and (visually) evaluate the quality of
Review Copy - Not for Redistribution
the results.

The code in Listing 27.1 performs the histogram equalization of the L*


channel of the L*a*b* (CIELAB) color space using two methods – global his-
togram equalization with histeq function and local histogram equalization
with adapthisteq function3 .

LISTING 27.1
Color histogram equalization.
1 % Color histogram equalization
2 img = imread(’office_4.jpg’);
3 lab = rgb2lab(img);
4 l = lab(:,:,1); % L* channel
5 l = l/100; % Values [0...100] of the L* -> [0...1]
6

7 l_g = histeq(l,256); % method (1) global histogram equalization


8 l_g = l_g*100; % Values [0...1] of the processed L* -> [0...100]
9 l_l = adapthisteq(l); % method (2) local histogram equalization
10 l_l = l_l*100; % Values [0...1] of the processed L* -> [0...100]
11

12 lab_g = cat(3, l_g, lab(:,:,2), lab(:,:,3));


13 lab_l = cat(3, l_l, lab(:,:,2), lab(:,:,3));
14 img_l_g = lab2rgb(lab_g);
15 img_l_l = lab2rgb(lab_l);
16 figure, montage({img, img_l_g, img_l_l})
17 title(’in, L* global hist eq, L* local hist eq’)

Comparing the results after global or local histogram equalization (Figure


27.1), we can note that both variations of the histogram equalization method
improve the perceived contrast while preserving the original colors (hues).
Upon closer inspection, we can see that the local histogram equalization
results (Figure 27.1(c)) allow for better visualization of the originally dark
objects at the left corner of the table while at the same time preserving the
details on the monitor.

2: Saturation adjustment in the HSV color space


1. Load the image into the workspace.
2. Extract the saturation channel using the rgb2hsv function.
3. Apply a “boost” to the saturation channel.
4. Recover the adjusted image with the hsv2rgb function.
5. Display before and after images and (visually) evaluate the quality of
the results.
Recipe 27: Color image adjustments 207

Copyright Material - Provided by Taylor & Francis


Review Copy - Not for Redistribution

FIGURE 27.1
Color histogram equalization. (a) Original image. (b) Processed image using global histogram
equalization. (c) Processed image using local histogram equalization.

The code in Listing 27.2 modifies the saturation color channel using two
distinct approaches – a multiplicative gain and an additive offset. To allow a
comparison between the approaches, the constants gain and offset were
chosen so that the percentage of pixels with 𝑆 values larger than 1, after the
respective operations, were approximately 3%.

LISTING 27.2
Modifying the saturation color channel: two different approaches.
1 % Color HSV S manipulation
2 img = imread (’peacock.jpg’);
3 [n_r, n_c] = size(img, 1, 2);
4 n_t = n_r*n_c; % Total number of pixels
5
6 hsv = rgb2hsv(img);
7 s = hsv(:,:,2);
8
9 gain = 1.7;
10 offset = 0.4;
11
12 s_g = s*gain; % Multiplicative gain
13 s_g_L = s_g > 1;
14 s_g_L_p = sum(s_g_L(:))/n_t % Proportion of pixels with S>1 after gain
15 s_o = s+offset; % Additive offset
16 s_o_L = s_o>1;
17 s_o_L_p = sum(s_o_L(:))/n_t % Proportion of pixels with S>1 after offset
18
19 hsv_s_g = cat(3, hsv(:,:,1), s_g, hsv(:,:,3));
20 hsv_s_o = cat(3, hsv(:,:,1), s_o, hsv(:,:,3));
21 img_s_g = hsv2rgb(hsv_s_g);
22 img_s_o = hsv2rgb(hsv_s_o);
23 figure, montage({img, img_s_g, img_s_o});
24 title(’in, S*gain, S+offset’)

Figure 27.2 shows the results. In both cases, as expected, as the saturation
increases, image colors appear more vivid or vibrant4 .
208 Image processing recipes in MATLAB®

Copyright Material - Provided by Taylor & Francis


Review Copy - Not for Redistribution

FIGURE 27.2
HSV-based adjustments. (a) Original image. (b) Processed image using multiplicative gain of
S*gain. (c) Processed image using an additive offset of S+offset.

3: RGB-based adjustment
1. Load the image into the workspace.
2. Apply the same operations on the 𝑅, 𝐺, and 𝐵 color channels.
3. Display before and after images and (visually) evaluate the quality of
the results.

In Recipe 13, we performed gamma correction on RGB images, using the


same value of 𝛾 for each color channel. We follow the same principle in List-
ing 27.3 where we apply a sigmoid function to the image’s 𝑅, 𝐺, and 𝐵
channels.

LISTING 27.3
Applying a sigmoid function to enhance contrast in RGB images.
1 % Color R, G, B sigmoid
2 img = imread(’wagon.jpg’);
3

4 x = 0:255; inflec = 127;


5 slope = 0.025; % smoother slope
6 % slope = 0.05; % sharper slope
7 sigmoid = uint8(255./((1+exp(-slope*(x-inflec)))));
8 figure, plot(sigmoid); xlim([0 255]) ; ylim([0 255]) ; grid on
9

10 img_s = intlut(img, sigmoid);


11 figure, montage({img, img_s})

Figure 27.3 shows the results. The sigmoid maps pixel values below in-
flec = 127 to lower values and values above inflec = 127 to higher
values. The higher the slope, the more vivid or vibrant the perceived output
colors.

4: Low-light enhancement
1. Load the image into the workspace.
Recipe 27: Color image adjustments 209

Copyright Material - Provided by Taylor & Francis


Review Copy - Not for Redistribution

FIGURE 27.3
RGB-based adjustment. (a) Original image. (b) Processed image using a sigmoid function with
slope = 0.025. (c) Processed image using a sigmoid function with a sharper slope (slope =
0.05).

2. Enhance the image using the imlocalbrighten function.


3. Display before and after images and (visually) evaluate the quality of
the results.

Improving images captured in notably low-light conditions is challenging.


While traditional methods aimed at enhancing brightness or contrast can im-
prove visibility in darker areas, they often result in the oversaturation of the
image’s brighter regions.
The imlocalbrighten function implements a low-light image enhance-
ment method that addresses this issue. It is based on an image dehazing tech-
nique and the empirical observation that the negative of an RGB low-light
image approximates to a hazy image [11]. Internally, imlocalbrighten
complements5 the R, G, and B channels of the input image, applies the dehaz-
ing process, and then complements the R, G, and B channels again to produce
the enhanced image.
Listing 27.4 presents a simple example, where the amount parameter
(within the range [0…1]) is used to specify the intensity of the brightening
operation. The default value is 1. Figure 27.4 shows results for amount=0.1
and amount=0.9.

LISTING 27.4
Low-light enhancement.
1 % Color low-light enhancement
2 img = imread (’car_3.jpg’);
3

4 amount = 0.1;
5 %amount = 0.9;
6 img_lle = imlocalbrighten(img, amount);
7 figure, montage({img, img_lle})
210 Image processing recipes in MATLAB®

Copyright Material - Provided by Taylor & Francis


Review Copy - Not for Redistribution

FIGURE 27.4
Low-light image enhancement. (a) Original image. (b) Processed image with amount=0.1. (c)
Processed image with amount=0.9.

Discussion (Recipe notes)


In this recipe, you learned how to manipulate color images to enhance
their overall visual appeal or emphasize specific details while preserving
the perceived hue dimension of the colors, which is the attribute commonly
associated with the “color name.”
We strongly encourage exploring the resources in the Learn more about it
section below for additional insights and examples.

Learn more about it


Useful MATLAB functions
Type the function name in the search field at www.mathworks.com/help/
matlab/
· adapthisteq · histeq · hsv2rgb · imlocalbrighten · intlut
· lab2rgb · rgb2hsv · rgb2lab ·

MATLAB documentation, demos, and examples


• Adjust the Contrast of Color
www.mathworks.com/help/vision/ug/adjust-the-contrast-of-color-
images.html
• Convert Between RGB and HSV Color Spaces
www.mathworks.com/help/images/convert-from-hsv-to-rgb-color-
space.html
• Low-Light Image Enhancement
www.mathworks.com/help/images/low-light-image-enhancement.html
Recipe 27: Color image adjustments 211

Notes
1 Copyright
Parts Material
III and IV of the book, -particularly
ProvidedRecipes
by Taylor &and
8, 11, 12, Francis
13.
2 The recipe has been prepared using MATLAB’s built-in images.
3
Review Copy - Not for Redistribution
See Recipe 8.
4 Since the performed offset operation shifts the entire 𝑆 dimension by a large con-
stant (0.4 corresponds to 40% of the [0…1] 𝑆 range), the effect on the image in part
(c) is more prominent if compared to the multiplicative gain in part (b).
5 Complementing an image is the same as computing its negative. See Recipe 11.
28
Recipe 28: Material
Copyright Image- pseudocoloring
Provided by Taylor & Francis
Review Copy - Not for Redistribution

This recipe teaches you how to apply pseudocoloring techniques to grayscale


images.

Pseudocoloring is a technique in image processing where each gray level in


an image is mapped to a specific color, enhancing the contrast and visualiza-
tion of features in monochromatic images. Pseudocoloring is commonly used
in medical imaging, remote sensing, and microscopy to enhance the visibility
of specific features that might be indistinguishable in grayscale.
The main types of pseudocoloring techniques include density slicing,
where distinct color ranges represent different intensity bands, and domain-
specific colormaps, such as the ”hot” and ”cold” maps used in thermal
imaging. By applying these techniques, interpreters can extract meaning-
ful information from the data more readily than by examining the original
grayscale image.

You will need (Ingredients)


• MATLAB R2020b or later
• MATLAB Image Processing Toolbox (IPT) version 2020b or later
• Image: AD21-0016-001_F3_P3_knife_plane_drop_v~orig.tif1 .

Steps (Preparation)
1. Load grayscale image.
2. Choose the desired colormap from the several MATLAB’s pre-defined
colormaps; alternatively, edit/create your own colormap.
3. (OPTIONAL) Use rgbplot function to view the 𝑅, 𝐺, and 𝐵 compo-
nents of the colormap.

212 DOI: 10.1201/9781003170198-36


Recipe 28: Image pseudocoloring 213

4. If necessary, reduce the number of gray levels of the image to 𝑁 , using


the grayslice function, and set the number of colors in the colormap
to 𝑁 .
5.Copyright
Apply the Material - Provided
desired colormap by grayscale
to the Taylor &image,
Francis
usually as a
parameter of function.
Review Copy - Not for Redistribution
imshow
6. Save the pseudocolored image providing the colormap as an input
parameter for the function imwrite.

To apply pseudocolors to a grayscale image in MATLAB, we employ the


indexed image representation, which links a specific colormap to the grayscale
image2 .
MATLAB provides several pre-defined colormaps with different color
combinations through specific functions following the colormaps’ names.
These functions receive the number of colors 𝑁 as input and output the
𝑁 × 3 colormap3 . Typing the command below, for example, you get the flag
colormap, which cycles the red, white, blue, and black colors.
cm = flag(5)

cm =
1 0 0
1 1 1
0 0 1
0 0 0
1 0 0

Listing 28.1 presents the pseudocoloring of a Schlieren photography of


shock waves produced by a supersonic jet [14], using pink, turbo, and
hot colormaps. Download the original image from NASA Image and Video
Library1 . To save the image, provide the desired colormap as an input param-
eter to the imwrite function (line 12). Figure 28.1 shows the results. Note
that the version with the hot colormap approximates one of those in [14].

LISTING 28.1
Pseudocoloring with different colormaps.
1 % Grayscale image pseudocolored with ”pink”, ”turbo” and ”hot” colormaps
2 img = imread(’AD21-0016-001_F3_P3_knife_plane_drop_v~orig.tif’);
3 img = rot90(img,-1); % rotate image 90 deg clockwise
4
5 figure
6 t = tiledlayout(1,4);
7 nexttile, imshow(img), title(’Original grayscale’)
8 nexttile, imshow(img,pink), title(’pink colormap’)
9 nexttile, imshow(img,turbo), title(’turbo colormap’)
10 nexttile, imshow(img,hot),title(’hot colormap’)
11
12 imwrite(img,hot,’pseudocolored.png’)

One of the main motivations to produce a pseudocolocor version of a


grayscale image is to improve the visualization of details, since our ability to
214 Image processing recipes in MATLAB®

Copyright Material - Provided by Taylor & Francis


Review Copy - Not for Redistribution

FIGURE 28.1
Grayscale image (a) pseudocolored with different colormaps: (b) pink; (c) turbo, and (d) hot.
Original image: courtesy of NASA.

1 255
217

0 0
0 200 400 600 0 200 400 600

FIGURE 28.2
Synthetic grayscale image with two “elevations.” In (a), the peaks are equal, as shown in the
respective diagonal line profile across the peaks in (b). In (c), the left peak is 15% attenuated so
that the maximum is at gray level 217, as shown in the respective diagonal line profile across the
peaks in (d).

discriminate colors is much greater than our ability to discriminate shades of


gray4 . In Listing 28.2 we generate a synthetic image containing two grayscale
“elevations” (function bwdist), and multiply the left one by 0.85 (15% at-
tenuation). Figure 28.2 presents the images before and after the attenuation,
accompanied by line profile plots (improfile function) showing the pixels’
values of the diagonal across the peaks of the image. The image is then saved
as synthPeaks2_gray_uneven.png to be pseudocolored.

LISTING 28.2
Generating a synthetic grayscale image to be pseudocolored.
1 % Generate synthetic grayscale image to further apply pseudocolors
2 img = zeros(407, 407); % a 407x407 image
3 img(103,103) = 1; img(307,307) = 1; % two nonzero pixels
4 % Distance transform: for every p, Euclid. dist. to nearest nonzero pixel
5 img_d = bwdist(img);
6 img_d_r = mat2gray(img_d); % rescale min->0, max->1
Recipe 28: Image pseudocoloring 215

7 img_d_r_c = 1 - img_d_r; % complement (photographic negative)


8
9 % Atenuate 15% of left ”elevation”
10 t = rot90(triu(ones(407,407)))*0.85;
11
12
Copyright Material - Provided by Taylor & Francis
t(~t) = 1;
img_d_r_c_a = img_d_r_c.*t;
13 Review Copy - Not for Redistribution
img_d_r_c_a_u = im2uint8(img_d_r_c_a); % double [0...1] to uint8 [0...255]
14
15 % Show grayscale’s equal and unequal peaks
16 figure
17 tiledlayout(1,4);
18 nexttile, imshow(img_d_r_c), title(’Equal peaks’)
19 nexttile, improfile(img_d_r_c,[1 407],[1 407]); title(’Diag. profile’)
20 ylim([0 1]), yticks([0 1]),
21 nexttile, imshow(img_d_r_c_a_u), title(’Left peak 15% attenuation’)
22 nexttile, improfile(img_d_r_c_a_u,[1 407],[1 407]); title(’Diag. profile’)
23 ylim([0 255]), yticks([0 217 255]),
24
25 imwrite(img_d_r_c_a_u,’synthPeaks2_gray_uneven.png’)

The code in Listing 28.3 applies pseudocolors to the synthetic image cre-
ated in Listing 28.2, using the jet colormap and 10 colors (stored in variable
s on line 4). To reduce the number of gray levels of the original image, we use
the grayslice function. It performs a multilevel thresholding with linearly
spaced thresholds and remaps the image accordingly (line 5). If you use the
jet colormap without specifying the value of s, you will get the results in
Figure 28.3 (b) (with the default 256 colors). This pseudocoloring technique
is commonly named as intensity slicing. Results are shown in Figure 28.3.
Figure 28.3(c) presents a colorbar depicting the correspondence between
the remapped values and the colors. As expected, the color of the lower peak
is orange, indicating that it is in second from the last range due to the applied
attenuation.
LISTING 28.3
Generating a synthetic grayscale image and applying pseudocolors.
1 % Pseudocoloring with 256 colors and ”intensity slicing” with 10 partitions
2 img = imread(’synthPeaks2_gray_uneven.png’);
3
4 s = 10; % number of slices for the ”intensity slicing”
5 img_s = grayslice(img, s); % multithreshold and remap
6
7 figure
8 tiledlayout(1,3);
9 nexttile, imshow(img), title(’Grayscale’)
10 nexttile, imshow(img, jet); title(’Pseudoc. 256 colors’)
11 nexttile, imshow(img_s, jet(s)); title(’Pseudoc. s colors’)
12 colorbar
13
14 imwrite(img_s,jet(s),’pseudocolored_slices.png’)

In addition to using MATLAB’s built-in colormaps, we can also create


our own.
Listing 28.4 shows code to create a colormap ranging from black to white,
with tones of cyan in between. The exponential mathematical functions (lines
216 Image processing recipes in MATLAB®

Copyright Material - Provided by Taylor & Francis


Review Copy - Not for Redistribution

FIGURE 28.3
Pseudocoloring a synthetic grayscale image with two “elevations.” (a) The grayscale image
where the left peak is 15% attenuated. (b) Pseudocoloring with jet colormap, 256 colors. (c)
Pseudocoloring with jet colormap and intensity slicing with 10 partitions.

4–6) ensure that each channel has values between 0 and 1. The code uses the
rgbplot function to visualize the resulting 𝑅, 𝐺, and 𝐵 components of the
designed colormap. Figure 28.4 shows the results.

LISTING 28.4
Creating a customized colormap.
1 % Pseudocoloring with a users’ created colormap
2 img = imread(’synthPeaks2_gray_uneven.png’);
3
4 r = linspace(0,1,256).^4; % function for the R component of the colormap
5 g = linspace(0,1,256).^0.35; % function for the G component of the colormap
6 b = linspace(0,1,256).^0.3; % function for the B component of the colormap
7 cm = [r’ g’ b’]; % assembly into colormap’s format
8
9 s = 10; % number of slices for the ”intensity slicing”
10 img_s = grayslice(img, s); % multithreshold and remap
11 idx = round(linspace(1,256,s)); % s equidistant indices to downsample cm
12 cm_s = cm(idx,:); % downsample cm from 256 to s colors
13
14 % Show R,G,B of the colormaps and the respective pseudocolored images
15 figure
16 tiledlayout(1,4);
17 nexttile, rgbplot(cm)
18 xlim([1 256]), title(’R,G,B of the 256 colors colormap’)
19 nexttile, imshow(img, cm), title(’Pseudoc. 256 colors’)
20 nexttile, rgbplot(cm_s)
21 xlim([1 s]), title(’R,G,B of the 10 colors colormap’)
22 nexttile, imshow(img_s, cm_s), title(’Pseudoc. 10 colors’)
23 colorbar
24
25 imwrite(img_s,cm_s,’pseudocolored_slices_custom_cm.png’)
Recipe 28: Image pseudocoloring 217

0.8

Copyright
0.6
Material - Provided by Taylor & Francis
Review Copy - Not for Redistribution
0.4

0.2

0
50 100 150 200 250

1
10

0.8
8

0.6
6

0.4
4

0.2 2

0 0
2 4 6 8 10

FIGURE 28.4
Customized colormap created using exponential functions. The plots are obtained with rgb-
plot function, which shows the colormap’s 𝑅, 𝐺, and 𝐵 components. (a) is a colormap with
256 colors, and (c) with 10 colors. (b) and (d) are the respective pseudocolorized images.

Discussion (Recipe notes)


In this recipe, you learned how to apply pseudocolors to a grayscale image to
improve its visualization. MATLAB provides several pre-defined colormaps,
but you can also edit them or design your own colormaps.
We strongly encourage exploring the resources below for additional in-
sights and examples.
218 Image processing recipes in MATLAB®

Learn more about it


UsefulCopyright Material
MATLAB functions - Provided by Taylor & Francis
Review
Type the function Copy
name in the- search
Not for Redistribution
field at www.mathworks.com/help/
matlab/
· bwdist · colorbar · colormap · grayslice · linspace · rgbplot
· tiledlayout ·

MATLAB documentation, demos, and examples


• Colormap Editor
www.mathworks.com/help/matlab/ref/colormapeditor.html
• Colormaps
www.mathworks.com/help/images/convert-from-hsv-to-rgb-color-
space.html
• How Image Data Relates to a Colormap
www.mathworks.com/help/matlab/creating_plots/how- image- data-
relates-to-a-colormap.html

Notes
1 Available at: https://images.nasa.gov/details/AD21-0016-001_F3_P3_kni
fe_plane_drop_v
2 In Recipe 10, you can find an inspection of an indexed image using the Pixel Region
tool (Figure 10.1). Thus, the original gray levels are indexes for the colormap, pro-
viding the image’s pseudocolors. The colormap follows the 𝑁 × 3 format, where
𝑁 is the number of colors, and the columns store 𝑅, 𝐺, and 𝐵 values in the [0, 1.0]
range.
3 Up to MATLAB 2019a colormaps’ default number of colors was 64. From MATLAB
2019b it is 256.
4 Under ideal conditions, a human observer discriminates around one thousand
shades of gray [16], against around two million colors [9].
Copyright Material - Provided by Taylor & Francis
Review Copy - Not for Redistribution

Part IX

Batch processing and


handling large images

Part IX – Batch processing and handling large images


The recipes in Part IX focus on two practical problems associated with the
image processing pipeline that have become increasingly important in recent
years: (1) handling very large images and (2) performing batch processing
operations on a collection of images.

Recipe 29 shows how to handle gigapixel-sized images in MATLAB, using


examples from digital pathology.
Recipe 30 shows how to streamline batch image processing operations both
interactively as well as programmatically.

DOI: 10.1201/9781003170198-37 219


Copyright Material - Provided by Taylor & Francis
Review Copy - Not for Redistribution
29
Recipe 29: Material
Copyright Processing very
- Provided large&images
by Taylor Francis
Review Copy - Not for Redistribution

This recipe teaches you how to read, store, process, and display very large
images using MATLAB.

Handling extremely large images presents significant challenges. These chal-


lenges can stem from the image’s size exceeding memory capacity or from the
image surpassing processing capabilities once loaded. Moreover, the storage
and retrieval of such large images usually demand specialized hardware and
software solutions to manage the data effectively.
This recipe demonstrates how to read, store, process, and display very
large images. The focal point is the MATLAB blockedImage object that can
be used to process images, volumes, or multidimensional images that are too
large to fit into memory.

You will need (Ingredients)


• MATLAB R2021a or later
• MATLAB Image Processing Toolbox (IPT) version R2021a or later
• Image files: tumor_091R.tif1

Steps (Preparation)
The main steps for processing very large images are:

1. Load a blocked image into the workspace using blockedImage.


2. (OPTIONAL) Display the image using bigimageshow.
3. Creates a blockedImageDatastore object that manages a collection
of image blocks of one or more blockedImage objects.
4. (OPTIONAL) Display the blocked image using montage.

DOI: 10.1201/9781003170198-38 221


222 Image processing recipes in MATLAB®

5. Apply image processing operations on the blocked image, one block


at a time, using the apply function.
6. Display the before and after images using bigimageshow.
Copyright Material - Provided by Taylor & Francis
The code in Listing Copy
Review 29.1 illustrates
- Not forhow to perform the first four steps of
Redistribution
this recipe.

LISTING 29.1
Loading and displaying large images.
1 % Loading and displaying large images
2 file_name = ’tumor_091R.tif’;
3 % Store details about the original file
4 file_info = imfinfo(file_name);
5 % Create a blocked image
6 tumor_image = blockedImage(file_name);
7 % Display details of the blocked image at the command line.
8 disp(tumor_image)
9 % View the blocked image
10 bigimageshow(tumor_image)
11 % Inspect resolution levels
12 level_size_info = table((1:length(tumor_image.Size))’, ...
13 tumor_image.Size(:,1), tumor_image.Size(:,2), ...
14 tumor_image.Size(:,1)./tumor_image.Size(:,2), ...
15 ’VariableNames’,[”Resolution Level” ”Image Width” ...
16 ”Image Height” ”Aspect Ratio”]);
17 disp(level_size_info)
18 % Create a blockedImageDatastore, specifying the resolution level
19 % and the blocksize.
20 bls = selectBlockLocations(tumor_image,’ExcludeIncompleteBlocks’,true);
21 blocked_imds = blockedImageDatastore(tumor_image, ”BlockLocationSet”,
bls);
22 %Read all the blocks in the datastore.
23 b = readall(blocked_imds);
24 % Display the big image with grid lines indicating blocks
25 figure, bigimageshow(tumor_image,...
26 ’GridVisible’,’on’, ’GridLevel’, 1,...
27 ’GridLineWidth’, 2, ’GridColor’,’k’,’GridAlpha’,0.3);
28 % Display the blocked image
29 figure, montage(b)

We start by inspecting the image file’s properties (line 4). This is a multires-
olution color image whose maximum resolution is 5000×5358×3 pixels. Line
6 creates a variable tumor_image that reads the image file contents using the
blockedImage function.
Recipe 29: Processing very large images 223

Line 8 produces the following message in the Command Window:

blockedImage with properties:

Copyright
Read Material - Provided by Taylor & Francis
only properties
Review Copy
Source: - Not for Redistribution
”/../tumor_091R.tif”
Adapter: [1×1 images.blocked.TIFF]
Size: [3×3 double]
SizeInBlocks: [3×3 double]
ClassUnderlying: [3×1 string]

Settable properties
BlockSize: [3×3 double]

Let’s explore the meaning of these properties of the blockedImage


object [1]:

• Adapter: An object that reads and writes 2D blocked image data as a


single TIFF file.
• Size: Image size at each level, specified as an 𝐿 × 𝑁 matrix of positive
integers, where 𝐿 is the number of resolution levels and 𝑁 is the num-
ber of dimensions of the image. This image has three levels, ranging
from 5000 × 5358 × 3 to 625 × 670 × 3 pixels.
• SizeInBlocks: Size, expressed as the number of blocks and speci-
fied as an 𝐿 × 𝑁 matrix of positive integers, where 𝐿 is the number of
resolution levels, and 𝑁 is the number of dimensions. This image has
three levels, ranging from 5 × 6 × 1 to 1 × 1 × 1 pixels.
• ClassUnderlying: Pixel data type, specified as a string array with
𝐿 elements, where 𝐿 is number of resolution levels. Each element in
the array is the data type of a pixel from the corresponding resolution
level. For this image, the data type across all levels is uint8.
• BlockSize: Block size, specified as an 𝐿 × 𝑁 matrix of positive inte-
gers, where 𝐿 is the number of resolution levels and 𝑁 is the number
of dimensions. BlockSize serves as the default size of data loaded
into main memory at any time. It is the smallest unit of data that can
be manipulated with the blockedImage interface. For this image, the
block size for levels 1 and 2 is 1024 × 1024 × 3 pixels, whereas, for level
3, the block size is 625 × 670 × 3 pixels.

Line 10 displays the image in a figure window, using the bigimageshow


function.
Lines 12–17 display the image width and height at each level, producing
the result below. Note that the aspect ratio can change slightly among levels.
224 Image processing recipes in MATLAB®

Copyright Material - Provided by Taylor & Francis


Review Copy - Not for Redistribution

FIGURE 29.1
Viewing a large blockedImage with grid lines indicating blocks, using the bigimageshow
function.

Resolution Level Image Width Image Height Aspect Ratio


________________ ___________ ____________ ____________

1 5000 5358 0.93318


2 1250 1340 0.93284
3 625 670 0.93284

Lines 20–21 create a blockedImageDatastore object, specifying the


resolution level and the block size.
In line 23, we read all the data from the blockedImageDatastore
and store it into a variable b, a cell array containing an element for every
individual block.
Lines 25–27 display the image in a figure window, with grid lines indicat-
ing blocks (Figure 29.1).
Line 29 shows how to display the contents of the image from variable b,
using the montage function (Figure 29.2). Note that since we chose to ex-
clude incomplete blocks in line 20, the resulting image is a concatenation of
the 20 complete blocks of size 1024 × 1024 × 3 each. In other words, some of
the contents at the bottom and the right portions of the original image have
been lost.
Recipe 29: Processing very large images 225

Copyright Material - Provided by Taylor & Francis


Review Copy - Not for Redistribution

FIGURE 29.2
Viewing a large blockedImage using montage.

Using the code in Listing 29.2, we will show how to process the image
blockwise using the apply function. This corresponds to steps 5 and 6 of
this recipe.

LISTING 29.2
Processing large images in blockwise fashion.
1 %Processing large images in a blockwise fashion
2 %% Part 1 (Negative)
3 negative_img = apply(tumor_image, @(bs)imcomplement(bs.Data));
4 figure, bigimageshow(negative_img)
5
6 %% Part 2 (Edge detection)
7 edge_img = apply(tumor_image, @(bs)~edge(im2gray(bs.Data)));
8 figure, bigimageshow(edge_img) % undesirable blockiness effect
9
10 % Solving the blockiness problem
11 im_low_res = gather(tumor_image); % collect blocks from the coarsest level
12 [~, thresh] = edge(im2gray(im_low_res)); % learn threshold
13 edge_im_low_res = edge(im2gray(im_low_res),thresh); % test on coarse level
14 figure, imshow(edge_im_low_res)
15 % Validate on the largest image
16 edge_img_2 = apply(tumor_image,@(bs)~edge(im2gray(bs.Data),thresh));
17 % Visualize the result at the finest level
18 figure, bigimageshow(edge_img_2);

Line 3 shows how to compute the negative of a blockedImage in a block-


by-block fashion2 using the apply function. Everything works fine, as shown
in Figure 29.3.
Next, we try to use the same code structure to extract the edges of the
large image3 in a blockwise fashion (see line 7). Alas, the result is not quite
226 Image processing recipes in MATLAB®

Copyright Material - Provided by Taylor & Francis


Review Copy - Not for Redistribution

FIGURE 29.3
Negative of a blockedImage processed block-by-block.

what we expected, and there is a noticeable blockiness in the resulting im-


age (Figure 29.4(a)). The reason behind the problem is that the default edge
function uses a sensitivity threshold, which is computed for the entire im-
age. Since, in our case, each block is processed as a separate image, different
threshold values will be calculated for each block, hence the blockiness effect.
Lines 11–18 show an elegant way around this problem (inspired by [7])
that takes advantage of the multiple resolution levels in the image and the as-
sumption that the distribution of individual pixel values should be roughly
equal across all the levels. In line 11, we use the gather function to collect
blocks from the coarsest level and use them to build an image, im_low_res,
of size 625×670×3 pixels. Line 12 shows how to run edge on the coarse image
just to learn the automatically computed threshold and reuse that thresh-
old in line 13. Upon inspecting the results for the coarse image (line 14), we
validate the approach on the large image (line 16). Line 18 displays the re-
sults and allows us to confirm that the blockiness effect is gone, as expected
(Figure 29.4(b)).
Recipe 29: Processing very large images 227

Copyright Material - Provided by Taylor & Francis


Review Copy - Not for Redistribution

FIGURE 29.4
Edge detection results for a blockedImage processed block-by-block: (a) incorrect result
(blockiness effect); (b) correct result.

Discussion (Recipe notes)


In this recipe, you learned several techniques for handling large images. The
process can be very challenging, even using the blockedImage functional-
ity from MATLAB.
By their very nature and size, gigapixel-sized images are inherently un-
wieldy. Moreover, additional complicating factors include the choice of block
size and location, strategies for handling incomplete blocks, and performance
considerations4 .
We strongly encourage exploring the resources in the Learn more about it
section below for additional insights and examples.

Learn more about it


Useful MATLAB functions
Type the function name in the search field at www.mathworks.com/help/
matlab/
· compactitem · apply · bigimageshow · blockproc · blockedImage
· blockedImageDatastore · edge · gather · imcomplement · montage
· readall · selectBlockLocations ·
228 Image processing recipes in MATLAB®

MATLAB documentation, demos, and examples


• Block Processing Large Images
www.mathworks.com/help/images/block-processing-large-images.
Copyright
html Material - Provided by Taylor & Francis
• Block Review Copy - Not for Redistribution
Size and Performance
www.mathworks.com/help/images/block-size-and-performance.html
• Distinct Block Processing
www.mathworks.com/help/images/distinct-block-processing.html
• Neighborhood or Block Processing: An Overview
www.mathworks.com/help/images/neighborhood-or-block-processing-
an-overview.html
• Parallel Block Processing on Large Image Files
www.mathworks.com/help/images/parallel- block- processing- on-
large-image-files.html
• Process Blocked Images Efficiently Using Partial Images or Lower
Resolutions
www.mathworks.com/help/images/process-big-image-efficiently.
html
• Read Whole-Slide Images with Custom Blocked Image Adapter
www.mathworks.com/help/images/read- whole- slide- images- with-
custom-blocked-image-adapter.html

Notes
1 This image is a modified version of an image of a lymph node containing tumor
tissue, tumor_091.tif, from the CAMELYON16 data set. The original image has
eight resolution levels, and the finest level has a resolution of 53760 × 61440 × 3
pixels, i.e., 3.3 gigapixels. The modified image has only three coarse resolution
levels and is substantially smaller, which allows for less demanding computational
requirements.
2 When you run the code in MATLAB, some telltale signs that the processing is
happening block by block include the appearance of a progress indicator (de-
pending on image size and computational complexity of the operation) and the
(column-wise) block-by-block rendering of the image using bigimageshow.
3 We chose to use the negative of the edge detection results, for better visualization
on paper.
4 Processing large images in blockwise fashion can be done in parallel, if you have
the appropriate hardware and the MATLAB Parallel Computing Toolbox.
30
Recipe 30: Material
Copyright Batch -processing a set& of
Provided by Taylor images
Francis
Review Copy - Not for Redistribution

This recipe teaches you how to streamline an image processing workflow in


MATLAB using batch processing techniques.

Batch image processing is a method of applying the same image processing


operations to a large number of images at once. This process is often used
in industries such as photography, graphic design, and web development,
where large quantities of images need to be processed quickly and efficiently.
In MATLAB, batch image processing can be done interactively or program-
matically. By processing images in batches, it becomes possible to automate
and streamline tasks such as preprocessing, feature extraction, and analysis,
saving time and computational resources in various applications.

You will need (Ingredients)


• MATLAB R2015a or later
• MATLAB Image Processing Toolbox (IPT) version R2015a or later
• Selected images from the PillQC data set [6]

Steps (Preparation)
Part 1: Processing a set of images programmatically
The main steps for applying the same image processing operation(s) on a set
of images are:

1. Identify a folder containing the images that you want to process as a


batch.
2. Create an array of file names containing the names of the image files
in that folder.
3. Preallocate an array of appropriate size and read images into the array.
4. Process each image in the sequence using a function that implements
the desired image processing operation.

DOI: 10.1201/9781003170198-39 229


230 Image processing recipes in MATLAB®

5. (OPTIONAL) Display before and after images and visually evaluate the
quality of the results.

Listing 30.1 illustrates the first three steps of this recipe, i.e., how to
Copyright Material - Provided by Taylor & Francis
download and organize an image collection before any batch processing.
We use the Review
PillQC Copy - Not
data set, for Redistribution
available on GitHub [6]. This data set is
typically used to train and test image anomaly detection deep learning mod-
els [3]. It contains images from three classes: 149 normal images without
defects, 43 chip images with chip defects in the pills, and 138 dirt images with
dirt contamination. For this example, we will only use the 43 images from
the chip subfolder.
Lines 3–19 contain code for downloading the dataset and organizing
the images into subfolders under the folder whose path is stored in the
image_dir variable. Lines 22–26 create a cell array of filenames. Lines 29–
36 create a 4D variable input_batch to store the images. In this particular
case, since there are 43 RGB images of 225 × 225 pixels each, the size of the
input_batch variable is 225 × 225 × 3 × 43.

LISTING 30.1
Downloading and organizing an image collection for batch processing.
1 % Downloading and organizing an image collection for batch processing
2 %% Download PillQC dataset
3 data_dir = fullfile(’./’,”PillQCdataset”);
4
5 if ~exist(data_dir,”dir”)
6 mkdir(data_dir);
7 end
8
9 image_dir = fullfile(data_dir,”/pillQC-main”);
10
11 if ~exist(image_dir,”dir”)
12 disp(”Downloading Pill QC data set.”);
13 disp(”This can take several minutes to download and unzip...”);
14 unzip(”https://github.com/matlab-deep-learning/pillQC/archive/” + ...
15 ”refs/heads/main.zip”, data_dir);
16 delete(fullfile(image_dir,”*.m”),fullfile(image_dir,”*.mlx”), ...
17 fullfile(image_dir,”*.mat”),fullfile(image_dir,”*.md”));
18 disp(”Done.”);
19 end
20
21 %% Create array of filenames
22 file_folder = fullfile(image_dir,’images’,’chip’);
23 dir_output = dir(fullfile(file_folder,’*.jpg’));
24
25 file_names = {dir_output.name}’
26 num_images = numel(file_names)
27
28 %% Preallocate an (m x n x 3 x p) array and read images into the array
29 cd(file_folder)
30 img = imread(file_names{1});
31 input_batch = zeros([size(img) num_images],class(img));
32 input_batch(:,:,:,1) = img;
33
34 for p = 2:num_images
35 input_batch(:,:,:,p) = imread(file_names{p});
36 end
Recipe 30: Batch processing a set of images 231

The code in Listing 30.2 illustrates how to perform two separate opera-
tions – histogram equalization and binarization – on a sequence of images.

LISTING 30.2
Copyright Material - Provided by Taylor & Francis
Examples of batch processing: histogram equalization and binarization.
1
Review
% Examples of Copy - Not for
batch processing Redistribution
2 %% Part 1: histogram equalization
3 output_batch = histeq(input_batch);
4
5 % View each image pair (before/after processing)
6 figure;
7 for k = 1:num_images
8 imshow(input_batch(:,:,:,k));
9 title(sprintf(’Original Image # %d’,k));
10 pause(1);
11 imshow(output_batch(:,:,:,k));
12 title(sprintf(’Processed Image # %d’,k));
13 pause(1);
14 end
15
16 %% Part 2: binarization
17 for p = 1:num_images
18 output_batch_2(:,:,p) = imbinarize(im2gray(input_batch(:,:,:,p)));
19 end
20
21 % View original and processed images
22 figure, montage(input_batch)
23 figure, montage(output_batch_2)

In line 3, we perform histogram equalization across the entire batch in a


single line of code. This is possible because both input and output images
are RGB images, i.e., they have the same number of dimensions. Lines 6–14
show how we can visualize the results for all images in the batch in a loop
that alternates between the input and output image every second or so.
When the image processing operation results in an image of different di-
mensions (e.g., the input is an RGB image, and the output is a grayscale
image), we have to process each image inside a loop (lines 17–19) so that
each RGB image is processed separately and the result is assigned to a vari-
able output_batch_2 whose dimensions are 𝑚×𝑛×𝑝, where 𝑚 = 𝑛 = 225
and 𝑝 = 43 in this case.
Lines 22–23 show how to exhibit all images before and after batch bina-
rization (Figure 30.1).

Part 2: Batch processing a set of images interactively


The Image Batch Processor App provides a convenient way to process a batch
of images in a semi-automated way. Similarly to other MATLAB apps, it
232 Image processing recipes in MATLAB®

Copyright Material - Provided by Taylor & Francis


Review Copy - Not for Redistribution

FIGURE 30.1
Batch processing of 43 images: (a) original images; (b) binarized images.

offers a rich GUI for initial setup and interactive experimentation and visu-
alization of results that also allows you to export the processed images and
the processing pipeline for future reuse in a purely programmatic way.
The main steps for using the Image Batch Processor App1 are:

1. Identify a folder containing the images that you want to process as a


batch.
2. Open the Image Batch Processor App.
3. Load images from the folder of interest into the Image Batch Processor
App.
4. Specify batch processing function.
5. Process images using batch processing function.
6. Visualize results.
7. Export processed images and processing pipeline.

We will use the same data set and folder structure as Part 1. After opening
the Image Batch Processor App from the MATLAB toolstrip (under the Image
Processing and Computer Vision section), you should see a GUI that looks like
the one in Figure 30.2.
Recipe 30: Batch processing a set of images 233

Copyright Material - Provided by Taylor & Francis


Review Copy - Not for Redistribution

FIGURE 30.2
Image Batch Processor App: initial GUI.

FIGURE 30.3
Snapshot of the Image Batch Processor App after selecting the chip folder and a particular image
for detailed inspection.

Click on the Add button, locate and select the chip folder containing the
same 43 images used earlier in this recipe. You should see a screen similar to
the one shown in Figure 30.3.
Next, click on the Create button to create a new batch processing function
using a template. This will take you to the MATLAB editor, where you’ll see
a template function myimfcn that looks like Listing 30.32 .

LISTING 30.3
Batch processing: image binarization using an autogenerated template function.
1 function results = myimfcn(varargin)
2 % Image Processing Function
3 % VARARGIN - Can contain up to two inputs:
4 % IM - First input is a numeric array containing the image data.
5 % INFO - Second input is a scalar structure containing information about
6 % the input image source.
234 Image processing recipes in MATLAB®

Copyright Material - Provided by Taylor & Francis


Review Copy - Not for Redistribution

FIGURE 30.4
Snapshot of the Image Batch Processor App after processing the batch using the default function.

7 % RESULTS - A scalar struct with the processing results.


8
9 %----------------------
10 % Auto-generated by imageBatchProcessor App.
11 % When used by the App, this function will be called
12 % for each input image file automatically.
13 %----------------------
14
15 % Input parsing
16 im = varargin{1};
17 if nargin == 2
18 % Obtain information about the input image source
19 info = varargin{2};
20 end
21 %----------------------
22 % Replace the sample below with your code
23 imgray = im2gray(im);
24 bw = imbinarize(imgray);
25 results.imgray = imgray;
26 results.bw = bw;
27 %----------------------

We will use the template function to have a feel for the way the app per-
forms the batch processing and displays the results. Select the Process All
button on the drop-down menu of the “green play” button and click on it.
You will get the results at the GUI, as shown in Figure 30.4. Note that the de-
fault function creates two variables, imgray and bw, which can be displayed,
saved, and used separately if needed.
Recipe 30: Batch processing a set of images 235

Next, we will edit this function to add a post-processing step to remove


spurious pixels from the binarized image. We chose to apply a morphological
opening operation using a disk-shaped structuring element3 . The result-
ing image, bwClean,
Copyright is now- part
Material of the results
Provided of the&batch
by Taylor operation. The
Francis
modified function is shown in Listing 30.4.
Review Copy - Not for Redistribution
LISTING 30.4
Batch processing: modified image binarization function.
1 function results = myBinarize(varargin)
2

3 % Input parsing
4 im = varargin{1};
5

6 if nargin == 2
7 % Obtain information about the input image source
8 info = varargin{2};
9 end
10

11 % Our code
12 imgray = im2gray(im); % Convert to grayscale
13 bw = imbinarize(imgray); % Binarize
14 bwClean = imopen(bw,strel(”disk”,3,4)); % Remove spurious pixels
15

16 results.imgray = imgray;
17 results.bw = bw;
18 results.bwClean = bwClean;

After running the modified function across all 43 images, we can browse
the results. Figure 30.5 shows the details for a particular image whose orig-
inal file name is chip0005.jpg. The same figure shows the three options
available under the Export button. For the next step, we will export the re-
sult of all processed images to the workspace using the options indicated in
Figure 30.6.
Next, we will use the code in Listing 30.5 to convert the table to an array
(line 1), create separate variables for all images (original, gray, bw, and
bwClean) (lines 2–7), and display all images in a montage (Figure 30.7).

LISTING 30.5
Postprocessing and displaying results of batch processing.
1 A = table2array(allresults);
2 imgNumber = 5;
3 bw = A{imgNumber,1};
4 bwClean = A{imgNumber,2};
5 gray = A{imgNumber,3};
6 originalFileName = A{imgNumber,4};
7 original = imread(originalFileName);
8

9 montage({original, gray, bw, bwClean})


236 Image processing recipes in MATLAB®

Copyright Material - Provided by Taylor & Francis


Review Copy - Not for Redistribution

FIGURE 30.5
Snapshot of the Image Batch Processor App after processing the batch using the modified function
and getting ready to export the results.

FIGURE 30.6
Export options: choosing all fields and selecting a table as the data structure for variable all-
results.
Recipe 30: Batch processing a set of images 237

Copyright Material - Provided by Taylor & Francis


Review Copy - Not for Redistribution

FIGURE 30.7
Montage for an example image: original, grayscale, binarized, and post-processed.

FIGURE 30.8
Generate function options. Note that in this case, we chose to save the final result (bwClean) as
JPG files in the directory specified as outDir.
238 Image processing recipes in MATLAB®

Copyright Material - Provided by Taylor & Francis


Review Copy - Not for Redistribution

FIGURE 30.9
Example of batch processing for a normal chip image.

Lastly, we will click on the Export button one more time and select the
Generate function option. This opens a dialog box (Figure 30.8) and cre-
ates a customized function to be used next time we need to perform the
same operations on another set of images. The resulting function is called
batchmyBinarize and has more than 500 (!) lines of code.
The code in Listing 30.6 shows how the generated function can be used
to apply the same image processing pipeline to the images in another image
collection, in this case, the normal subfolder of the data set. Lines 1–2 config-
ure the source and output folders, line 4 calls the batch processing function,
and lines 5–7 display the result for a particular image (Figure 30.9).

LISTING 30.6
Reusing a function for batch processing on another image collection.
1 sourceDir = ”./PillQCdataset/pillQC-main/images/normal”;
2 outputDir = fullfile(sourceDir,’results’);
3
4 result = batchmyBinarize(sourceDir, outputDir);
5 exampleInput = imread(fullfile(sourceDir,’normal0024.jpg’));
6 exampleOutput = imread(fullfile(sourceDir,’normal0024_bwClean.tiff’));
7 montage({exampleInput exampleOutput})

Discussion (Recipe notes)


The ability to process images in batch mode is a time saver for numerous
image processing applications where images come from the same source
(sensor, camera, microscope) and have similar characteristics (size, number
of colors, file format, etc.).
In this recipe, you learned several ways to perform batch processing opera-
tions with different levels of interactivity involved in the process. In practice,
you might want to use the interactive mode to test how well a certain im-
age processing operation (or pipeline) works on a set of images and then
Recipe 30: Batch processing a set of images 239

export the code for applying the same methods to other sets of images
programmatically.

Copyright Material - Provided by Taylor & Francis


Review Copy - Not for Redistribution
Learn more about it
Useful MATLAB functions
Type the function name in the search field at www.mathworks.com/help/
matlab/
· imageBatchProcessor · table2array ·

MATLAB documentation, demos, and examples


• Image Batch Processor App
www.mathworks.com/help/images/ref/imagebatchprocessor-app.html
• Perform an Operation on a Sequence of Images
www.mathworks.com/help/images/process-image-sequences.html
• Process Folder of Images Using Image Batch Processor App
www.mathworks.com/help/images/batch-processing-using-the-image-
batch-processor-app.html
• View Image Sequences in Video Viewer
www.mathworks.com/help/images/view-image-sequences-in-video-
viewer-app.html
• Work with Image Sequences as Multidimensional Arrays
www.mathworks.com/help/images/what-is-an-image-sequence.html

Notes
1 Since R2023a, MATLAB supports opening the Image Batch Processor and load-
ing images stored in a specific folder (or image datastore) in a single line in the
command window. Therefore, steps 1–3 have become a single line of code.
2 Some lines were omitted for space reasons.
3 See Recipe 24.
Copyright Material - Provided by Taylor & Francis
Review Copy - Not for Redistribution
Bibliography

Copyright
1. Big Material
or multiresolution - Provided
image made from by Taylor
discrete & -Francis
blocks MATLAB. www.math
works.com/help/images/ref/blockedimage.html
Review Copy - Not for Redistribution . (Accessed on 2023-07-07).
2. CIE | International Commission on Illumination / Comission Internationale de
L’eclairage / Internationale Beleuchtungskommission http://cie.co.at/.
(Accessed on 2023-08-28).
3. Detect image anomalies using explainable fcdd network - MATLAB & Simulink.
www.mathworks.com/help/vision/ug/detect-image-anomalies-using-ex
plainable-one-class-classification-neural-network.html. (Accessed
on 2023-07-08).
4. How to posterize a photo in photoshop - Adobe. www.adobe.com/creativeclou
d/photography/discover/posterize-photo.html. (Accessed on 2023-07-06).
5. HSL and HSV - Wikipedia. https://en.wikipedia.org/wiki/HSL_and_HSV.
(Accessed on 2023-08-28).
6. A pill quality control dataset and associated anomaly detection example. https:
//github.com/matlab-deep-learning/pillQC. (Accessed on 2023-07-08).
7. Process blocked images efficiently using partial images or lower resolutions -
MATLAB & Simulink. www.mathworks.com/help/images/process- big-
image-efficiently.html. (Accessed on 2023-07-08).
8. Parameter values for ultra-high definition television systems for production and
international programme exchange. ITU-R Recommendation BT.2020-1, 2015.
9. J. M. M. Linhares, P. D. Pinto, and S. M. C. Nascimento. The number of discernible
colors in natural scenes. J. Opt. Soc. Am. A, 25(12):2918–2924, Dec 2008.
10. W. Burger and M. J. Burge. Digital Image Processing: an algorithmic introduction
using Java. Springer, New York, 2008.
11. X. Dong, G. Wang, Y. Pang, W. Li, J. Wen, W. Meng, and Y. Lu. Fast efficient
algorithm for enhancement of low lighting video. In 2011 IEEE International
Conference on Multimedia and Expo, pages 1–6, 2011.
12. R. C. Gonzalez and R. E. Woods. Digital Image Processing. Prentice-Hall, Upper
Saddle River, NJ, third edition, 2008.
13. A. K. Jain. Fundamentals of Digital Image Processing. Prentice-Hall, Englewood
Cliffs, 1989.
14. M. Kamlet. Nasa captures first air-to-air images of supersonic shockwave
interaction in flight. www.nasa.gov/centers/armstrong/features/supersonic-
shockwave-interaction.html. (Accessed on 2023-25-08).
15. M. Kass, A. Witkin, and D. Terzopoulos. Snakes: Active contour models.
International journal of computer vision, 1(4):321–331, 1988.
16. T. Kimpe and T. Tuytschaever. Increasing the number of gray shades in medical
display systems—how much is enough? Journal of digital imaging, 20(4):422–432,
2007.
17. Li Fei-Fei, R. Fergus, and P. Perona. Learning generative visual models from few
training examples: an incremental bayesian approach tested on 101 object cate-
gories. In 2004 Conference on Computer Vision and Pattern Recognition Workshop,
pages 178–178, 2004.
18. O. Marques. Practical Image and Video Processing Using MATLAB. Wiley - IEEE.
Wiley, 2011.

241
242 Bibliography

19. N. Otsu. A threshold selection method from gray-level histograms. IEEE


Transactions on Systems, Man, and Cybernetics, 9(1):62–66, 1979.
20. C. Poynton. Digital Video and HDTV Algorithms and Interfaces. Morgan Kaufmann
Publishers, San Francisco, 2003.
Copyright Material - Provided by Taylor & Francis
21. W. K. Pratt. Digital Image Processing. New York: Wiley, fourth edition, 2007.
Review
22. E. Reinhard, E. A.Copy
Khan, -A.Not for Redistribution
O. Akyz, and G. M. Johnson. Color Imaging:
Fundamentals and Applications. A. K. Peters, Ltd., USA, 2008.
23. R. Szeliski. Computer Vision Algorithms and Applications. Springer, London; New
York, 2011.
24. A. Zeileis, J. C. Fisher, K. Hornik, R. Ihaka, C. D. McWhite, P. Murrell, R. Stauffer,
and C. O. Wilke. colorspace: A toolbox for manipulating and assessing colors
and palettes. Journal of Statistical Software, 96(1):1–49, 2020.
25. A. Zeileis, K. Hornik, and P. Murrell. Escaping RGBland: Selecting colors for
statistical graphics. Computational Statistics & Data Analysis, 53(9):3259–3270,
2009.
26. K. Zuiderveld. Contrast limited adaptive histogram equalization. Graphics gems,
pages 474–485, 1994.
Index

A quantization 67, 70n


Copyright Material - Provided by Taylor & Francis
color channels, RGB images 58–61
acquisition, byReview
webcam 14–19
Copy - Not for color
Redistribution
images
active contours, segmentation 132–134
auto-contrast 77–78
adaptive thresholding 94–95, 126
individual channel histograms
non-uniform illumination 95
58–62
adjustment, RGB-based 208–209
luminance 61
affine transformations
red, green, blue (RGB) 77, 137 141,
combining 2D 42
230–231
transformation coefficients 39
color spaces
aspect ratio, resizing 31–32
converting 191–204
auto-contrast 77
hue 196
color image 77–78
perceptually uniform 198–199
specified range 77
printing 201–202
B TV and video 199–200
visualization 66
batch processing colormaps
displaying results 235–236 creating customized 216–217
histogram equalization 231 pseudocoloring 213–214
image binarization 231, 233–235 Commission Internationale de l’Eclairage
image collection 230–231 (CIE) 191, 198–199
image set 229–239 chromaticity diagrams colors spaces
image set interactively 231–238 193–194
postprocessing 235–236 compound operations, opening and
reusing function 238 closing 169–171
bin number, histograms 48–49 computational cost, look-up table versus
binarization 123–128 naïve approach 88–89
binary images, connected components computing
159–165 connected components 175–187
blockwise processing 225 coordinate type with regionprops
browsing, images 20–26 function 181–182
bwmorph 172–173 Feret features with regionprops
function 183–184
C grayscale pixel value with
CIE see Commission Internationale de regionprops function
l’Eclairage 184–185
CLAHE see Contrast-Limited Adaptive image type with regionprops function
Histogram Equalization 182–183
color scalar type with regionprops function
adjustments 205–211 179–180
auto-contrast image 77–78 connected components
dominant colors 67, 70n binary images 159–165
histogram equalization 205–206 computing 175–187
histograms and dominant colors finding and counting 161
63–70 imclearborder 164

243
244 Index

contrast E
adjustment 75–78
emboss 114–115
improvement by histogram
enhancement, low-light 209–210
equalization 205–207
Copyright Material
contrast improvement, histogram
- Provided by Taylor
erosion, primary&operations
Francis167–169
Review
equalization Copy - Not for Redistribution
205–207
Contrast-Limited Adaptive Histogram F
Equalization (CLAHE) 54–55 Feret features 183–184
conversion 10–13 with regionprops function 184
file format 10 file format, conversion 10
image class 11–12 filters
image type 10–11 Gaussian 103–105
quality settings 7 Laplacian 111
converting mean 102
color spaces 191–204 median 105–106
RGB to CMYK color space 201–202 non-uniform averaging 102–103
RGB to grayscale 200–201 open-close 172
RGB to HSV color space 196–197 sharpening 109–113
coordinate features 180–182 smoothing 101–108
with regionprops function and special effects 114–119
181–182 foreground-background segmentation,
cropping 35–36 global thresholding 160
interactive 36 frames per second (FPS) 16
programmatic 35
rotation 33
custom point transformation functions G
81–90 gamma correction 90–93
Gaussian bars, plotting 104
Gaussian filter 103–105
D geometric operations, MATLAB 30
geometric transformations 29–37
data class conversion global thresholding 125
Image Processing Toolbox (IPT) foreground-background
functions 12 segmentation 160
MATLAB functions 11 GrabCut 148–151
Digital Imaging and Communications in segmentation 149–150
Medicine (DICOM) 9n graph-based segmentation 148–155
dilation, primary operations 167–169 grayscale images
displaying histograms 47
batch processing results 235–238 median filter 106
histograms 47–48 pixel value 184–185
image contents 6 pseudocolors 215–216
image file 7 regionprops function 184–185
images 3–8, 222 statistics histograms 47–51
MATLAB images in Image Browser
App 4
H
distortions, combining multiple 42
dominant colors 67, 70n high dynamic range (HDR) 9n
and histograms 63–70 histogram equalization 52–55
Index 245

batch processing 231 image collection, batch processing


color 206 230–231
contrast improvement 205–207 image contents, displaying 6
Contrast-Limited Adaptive image features, and regionprops function
Copyright Material - Provided
Histogram Equalization (CLAHE)
by Taylor
182–183
& Francis
54–55 Review Copy - Not for image Redistribution
file, reading 5–6
and histogram matching 52–57 Image Processing Toolbox (IPT)
histogram matching 55–56 functions 12
and histogram equalization 52–57 image set
histograms batch processing 229–239
basic display 47–48 interactive processing 231–238
bin number 48–49 image type, conversion 10–11
and dominant colors 63–70 image type
grayscale image 47 features 182–183
grayscale image statistics 47–51 with regionprops function 182–183
improved display 48–51 ImageDatastore creation and processing
luminance component of color image 24–25
61 images, browsing 20–26
RGB color channels 58–61 imclearborder, connected components
and stem 50 164
HLS see hue, luminance, saturation individual channel histograms, color
HSI see hue, saturation, intensity images 58–62
HSV see hue, saturation, value input/output options, regionprops
hue 199, 206, 210 function 177–178
changes 77 intensity transformation functions 73–80
color spaces 196 International Color Consortium (ICC)
luminance, saturation (HLS) 204n 203
saturation, intensity (HSI) 204n inverse logarithmic transformation
saturation, value (HSV) 196–198, function 86
204n IPT see Image Processing Toolbox
hue-oriented color spaces 196–198, 204n
K
k-means clustering, segmentation
I 136–141
ICC see International Color Consortium
L
identity point transformation function
82 L*a*b* color model, segmentation
image acquisition 14–19 137–139
Image Acquisition Toolbox (IAT) 14, labeling connected components 162
18–19 Laplacian filter 111
image auto-contrast, color 77–78 Laplacian mask, sharpening 110–113
image binarization lazy snapping 148, 150–154
autogenerated template function segmentation 154
233–234 leveling, non-uniform illumination
batch processing 231, 233–236 94–96
modified function 235 linear functions 81–83
Image Browser App, MATLAB images 4 loading images 3–8
image class, conversion 11–12 large 222
246 Index

logarithmic transformation function 85 P


look-up table (LUT) 71, 81, 83, 87–89, 93
perceptually uniform color spaces
cost versus naïve approach 88–89
198–199
low-light enhancement 209–210
Copyright
luminance
Material - Provided by Taylor
piecewise-linear &functions
Francis 83–84
piecewise-linear point transformation
color imageReview
61 Copy - Not for Redistribution
function 84
hue, saturation (HLS) 196
PMF see probability mass function
posterization 117–118
M primary operations
dilation 167–169
masking, unsharp 109–110
erosion 167–169
MATLAB, geometric operations 30
printing color spaces 201–202
MATLAB functions, data class
probability mass function (PMF) 51
conversion 11
processing
MATLAB images, Image Browser App 4
blockwise 225
mean filter 102
large images 221–228
median filter 105–106
programmatically 229–232
grayscale image 106
pseudocoloring 212–217
morphological dilation and erosion 168
different colormaps 213–214
morphological image processing 95–96
synthetic grayscale image 214–215
non-uniform illumination 96
morphological techniques 173
basic 166–174 Q
open-close filter 172 quality settings, conversion 7
opening and closing 170 quantization 137–138
color 67, 70n
N
R
National Imagery Transmission Format
(NITF) 9n reading, image file 5–6
National Television System Committee red, green, blue (RGB)
(NTSC) 200 adjustment 208–209
negative point transformation function channels 58–59, 62–63
83 color channels 58–61
negative transformation function 78–80 color data 191, 194–195
non-linear functions 84–89 color image 77, 137, 141–142, 230–231
non-uniform averaging filter 102–103 conversion 10–11, 13, 61, 197–202
non-uniform illumination standard (sRGB) 191, 195
adaptive thresholding 94–95 true color 17, 60
leveling 94–96 wide-gamut 195
morphological 96 red, green, blue (RGB) color spaces
NTSC see National Television System 65–67, 195–198, 200, 205
Committee CMYK color space conversion
201–202
grayscale conversion 200–201
O
HSV color space conversion 196–197
open-close filter 170–172 region of interest (ROI) processing 108,
opening, image file 6 131–132, 157
Otsu thresholding 127 region-based segmentation 131–135
Index 247

regionprops function special effects, and filters 114–119


and coordinate features 181–182 standard red, green, blue (sRGB) 191,
and Feret features 183–184 195
and grayscale pixel value 184–185 stem, and histograms 50
Copyright Material - Provided
and image features 182–183
by Taylor & Francis
structuring element (SE), designing 172
Review
input/output optionsCopy
177–178- Not for superpixel
Redistribution
oversegmentation, SLIC
and scalar features 179–180 143–147
resizing 30–32 synthetic grayscale image, pseudocolors
aspect ratio 31–32 214–215
and scaling 40
RGB see red, green, blue T
ROI see region of interest thresholding, adaptive 94–95, 126
rotation 32–33, 40–41 transformation coefficients, affine
cropped output 33 transformations 39
two-D affine transformation 41 transformation functions
custom point 81–90
S
intensity 73–80
saturation inverse logarithmic 86
color channel modifying 206–207 logarithmic 85
hue, intensity (HSI) 204n negative 78–80
hue, luminance (HLS) 204n negative point 83
hue, value (HSV) 196–198, 206–207 piecewise-linear point 84
saving images 3–8 sigmoid 88
scalar features 178–180 translation 33–35
and regionprops function 179–180 cropped output 34
scaling TV and video color spaces 199–200
2D affine transformation 40 two-D affine transformations
and resizing 40 combining 42
SE see structuring element rotation 41
segmentation 138–141 shearing 41–42
active contours 132–134
complex image 139 U
foreground-background 160 Universal Serial Bus (USB) 14–15
GrabCut 149–150 Video Class (UVC) devices 14
graph-based 148–155 unsharp masking 109–110
k-means clustering 136–141
L*a*b* color model 137–139 V
lazy snapping 150–154
region-based 131–135 vignette 116–117
sepia 115–116 visualization, color spaces 66
sharpening
filters 109–113 W
Laplacian mask 110–113 warping 38–44
shearing 41–42 webcam
two-D affine transformation 41–42 acquisition 14–19
sigmoid function, RGB contrast 208–209 basic setup 15
sigmoid transformation function 88 image sequence acquisition 17
simple linear iterative clustering (SLIC) improved setup 17
143–147 wide-gamut red, green, blue (RGB) 195
smoothing filters 101–108 writing image to a file 7
Copyright Material - Provided by Taylor & Francis
Review Copy - Not for Redistribution

You might also like