You are on page 1of 60

Lecture notes

Digital Image Processing by Jorma Kekalainen

Digital Image Processing


Jorma Kekalainen

Digital Image Processing


Point Operations

Lecture weeks 11 and 12

Page 1

Lecture notes

Digital Image Processing by Jorma Kekalainen

Image processing operations


Any image processing operation transforms the
gray values of the pixels.
However, image processing operations may be
divided into into three classes based on the
information required to perform the
transformation.
From the most complex to the simplest, they are:
transforms
spatial filters
point operations
Jorma Kekalainen

Digital Image Processing

579

Transforms
We require a knowledge of all the gray levels in the entire
image to transform the image.
I.e., the entire image is processed as a single large block.

Jorma Kekalainen

Lecture weeks 11 and 12

Digital Image Processing

580

Page 2

Lecture notes

Digital Image Processing by Jorma Kekalainen

Spatial filters and point


operations
Spatial filters
To change the gray level of a given pixel we
need only know the value of the gray levels in
a small neighborhood of pixels around the
given pixel.
Point operations
A pixel's gray value is changed without any
knowledge of its surrounds.
Jorma Kekalainen

Digital Image Processing

581

Point operations
Although point operations are the simplest,
they contain some of the most powerful and
widely used of all image processing
operations.
They are especially useful in image preprocessing, where an image is required to be
modified before the main job is attempted.

Jorma Kekalainen

Lecture weeks 11 and 12

Digital Image Processing

582

Page 3

Lecture notes

Digital Image Processing by Jorma Kekalainen

Arithmetic operations
These operations act by applying a simple
function y=f(x) to each gray value in the image.
Thus f(x) is a function which maps the range 0
255 onto itself.
Simple functions include adding or subtract a
constant value to each pixel: y=xC or multiplying
each pixel by a constant: y=Cx.
In each case we may have to fix the output
slightly in order to ensure that the results are
integers in the range 0 255.
Jorma Kekalainen

Digital Image Processing

583

Arithmetic operations
We can do this by first rounding the result (if
necessary) to obtain an integer, and then clipping
the values by setting:
255, if y > 255
y=
0, if y < 0

We can see how these operations affect an image


by looking at the graph of old gray values against
new values.
The following figure shows the result of adding or
subtracting 128 from each pixel in the image.
Jorma Kekalainen

Lecture weeks 11 and 12

Digital Image Processing

584

Page 4

Lecture notes

Digital Image Processing by Jorma Kekalainen

Adding and subtracting a


constant

Note: When we add 128, all gray values of 127 or greater will be mapped to 255.
And when we subtract 128, all gray values of 128 or less will be mapped to 0.
By looking at these graphs, we see that in general adding a constant will lighten an
Jorma Kekalainen
Digital Image Processing
585
image, and subtracting a constant will darken it.

Testing example
We can test the previous
conclusions on the cameraman
image cameraman.tif.
We start by reading the image
in:
>> c=imread(cameraman.tif');
>> whos c
Name Size Bytes Class
c 256x256 65536 uint8 array

Jorma Kekalainen

Lecture weeks 11 and 12

Digital Image Processing

586

Page 5

Lecture notes

Digital Image Processing by Jorma Kekalainen

Testing example
We can do this straightly c1=c+128 or we can first turn
c into a matrix of type double, add the 128, and then
turn back to uint8 for display:
>> c1=uint8(double(c)+128);
A more elegant way, is to use the Matlab function
imadd which is designed precisely to do this:
>> c1=imadd(c,128);
Subtraction is similar; we can transform out matrix in
and out of double, or use the imsubtract function:
>> c2=imsubtract(c,128);
Jorma Kekalainen

Digital Image Processing

587

Adding or subtracting a constant


Now we can view the results:
>> imshow(c1),figure,imshow(c2)

c1: Adding 128

Jorma Kekalainen

Lecture weeks 11 and 12

Digital Image Processing

c2: Subtracting 128

588

Page 6

Lecture notes

Digital Image Processing by Jorma Kekalainen

Lightening or darkening of an
image by multiplication
We can also perform lightening or darkening of an
image by multiplication.
To implement these functions, we use the immultiply
function.
All these resulting images can be viewed with imshow.
Compare the results of darkening c2 and c3.
Note that c3, although darker than the original, is still
quite clear, whereas a lot of information has been lost
by the subtraction process, as can be seen in image c2.
This is because in image c2 all pixels with gray values
128 or less have become zero.
Jorma Kekalainen

Digital Image Processing

589

Dividing with a constant


c3=immultiply(c,0.5); or c3=imdivide(c,2);

c3: y=x/2
Jorma Kekalainen

Lecture weeks 11 and 12

Digital Image Processing

590

Page 7

Lecture notes

Digital Image Processing by Jorma Kekalainen

Multiplicating with a constant


c4=immultiply(c,2);

Jorma Kekalainen

Digital Image Processing

c4: y=2x

591

Comparing results
A similar loss of information has occurred in
the images c1 and c4.
Note that the light colored buildings in the
background; in both c1 and c4 have been
disappeared.
However, the buildings are quite visible in
image c5.

Jorma Kekalainen

Lecture weeks 11 and 12

Digital Image Processing

592

Page 8

Lecture notes

Digital Image Processing by Jorma Kekalainen

Effect of mixed operations


c5=imadd(immultiply(c,0.5),128); or
c5=imadd(imdivide(c,2),128);

c5: y=x/2+128
Jorma Kekalainen

Digital Image Processing

593

Image complementation
The complement of a grayscale image is its
photographic negative.
If an image matrix m is of type double and so its
gray values are in the range 0.0 1.0, we can
obtain its negative with the command
>> 1-m
If the image is binary, we can use
>> ~m
If the image is of type uint8, the best approach is
the imcomplement function.
Jorma Kekalainen

Lecture weeks 11 and 12

Digital Image Processing

594

Page 9

Lecture notes

Digital Image Processing by Jorma Kekalainen

Binary conversion
BI=im2bw(Deepiga,0.5);% Binary conversion with threshold
>> imshow(BI)
>> figure, imshow(~BI)

Jorma Kekalainen

Digital Image Processing

595

Note: im2bw

Convert image to binary image, based on


threshold
Syntax

BW = im2bw(I, level)
BW = im2bw(X, map, level)

BW = im2bw(RGB, level)
Description
BW = im2bw(I, level) converts the grayscale
image I to a binary image.
The output image BW replaces all pixels in the
input image with luminance greater than level

with the value 1 (white) and replaces all other


pixels with the value 0 (black).
Specify level in the range [0,1].
This range is relative to the signal levels
possible for the image's class.
Therefore, a level value of 0.5 is midway
between black and white, regardless of class.

Jorma Kekalainen

Lecture weeks 11 and 12

To compute the level argument, you


can use the function graythresh.
If you do not specify level, im2bw
uses the value 0.5.
BW = im2bw(X, map, level) converts
the indexed image X with colormap
map to a binary image.
BW = im2bw(RGB, level) converts the
truecolor image RGB to a binary
image.
If the input image is not a grayscale
image, im2bw converts the input
image to grayscale, and then converts
this grayscale image to binary by
thresholding.

Digital Image Processing

596

Page 10

Lecture notes

Digital Image Processing by Jorma Kekalainen

Image complementation
Next figure shows the complement function y=255-x, and the
result of the commands
>> cc=imcomplement(c);
>> imshow(cc)

Jorma Kekalainen

Digital Image Processing

597

Part complementation
Interesting special effects can be obtained by
complementing only part of the image.
E.g., by taking the complement of pixels of gray
value 128 or less, and leaving other pixels
untouched.
Or we could take the complement of pixels which
are 128 or greater, and leave other pixels
untouched.
Figure shows these functions.
The effect of these functions is called solarization.
Jorma Kekalainen

Lecture weeks 11 and 12

Digital Image Processing

598

Page 11

Lecture notes

Digital Image Processing by Jorma Kekalainen

Solarization

Complementing only dark pixels


Jorma Kekalainen

Complementing only light pixels

Digital Image Processing

599

Histograms
Given a grayscale image, its histogram consists of
the histogram of its gray levels; that is, a graph
indicating the number of times each gray level
occurs in the image.
We can deduce a great deal about the
appearance of an image from its histogram:
In a dark image, the gray levels (and hence the
histogram) would be clustered at the lower end.
In a uniformly bright image, the gray levels would be
clustered at the upper end.
In a well contrasted image, the gray levels would be
well spread out over much of the range.
Jorma Kekalainen

Lecture weeks 11 and 12

Digital Image Processing

600

Page 12

Lecture notes

Digital Image Processing by Jorma Kekalainen

Different contrasted images


Low contrast, medium contrast and high
contrast images

Jorma Kekalainen

Digital Image Processing

601

imhist
We can view the histogram of an image in
Matlab by using the imhist function:
>> p=imread('pout.tif');
>> imshow(p),figure,imhist(p),axis tight
(the axis tight command ensures the axes of
the histogram are automatically scaled to fit
all the values in).

Jorma Kekalainen

Lecture weeks 11 and 12

Digital Image Processing

602

Page 13

Lecture notes

Digital Image Processing by Jorma Kekalainen

Image and its histogram


pout.tif

Jorma Kekalainen

Digital Image Processing

603

Histogram spreading out


Since the gray values are all clustered together
in the center of the histogram, we would
expect the image to be poorly contrasted.
Given a poorly contrasted image, we would
like to enhance its contrast, by spreading out
its histogram.
There are two ways of doing this:
histogram stretching (contrast stretching)
histogram equalization.
Jorma Kekalainen

Lecture weeks 11 and 12

Digital Image Processing

604

Page 14

Lecture notes

Digital Image Processing by Jorma Kekalainen

Histogram stretching (Contrast


stretching)
Suppose we have an image with the histogram shown in
figure below, associated with a table of the numbers ni of gray
values:
Gray level i
ni

A histogram of a poorly contrasted image

Jorma Kekalainen

Digital Image Processing

605

Histogram stretching
We can stretch the gray
levels in the centre of the
range out by applying the
piecewise linear function
shown at the right.
This function has the effect
of stretching the gray levels
5-9 to gray levels 2-14
according to the equation:

The equation yields:

where i is the original gray


level and j its result after the
transformation.
Jorma Kekalainen
Digital Image Processing

Lecture weeks 11 and 12

606

Page 15

Lecture notes

Digital Image Processing by Jorma Kekalainen

Stretching function
Gray levels outside this
range are either left alone
as in this case (or
transformed according to
the linear functions at the
ends of the graph).

Jorma Kekalainen

Digital Image Processing

607

Corresponding stretched
histogram
A stretched histogram indicates an image
with greater contrast than the original.

Jorma Kekalainen

Lecture weeks 11 and 12

Digital Image Processing

A histogram of a poorly
contrasted image

608

Page 16

Lecture notes

Digital Image Processing by Jorma Kekalainen

Use of imadjust in stretching


To perform histogram
stretching in Matlab the
imadjust function may
be used.
In its simplest form, the
command
imadjust(im,[a,b],[c,d])
stretches the image
according to the
function shown in
figure.

The stretching function


given by imadjust

Note: Pixel values less than a are all converted to c and pixel values greater than b are
Jorma Kekalainen
Digital Image Processing
609
all converted
to d.

Use of imadjust in stretching


Since imadjust is designed to work equally well on images of
type double, uint8 or uint16 the values of a, b, and d must be
between 0 and 1; the function automatically converts the
image (if needed) to be of type double.
If either of [a,b] or [c,d] are chosen to be [0,1], the
abbreviation [] may be used.
Thus, for example, the command
>> imadjust(im,[],[])
does nothing, and the command
>> imadjust(im,[],[1,0])
inverts the gray values of the image,
to produce a result similar
to a photographic negative.
Jorma Kekalainen
Digital Image Processing
610

Lecture weeks 11 and 12

Page 17

Lecture notes

Digital Image Processing by Jorma Kekalainen

Example

P=imread('pout.tif');
figure, imshow(P)
title('Image before histogram streching')
figure, imhist(P)
title('Image histogram before histogram streching')
Padj=imadjust(P,[75/255,150/255],[0,1]);
figure, imshow(Padj)
title('Image after histogram streching')
figure, imhist(Padj)
title('Image after histogram streching')
figure,plot(P,Padj,'.'),axis tight
title('Stretching function imadjust(P,[75/255,150/255],[0,1])')

Jorma Kekalainen

Digital Image Processing

611

Example

Jorma Kekalainen

Lecture weeks 11 and 12

Digital Image Processing

612

Page 18

Lecture notes

Digital Image Processing by Jorma Kekalainen

Example: Adjusting an RGB image

RGB2 = imadjust(RGB1,[.2 .3 0; .6 .7 1],[]);


figure, imshow(RGB1)
title('Original image ''football.jpg''')
RGB1 = imread('football.jpg');
figure, imshow(RGB2)
title('Adjusted image')

Jorma Kekalainen

Digital Image Processing

613

Gamma value
The imadjust function has one other optional
parameter: the gamma value, which describes
the shape of the function between the
coordinates (a,c) and (b,d).
If gamma is equal to 1, which is the default, then
a linear mapping is used, as shown in the
previous case.
However, values less than one produce a function
which is concave downward, and values greater
than one produce a function which is concave
upward, as shown in the following figure.
Jorma Kekalainen

Lecture weeks 11 and 12

Digital Image Processing

614

Page 19

Lecture notes

Digital Image Processing by Jorma Kekalainen

Gamma not equal to 1


The imadjust function with gamma not equal
to 1

concave upward
concave downward

Jorma Kekalainen

Digital Image Processing

615

Example
The function used is a slight variation on the standard
line between two points:

Use of the gamma value alone can be enough to


substantially change the appearance of the image.
For example:
t=imread('tire.tif');
th=imadjust(t,[],[],0.5);% =0.5
imshow(t),figure,imshow(th)
produces the result shown in the following figure.
Jorma Kekalainen

Lecture weeks 11 and 12

Digital Image Processing

616

Page 20

Lecture notes

Digital Image Processing by Jorma Kekalainen

Example
The tire image and after adjustment with the
gamma value =0.5.

Jorma Kekalainen

Digital Image Processing

617

Stretching function
We may view the imadjust stretching function
with the plot function.
For example,
>> plot(t,th,'.'),axis tight
produces the plot shown in the following figure.
Since t and th are matrices which contain the
original values and the values after the imadjust
function, the plot function simply plots them,
using dots () to do it.
Jorma Kekalainen

Lecture weeks 11 and 12

Digital Image Processing

618

Page 21

Lecture notes

Digital Image Processing by Jorma Kekalainen

Stretching function
Stretching function used in the previous tire image.
=0.5 (<1 concave downward)

Jorma Kekalainen

Digital Image Processing

619

Piecewise linear stretching function


We can write our own function to perform piecewise linear
stretching as shown in figure below.
To do this, we will make use of the find function, to find the
pixel values in the image between ai and ai +1.
Since the line between the coordinates (ai, bj)
and (ai+1, bj+1) has the equation

the heart of our function will be the lines


pix=find(im >= a(i) & im < a(i+1));
out(pix)=(im(pix)-a(i))*(b(i+1)-b(i))/(a(i+1)-a(i))+b(i);
where im is the input image and out is the output image.
Jorma Kekalainen

Lecture weeks 11 and 12

Digital Image Processing

620

Page 22

Lecture notes

Digital Image Processing by Jorma Kekalainen

Matlab function for piecewise


linear stretching function
A simple procedure which takes as inputs
images of type uint8 or double.
function out = histpwl(im,a,b)
% HISTPWL(IM,A,B) applies a piecewise linear
%transformation to the pixel values
% of image IM, where A and B are vectors
% containing the x and y coordinates of the
% ends of the line segments.
%IM can be of type UINT8 or DOUBLE, and
% the values in A and B must be between
% 0 and 1.
% For example:
% histpwl(x,[0,1],[1,0])
% simply inverts the pixel values.
%
Jorma Kekalainen

classChanged = 0;
if ~isa(im, 'double'),
classChanged = 1;
im = im2double(im);
end
if length(a) ~= length (b)
error('Vectors A and B must be of equal size');
end
N=length(a);
out=zeros(size(im));
for i=1:N-1
pix=find(im>=a(i) & im<a(i+1));
out(pix)=(im(pix)-a(i))*(b(i+1)-b(i))/(a(i+1)-a(i))+b(i);
end
pix=find(im==a(N));
out(pix)=b(N);
if classChanged==1
out = uint8(255*out);
end

Digital Image Processing

621

Example: histpwl
As an example of the use of this function:
>> th=histpwl(t,[0 .25 .5 .75 1],[0 .75 .25 .5 1]);
>> imshow(th)
>> figure,plot(t,th,'.'),axis tight
produces the figures shown below.

Jorma Kekalainen

Lecture weeks 11 and 12

Digital Image Processing

622

Page 23

Lecture notes

Digital Image Processing by Jorma Kekalainen

Histogram equalization
The trouble with any of the above methods of
histogram stretching is that they require user
input.
Sometimes a better approach is provided by
histogram equalization, which is an entirely
automatic procedure.
Suppose our image has L different gray levels
0, 1, 2, , L-1 and that gray level i occurs ni
times in the image.
Jorma Kekalainen

Digital Image Processing

623

Histogram equalization
Suppose also that the total number of pixels in
the image is n so that n0+n1+n2+ +nL-1=n.
To transform the gray levels to obtain a better
contrasted image, we change gray level i to

and this number is rounded to the nearest


integer.
Jorma Kekalainen

Lecture weeks 11 and 12

Digital Image Processing

624

Page 24

Lecture notes

Digital Image Processing by Jorma Kekalainen

Example
Suppose a 4-bit grayscale image has the histogram
shown in the following figure associated with a table
of the numbers (n=360)

We would expect this image to be uniformly bright,


with a few dark dots on it.
To equalize this histogram, we form running totals of
the ni , and multiply each by 15/360=1/24:
Jorma Kekalainen

Digital Image Processing

625

Example
Histogram indicating poor contrast

Jorma Kekalainen

Lecture weeks 11 and 12

Digital Image Processing

626

Page 25

Lecture notes

Digital Image Processing by Jorma Kekalainen

Example

To equalize this
histogram, we
form running
totals of the ni ,
and multiply each
by 15/360=1/24.

Jorma Kekalainen

Digital Image Processing

627

Example
We now have the following transformation of gray
values, obtained by reading off the first and last
columns in the previous table:

The histogram of the j values is shown in the


following figure.
This is far more spread out than the original
histogram, and so the resulting image should exhibit
greater contrast.
Jorma Kekalainen

Lecture weeks 11 and 12

Digital Image Processing

628

Page 26

Lecture notes

Digital Image Processing by Jorma Kekalainen

Example
The histogram before
equalization

Jorma Kekalainen

The histogram after


equalization

Digital Image Processing

629

histeq
To apply histogram equalization in Matlab, use
the histeq function; for example:
>> p=imread('pout.tif');
>> ph=histeq(p);
>> imshow(ph),figure,imhist(ph),axis tight
applies histogram equalization to the pout image,
and produces the resulting histogram.
Notice the far greater spread of the histogram
which corresponds to the greater increase of
contrast in the image.
Jorma Kekalainen

Lecture weeks 11 and 12

Digital Image Processing

630

Page 27

Lecture notes

Digital Image Processing by Jorma Kekalainen

Results after histogram


equalization
The image and its histogram after equalization

Jorma Kekalainen

Digital Image Processing

631

Example: Very dark image


We can obtain a dark image by taking the index values only of
an indexed color image.
>> AT=imread('ancienttrees.tif');
We can display this matrix and its histogram with commands:
>> imshow(AT),figure,imhist(AT),axis tight We now have an index
matrix AT consisting of
low values only.

Jorma Kekalainen

Lecture weeks 11 and 12

Since the
index matrix
contains only
low values it
will appear
dark when
displayed.
Digital Image Processing

632

Page 28

Lecture notes

Digital Image Processing by Jorma Kekalainen

Example: After equalization


But we can apply histogram equalization to this image, and
display the results:
The image is no longer very dark and
>> ATh=histeq(AT);
the corresponding histogram is no
>> imshow(ATh),figure,
longer heavily clustered at the lower
end of the scale
>> imhist(ATh),axis tight

Jorma Kekalainen

Digital Image Processing

633

Example: Before and after


histogram equalization

Jorma Kekalainen

Lecture weeks 11 and 12

Digital Image Processing

634

Page 29

Lecture notes

Digital Image Processing by Jorma Kekalainen

Example

H=imread('he.tif');
figure,imshow(H),figure,imhist(H(:,:,1)),axis tight
[he,emap]=gray2ind(H);
figure, imshow(he)
title('Index values only')
figure,imhist(he(:,:,1)),axis tight
title('Index matrix consisting low values only')
hee=histeq(he(:,:,1));
figure, imshow(hee),
title('After equalization')
figure,imhist(hee),axis tight
title('After equalization')

Jorma Kekalainen

Digital Image Processing

635

Example
H=imread('he.tif');
figure,imshow(H),figure,imhist(H(:,:,1)),axis
tight

Jorma Kekalainen

Lecture weeks 11 and 12

Digital Image Processing

636

Page 30

Lecture notes

Digital Image Processing by Jorma Kekalainen

Example

[he,emap]=gray2ind(H);
figure, imshow(he)
title('Index values only')
figure,imhist(he(:,:,1)),axis tight
title('Index matrix consisting low values only')

Jorma Kekalainen

Digital Image Processing

637

After equalization
hee=histeq(he(:,:,1));
figure, imshow(hee),
title('After equalization')
figure,imhist(hee),axis tight
title('After equalization')

Jorma Kekalainen

Lecture weeks 11 and 12

Digital Image Processing

638

Page 31

Lecture notes

Digital Image Processing by Jorma Kekalainen

Example

Jorma Kekalainen

Digital Image Processing

639

Comment
Consider the histogram
beside.
To apply histogram
stretching, we would
need to stretch out the
values between gray
levels 9 and 13.
Thus, we would need to
apply a piecewise
function similar to that
shown beside.
Jorma Kekalainen

Lecture weeks 11 and 12

Digital Image Processing

640

Page 32

Lecture notes

Digital Image Processing by Jorma Kekalainen

Comment
Let's consider the
Cumulative histogram
cumulative histogram,
which is shown beside.
The dashed line is simply
joining the top of the
histogram bars.
However, it can be
interpreted as an
appropriate histogram
stretching function.
To do this, we need to
scale the y values so that
they are between 0 and
But this is precisely the method
15, rather than 0 and 360. described in the histogram
Jorma Kekalainen

Digital Image Processing

equalization section.

641

Digital Image Processing


Exercises

Lecture weeks 11 and 12

Page 33

Lecture notes

Digital Image Processing by Jorma Kekalainen

Exercise on image import and


export
(a) Read and display any image with very bad
contrast (Construct such an image if needed)
(b) Check the image appearance in the
workspace
(c) Improve image contrast
(d) Write the image to a disk file
(e) Check the contents of the newly written
file
Jorma Kekalainen

Digital Image Processing

643

(a) Read and display an image


First, we clear the Matlab workspace of any
variables and close open figure windows.
clear, close all
To read an image, we use the imread command.
Here we read an image file ex4-fig1.png in an
array named I.
I = imread('ex4-fig1.png');
An acronym png means Portable Network Graphics
(PNG) which is one of many graphics file formats.
Note: For the list of supported graphics file formats, see the imread
Jorma Kekalainen
Digital Image Processing
644
function
reference documentation.

Lecture weeks 11 and 12

Page 34

Lecture notes

Digital Image Processing by Jorma Kekalainen

(a) Read and display an image

>> imfinfo('ex4-fig1.png')
ans =
Filename: [1x46 char]
FileModDate: '07-Aug-2015 21:48:40'
FileSize: 16388
Format: 'png'
FormatVersion: []
Width: 357
Height: 221
BitDepth: 8
ColorType: 'grayscale'
FormatSignature: [137 80 78 71 13 10 26 10]
Colormap: []
Histogram: []

Jorma Kekalainen

Digital Image Processing

645

(a) Read and display an image


Now display the image.
The toolbox includes two image display functions:
imshow and imtool.
imshow is the toolbox's fundamental image display
function.
imtool starts the Image Tool which presents an
integrated environment for displaying images and
performing some common image processing tasks.
Note: The Image Tool provides all the image display capabilities of imshow but also
provides access to several other tools for navigating and exploring images, such as
scroll bars, the Pixel Region tool, Image Information tool, and the Contrast Adjustment
Digital
Image Processing
646
tool.Jorma
ForKekalainen
more information, see Image
Display
and Exploration Overview.

Lecture weeks 11 and 12

Page 35

Lecture notes

Digital Image Processing by Jorma Kekalainen

(a) Read and display an image


We can use either function to display an image.
Here we use imshow function
>> figure, imshow(I)
>> title('Grayscale image ex4-fig1.png')

Jorma Kekalainen

Digital Image Processing

647

(b) Check the image appearance in


the workspace
To see how the imread function stores the image data
in the workspace, we check the Workspace browser in
the Matlab desktop.
The Workspace browser displays information about all
the variables we create during a Matlab session.
The imread function returned the image data in the
variable I, which is a 221x357 element array of uint8
data.
Values of I are between 0 and 58.
Matlab can store images as uint8, uint16, or double
arrays.
Note: For more information about image storage classes, see Converting Between
Jorma Kekalainen
Digital Image Processing
648
Image
Classes.

Lecture weeks 11 and 12

Page 36

Lecture notes

Digital Image Processing by Jorma Kekalainen

(b) Check the image appearance in


the workspace
We can also get information about variables in
the workspace by calling the whos command.
>> whos
Name
I

Jorma Kekalainen

Size

221x357

Bytes Class Attributes


78897 uint8

Digital Image Processing

649

(c) Improve image contrast


The image ex4-fig1.png is too dark (0I 58) and so a low
contrast image.
To see the distribution of intensities in ex4-fig1.png, we can
create a histogram by calling the imhist function.
>> figure, imhist(I), title('Histogram of the image ex4-fig1')
Notice how the intensity range is
rather narrow. It does not cover
the potential range of [0, 255].
There are missing the medium
and high values that would result
in good contrast.
Note: Precede the call to imhist with the figure command so that the histogram does
Jorma Kekalainen
Digital Image Processing
650
not overwrite
the display of the image
I in the current figure window.

Lecture weeks 11 and 12

Page 37

Lecture notes

Digital Image Processing by Jorma Kekalainen

(c) Improve image contrast


Histogram equalization
The toolbox provides several ways to improve
the contrast in an image.
One way is to call the histeq function to
spread the intensity values over the full range
of the image.
This process called histogram equalization.

Jorma Kekalainen

Digital Image Processing

651

(c) Improve image contrast


We equalize the histogram giving a command
I2 = histeq(I);
Display the new equalized image, I2, in a new figure
window.
>> figure, imshow(I2)
>> title('Equalized image')

Jorma Kekalainen

Lecture weeks 11 and 12

Digital Image Processing

652

Page 38

Lecture notes

Digital Image Processing by Jorma Kekalainen

(c) Improve image contrast


Call imhist to create a histogram of the equalized
image I2.
>> figure, imhist(I2)
>>title('Histogram of the image after equalization')

Jorma Kekalainen

Digital Image Processing

653

(c) Improve image contrast


Comparison of histograms
After equalization process, the histogram of I2 is clearly more
spread out than the histogram of I (0 I 58) .
>> subplot(121),imhist(I);
>> title('Histogram of the image ex4-fig1');
>> subplot(122),imhist(I2);
>> title('Histogram of the image after equalization')

Jorma Kekalainen

Lecture weeks 11 and 12

Digital Image Processing

654

Page 39

Lecture notes

Digital Image Processing by Jorma Kekalainen

(c) Improve image contrast


Comparison of images

>> subplot(121),imshow(I)
>> title('Grayscale image ex4-fig1.png')
>> subplot(122),imshow(I2)
>> title('Equalized image')
>> truesize

Jorma Kekalainen

Digital Image Processing

655

Note
The Image Toolbox includes several other functions
that perform contrast adjustment, including the
imadjust and adapthisteq functions.
See Adjusting Pixel Intensity Values for more information.

In addition, the toolbox includes an interactive tool,


called the Adjust Contrast tool, that we can use to
adjust the contrast and brightness of an image
displayed in the Image Tool.
To use this tool, call the imcontrast function or access
the tool from the Image Tool.
For more information, see Adjusting Image Contrast
Using the Adjust Contrast Tool.
Jorma Kekalainen

Lecture weeks 11 and 12

Digital Image Processing

656

Page 40

Lecture notes

Digital Image Processing by Jorma Kekalainen

(d) Write the image to a disk file


To write the newly adjusted image I2 to a disk
file, use the imwrite function.
If we include the filename extension '.png', the
imwrite function writes the image to a file in
Portable Network Graphics (PNG) format, but we
can also specify other formats, e.g.,
imwrite (I2, 'ex4-fig12.tif');
See the imwrite function reference page for a list
of file formats it supports.
See also Writing Image Data to a File for more
information about writing image data to files.
Jorma Kekalainen
Digital Image Processing
Note:
Tagged Image File Format
(TIFF) 'tiff' or 'tif'.

657

(e) Check the contents of the


newly written file
To see what imwrite wrote to the disk file, use
the imfinfo function.
imfinfo('ex4-fig12.tif')
The imfinfo function returns information
about the image in the file, such as its format,
size, width, and height.
See Getting Information About a Graphics File
for more information about using imfinfo.
Jorma Kekalainen

Lecture weeks 11 and 12

Digital Image Processing

658

Page 41

Lecture notes

Digital Image Processing by Jorma Kekalainen

imfinfo('ex4-fig12.tif') and
imfinfo('ex4-fig12.png')

>> imfinfo('ex4-fig12.tif')
ans =
Filename: [1x47 char]
FileModDate: '07-elo-2015 23:23:34'
FileSize: 46868
Format: 'tif'
FormatVersion: []
Width: 357
Height: 221
BitDepth: 8
ColorType: 'grayscale'

Jorma Kekalainen

>> imwrite (I2,'ex4-fig12.png');


>> imfinfo('ex4-fig12.png')
ans =
Filename: [1x47 char]
FileModDate: '07-Aug-2015 23:29:41'
FileSize: 17879
Format: 'png'
FormatVersion: []
Width: 357
Height: 221
BitDepth: 8
ColorType: 'grayscale'

Digital Image Processing

659

Exercise
The following data is picked from a small (8*8)
image of type uint8 which has gray values in the
range 0 to 19. Compute the gray level histogram
of the image and then equalize this histogram.
Compare histograms and images before and after
equalization.
data=[12 6 5 13 14 14 16 15 11 10 8 5 8 11 14 14
9 8 3 4 7 12 18 19 10 7 4 2 10 12 13 17 16 9 13 13
16 19 19 17 12 10 14 15 18 18 16 14 11 8 10 12
14 13 14 15 8 6 3 7 9 11 12 12]
Jorma Kekalainen

Lecture weeks 11 and 12

Digital Image Processing

660

Page 42

Lecture notes

Digital Image Processing by Jorma Kekalainen

Solution
data=[12 6 5 13 14 14 16 15 11 10 8 5 8 11 14 14 9 8 3 4 7 12 18 19 10 7 4 2 10 12
13 17 16 9 13 13 16 19 19 17 12 10 14 15 18 18 16 14 11 8 10 12 14 13 14 15 8 6 3
7 9 11 12 12]
imdata =
12 11 9 10 16 12 11 8
imdata=reshape(data,8,8)
6 10 8 7 9 10 8 6
imdata1=imdata'
5 8 3 4 13 14 10 3
IM=uint8(imdata1)
13 5 4 2 13 15 12 7
figure, imhist(IM)
14 8 7 10 16 18 14 9
title('Histogram of the image')
14 11 12 12 19 18 13 11
eqIM=histeq(IM)
16 14 18 13 19 16 14 12
figure, imhist(eqIM)
15 14 19 17 17 14 15 12
title('Histogram of the equalized image')
imdata1 =
figure, imshow(IM)
12 6 5 13 14 14 16 15
title('The small image before equalization')
11 10 8 5 8 11 14 14
figure, imshow(eqIM)
9 8 3 4 7 12 18 19
title('The small image after equalization')
10 7 4 2 10 12 13 17

16 9 13 13 16 19 19 17
12 10 14 15 18 18 16 14
11 8 10 12 14 13 14 661
15
Jorma Kekalainen
Digital Image Processing
8 6 3 7 9 11 12 12

Note
nbins=8;
figure,hist(data,nbins)
title('Histogram plot of the original data')

Jorma Kekalainen

Lecture weeks 11 and 12

Digital Image Processing

662

Page 43

Lecture notes

Digital Image Processing by Jorma Kekalainen

Solution
IM =
12 6 5 13 14 14 16 15
11 10 8 5 8 11 14 14
9 8 3 4 7 12 18 19
10 7 4 2 10 12 13 17
16 9 13 13 16 19 19 17
12 10 14 15 18 18 16 14
11 8 10 12 14 13 14 15
8 6 3 7 9 11 12 12
eqIM =
130 28 20 154 178 178 215 202
105 89 57 20 57 105 178 178
73 57 4 12 40 130 239 251
89 40 12 0 89 130 154 227
215 73 154 154 215 251 251 227
130 89 178 202 239 239 215 178
105 57 89 130 178 154 178 202
28 4 40 73 105 130 130
Jorma57
Kekalainen
Digital Image Processing

663

Solution
The images before and after equalization

Jorma Kekalainen

Lecture weeks 11 and 12

Digital Image Processing

664

Page 44

Lecture notes

Digital Image Processing by Jorma Kekalainen

Exercise
Create a dark image with
c=imread('cameraman.tif');
[x,map]=gray2ind(c);

The matrix x, when viewed, will appear as a


very dark version of the cameraman image.
Apply histogram equalization to it, and
compare the result with the original image.

Jorma Kekalainen

Digital Image Processing

665

Digital Image Processing


Thresholding

Lecture weeks 11 and 12

Page 45

Lecture notes

Digital Image Processing by Jorma Kekalainen

Single thresholding
A grayscale image is turned into a binary (black and
white) image by first choosing a gray level T in the
original image, and then turning every pixel black or
white according to whether its gray value is greater than
or less than T
white if its gray level is >T
A pixel becomes
black if its gray level is T
Thresholding is a vital part of image segmentation,
where we wish to isolate objects from the background.
It is also an important component of robot vision.
Jorma Kekalainen

Digital Image Processing

667

Thresholding in Matlab
Thresholding can be done simply in Matlab.
Suppose we have an image, stored as the
variable X.
Then the command
X>T
will perform the thresholding.
We can view the result with imshow.
Jorma Kekalainen

Lecture weeks 11 and 12

Digital Image Processing

668

Page 46

Lecture notes

Digital Image Processing by Jorma Kekalainen

Example
The commands
>> r=imread('rice.tif');
>> imshow(r),figure,imshow(r>120)
will produce the images shown below.

Jorma Kekalainen

Digital Image Processing

The resulting image can then


be further processed to
find the number, or average
size of the grains.

669

Note
To see how this thresholding works, recall that in
Matlab, an operation on a single number, when applied
to a matrix, is interpreted as being applied
simultaneously to all elements of the matrix; this is
vectorization.
The command X>T will thus return 1 (for true) for all
those pixels for which the gray values are greater than
T, and 0 (for false) for all those pixels for which the gray
values are less than or equal to T.
We thus end up with a matrix of 0's and 1's, which can
be viewed as a binary image.
Jorma Kekalainen

Lecture weeks 11 and 12

Digital Image Processing

670

Page 47

Lecture notes

Digital Image Processing by Jorma Kekalainen

Example
Consider the following 8*8 image
3 148 117 148 145 178 132 174
2 176 174 110 185 155 118 165
0 100 124 113 193 136 146 108
0 155 170 106 158 130 178 170
9 196 138 113 108 127 144 139
6 188 143 183 137 162 105 169
9 122 156 119 188 179 100 151
8 176 137 114 135 123 134 183

Threshold it at
(a) level 100
(b) level 150
Jorma Kekalainen

Digital Image Processing

671

Example
>> data=[3 148 117 148 145 178 132 174
2 176 174 110 185 155 118 165
0 100 124 113 193 136 146 108
0 155 170 106 158 130 178 170
9 196 138 113 108 127 144 139
6 188 143 183 137 162 105 169
9 122 156 119 188 179 100 151
8 176 137 114 135 123 134 183
]
data =
3 148
2 176
0 100
0 155
9 196
6 188
9 122
8 176

117
174
124
170
138
143
156
137

Jorma Kekalainen

Lecture weeks 11 and 12

148
110
113
106
113
183
119
114

145
185
193
158
108
137
188
135

178
155
136
130
127
162
179
123

132
118
146
178
144
105
100
134

174
165
108
170
139
169
151
183

IM=uint8(data)
thIM1=IM>100
thIM2=IM>150
figure, imshow(IM)
title('The small image before thresholding')
figure, imshow(thIM1)
title('The small image after thresholding>100')
figure, imshow(thIM2)
title('The small image after thresholding>150')

Digital Image Processing

672

Page 48

Lecture notes

Digital Image Processing by Jorma Kekalainen

Example
IM =
3 148
2 176
0 100
0 155
9 196
6 188
9 122
8 176

117
174
124
170
138
143
156
137

148
110
113
106
113
183
119
114

145
185
193
158
108
137
188
135

178
155
136
130
127
162
179
123

132
118
146
178
144
105
100
134

174
165
108
170
139
169
151
183

Jorma Kekalainen

thIM1 =
0 1
0 1
0 0
0 1
0 1
0 1
0 1
0 1
thIM2 =
0 0
0 1
0 0
0 1
0 1
0 1
0 0
0 1

1
1
1
1
1
1
1
1

1
1
1
1
1
1
1
1

1
1
1
1
1
1
1
1

1
1
1
1
1
1
1
1

1
1
1
1
1
1
0
1

1
1
1
1
1
1
1
1

0
1
0
1
0
0
1
0

0
0
0
0
0
1
0
0

0
1
1
1
0
0
1
0

1
1
0
0
0
1
1
0

0
0
0
1
0
0
0
0

1
1
0
1
0
1
1
1

Digital Image Processing

673

Note
>> whos
Name

Size

8x8
8x8
8x8
8x8

IM
data
thIM1
thIM2

Jorma Kekalainen

Lecture weeks 11 and 12

Bytes Class
64
512
64
64
Digital Image Processing

Attributes

uint8
double
logical
logical
674

Page 49

Lecture notes

Digital Image Processing by Jorma Kekalainen

Example
The rice image shown previously has light grains on a dark
background; an image with dark objects over a light
background may be treated the same:
>> b=imread('bacteria.tif');
>> imshow(b),figure,imshow(b>100)
will produce the images shown below

Jorma Kekalainen

Digital Image Processing

675

im2bw function
Besides the above method, Matlab has the im2bw
function, which thresholds an image of any data
type, using the general syntax
im2bw(image,level)
where level is a value between 0 and 1 (inclusive),
indicating the fraction of gray values to be turned
white.
This command will work on grayscale,
colored and indexed images of data
type
Jormauint8,
Kekalainen uint16 or double.
Digital Image Processing

Lecture weeks 11 and 12

676

Page 50

Lecture notes

Digital Image Processing by Jorma Kekalainen

im2bw function
For example, the thresholded rice and bacteria images above
could be obtained using
>> im2bw(r,0.47);
>> im2bw(b,0.39);
The im2bw function automatically scales the value level to a
gray value appropriate to the image type, and then performs a
thresholding by our first method.

Jorma Kekalainen

Digital Image Processing

677

Exercise

Consider the following 8*8 image


3 148 117 148 145 178 132 174
2 176 174 110 185 155 118 165
0 100 124 113 193 136 146 108
0 155 170 106 158 130 178 170
9 196 138 113 108 127 144 139
6 188 143 183 137 162 105 169
9 122 156 119 188 179 100 151
8 176 137 114 135 123 134 183
Using im2bw function threshold it at
(a) level 100
(b) level 150

Jorma Kekalainen

Lecture weeks 11 and 12

Digital Image Processing

678

Page 51

Lecture notes

Digital Image Processing by Jorma Kekalainen

Solution
>> data=[3 148 117 148 145 178 132 174
2 176 174 110 185 155 118 165
0 100 124 113 193 136 146 108
0 155 170 106 158 130 178 170
9 196 138 113 108 127 144 139
6 188 143 183 137 162 105 169
9 122 156 119 188 179 100 151
8 176 137 114 135 123 134 183
]
data =
3 148 117 148 145 178 132 174
2 176 174 110 185 155 118 165
0 100 124 113 193 136 146 108
0 155 170 106 158 130 178 170
9 196 138 113 108 127 144 139
6 188 143 183 137 162 105 169
9 122 156 119 188 179 100 151
8 176 137 114 135 123 134 183
Jorma Kekalainen

IM=uint8(data)
thIM1=im2bw(IM,100/255)
thIM2= im2bw(IM,150/255)
figure, imshow(IM)
title('The small image before thresholding')
figure, imshow(thIM1)
title('The small image after thresholding>100')
figure, imshow(thIM2)
title('The small image after thresholding>150')

Digital Image Processing

679

Solution
data =
3 148
2 176
0 100
0 155
9 196
6 188
9 122
8 176

117
174
124
170
138
143
156
137

148
110
113
106
113
183
119
114

Jorma Kekalainen

Lecture weeks 11 and 12

145
185
193
158
108
137
188
135

178
155
136
130
127
162
179
123

132
118
146
178
144
105
100
134

174
165
108
170
139
169
151
183

thIM1 =
0 1
0 1
0 0
0 1
0 1
0 1
0 1
0 1

1
1
1
1
1
1
1
1

1
1
1
1
1
1
1
1

1
1
1
1
1
1
1
1

1
1
1
1
1
1
1
1

1
1
1
1
1
1
0
1

1
1
1
1
1
1
1
1

thIM2 =
0 0 0 0 0 1
0 1 1 0 1 1
0 0 0 0 1 0
0 1 1 0 1 0
0 1 0 0 0 0
0 1 0 1 0 1
0 0 1 0 1 1
0 Digital
1 Image
0 0Processing
0 0

0
0
0
1
0
0
0
0

1
1
0
1
0
1
1
1

680

Page 52

Lecture notes

Digital Image Processing by Jorma Kekalainen

Example
Besides isolating objects from the background,
thresholding provides a very simple way of
showing hidden aspects of an image.
For example, the image paper.tif appears all
white, as nearly all the gray values are very
high.
However, thresholding at a high level
produces an image of far greater interest.
Jorma Kekalainen

Digital Image Processing

681

Example
We can use the commands
>> p=imread('paper1.tif');
>> imshow(p),figure,imshow(p>241)
to provide the images shown below

Jorma Kekalainen

Lecture weeks 11 and 12

Digital Image Processing

682

Page 53

Lecture notes

Digital Image Processing by Jorma Kekalainen

Double thresholding
Here we choose two values T1 and T2 and apply a thresholding
operation as
white if its gray level is between T1 and T2
A pixel becomes
black if its gray level is otherwise
We can implement this by a simple variation on the above
method:
X>T1 & X<T2
Since the ampersand acts as a logical and, the result will only
produce a one where both inequalities are satisfied.
Jorma Kekalainen

Digital Image Processing

683

Example

Consider the following 8*8 image


3 148 117 148 145 178 132 174
2 176 174 110 185 155 118 165
0 100 124 113 193 136 146 108
0 155 170 106 158 130 178 170
9 196 138 113 108 127 144 139
6 188 143 183 137 162 105 169
9 122 156 119 188 179 100 151
8 176 137 114 135 123 134 183

Double threshold it at
(a) level 100
(b) level 150

Apply thresholding operation so that


white if its gray level is between 100 and 150
A pixel becomes
Jorma Kekalainen
Processing
blackDigital
if itsImage
gray
level is otherwise

Lecture weeks 11 and 12

684

Page 54

Lecture notes

Digital Image Processing by Jorma Kekalainen

Example
>> data=[3 148 117 148 145 178 132 174
2 176 174 110 185 155 118 165
0 100 124 113 193 136 146 108
0 155 170 106 158 130 178 170
IM=uint8(data)
9 196 138 113 108 127 144 139
thIM=IM>100 & IM<150
6 188 143 183 137 162 105 169
figure, imshow(IM)
9 122 156 119 188 179 100 151
title('Image before thresholding')
8 176 137 114 135 123 134 183
figure, imshow(thIM)
]

title('Image after thresholding (IM>100 & IM<150)')

data =
3 148 117
2 176 174
0 100 124
0 155 170
9 196 138
6 188 143
9 122 156
Jorma Kekalainen
8 176 137

148
110
113
106
113
183
119
114

145
185
193
158
108
137
188
135

178
155
136
130
127
162
179
123

132
118
146
178
144
105
100
134

174
165
108
170
139
169
151
183 Digital Image Processing

685

Example
IM =
3 148
2 176
0 100
0 155
9 196
6 188
9 122
8 176

117
174
124
170
138
143
156
137

148
110
113
106
113
183
119
114

Jorma Kekalainen

Lecture weeks 11 and 12

145
185
193
158
108
137
188
135

178
155
136
130
127
162
179
123

132
118
146
178
144
105
100
134

174
165
108
170
139
169
151
183

thIM =
0 1
0 0
0 0
0 0
0 0
0 0
0 1
0 0

Digital Image Processing

1
0
1
0
1
1
0
1

1
1
1
1
1
0
1
1

1
0
0
0
1
1
0
1

0
0
1
1
1
0
0
1

1
1
1
0
1
1
0
1

0
0
1
0
1
0
0
0

686

Page 55

Lecture notes

Digital Image Processing by Jorma Kekalainen

Example
Consider the following sequence of commands, which start by
producing an 8-bit gray version of the indexed image spine.tif:
Double thresholding brings out subtle
[x,map]=imread('spine.tif');
features of the spine which single
s=uint8(ind2gray(x,map));
thresholding would be unable to do.
figure,imshow(s),figure,imshow(s>115 & s<125)

Jorma Kekalainen

Digital Image Processing

687

Note
We can obtain similar results using im2bw:
imshow(im2bw(x,map,0.45)&~im2bw(x,map,0.5))

Jorma Kekalainen

Lecture weeks 11 and 12

Digital Image Processing

688

Page 56

Lecture notes

Digital Image Processing by Jorma Kekalainen

Note: ~
~ means logical not.
~A performs a logical not of input array A, and
returns an array containing elements set to
either logical 1 (TRUE) or logical 0 (FALSE).
An element of the output array is set to 1 if A
contains a zero value element at that same
array location.
Otherwise, that element is set to 0.
Jorma Kekalainen

Digital Image Processing

689

Applications of thresholding
Thresholding can be useful in the following situations:
(a) When we want to remove unnecessary detail from an
image and to concentrate on essentials.
(b) To bring out hidden detail.
(c) When we want to remove a varying background from text
or a drawing.
Examples of (a) were given in the rice and bacteria images: by
removing all gray level information, the rice and bacteria were
reduced to binary blobs.
But this information may be all we need to investigate sizes,
shapes, or numbers of blobs.
Jorma Kekalainen

Lecture weeks 11 and 12

Digital Image Processing

690

Page 57

Lecture notes

Digital Image Processing by Jorma Kekalainen

Example

R=rand(256)*128+127;
T=imread('text.png');figure,imshow(T)
TT=uint8(R.*double(not(T)));
figure,imshow(TT)

The first command simply uses the


rand function (which produces matrices
of uniformly generated random
numbers between 0.0 and 1.0), and
scales the result so that random
numbers are between 127 and 255.
We then read in the text image, which
shows white text on a dark background.
Jorma Kekalainen

Digital Image Processing

691

Example
The third command row
TT=uint8(R.*double(not(T)));
does several things at once: not(T) reverses the text image so
as to have black text on a white background; double changes
the numeric type so that the matrix can be used with
arithmetic operations; finally the result is multiplied into the
random matrix, and the whole thing converted to uint8 for
display.

Jorma Kekalainen

Lecture weeks 11 and 12

Digital Image Processing

692

Page 58

Lecture notes

Digital Image Processing by Jorma Kekalainen

Example
If we threshold this image and display the result with
>> imshow(TT>100)
the background has been completely removed.

Jorma Kekalainen

Digital Image Processing

693

Example

R=rand(107,122)*128+127;
L=imread('logo.tif');
figure, imshow(L)
TL=uint8(R.*double(not(L)));
figure, imshow(TL)
figure, imshow(TL>100)

Jorma Kekalainen

Lecture weeks 11 and 12

Digital Image Processing

694

Page 59

Lecture notes

Digital Image Processing by Jorma Kekalainen

Exercise
Superimpose the image text.png onto the
image cameraman.tif:
t=imread('text.png');
c=imread('cameraman.tif');
m=uint8(double(c).*double(~t));
Can you threshold this new image m to isolate
the text?

Jorma Kekalainen

Digital Image Processing

695

Solution

Superimpose the image text.png onto the image cameraman.tif:


t=imread('text.png');
c=imread('cameraman.tif');
m=uint8(double(c).*double(~t));
figure, imshow(m),figure, imshow(m>100)
figure, imshow(m>10),figure, imshow(m>5)

Ans. Yes, we can threshold this new image m


to isolate the text!

Jorma Kekalainen

Lecture weeks 11 and 12

Digital Image Processing

696

Page 60

You might also like