Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Page 1
Lecture notes
Introduction
Here, we look at image quality, and how that
may be affected by various image attributes.
Quality is, of course, a highly subjective
matter.
However, for human vision in general, images
are preferred to be sharp and detailed.
This is a consequence of two properties of an
image: spatial resolution, and quantization.
Jorma Kekalainen
501
502
Page 2
Lecture notes
503
Example
c=imread('cameraman.tif');figure,imshow(c)
cd=double(c);figure,imshow(cd)
Jorma Kekalainen
Page 3
Lecture notes
Example
As we can see, figure (b) doesn't look like the original
picture at all!
This is because for a matrix of type double, the imshow
function expects the values to be between 0 and 1, where 0
is displayed as black, and 1 is displayed as white.
A value 0<v<1 is displayed as grayscale [255v].
Conversely, values greater than 1 will be displayed as 1
(white) and values less than 0 will be displayed as zero
(black).
In the cameraman image, every pixel has value greater
than or equal to 1 (in fact the minimum value is 7), so that
every pixel will be displayed as white.
Jorma Kekalainen
505
Example
To display the matrix cd, we need to scale it to
the range 0 - 1.
This is easily done simply by dividing all values
by 255:
imshow(cd/255)
and the result will be the cameraman image
as shown in the previous figure (a).
Jorma Kekalainen
506
Page 4
Lecture notes
Scaling
We can vary the display by changing the scaling of the matrix.
Results of the commands:
imshow(cd/512)
imshow(cd/128)
Jorma Kekalainen
507
Results of scaling
Dividing by 512 darkens the image, as all
matrix values are now between 0 and 0.5, so
that the brightest pixel in the image is a midgray.
Dividing by 128 means that the range is 0-2,
and all pixels in the range 1-2 will be displayed
as white.
Thus the image has an over-exposed, washedout appearance.
Jorma Kekalainen
508
Page 5
Lecture notes
Comment
The display of the result of a command whose
output is a matrix of type double can be
greatly affected by a judicious choice of a
scaling factor.
Jorma Kekalainen
509
im2double
We can convert the original image to double more
properly using the function im2double.
This applies correct scaling so that the output values
are between 0 and 1.
So the commands
cd=im2double(c);
imshow(cd)
will produce a correct image.
Jorma Kekalainen
510
Page 6
Lecture notes
Jorma Kekalainen
511
512
Page 7
Lecture notes
Image types
>> whos
Name
c
c2
c3
cd
cd1
cd512
Jorma Kekalainen
Size
Bytes
256x256
256x256
256x256
256x256
256x256
256x256
65536
65536
65536
524288
524288
524288
Digital Image Processing
Class
Attributes
uint8
uint8
uint8
double
double
double
513
Example
load('mandrill.mat');
m=im2uint8(ind2gray(X,map));
figure,imshow(m)
title('imshow(m)')
figure,imshow(X,map)
title('imshow(X,map)')
Jorma Kekalainen
514
Page 8
Lecture notes
The following example reads an image into the Matlab workspace and
then displays the image in a MATLAB figure window.
moon = imread('moon.tif');
imshow(moon);
You can also pass imshow the name of a file containing an image.
Note that when you use this syntax, imread does not store the image data
in the Matlab workspace. If you want to bring the image into the
workspace, you must use the getimage function.
This example assigns the image data from moon.tif to the variable moon,
if the figure window in which it is displayed is currently active
imshow('moon.tif');
moon = getimage;
For more information about using imshow to display the various image
types supported by the toolbox, see Display Different Image Types.
Jorma Kekalainen
515
Images displayed
Jorma Kekalainen
516
Page 9
Lecture notes
Initial magnification
Jorma Kekalainen
518
Page 10
Lecture notes
The Image Processing Toolbox Preferences dialog box contains display preferences
for imtool and imshow. We can set all preferences at the command line with the
iptsetpref function.
Jorma Kekalainen
519
520
Page 11
Lecture notes
Jorma Kekalainen
521
F=imread('peppers.png');
imshow(F(:,:,1))
figure, imshow(F(:,:,2))
figure, imshow(F(:,:,3))
Jorma Kekalainen
522
Page 12
Lecture notes
Jorma Kekalainen
523
Note: When we use subplot to display multiple color images in one figure window,
the images must share the colormap of the last image displayed. In some cases the
display results can be unacceptable. As an alternative, we can use the subimage
Jorma Kekalainen
Digital Image Processing
524
function,
or we can map all images to
the same colormap as we load them.
Page 13
Lecture notes
[X1,map1]=imread('forest.tif');
[X2,map2]=imread('trees.tif');
subplot(1,2,1), imshow(X1,map1)
subplot(1,2,2), imshow(X2,map2)
[X1,map1]=imread('forest.tif');
[X2,map2]=imread('trees.tif');
subplot(1,2,1), subimage(X1,map1)
subplot(1,2,2), subimage(X2,map2)
Jorma Kekalainen
525
[X1,map1]=imread('forest.tif');
[X2,map2]=imread('trees.tif');
subplot(1,2,1), subimage(X1,map1)
subplot(1,2,2), subimage(X2,map2)
[X1,map1]=imread('forest.tif');
[X2,map2]=imread('trees.tif');
subplot(1,2,1), imshow(X1,map1)
subplot(1,2,2), imshow(X2,map2)
Jorma Kekalainen
526
Page 14
Lecture notes
Jorma Kekalainen
527
528
Page 15
Lecture notes
Binary images
Recall that a binary image will have only two
values: 0 and 1.
Matlab does not have a binary data type as such,
but it does have a logical flag, where uint8 values
as 0 and 1 can be interpreted as logical data.
The logical flag will be set by the use of relational
operations such as ==, < or > or any other
operations which provide a yes/no answer.
Jorma Kekalainen
529
Example
For example, suppose we take the cameraman matrix and
create a new matrix with the relational operator >
>> c1=c>128;
If we now check all of our variables with whos, the output will
include the line:
c1
256x256
Jorma Kekalainen
65536 logical
530
Page 16
Lecture notes
Bit planes
Grayscale images can be transformed into a sequence of
binary images by breaking them up into their bit-planes.
If we consider the gray value of each pixel of an 8-bit image
as an 8-bit binary word, then the 0th bit plane consists of
the last bit of each gray value.
Since this bit has the least effect in terms of the magnitude
of the value, it is called the least significant bit, and the
plane consisting of those bits the least significant bit plane.
Similarly the 7th bit plane consists of the first bit in each
value.
This bit has the greatest effect in terms of the magnitude of
the value, so it is called the most significant bit, and the
plane consisting of those bits the most significant bit plane.
Jorma Kekalainen
531
Example
Jorma Kekalainen
532
Page 17
Lecture notes
c=imread('Hedyg-scale.tif');
cd=double(c);
c0=mod(cd,2);
imshow(c0)
Jorma Kekalainen
533
Jorma Kekalainen
534
Page 18
Lecture notes
Jorma Kekalainen
535
Jorma Kekalainen
536
Page 19
Lecture notes
Jorma Kekalainen
537
Jorma Kekalainen
538
Page 20
Lecture notes
Jorma Kekalainen
539
Jorma Kekalainen
540
Page 21
Lecture notes
cc=2*(2*(2*(2*(2*(2*(2*c7+c6)+c5)+c4)+c3)+c2)+c1)+c0;
figure, imshow(uint8(cc))
Original grayscale image
Jorma Kekalainen
541
Original image
Jorma Kekalainen
542
Hedy Lamarr aka Hedwig Kiesler Hollywood star and inventor (Patent US 2292387 A)
Page 22
Lecture notes
Note
The least significant bit plane, c0, is practically a random array.
The most significant bit plane, c7, is actually a threshold of the
image at level 127:
ct=c>127;
all(c7(:)==ct(:))
ans =
1
We can recover and display the original image with
cc=2*(2*(2*(2*(2*(2*(2*c7+c6)+c5)+c4)+c3)+c2)+c1)+c0;
imshow(uint8(cc))
Jorma Kekalainen
543
whos
>> whos
Name
c
c0
c1
c2
c3
c4
c5
c6
c7
c8
cc
cd
467x557
467x557
467x557
467x557
467x557
467x557
467x557
467x557
467x557
467x557
467x557
467x557
Jorma Kekalainen
Size
Bytes
Class
260119
2080952
2080952
2080952
2080952
2080952
2080952
2080952
2080952
2080952
2080952
2080952
Attributes
uint8
double
double
double
double
double
double
double
double
double
double
double
544
Page 23
Lecture notes
c=imread('Hedyg-scale.tif');
cd=double(c);
>> c0=mod(cd,2);
c1=mod(floor(cd/2),2);
c2=mod(floor(cd/4),2);
c3=mod(floor(cd/8),2);
c4=mod(floor(cd/16),2);
c5=mod(floor(cd/32),2);
c6=mod(floor(cd/64),2);
c7=mod(floor(cd/128),2);
Jorma Kekalainen
imshow(c0)
figure, imshow(c1)
figure, imshow(c2)
figure, imshow(c3)
figure, imshow(c4)
figure, imshow(c5)
figure, imshow(c6)
figure, imshow(c7)
cc=2*(2*(2*(2*(2*(2*(2*c7
+c6)+c5)+c4)+c3)+c2)+c1)+c
0;
figure,imshow(uint8(cc))
545
Exercise
Transform the image cameraman.tif into a
sequence of binary images by breaking them
up into their bit-planes.
Jorma Kekalainen
546
Page 24
Lecture notes
Solution
c=imread('cameraman.tif');
cd=double(c);
%We now isolate the bit planes by simply dividing the matrix cd by successive powers of 2, taking
%the remainder, and seeing if the final bit is 0 or 1. We can do this with the mod function.
c0=mod(cd,2);
c1=mod(floor(cd/2),2);
c2=mod(floor(cd/4),2);
c3=mod(floor(cd/8),2);
c4=mod(floor(cd/16),2);
c5=mod(floor(cd/32),2);
c6=mod(floor(cd/64),2);
c7=mod(floor(cd/128),2);
>> imshow(c0)
>> figure, imshow(c1)
>> figure, imshow(c2)
>> figure, imshow(c3)
>> figure, imshow(c4)
>> figure, imshow(c5)
>> figure, imshow(c6)
>> figure, imshow(c7)
>>
Jorma Kekalainen
547
Jorma Kekalainen
548
Page 25
Lecture notes
Jorma Kekalainen
549
Note
We can recover and display the original image
with
cc=2*(2*(2*(2*(2*(2*(2*c7+c6)+c5)+c4)+c3)+c2)+c1)+c0;
imshow(uint8(cc))
Jorma Kekalainen
550
Page 26
Lecture notes
mod
Remainder after division (modulo operation)
Z = mod(X,Y)
Z = mod(X,Y) returns the remainder after
division of X by Y, where X is the dividend and
Y is the divisor.
This function is called the modulo operation
and is computed using Z = X - Y.*floor(X./Y).
The mod function follows the convention that
mod(X,0) returns X.
Note:Jorma
rem(X,Y)
and mod(X,Y) are equal if Digital
X and
Y have the same sign, but differ by Y if X and
Y
Kekalainen
Image Processing
551
have different signs. Notice that rem retains the sign of X, while mod retains the sign of Y.
Example
Compute 22 modulo 5.
Z = mod(X/Y)=mod(22,5)
Z =2
Check
Z = X - Y.*floor(X./Y)=22-5*floor(22/5)
Z = 22-5*4=2
Jorma Kekalainen
552
Page 27
Lecture notes
Example
Z = X - Y.*floor(X./Y)
>> X=22;
>> Y=5;
>> X./Y
ans =
4.4000
>> floor(4.4)
ans =
4
>> Z=X-Y.*4
Z=
2
>> Z = X - Y.*floor(X./Y)
Z=
2
>> Z=mod(X,Y)
Z=
2
Jorma Kekalainen
553
Exercise
Jorma Kekalainen
554
Page 28
Lecture notes
Solution
>> A=floor(255*rand(5))
A=
207 24 40 36 167
230 71 247 107 9
32 139 244 233 216
232 244 123 202 238
161 246 204 244 173
>> A0=rem(A,2)
A0 =
1 0 0 0 1
0 1 1 1 1
0 1 0 1 0
0 0 1 0 0
1 0 0 0 1
>> A1=rem(floor(A/2),2)
A1 =
1 0 0 0 1
1 1 1 1 0
0 1 0 0 0
0 0 1 1 1
0 1 0 0 0
Jorma Kekalainen
>> A2=rem(floor(A/4),2)
A2 =
1 0 0 1 1
1 1 1 0 0
0 0 1 0 0
0 1 0 0 1
0 1 1 1 1
>> A3=rem(floor(A/8),2)
A3 =
1 1 1 0 0
0 0 0 1 1
0 1 0 1 1
1 0 1 1 1
0 0 1 0 1
>> A4=rem(floor(A/16),2)
A4 =
0 1 0 0 0
0 0 1 0 0
0 0 1 0 1
0 1 1 0 0
0 1 0 1 0
555
Solution
>> A5=rem(floor(A/32),2)
A5 =
0 0 1 1 1
1 0 1 1 0
1 0 1 1 0
1 1 1 0 1
1 1 0 1 1
>> A6=rem(floor(A/64),2)
A6 =
1 0 0 0 0
1 1 1 1 0
0 0 1 1 1
1 1 1 1 1
0 1 1 1 0
>> A7=rem(floor(A/128),2)
A7 =
1 0 0 0 1
1 0 1 0 0
0 1 1 1 1
1 1 0 1 1
1 1 1 1 1
Jorma Kekalainen
>>
AA=2*(2*(2*(2*(2*(2*(2*A7+A6)+A5)+A4)+A3)+A2)+A1)+A0
AA =
207 24 40 36 167
230 71 247 107 9
32 139 244 233 216
232 244 123 202 238
161 246 204 244 173
>> Ath=A>127
Ath =
1 0 0 0 1
1 0 1 0 0
0 1 1 1 1
1 1 0 1 1
1 1 1 1 1
>> A=floor(255*rand(5))
A=
207 24 40 36 167
230 71 247 107 9
32 139 244 233 216
232 244 123 202 238
161 246 204 244 173
556
Page 29
Lecture notes
Spatial resolution
Spatial resolution is the density of pixels over
the image: the greater the spatial resolution,
the more pixels are used to display the image.
We can experiment with spatial resolution
with Matlab's imresize function.
Jorma Kekalainen
558
Page 30
Lecture notes
imresize function
Suppose we have an 256*256 8-bit grayscale image saved to
the matrix x.
Then the command
imresize(x,1/2, 'nearest');
will halve the size of the image by taking out every other row
and every other column, thus leaving only those matrix
elements whose row and column indices are even:
imresize(x,1/2, 'nearest')
Jorma Kekalainen
559
imresize function
If we apply imresize to The effective resolution of this
the result with the
new image is only 128*128.
parameter 2 rather than We can do all this at the same
1/2, all the pixels are
time:
repeated to produce an imresize(imresize(x,1/2,
'nearest'),2,'nearest');
image with about the
same size as the
original, but with half
the resolution in each
direction:
Jorma Kekalainen
560
Page 31
Lecture notes
28
198
99
61
102
24
Jorma Kekalainen
33 90 186 48 20
240 209 165 175 236
243 3 114 46 197
146 10 139 93 124
15 43 75 159 111
59 165 189 198 113
Digital Image Processing
561
240
240
146
146
59
59
Jorma Kekalainen
562
Page 32
Lecture notes
3
3
3
3
165
165
165
165
3 3 3
3 3 3
3 3 3
3 3 3
165 165
165 165
165 165
165 165
197
197
197
197
165
165
165
165
Jorma Kekalainen
197
197
197
197
113
113
113
113
197
197
197
197
113
113
113
113
197
197
197
197
113
113
113
113
113
113
113
113
563
159
159
159
159
159
159
159
159
Jorma Kekalainen
159
159
159
159
159
159
159
159
159
159
159
159
159
159
159
159
159
159
159
159
159
159
159
159
159
159
159
159
159
159
159
159
159
159
159
159
159
159
159
159
159
159
159
159
159
159
159
159
159
159
159
159
159
159
159
159
564
Page 33
Lecture notes
>>
Aold1=imresize_old(imresize_old(
A,1/4),4)
Aold =
Aold1 =
240
240
146
146
59
59
Jorma Kekalainen
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
565
Note
About the same effect we can achieve
applying the command
imresize_old( imresize_old(x,1/2),2);
Jorma Kekalainen
566
Page 34
Lecture notes
Effective resolution
By changing the parameters of imresize, we
can change the effective resolution of the
image to smaller amounts:
Jorma Kekalainen
567
Example
To see the effects of these commands,
suppose we apply them to the image
Deepiga.png:
x=imread('Deepiga.png');
The effects of decreasing blockiness or
pixelization become quite pronounced as the
resolution increases.
Jorma Kekalainen
568
Page 35
Lecture notes
Example
x=imread('Deepiga.png');
x6=imresize(imresize(x,1/64, 'nearest'),64,'nearest');
figure,imshow(x6)
x5=imresize(imresize(x,1/32, 'nearest'),32,'nearest');
figure,imshow(x5)
x4=imresize(imresize(x,1/16, 'nearest'),16,'nearest');
figure,imshow(x4)
x3=imresize(imresize(x,1/8 ,'nearest'),8,'nearest');
figure,imshow(x3)
x2=imresize(imresize(x,1/4, 'nearest'),4,'nearest');
figure,imshow(x2)
x1=imresize(imresize(x,1/2 ,'nearest'),2,'nearest');
figure,imshow(x1)
figure,imshow(x)
Jorma Kekalainen
569
Jorma Kekalainen
570
Page 36
Lecture notes
Jorma Kekalainen
571
Jorma Kekalainen
572
Page 37
Lecture notes
Jorma Kekalainen
573
Jorma Kekalainen
574
Page 38
Lecture notes
Jorma Kekalainen
575
Jorma Kekalainen
576
Page 39