Académique Documents
Professionnel Documents
Culture Documents
Page 1
Lecture notes
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
580
Page 2
Lecture notes
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
582
Page 3
Lecture notes
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
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
584
Page 4
Lecture notes
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
586
Page 5
Lecture notes
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
587
Jorma Kekalainen
588
Page 6
Lecture notes
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
589
c3: y=x/2
Jorma Kekalainen
590
Page 7
Lecture notes
Jorma Kekalainen
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
592
Page 8
Lecture notes
c5: y=x/2+128
Jorma Kekalainen
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
594
Page 9
Lecture notes
Binary conversion
BI=im2bw(Deepiga,0.5);% Binary conversion with threshold
>> imshow(BI)
>> figure, imshow(~BI)
Jorma Kekalainen
595
Note: im2bw
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
Jorma Kekalainen
596
Page 10
Lecture notes
Image complementation
Next figure shows the complement function y=255-x, and the
result of the commands
>> cc=imcomplement(c);
>> imshow(cc)
Jorma Kekalainen
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
598
Page 11
Lecture notes
Solarization
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
600
Page 12
Lecture notes
Jorma Kekalainen
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
602
Page 13
Lecture notes
Jorma Kekalainen
603
604
Page 14
Lecture notes
Jorma Kekalainen
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:
606
Page 15
Lecture notes
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
607
Corresponding stretched
histogram
A stretched histogram indicates an image
with greater contrast than the original.
Jorma Kekalainen
A histogram of a poorly
contrasted image
608
Page 16
Lecture notes
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.
Page 17
Lecture notes
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
611
Example
Jorma Kekalainen
612
Page 18
Lecture notes
Jorma Kekalainen
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
614
Page 19
Lecture notes
concave upward
concave downward
Jorma Kekalainen
615
Example
The function used is a slight variation on the standard
line between two points:
616
Page 20
Lecture notes
Example
The tire image and after adjustment with the
gamma value =0.5.
Jorma Kekalainen
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
618
Page 21
Lecture notes
Stretching function
Stretching function used in the previous tire image.
=0.5 (<1 concave downward)
Jorma Kekalainen
619
620
Page 22
Lecture notes
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
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
622
Page 23
Lecture notes
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
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
624
Page 24
Lecture notes
Example
Suppose a 4-bit grayscale image has the histogram
shown in the following figure associated with a table
of the numbers (n=360)
625
Example
Histogram indicating poor contrast
Jorma Kekalainen
626
Page 25
Lecture notes
Example
To equalize this
histogram, we
form running
totals of the ni ,
and multiply each
by 15/360=1/24.
Jorma Kekalainen
627
Example
We now have the following transformation of gray
values, obtained by reading off the first and last
columns in the previous table:
628
Page 26
Lecture notes
Example
The histogram before
equalization
Jorma Kekalainen
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
630
Page 27
Lecture notes
Jorma Kekalainen
631
Jorma Kekalainen
Since the
index matrix
contains only
low values it
will appear
dark when
displayed.
Digital Image Processing
632
Page 28
Lecture notes
Jorma Kekalainen
633
Jorma Kekalainen
634
Page 29
Lecture notes
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
635
Example
H=imread('he.tif');
figure,imshow(H),figure,imhist(H(:,:,1)),axis
tight
Jorma Kekalainen
636
Page 30
Lecture notes
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
637
After equalization
hee=histeq(he(:,:,1));
figure, imshow(hee),
title('After equalization')
figure,imhist(hee),axis tight
title('After equalization')
Jorma Kekalainen
638
Page 31
Lecture notes
Example
Jorma Kekalainen
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
640
Page 32
Lecture notes
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
equalization section.
641
Page 33
Lecture notes
643
Page 34
Lecture notes
>> 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
645
Page 35
Lecture notes
Jorma Kekalainen
647
Page 36
Lecture notes
Jorma Kekalainen
Size
221x357
649
Page 37
Lecture notes
Jorma Kekalainen
651
Jorma Kekalainen
652
Page 38
Lecture notes
Jorma Kekalainen
653
Jorma Kekalainen
654
Page 39
Lecture notes
>> subplot(121),imshow(I)
>> title('Grayscale image ex4-fig1.png')
>> subplot(122),imshow(I2)
>> title('Equalized image')
>> truesize
Jorma Kekalainen
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.
656
Page 40
Lecture notes
657
658
Page 41
Lecture notes
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
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
660
Page 42
Lecture notes
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
662
Page 43
Lecture notes
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
664
Page 44
Lecture notes
Exercise
Create a dark image with
c=imread('cameraman.tif');
[x,map]=gray2ind(c);
Jorma Kekalainen
665
Page 45
Lecture notes
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
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
668
Page 46
Lecture notes
Example
The commands
>> r=imread('rice.tif');
>> imshow(r),figure,imshow(r>120)
will produce the images shown below.
Jorma Kekalainen
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
670
Page 47
Lecture notes
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
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
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')
672
Page 48
Lecture notes
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
673
Note
>> whos
Name
Size
8x8
8x8
8x8
8x8
IM
data
thIM1
thIM2
Jorma Kekalainen
Bytes Class
64
512
64
64
Digital Image Processing
Attributes
uint8
double
logical
logical
674
Page 49
Lecture notes
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
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
676
Page 50
Lecture notes
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
677
Exercise
Jorma Kekalainen
678
Page 51
Lecture notes
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')
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
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
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
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
682
Page 53
Lecture notes
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
683
Example
Double threshold it at
(a) level 100
(b) level 150
684
Page 54
Lecture notes
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)
]
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
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
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
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
687
Note
We can obtain similar results using im2bw:
imshow(im2bw(x,map,0.45)&~im2bw(x,map,0.5))
Jorma Kekalainen
688
Page 56
Lecture notes
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
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
690
Page 57
Lecture notes
Example
R=rand(256)*128+127;
T=imread('text.png');figure,imshow(T)
TT=uint8(R.*double(not(T)));
figure,imshow(TT)
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
692
Page 58
Lecture notes
Example
If we threshold this image and display the result with
>> imshow(TT>100)
the background has been completely removed.
Jorma Kekalainen
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
694
Page 59
Lecture notes
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
695
Solution
Jorma Kekalainen
696
Page 60