Introduction to Python in Earth Science Data Analysis
Introduction to Python in Earth Science Data Analysis
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
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.
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.
DOI: 10.1201/9781003170198
Typeset in Palatino
by Newgen Publishing UK
Copyright Material - Provided by Taylor & Francis
Review Copy - Not for Redistribution
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
ix
x Contents
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
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.
• 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:
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
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.
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’)
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
FIGURE 1.1
Using the Image Browser App to browse through demo images and obtain additional information
about the trailer.jpg image.
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’);
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
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.
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.
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.
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.
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:
An image can be read from disk using imread and saved back to disk
using imwrite2 .
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
• 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.
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.
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:
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:
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
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®
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).
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.
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
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®
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
FIGURE 4.4
Image Browser: preview. The selected image and its name, size, and class are presented in the
Preview area.
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)
[img_2,fileinfo] = readimage(water_lilly,30);
figure, imshow(img_2)
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
autoLabels = imds.Labels;
uLabels = unique(imds.Labels)
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
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
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
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
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.
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
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
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);
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®
500
500
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.
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.
FIGURE 5.8
Image cropping example. Original image: courtesy of The MathWorks.
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);
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);
In Recipe 6, we will expand upon this topic and teach you how to perform
image warping and other geometric operations.
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
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:
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.
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®
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);
FIGURE 6.1
Image resizing example. Original image: courtesy of The MathWorks.
Recipe 6: Image warping 41
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);
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);
FIGURE 6.4
Combining multiple distortions in one pass. Original image: courtesy of The MathWorks.
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
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.
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.
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
FIGURE 7.1
An image and its histogram. Original image: courtesy of The MathWorks.
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
3000
2000
1000
0
0 50 100 150 200 250
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:
Image
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
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.
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.
52 DOI: 10.1201/9781003170198-11
Recipe 8: Histogram equalization and histogram matching 53
3000
Copyright Material - Provided by Taylor & Francis
Review Copy - Not 2000
for Redistribution
1000
0
0 50 100 150 200 250
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.
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®
0
0 50 100 150 200 250
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’)
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
2000
Copyright Material - Provided by Taylor & Francis
Review Copy - Not for Redistribution
0
0 50 100 150 200 250
2000
0
0 50 100 150 200 250
10000
5000
0
0 50 100 150 200 250
FIGURE 8.3
Histogram matching. Original image: courtesy of The MathWorks.
• 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
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.
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.
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
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
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®
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.
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.
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)
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
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.
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®
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®
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
FIGURE 10.5
Color quantization (b) and histogram (c) of the image lighthouse.png (a), for 𝑛 = 4. Original
image: courtesy of The MathWorks.
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®
Part IV
Point transformations
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:
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®
FIGURE 11.1
Basic intensity transformation functions.
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:
FIGURE 11.2
Examples of gray level transformations for contrast enhancement. Redrawn from [12].
Examples
𝐿−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®
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
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®
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
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®
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).
Steps (Preparation)
The process consists of these steps:
DOI: 10.1201/9781003170198-16 81
82 Image processing recipes in MATLAB®
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.
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
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
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
10 img_1 = imread(’coins.png’);
11 img_2 = intlut(img_1,my_lut);
12
𝑠 = 𝑐 ⋅ log(1 + 𝑟) (12.2)
Recipe 12: Custom point transformation functions 85
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
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
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
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
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
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®
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
8 img_1 = imread(’vpfig.png’);
9 img_2 = intlut(img_1, sigmoid_function);
10 figure, montage({img_1, img_2})
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
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
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
𝑠 = 𝑐 ⋅ 𝑟𝛾 (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:
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
FIGURE 13.1
Examples of power-law transformations for different values of 𝛾.
Steps (Preparation)
The process consists of these steps:
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®
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
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.
Steps (Preparation)
Method 1: Adaptive thresholding
For background illumination correction using adaptive thresholding, the
main steps are:
94 DOI: 10.1201/9781003170198-18
Recipe 14: Leveling non-uniform illumination 95
FIGURE 14.1
Background illumination correction using adaptive thresholding. Original image: courtesy of
The MathWorks.
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).
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).
FIGURE 14.2
Background illumination correction using morphological image processing. Original image:
courtesy of The MathWorks.
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
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).
Steps (Preparation)
The process of image smoothing in MATLAB usually follows these steps and
illustrates them for four different options of filters:
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.
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’);
1 2 1
1 ⎡
ℎ(𝑥, 𝑦) = ⎢ 2 4 2 ⎤
⎥ (15.1)
16
⎣ 1 2 1 ⎦
Recipe 15: Smoothing filters 103
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
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’);
1 −(𝑥2 + 𝑦2 )
ℎ(𝑥, 𝑦) = exp [ ] (15.2)
2𝜋𝜎2 2𝜎2
1. The Euclidean distance between a given point and the center of the
mask.
104 Image processing recipes in MATLAB®
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.
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’);
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.
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
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.
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.
• ‘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.
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.
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.
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
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’);
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®
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
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.
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
FIGURE 17.1
Emboss special effect. (a) Original image; (b) after embossing. Original image: courtesy of The
MathWorks.
−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®
FIGURE 17.2
Sepia special effect. (a) Original image; (b) after sepia. Original image: courtesy of The Math-
Works.
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
FIGURE 17.3
Vignette special effect. (a) Original image; (b) Gaussian function of 𝜎 = 350; (c) after vignette.
Original image: courtesy of The MathWorks.
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®
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’)
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.
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
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.
This recipe teaches you how to binarize a grayscale image using thresholding
techniques.
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.
Steps (Preparation)
For binarization using a global image threshold, the main steps are:
Recipe 18: Image binarization 125
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®
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’)
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)
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.
FIGURE 18.7
Effect of illumination (left) and noise (right) on thresholding. Original image: courtesy of The
MathWorks.
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
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.
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
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})
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 .
Steps (Preparation)
For image quantization using k-means clustering, the main steps are:
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 .
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
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
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.
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®
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})
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.
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
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
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.
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.
· superpixels · superpixels3 ·
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
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
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.
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.
FIGURE 22.2
A closer look at some errors: (a) superpixel boundaries (cyan) overlaid on the input image;
(b) overlay image.
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
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)
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
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.
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.
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.
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:
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}
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®
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
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®
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:
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)])
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.
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.
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®
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 𝐸.
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
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 𝐶.
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
FIGURE 24.4
Example of applying morphological erosion and dilation with SEs of 𝑟 = 3 and 𝑟 = 5.
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®
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))’)
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®
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
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.
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
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 §ions,
Francisfeatures (in-
put parameter properties for the regionprops
Review Copy - Not for Redistribution function) are grouped
according to different criteria for a better organization.
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):
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®
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.
FIGURE 25.3
Features of coordinate type computed with regionprops function. (a) ”Centroid”;
(b) ”BoundingBox”; (c) ”ConvexHull”; (d) ”Extrema”.
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
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’)
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.
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
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®
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’)
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
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
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.
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
This recipe teaches the main characteristics of the color spaces available in
MATLAB and how to perform conversions among them.
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
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.
• 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.
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.
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)
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
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
• 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
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].
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
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.
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
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®
This recipe teaches you how to adjust color images to make them more
interpretable or visually pleasant, using different color spaces.
Steps (Preparation)
1: Contrast improvement using histogram equalization
1. Load the image into the workspace.
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
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®
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.
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
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
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).
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®
FIGURE 27.4
Low-light image enhancement. (a) Original image. (b) Processed image with amount=0.1. (c)
Processed image with amount=0.9.
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
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.
cm =
1 0 0
1 1 1
0 0 1
0 0 0
1 0 0
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’)
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).
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
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’)
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.
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
This recipe teaches you how to read, store, process, and display very large
images using MATLAB.
Steps (Preparation)
The main steps for processing very large images are:
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
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]
FIGURE 29.1
Viewing a large blockedImage with grid lines indicating blocks, using the bigimageshow
function.
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);
FIGURE 29.3
Negative of a blockedImage processed block-by-block.
FIGURE 29.4
Edge detection results for a blockedImage processed block-by-block: (a) incorrect result
(blockiness effect); (b) correct result.
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
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:
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)
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:
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
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®
FIGURE 30.4
Snapshot of the Image Batch Processor App after processing the batch using the default function.
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
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
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
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®
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})
export the code for applying the same methods to other sets of images
programmatically.
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
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