Image Processing
– An Introduction
Prof. Zouhair GUENNOUN
zouhair@emi.ac.ma
06-66.29.37.23
Filière Réseaux & Télécommunications - RTC
Source: Digital Image Processing using MATLAB. 3/E. Rafael C. Gonzalez, Richard E. Woods. Prentice Hall. 2008 Chp 2
Digital Image Processing - An Introduction 1
Outline
• Introduction
• Gray images in Matlab
Digital Image Processing - An Introduction 2
Introduction to Monochrome Images
• An image is the optical representation of objects illuminated by a
light source.
• Since we want to process images using a computer, we represent
them as functions of discrete spatial variables.
• For monochrome (black-and-white) images,
a scalar function f(i, j) can be used to represent
the light intensity at each spatial coordinate (i, j).
• Next Figure illustrates the convention we will use
for spatial coordinates to represent images.
– If we assume the coordinates to be a set of positive
integers, for example i=1,…,M and j=1,…,N, then
an image can be conveniently represented by a matrix.
Image Processing 3
f 1,1 f 1,2 f 1, N
f 2,1 f 2, n f 2, N
f i, j
f M ,1 f M ,2 f M , N
• We call this an M×N image, and the elements of the matrix are
known as pixels.
• The pixels in digital images usually take on integer values in the
finite range, 0f(i,j)Lmax
– where 0 represents the minimum intensity level (black), and Lmax is
the maximum intensity level (white) that the digital image can take on.
The interval [0,Lmax] is known as a gray scale.
Image Processing 4
Outline
• Introduction
• Gray images in Matlab
– Reading Images
• Images Format
– Displaying Images
– Writing Images
– Data Classes
– Image Types
• Intensity images
• Binary images
– Converting between data classes & image types
Digital Image Processing - An Introduction 5
Reading Images
• Images are read into the MATLAB environment using
function imread, whose syntax is:
>> imread('filename')
– filename is a string containing the complete name of the image
file (including any applicable extension)
– Function size gives the row and column dimensions (MxN) of an
image:
• Example:
>> f = imread(‘chest-xray.tif');
>> [M, N] = size(f)
Digital Image Processing - An Introduction 6
Images format
Format Name Description Recognized Extensions
TIFF Tagged Image File Format .tif,.tiff
JPEG Joint Photographic Experts Group .jpg, .jpeg
GIF Graphics Interchange Format* .gif
BMP Windows Bitmap .bmp
PNG Portable Network Graphics .png
XWD X Window Dump .xwd
* GIF is supported by imread, but not by imwrite
Digital Image Processing - An Introduction 7
Displaying Images
• Images are displayed in MATLAB using function
imshow, which has the basic syntax:
>> imshow(f, G)
– where f is an image array, and G is the number of
intensity levels used to display it.
– If G is omitted, it defaults to 256 levels.
• Another way of displaying images is by using the
image command, combined with the colormap
command:
>> image (f)
Digital Image Processing - An Introduction 8
• >> image(f);
>> colormap(gray(256));
>> axis(’image’);
• The image command works for both type uint8 and double images.
• The colormap command specifies the range of displayed gray levels,
assigning black to 0 and white to 255.
• Note:
– The maximum number of gray levels that will be displayed on the
monitor is 255, even if the image values take on a continuous range.
– If any pixel values are outside the range 0 to 255 (after processing),
they will be clipped to 0 or 255 respectively in the displayed image.
– A floating point pixel value will be rounded down (“floored”) to an
integer before it is displayed.
Image Processing 9
Digital Image Processing - An Introduction 10
Displaying Images
• The syntax :
>> imshow(f, [low high])
displays as black all values less than or equal to low, and as
white all values greater than or equal to high. The values in
between are displayed as intermediate intensity values
using the default number of levels.
• The syntax:
imshow(f, [ ])
sets variable low to the minimum value of array f and high
to its maximum value. This form of imshow is useful for
displaying images that have a low dynamic range or that
have positive and negative values.
Digital Image Processing - An Introduction 11
Digital Image Processing - An Introduction 12
Reading & Displaying an Image
• The following statements read from disk an image
called ‘rose1024. tif’, extract basic information about
the image, and display it using imshow:
>> f = imread('rose1024.tif');
>> imshow(f)
Digital Image Processing - An Introduction 15
Writing Images
• Images are written to disk using function
imwrite, which has the following basic syntax:
>> imwrite(f, 'filename')
– With this syntax, the string contained in filename
must include a recognized file format extension.
– Alternatively, the desired format can be specified
explicitly with a third input argument.
Digital Image Processing - An Introduction 16
Example
• The following command writes f to a TIFF file
named ‘patient10_run1’:
>> imwrite(f, 'patient10_run1', 'tif')
• Or, alternatively,
>> imwrite(f, 'patient10_run1.tif')
Digital Image Processing - An Introduction 17
Writing Images
• A more general imwrite syntax applicable only
to JPEG images is:
>> imwrite(f, 'filename.jpg', 'quality', q)
– where q is an integer between 0 and 100 (the
lower the number the higher the degradation due
to JPEG compression).
Digital Image Processing - An Introduction 18
Example
• The figure shows an image, f, typical of sequences of
images resulting from a given chemical process
(‘bubbles.tif’).
• It is desired to transmit these images on a routine basis to a
central site for visual and/or automated inspection.
• In order to reduce storage and transmission time, it is
important that the images be compressed as much as
possible while not degrading their visual appearance
beyond a reasonable level.
– In this case "reasonable" means no perceptible false contouring.
Digital Image Processing - An Introduction 19
Digital Image Processing - An Introduction 20
Example
• The other Figures show the results obtained by
writing image f to disk (in JPEG format),
with q = 50, 25,15, 5, and 0, respectively.
• For q = 25 the applicable syntax is
>> imwrite(f, 'bubbles25.jpg', 'quality', 25)
• The image for q = 15 has false contouring that is
barely visible, but this effect becomes quite
pronounced for q = 5 and q = 0.
– Thus, an acceptable solution with some margin for
error is to compress the images with q = 25.
Digital Image Processing - An Introduction 21
• In order to get an idea of the compression
achieved and to obtain other image file
details, we can use function imfinfo, which
has the syntax:
>> imfinfo filename
– where filename is the complete file name of the
image stored in disk, or
>> imfinfo(‘filename’)
Digital Image Processing - An Introduction 22
Example
•
>> imfinfo bubbles25.jpg
outputs the following information (note that some fields contain no
information in this case):
Filename: 'bubbles25.jpg‘
FileModDate: '04-Jan-2003 12:31:26‘
FileSize: 13849
Format: 'jpg'
FormatVersion: ‘’
Width: 714
Height: 682
BitDepth: 8
ColorType: 'grayscale'
FormatSignature: ‘’
Comment : {}
Digital Image Processing - An Introduction 23
• The number of bytes in the original image is computed
simply by multiplying Width by Height by BitDepth and
dividing the result by 8. The result is 486948.
• Dividing this by FileSize gives the compression ratio:
(486948/13849) = 35.16. This compression ratio was
achieved while maintaining image quality consistent
with the requirements of the application.
– In addition to the obvious advantages in storage space, this
reduction allows the transmission of approximately 35
times the amount of uncompressed data per unit time.
Digital Image Processing - An Introduction 24
• The information fields displayed by imfinfo can be captured
into a so-called structure variable that can be used for
subsequent computations.
• Using the preceding image as an example, and assigning
the name K to the structure variable, we use the syntax
>> K = imfinfo('bubbles25.jpg');
to store into variable K all the information generated by
command imfinfo.
– The information generated by imfinfo is appended to the
structure variable by means of fields, separated from K by a dot.
– For example, the image height and width are now stored in
structure fields K.Height and K.Width.
Digital Image Processing - An Introduction 25
Example
• Consider the following use of structure
variable K to compute the compression ratio
for bubbles25.jpg:
>> K = imfinfo('bubbles25.jpg');
>> image_bytes = K.Width*K.Height*K.BitDepth/8;
>> compressed_bytes = K.FileSize;
>> compression_ratio = image_bytes/compressed_bytes
compression_ratio = 35.1612
Digital Image Processing - An Introduction 26
• Note that imfinfo was used in two different
ways.
– The first was to type imfinfo bubbles25.jpg at the
prompt, which resulted in the information being
displayed on the screen.
– The second was to type
K = imfinfo ( ' bubbles25.jpg'), which resulted in
the information generated by imfinfo being stored
in K.
Digital Image Processing - An Introduction 27
• A more general imwrite syntax applicable only
to tif images has the form:
>> imwrite(g, 'filename.tif‘, 'compression',
'parameter', …
'resolution’, [colres rowres]);
Digital Image Processing - An Introduction 28
• where
– ' parameter ' can have one of the following
principal values:
• ' none ' indicates no compression;
• 'packbits' indicates packbits compression (the default
for nonbinary images); and
• ' ccitt ' indicates ccitt compression (the default for
binary images).
Digital Image Processing - An Introduction 29
• where
– The 1x2 array [colres rowres] contains two
integers that give the column resolution and row
resolution in dots-per-unit (the default values are
[72 72]).
• For example, if the image dimensions are in inches,
colres is the number of dots (pixels) per inch (dpi) in the
vertical direction, and similarly for rowres in the
horizontal direction.
• Specifying the resolution by a single scalar, res, is
equivalent to writing [res res].
Digital Image Processing - An Introduction 30
Example
• Figure ‘cktboard.jpg’ is an 8-bit X-ray image of
a circuit board generated during quality
inspection. It is in jpg format, at 200 dpi. The
image is of size 450x450 pixels, so its
dimensions are 2.25x2.25 inches.
– We want to store this image in tif format, with no
compression, under the name sf.
– In addition, we want to reduce the size of the
image to 1.5x1.5 inches while keeping the pixel
count at 450x450.
Digital Image Processing - An Introduction 31
• The following statement yields the desired
result:
>> imwrite(f, 'sf.tif', 'compression', 'none',
'resolution', [300 300])
– The values of the vector [colres rowres] were
determined by multiplying 200 dpi by the ratio
2.25/1.5, which gives 300 dpi.
Digital Image Processing - An Introduction 32
– Rather than doing the computation manually, we could
write
>> res = round(200*2.25/1.5);
>> imwrite (f, 'sf.tif, 'compression', 'none' ,'resolution', res)
• where function round rounds its argument to the nearest
integer.
• It is important to note that the number of pixels was not
changed by these commands.
• Only the scale of the image changed. The original 450x450
image at 200 dpi is of size 2.25x2.25 inches. The new 300-dpi
image is identical, except that its 450x450 pixels are
distributed over a 1.5x1.5-inch area.
• Processes such as this are useful for controlling the size of an
image in a printed document without sacrificing resolution.
Digital Image Processing - An Introduction 33
• Often, it is necessary to export images to disk the way they appear
on the MATLAB desktop. This is especially true with plots.
• The contents of a figure window can be exported to disk in two
ways.
– The first is to use the File pull-down menu in the figure window and
then choose Export. With this option, the user can select a location,
file name, and format.
– More control over export parameters is obtained by using the print
command:
>> print –f no –d fileformat –r resno filename
• where no refers to the figure number in the figure window of interest,
• fileformat refers to one of the file formats,
• resno is the resolution in dpi, and
• filename is the name we wish to assign the file.
Digital Image Processing - An Introduction 34
• For example, to export the contents of the figure
window as a tif file at 300 dpi, and under the name
hi_res_rose, we would type
>> print -f1 -dtiff -r300 hi_res_rose
– This command sends the file hi_res_rose. tif to the current
directory.
• If we simply type print at the prompt, MATLAB prints
(to the default printer) the contents of the last figure
window displayed. It is possible also to specify other
options with print, such as a specific printing device.
Digital Image Processing - An Introduction 35
Pixel Distributions –
Histogram of an Image
• The histogram of a digital image shows how its
pixel intensities are distributed.
– The pixel intensities vary
along the horizontal axis,
and the number of pixels
at each intensity is plotted
vertically, usually as
a bar graph.
– A typical histogram of an
8-bit image is shown.
Image Processing 36
• An example of using hist to plot a histogram of
a matrix would be
>> x=reshape(A,1,M*N);
>> hist(x,0:255);
– where A is an image, and M and N are the number
of rows and columns in A.
– The reshape command is creating a row vector
out of the image matrix, and the hist command
plots a histogram with bins centered at [0 : 255].
Digital Image Processing - An Introduction 37
Generating and Plotting Image Histograms
• The histogram of a digital image with L total possible
intensity levels in the range [0, G] is defined as the discrete
function:
h(rk) = nk
– where rk is the rth intensity level in the interval [0, G] and nk is
the number of pixels in the image whose intensity level is rk.
– The value of G is 255 for images of class uint8, 65535 for images
of class uint16, and 1.0 for images of class double.
– Keep in mind that indices in MATLAB cannot be 0, so r1
corresponds to intensity level 0, r2 corresponds to intensity level
1, and so on, with rL corresponding to level G.
– Note also that G = L — 1 for images of class uint8 and uint16.
Intensity Transformations and Spatial
38
Filtering
• Often, it is useful to work with normalized
histograms, obtained simply by dividing all
elements of h(rk) by the total number of pixels in
the image, which we denote by n:
p(rk) = h(rk)/n = nk/n
– for k = 1, 2,..., L.
– From basic probability, we recognize p(rk) as an
estimate of the probability of occurrence of intensity
level rk.
Intensity Transformations and Spatial
39
Filtering
• The core function in the toolbox for dealing with image
histograms is imhist, which has the following basic syntax:
h = imhist(f, b)
– where f is the input image, h is its histogram, h(rk), and b is
the number of bins used in forming the histogram (if b is
not included in the argument, b = 256 is used by default).
– A bin is simply a subdivision of the intensity scale.
• For example, if we are working with uint8 images and we Iet b = 2, then
the intensity scale is subdivided into two ranges: 0 to 127 and 128 to 255.
• The resulting histogram will have two values: h(1) equal to the number of
pixels in the image with values in the interval [0,127], and h(2) equal to
the number of pixels withTransformations
Intensity values in the interval [128,255].
and Spatial
40
Filtering
• We obtain the normalized histogram simply by
using the expression:
p = imhist(f, b)/numel(f)
– Function numel(f) gives the number of elements
in array f (i.e., the number of pixels in the image).
Intensity Transformations and Spatial
41
Filtering
• Consider the image, f, from ‘breast_xray.tif’. The
simplest way to plot its histogram is to use imhist with
no output specified:
>> imhist(f);
– Figure (a) shows the result. This is the histogram display
default in the toolbox.
• There are many other ways to plot a histogram, and we
take this opportunity to explain some of the plotting
options in MATLAB that are representative of those
used in image processing applications.
Intensity Transformations and Spatial
42
Filtering
Intensity Transformations and Spatial
43
Filtering
• Histograms often are plotted using bar graphs. For this
purpose we can use the function:
>> bar(horz, v, width)
– where v is a row vector containing the points to be plotted, horz
is a vector of the same dimension as v that contains the
increments of the horizontal scale, and width is a number
between 0 and 1.
– If horz is omitted, the horizontal axis is divided in units from 0 to
length(v).
– When width is 1, the bars touch; when it is 0, the bars are
simply vertical lines, as in Fig. (a). The default value is 0.8.
• When plotting a bar graph, it is customary to reduce the
resolution of the horizontal axis by dividing it into bands.
Intensity Transformations and Spatial
44
Filtering
• The following statements produce a bar graph, with the
horizontal axis divided into groups of 10 levels, as shown in
figure (b):
>> h = imhist(f);
>> h1 = h(1:10:256);
>> horz = 1:10:256;
>> bar(horz, h1)
>> axis([0 255 0 15000])
>> set(gca, 'xtick', 0:50:255)
>> set(gca, 'ytick’, 0:2000:15000)
• The peak located at the high end of the intensity scale in
Fig. (a) is missing in the bar graph as a result of the larger
horizontal increments used in the plot.
Intensity Transformations and Spatial
45
Filtering
• The axis function has the syntax:
axis([horzmin horzmax vertmin vertmax])
• gca means "get current axis," (i.e., the axes of the
figure last displayed) and xtick and ytick set the
horizontal and vertical axes ticks in the intervals shown.
• Axis labels can be added to the horizontal and vertical
axes of a graph using the functions xlabel ylabel
xlabel('text string', 'fontsize', size)
ylabel('text string', 'fontsize', size)
– where size is the font size in points.
Intensity Transformations and Spatial
46
Filtering
• Text can be added to the body of the figure by using
function text, as follows:
text(xloc, yloc, 'text string', 'fontsize', size)
– where xloc and yloc define the location where text starts.
• It is important to note that functions that set axis values
and labels are used after the function has been plotted.
• A title can be added to a plot using function title, whose
basic syntax is
title('titlestring')
– where titlestring is the string of characters that will appear on
the title, centered above the plot.
Intensity Transformations and Spatial
47
Filtering
• A stem graph is similar to a bar graph. The syntax is:
stem(horz, v, 'color_linestyle_marker', 'fill')
– where v is row vector containing the points to be plotted, and horz is
as described for bar. The argument, color_linestyle_marker is a triplet
of values.
– For example, stem(v, 'r--s' ) produces a stem plot where the lines and
markers are red, the lines are dashed, and the markers are squares.
– If fill is used, and the marker is a circle, square, or diamond, the
marker is filled with the color specified in color.
• The default color is black, the line default is solid, and the default marker is a
circle.
• The stem graph in Fig. (c) was obtained using the statements
>> h = imhist(f); h1 = h(1:10:256); horz = 1:10:256;
>> stem(horz, h1, 'fill'), axis([0 255 0 15000])
>> set(gca, 'xtick', [0:50:255]), set(gca, 'ytick', [0:2000:15000])
Intensity Transformations and Spatial
48
Filtering
Symbol Color Symbol Line Style Symbol Marker
k Black - Solid + Plus sign
w White -- Dashed o Circle
r Red : Dotted * Asterisk
g Green -. Dash-dot . Point
b Blue none No line x Cross
c Cyan s Square
y Yellow d Diamond
m Magenta none No marker
Intensity Transformations and Spatial
49
Filtering
• Finally, we consider function plot, which plots a set of
points by linking them with straight lines. The syntax is:
plot(horz, v, 'color_linestyle_marker')
– where the arguments are as defined previously for stem plots.
– As in stem, the attributes in plot can be specified as a triplet.
• When using none for linestyle or for marker, the attributes must be
specified individually.
• For example, the command:
>> plot(horz, v, 'color', 'g', 'linestyle', 'none’,
'marker', 's')
– plots green squares without Connecting lines between them.
– The defaults for plot are solid black lines with no markers.
Intensity Transformations and Spatial
50
Filtering
• The plot in Fig. (d) was obtained using the following statements:
>> h = imhist(f);
>> plot(h) % Use the default values.
>> axis([0 255 0 15000])
>> set(gca, 'xtick', [0:50:255])
>> set(gca, 'ytick', [0:2000:15000])
• It is possible to set the limits and ticks automatically by using functions
ylim and xlim, which, for our purposes here, have the syntax forms:
ylim('auto') xlim('auto')
• Among other possible variations of the syntax for these two functions,
there is a manual option, given by
ylim([ymin ymax]) xlim([xmin xmax])
– which allows manual specification of the limits.
– If the limits are specified for only one axis, the limits on the other axis are set
to 'auto' by default
Intensity Transformations and Spatial
51
Filtering
Data Classes
• Although we work with integer coordinates,
the values of pixels themselves are not
restricted to be integers in MATLAB.
– Next Table lists the various data classes supported
by MATLAB for representing pixel values.
• The first eight entries in the table are referred to as
numeric data classes.
• The ninth entry is the char class and, the last entry is
referred to as the logical data class.
Digital Image Processing - An Introduction 52
Data Classes
Name Description
Double-precision, floating-point numbers in the approximate
double
range -10308 to 10308 (8 bytes per element).
uint8 Unsigned 8-bit integers in the range [0,255] (1 byte per element),
uint16 Unsigned 16-bit integers in the range [0,65535] (2 bytes per element).
uint32 Unsigned 32-bit integers in the range [0,4294967295] (4 bytes per element).
int8 Signed 8-bit integers in the range [-128,127] (1 byte per element).
int16 Signed 16-bit integers in the range [-32768,32767] (2 bytes per element).
Signed 32-bit integers in the range [-2147483648, 2147483647] (4 bytes per
int32
element),
Single-precision floating-point numbers with values in the approximate range
single
-1038 to 1038 (4 bytes per element),
char Characters (2 bytes per element),
logical Values are 0 or 1 (1 byte per element).
Digital Image Processing - An Introduction 53
Data Classes
• All numeric computations in MATLAB are done using
double quantities, so this is also a frequent data class
encountered in image processing applications.
• Class uint8 also is encountered frequently, especially
when reading data from storage devices, as 8-bit
images are the most common representations found
in practice.
Digital Image Processing - An Introduction 54
Data Classes
• Data class double requires 8 bytes to represent a number,
uint8 and int8 require 1 byte each, uint16 and int16 require 2
bytes, and uint32, int32, and single, require 4 bytes each.
• The char data class holds characters in Unicode representation.
– A character string is merely a 1 x n array of characters.
• A logical array contains only the values 0 and 1, with each
element being stored in memory using one byte per element.
Logical arrays are created by using function logical or by using
relational operators
Digital Image Processing - An Introduction 55
Image Types
• The IPT toolbox supports four types of images:
– Intensity images
– Binary images
– RGB images
– Indexed images
• Most monochrome image processing
operations are carried out using binary or
intensity images
Digital Image Processing - An Introduction 56
Intensity Images
• An intensity image is a data matrix whose values
have been scaled to represent intensities.
– When the elements of an intensity image are of class
uint8, or class uint16, they have integer values in the
range [0,255] and [0,65535], respectively.
– If the image is of class double, the values are floating-
point numbers.
• Values of scaled, class double intensity images are in the
range [0,1] by convention.
Digital Image Processing - An Introduction 57
Binary Images
• Binary images have a very specific meaning in MATLAB. A
binary image is a logical array of 0s and 1s.
– Thus, an array of 0s and 1s whose values are of data class, say,
uint8, is not considered a binary image in MATLAB.
• A numeric array is converted to binary using function
logical.
– if A is a numeric array consisting of 0s and 1s, we create a logical
array B using the statement:
>> B = logical(A)
– If A contains elements other than 0s and 1s, use of the logical
function converts all nonzero quantities to logical 1s and all
entries with value 0 to logical 0s.
– Using relational and logical operators also creates logical arrays.
Digital Image Processing - An Introduction 58
Binary Images
• To test if an array is logical we use the islogical
function:
>> islogical(C)
– If C is a logical array, this function returns a 1.
Otherwise it returns a 0.
– Logical arrays can be converted to numeric arrays
using the data class conversion functions.
Digital Image Processing - An Introduction 59
Converting between
Data Classes and Image Types
• Converting between data classes and image
types is a frequent operation in image
processing applications.
– When converting between data classes, it is
important to keep in mind the value ranges for
each data class.
Digital Image Processing - An Introduction 60
Converting between Data Classes
• Converting between data classes is straightforward.
The general syntax is:
>> B = data_class_name(A)
– Suppose that A is an array of class uint8. A double-precision array, B, is
generated by the command:
>> B = double(A)
• This conversion is used frequently because MATLAB expects operands in
numerical computations to be double-precision, floating-point numbers.
– If C is an array of class double in which all values are in the range
[0,255] (but possibly containing fractional values), it can be converted
to an uint8 array with the command
>> D = uint8(C)
Digital Image Processing - An Introduction 61
Converting between Data Classes
• If an array of class double has any values outside the range
[0,255] and it is converted to class uint8, MATLAB converts
to 0 all values that are less than 0, and converts to 255 all
values that are greater than 255.
– Numbers in between are converted to integers by discarding
their fractional parts.
– Thus, proper scaling of a double array so that its elements are in
the range [0,255] is necessary before converting it to uint8.
• Converting any of the numeric data classes to logical results
in an array with logical 1s in locations where the input array
had nonzero values, and logical 0s in places where the
input array contained 0s.
Digital Image Processing - An Introduction 62
Converting between Image Classes and Types
• The IPT toolbox provides specific functions
that perform the scaling necessary to convert
between image classes and types.
Name Converts Input to: Valid Input Image Data Classes
im2uint8 uint8 logical, uint8, uint16, and double
im2uint16 uint16 logical, uint8, uint16, and double
mat2gray double (in range [0,1]) double
im2double double logical, uint8, uint16, and double
im2bw logical uint8, uint16, and double
Digital Image Processing - An Introduction 63
Function im2uint8
• Function im2uint8 detects the data class of the input
and performs all the necessary scaling for the toolbox
to recognize the data as valid image data.
• Function im2uint8 sets to 0 all values in the input that
are less than 0, sets to 255 all values in the input that
are greater than 1, and multiplies all other values by
255.
– Rounding the results of the multiplication to the nearest
integer completes the conversion.
– Note that the rounding behavior of im2uint8 is different
from the data-class conversion function uint8, which
simply discards fractional parts.
Digital Image Processing - An Introduction 64
Function im2uint8
• For example, consider the following 2x3 image
f of class double, which could be the result of
an intermediate computation:
>> f =[0.5, 1.5, 1.2;-0.5, 0.75, -0.25];
Performing the conversion
>> g = im2uint8(f)
yields the result
g = [128, 255, 255; 0, 191, 0]
Digital Image Processing - An Introduction 65
Function mat2gray
• Converting an arbitrary array of class double to an array of
class double scaled to the range [0, 1] can be accomplished
by using function mat2gray whose basic syntax is:
>> g = mat2gray(A, [Amin, Amax])
– where image g has values in the range 0 (black) to 1 (white).
– The specified parameters Amin and Amax are such that values
less than Amin in A become 0 in g, and values greater than
Amax in A correspond to 1 in g.
– Writing:
>> g = mat2gray(A);
sets the values of Amin and Amax to the actual minimum and
maximum values in A.
– The input is assumed to be of class double. The output also is of
class double.
Digital Image Processing - An Introduction 66
Function im2double
• Function im2double converts an input to class
double.
– If the input is of class uint8, uint16, or logical, function im2double
converts it to class double with values in the range [0,1].
– If the input is already of class double, im2double returns an array that
is equal to the input.
– For example, if an array of class double results from computations that
yield values outside the range [0,1], inputting this array into
im2double will have no effect.
– A double array having arbitrary values can be converted to a double
array with values in the range [0, 1] by using function mat2gray.
Digital Image Processing - An Introduction 67
Function im2double
• As an illustration, consider the class uint8 image
>> h = uint8([25 50; 128 200]);
Performing the conversion
>> g = im2double(h);
yields the result
g=
[0.0980, 0.1961; 0.4706, 0.7843]
– the conversion when the input is of class uint8 is done simply by
dividing each value of the input array by 255. If the input is of class
uint16 the division is by 65535.
Digital Image Processing - An Introduction 68
Function im2bw
• Function im2bw, which has the syntax:
>> g = im2bw(f, T)
– produces a binary image, g, from an intensity image, f,
by thresholding.
– The output binary image g has values of 0 for all pixels
in the input image with intensity values less than
threshold T, and 1 for all other pixels.
– The value specified for T has to be in the range [0, 1],
regardless of the class of the input.
– The output binary image is automatically declared as a
logical array by im2bw.
Digital Image Processing - An Introduction 69
Function im2bw
• If we write
>> g = im2bw(f ),
IPT uses a default value of 0.5 for T.
– If the input is an uint8 image, im2bw divides all its pixels
by 255 and then applies either the default or a specified
threshold.
– If the input is of class uint16, the division is by 65535.
– If the input is a double image, im2bw applies either the
default or a specified threshold directly.
– If the input is a logical array, the output is identical to the
input.
Digital Image Processing - An Introduction 70
Function im2bw
• A logical (binary) array can be converted to a numerical array by
different ways.
• We wish to convert the following double image
>> f = [1 2; 3 4];
to binary such that values 1 and 2 become 0 and the other
two values become 1.
– First we convert it to the range [0,1]:
>> g = mat2gray(f)
g=
0 0.3333
0.6667 1.0000
– Then we convert it to binary using a threshold, say, of value 0.6:
>> gb = im2bw(g, 0.6)
Digital Image Processing - An Introduction 71
Function im2bw
• We can generate a binary array directly using relational
operators. Thus we get the same result by writing:
>> gb = f > 2
00
11
• We could store in a variable, gbv, the fact that gb is a
logical array by using the islogical function, as follows:
>> gbv = islogical(gb)
gbv = 1
Digital Image Processing - An Introduction 72
• Suppose now that we want to convert gb to a numerical array of 0s
and 1s of class double. This is done directly:
>> gbd = im2double(gb)
gbd =
0 0
1 1
• If gb had been a numeric array of class uint8, applying im2double to
it would have resulted in an array with values:
0 0
0.0039 0.0039
because im2double would have divided all the elements by 255.
– This did not happen in the preceding conversion because im2double
detected that the input was a logical array, whose only possible values
are 0 and 1.
Digital Image Processing - An Introduction 73
• If the input in fact had been an uint8 numeric
array and we wanted to convert it to class
double while keeping the 0 and 1 values, we
would have converted the array by writing:
>> gbd = double(gb)
gbd =
0 0
1 1
Digital Image Processing - An Introduction 74
• MATLAB supports nested statements, so we could
have started with image f and arrived at the same
result by using the one-line statement
>> gbd = im2double(im2bw(mat2gray(f), 0.6));
or by using partial groupings of these functions.
• Of course, the entire process could have been
done in this case with a simpler command:
>> gbd = double(f > 2);
Digital Image Processing - An Introduction 75
Transforming images
• The image in ‘rose1024.tif’ is a 1024x1024
intensity image, f, of class uint8.
• This image can be:
– flipped vertically using the statement
>> fp = f(end:-1:1, :);
– flipped horizontally using the statement
>> fp = f(:, end:-1:1);
Digital Image Processing - An Introduction 76
Digital Image Processing - An Introduction 77
Extracting a sub-image and
subsampling
• A section out of the original image can be
obtained using the command:
>> fc = f(257:768, 257:768);
• A subsampled image can be obtained using
the statement:
>> fs = f(1:2:end, 1:2:end);
Digital Image Processing - An Introduction 78
Digital Image Processing - An Introduction 79
Scanning a column/row of an image
• A horizontal scan line through the middle of
the original image is obtained using the
command:
>> plot(f(512, :))
Digital Image Processing - An Introduction 80
Image arithmetic
• The IPT toolbox supports the image arithmetic
functions listed in next Table.
– Although these functions could be implemented
using MATLAB arithmetic operators directly, the
advantage of using the IPT functions is that they
support the integer data classes whereas the
equivalent MATLAB math operators require inputs
of class double.
Digital Image Processing - An Introduction 81
• Imadd: Adds two images; or adds a constant to an image.
• Imsubtract: Subtracts two images; or subtracts a constant from an
image.
• Immultiply: Multiplies two images, where the multiplication is
carried out between pairs of corresponding image elements; or
multiplies a constant times an image.
• Imdivide: Divides two images, where the division is carried out
between pairs of corresponding image elements; or divides an
image by a constant.
• Imabsdiff: Computes the absolute difference between two
images.
• Imcomplement: Complements an image.
• Imlincomb: Computes a linear combination of two or more
images.
Digital Image Processing - An Introduction 82
Imcomplement effect
Digital Image Processing - An Introduction 83
• To obtain the number of elements in a 2-D
array, A:
>> n = length(A(:));
• Another way to obtain the number of
elements in an array directly is to use function
numel, whose syntax is:
>> n = numel(A)
Digital Image Processing - An Introduction 84
• MATLAB provides a direct way to implement 2-D function
evaluations via function meshgrid, which has the syntax:
>> [C, R] = meshgrid(c, r)
• This function transforms the domain specified by row vectors c and
r into arrays C and R that can be used for the evaluation of
functions of two variables and 3-D surface plots
– note that columns are listed first in both the input and output of
meshgrid.
• The rows of output array C are copies of the vector c, and the
columns of the output array R are copies of the vector r.
Digital Image Processing - An Introduction 85
• For example, suppose that we want to form a 2-D function whose
elements are the sum of the squares of the values of coordinate variables
x and y for x = 0, 1, 2 and y = 0, 1.
– The vector r is formed from the row components of the coordinates:
>> r = [ 0 1 2 ];
– Similarly, c is formed from the column component of the coordinates:
>> c = [0 1 ]; % both r and c are row vectors here.
– Substituting these two vectors into meshgrid results in the following arrays:
>> [C, R]= meshgrid(c, r)
C=
0 1
0 1
0 1
R=
0 0
1 1
2 2
Digital Image Processing - An Introduction 86
• The function in which we are interested is
implemented as:
>> h = R.^2 + C.^2
which gives the following result:
h=
0 1
1 2
4 5
Digital Image Processing - An Introduction 87
• Do the following:
1. Use the meshgrid command to generate the
discrete-space 2-D signal :
f(m, n) = 255|sinc(0.2m) sinc(0.2n)|
for −50 m 50 and −50 n 50.
>> [M, N] = meshgrid(-50:50, -50:50);
>> f = 255*abs(sinc(0.2*M) .* sinc(0.2*N));
Digital Image Processing - An Introduction 88
• Do the following: …
1. Use the mesh command to display the signal as a surface
plot.
>> mesh(M, N, f)
2. Display the signal as an image. Use the command
colormap(gray(256)) just after issuing the image
command to obtain a grayscale image.
>> image(f), colormap(gray(256))
• For which applications do you think the surface plot
works better? When would you prefer the image?
Digital Image Processing - An Introduction 89
Digital Image Processing - An Introduction 90
• We will concentrate on 8-bit images, meaning that
each pixel is represented by a single byte.
– Since a byte can take on 256 distinct values, Lmax is 255 for
an 8-bit image.
• In order to process images within Matlab, we need to
first understand their numerical representation.
• Load the image file ‘yacht.tif’ within Matlab.
– This is an 8-bit monochrome image.
– Read it into a matrix using
>> A = imread(’yacht.tif’);
Image Processing 94
• To display your variables, type :
>> whos
– Notice under the “Class” column that the A matrix elements are of
type uint8 (unsigned integer, 8 bits). This means that Matlab is using a
single byte to represent each pixel. Matlab cannot perform numerical
computation on numbers of type uint8, so we usually need to convert
the matrix to a floating point representation.
• Create a double precision representation of the image using
>> B = double(A);
>> whos
– Notice the difference in the number of bytes between A and B.
– In future sections, we will be performing computations on our images,
so we need to remember to convert them to type double before
processing them. Image Processing 95
• Display ‘yacht.tif’ using the following sequence of commands:
>> image(B);
>> colormap(gray(256));
>> axis(’image’);
• The image command works for both type uint8 and double images.
• The colormap command specifies the range of displayed gray levels,
assigning black to 0 and white to 255.
– It is important to note that if any pixel values are outside the range 0
to 255 (after processing), they will be clipped to 0 or 255 respectively
in the displayed image.
– It is also important to note that a floating point pixel value will be
rounded down (“floored”) to an integer before it is displayed.
Therefore the maximum number of gray levels that will be displayed
on the monitor is 255, even if the image values take on a continuous
range.
Image Processing 96
• Now we will practice some simple operations on
the ‘yacht.tif’ image.
– Make a horizontally flipped version of the image by
reversing the order of each column.
– Similarly, create a vertically flipped image.
– Now, create a “negative” of the image by subtracting
each pixel from 255 (here’s an example of where
conversion to double is necessary.)
– Finally, multiply each pixel of the original image by 0.5
(1.5), and print the result.
Image Processing 97
Image Processing 98
Image Processing 99
• Write a simple Matlab function Hist(A) which will plot the
histogram of image matrix A.
– You may use Matlab’s hist function, however that function
requires a vector as input.
• An example of using hist to plot a histogram of a matrix
would be
>> x=reshape(A,1,M*N);
>> hist(x,0:255);
– where A is an image, and M and N are the number of rows and
columns in A.
– The reshape command is creating a row vector out of the image
matrix, and the hist command plots a histogram with bins
centered at [0 : 255].
Image Processing 100
• Load the file ‘house.tif’
• Test your Hist function on the image.
• Label the axes of the histogram and give it a
title.
– Comment on the distribution of the pixel
intensities.
Image Processing 101
Image Processing 102
Quantization
• Quantization is the act of rounding off the value of a signal or
quantity to certain discrete levels.
• Digital images are quantized:
– The gray levels in a black and white photograph must be quantized in
order to store an image in a computer.
– The “brightness” of the photo at each pixel is assigned an integer
value between 0 and 255 (typically), where 0 corresponds to black,
and 255 to white.
• Since an 8-bit number can represent 256 different values, such an image is called an
“8-bit grayscale” image.
– An image which is quantized to just 1 bit per pixel (in other words only
black and white pixels) is called a halftone image.
• Quantization is sometimes used for compression.
Digital Image Processing - An Introduction 103
8-bit grayscale
50
7-bit grayscale
100
6-bit grayscale
150
5-bit grayscale
200
4-bit grayscale
250
50 100 150 200 250
Digital Image Processing - An Introduction 104
50
100
150
200
250
50 100 150 200 250
Digital Image Processing - An Introduction 105
8, 4, 3, 2, & 1-bit Quantizing of “LENA”
4-bit 3-bit
•
8-bit
2-bit 1-bit
Digital Image Processing - An Introduction 106
Image Quantization
• The image in fountainbw.tif is an 8-bit grayscale image.
• We will investigate what happens when we quantize it to
fewer bits per pixel (b/pel).
– Load it into Matlab and display it using the following sequence
of commands:
>>y = imread(’fountainbw.tif’);
>> image(y), colormap(gray(256));
>> axis(’image’);
• The image array will initially be of type uint8, so you may
need to convert the image matrix to type double before
performing any computation.
– Use the command z=double(y) for this.
Digital Image Processing - An Introduction 107
• To uniformly quantize a signal. Let: Max X Min X
N 1
– where X is the signal to be quantized, and N is the number
of quantization levels.
• To force the data to have a uniform quantization step
of ,
1. Subtract Min(X) from the data and divide the result by .
2. Round the data to the nearest integer.
3. Multiply the rounded data by and add Min(X) to
convert the data back to its original scale.
Digital Image Processing - An Introduction 108
• Write a Matlab function Y = Uquant(X,N) which will uniformly
quantize an input array X (either a vector or a matrix) to N discrete
levels.
• Use this function to quantize the fountain image to 7 b/pel, 6, 5, 4,
3, 2, 1 b/pel, and observe the output images.
• Display copies of only the 7, 4, 2, and 1 b/pel images, as well as the
original.
• Describe the artifacts (errors) that appear in the image as the
number of bits is lowered?
• Note the number of b/pel at which the image quality noticeably
deteriorates.
• Compare each of these four quantized images to the original.
Digital Image Processing - An Introduction 109
Digital Image Processing - An Introduction 110
Digital Image Processing - An Introduction 111
Digital Image Processing - An Introduction 112
Digital Image Processing - An Introduction 113
Digital Image Processing - An Introduction 114
Digital Image Processing - An Introduction 115
Digital Image Processing - An Introduction 116
Digital Image Processing - An Introduction 117
Peak-Signal-to-Noise Ratio (PSNR)
• Mean Squared Error (MSE)
𝑀 𝑁 2
𝑖=1 𝑗=1 𝑓 𝑖, 𝑗 − 𝐹 𝑖, 𝑗
𝑀𝑆𝐸 =
𝑀⋅𝑁
• PSNR:
255
𝑃𝑆𝑁𝑅 = 20 ∙ 𝑙𝑜𝑔10
𝑀𝑆𝐸
Digital Image Processing - An Introduction 118