Sei sulla pagina 1di 39

Lecture notes

Digital Image Processing by Jorma Kekalainen

Digital Image Processing


Jorma Kekalainen

Digital Image Processing


Displaying Images

Lecture weeks 9 and 10

Page 1

Lecture notes

Digital Image Processing by Jorma Kekalainen

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

Digital Image Processing

501

Imshow function and grayscale


images
We have seen that if x is a matrix of type uint8, then
the command
imshow(x)
will display x as an image.
This is reasonable, since the data type uint8 restricts
values to be integers between 0 and 255.
However, not all image matrices come so nicely
bundled up into this data type, and lots of Matlab
image processing commands produces output
matrices which are of type double.
Jorma Kekalainen

Lecture weeks 9 and 10

Digital Image Processing

502

Page 2

Lecture notes

Digital Image Processing by Jorma Kekalainen

Imshow function and grayscale


images
We have two choices with a matrix of this double type:
1. convert to type uint8 and then display,
2. display the matrix directly.
The second option is possible because imshow will
display a matrix of type double as a grayscale image as
long as the matrix elements are between 0 and 1.
Suppose we take an image and convert it to type
double:
c=imread('cameraman.tif');
cd=double(c);
imshow(c),figure,imshow(cd)
The results are shown in the following figure.
Jorma Kekalainen

Digital Image Processing

503

Example
c=imread('cameraman.tif');figure,imshow(c)
cd=double(c);figure,imshow(cd)

(a) Original image

Jorma Kekalainen

Lecture weeks 9 and 10

(b) After conversion to type double


504

Digital Image Processing

Page 3

Lecture notes

Digital Image Processing by Jorma Kekalainen

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

Digital Image Processing

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

Lecture weeks 9 and 10

Digital Image Processing

506

Page 4

Lecture notes

Digital Image Processing by Jorma Kekalainen

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

Digital Image Processing

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

Lecture weeks 9 and 10

Digital Image Processing

508

Page 5

Lecture notes

Digital Image Processing by Jorma Kekalainen

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

Digital Image Processing

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

Lecture weeks 9 and 10

Digital Image Processing

510

Page 6

Lecture notes

Digital Image Processing by Jorma Kekalainen

double vs. im2double


It is important to make the distinction between the two
functions double and im2double: double changes the
data type but does not change the numeric values;
im2double changes both the numeric data type and
the values.
The exception, of course, is if the original image is of
type double, in which case im2double does nothing.
Although the command double is not of much use for
direct image display, it can be very useful for image
arithmetic as we have seen with scaling examples.

Jorma Kekalainen

Digital Image Processing

511

uint8 vs. im2uint8


Corresponding to the functions double and
im2double are the functions uint8 and im2uint8.
If we take our image cd of type double, properly
scaled so that all elements are between 0 and 1,
we can convert it back to an image of type uint8
in two ways:
c2=uint8(255*cd);
c3=im2uint8(cd);
Use of im2uint8 is to be preferred; it takes other
data types as input, and always returns a correct
result.
Jorma Kekalainen

Lecture weeks 9 and 10

Digital Image Processing

512

Page 7

Lecture notes

Digital Image Processing by Jorma Kekalainen

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

Lecture weeks 9 and 10

Digital Image Processing

514

Page 8

Lecture notes

Digital Image Processing by Jorma Kekalainen

Displaying images using the


imshow function

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

Digital Image Processing

515

Images displayed

Jorma Kekalainen

Lecture weeks 9 and 10

Digital Image Processing

516

Page 9

Lecture notes

Digital Image Processing by Jorma Kekalainen

Specifying the initial image


magnification
By default, imshow attempts to display an image in its
entirety at 100% magnification (one screen pixel for each
image pixel).
However, if an image is too large to fit in a figure window
on the screen at 100% magnification, imshow scales the
image to fit onto the screen and issues a warning message.
To override the default initial magnification behavior for a
particular call to imshow, specify the InitialMagnification
parameter.
E.g., to view an image at 150% magnification, use this code
pout = imread('pout.tif');
imshow(pout, 'InitialMagnification', 150)
Note: imshow attempts to honor the magnification we specify. However, if the image does not
fit on the screen at the specified magnification, imshow scales the image to fit and issues a
warning message. We can also specify the text string 'fit' as the initial magnification value. In
Jorma Kekalainen
Image Processing
517
this case,
imshow scales the image to fitDigital
the current
size of the figure window.

Initial magnification

Jorma Kekalainen

Lecture weeks 9 and 10

Digital Image Processing

518

Page 10

Lecture notes

Digital Image Processing by Jorma Kekalainen

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

Digital Image Processing

519

Controlling the appearance of the


figure
By default, when imshow displays an image in a figure, it
surrounds the image with a gray border.
We can change this default and suppress the border using
the 'border' parameter, as shown in the following example
imshow('moon.tif', 'border','tight')

The 'border' parameters affect only the image being


displayed in the call to imshow.
If we want all the images that we display using imshow to
appear without the gray border, we set the Image
Processing Toolbox 'ImshowBorder' preference to 'tight'.
We can also use preferences to include visible axes in the
figure. For more information about preferences, see
iptprefs.
Jorma Kekalainen

Lecture weeks 9 and 10

Digital Image Processing

520

Page 11

Lecture notes

Digital Image Processing by Jorma Kekalainen

Displaying suppressed border

Jorma Kekalainen

Digital Image Processing

521

Displaying each image in a


separate figure
The simplest way to display multiple images is to display them in
separate figure windows.
Matlab does not place any restrictions on the number of images
you can display simultaneously.
imshow always displays an image in the current figure.
If we display two images in succession, the second image replaces
the first image.
To view multiple figures with imshow, use the figure command to
explicitly create a new empty figure before calling imshow for the
next image.
For example, to view the first three frames in an array of truecolor
images F,

F=imread('peppers.png');
imshow(F(:,:,1))
figure, imshow(F(:,:,2))
figure, imshow(F(:,:,3))

Jorma Kekalainen

Lecture weeks 9 and 10

Digital Image Processing

522

Page 12

Lecture notes

Digital Image Processing by Jorma Kekalainen

Each image displayed separately

Jorma Kekalainen

Digital Image Processing

523

Displaying multiple images in the


same figure
We can use the imshow function with the Matlab
subplot function or the Matlab subimage function to
display multiple images in a single figure window.
subplot divides a figure into multiple display regions.
The syntax of subplot is subplot(m,n,p).
This syntax divides the figure into an m-by-n matrix of
display regions and makes the pth display region active.

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.

Lecture weeks 9 and 10

Page 13

Lecture notes

Digital Image Processing by Jorma Kekalainen

Displaying multiple images in the


same figure
E.g., we can use this syntax to display two images side by side

[X1,map1]=imread('forest.tif');
[X2,map2]=imread('trees.tif');
subplot(1,2,1), imshow(X1,map1)
subplot(1,2,2), imshow(X2,map2)

subimage converts images to truecolor before displaying them and


therefore circumvents the colormap sharing problem.
This example uses subimage to display the forest and the trees
images with better results

[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

Digital Image Processing

525

Multiple images displayed in the same


figure

[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

Lecture weeks 9 and 10

Digital Image Processing

526

Page 14

Lecture notes

Digital Image Processing by Jorma Kekalainen

Note: Image sequence

Some applications work with collections of images related by time, such as


frames in a movie, or by spatial location, such as magnetic resonance
imaging (MRI) slices.
These collections of images are referred to as image sequences, image
stacks, or videos.
The ability to create N-dimensional arrays can provide a convenient way to
store image sequences.
E.g., an m-by-n-by-p array can store an array of p two-dimensional images,
such as grayscale or binary images, as shown in the following figure.
An m-by-n-by-3-by-p array can store truecolor images where each image is
made up of three planes.
Multidimensional array containing
an image sequence

Jorma Kekalainen

Digital Image Processing

527

Note: Image sequence


The imshow function can display one frame at a
time, using standard Matlab array indexing
syntax.
To animate an image sequence or provide
navigation within the sequence, use the Video
Viewer app (implay).
The Video Viewer app provides playback controls
that you can use to navigate among the frames in
the sequence.
To get a static view of all the frames in an image
sequence at one time, use the montage function.
Jorma Kekalainen

Lecture weeks 9 and 10

Digital Image Processing

528

Page 15

Lecture notes

Digital Image Processing by Jorma Kekalainen

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

Digital Image Processing

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

Lecture weeks 9 and 10

65536 logical

Digital Image Processing

530

Page 16

Lecture notes

Digital Image Processing by Jorma Kekalainen

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

Digital Image Processing

531

Example

If we take a grayscale image, we start by making it a matrix of type double;


this means we can perform arithmetic on the values. E.g.,
>> 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);
A corresponding example is seen in the following figures.

Jorma Kekalainen

Lecture weeks 9 and 10

Digital Image Processing

532

Page 17

Lecture notes

Digital Image Processing by Jorma Kekalainen

Example: The least significant bit plane


(= 0th bit plane)

c=imread('Hedyg-scale.tif');
cd=double(c);
c0=mod(cd,2);
imshow(c0)

Jorma Kekalainen

Digital Image Processing

533

Example: The 1st bit plane


c1=mod(floor(cd/2),2); figure, imshow(c1)

Jorma Kekalainen

Lecture weeks 9 and 10

Digital Image Processing

534

Page 18

Lecture notes

Digital Image Processing by Jorma Kekalainen

Example: The 2nd bit plane


c2=mod(floor(cd/4),2); figure, imshow(c2)

Jorma Kekalainen

Digital Image Processing

535

Example: The 3rd bit plane


c3=mod(floor(cd/8),2); figure, imshow(c3)

Jorma Kekalainen

Lecture weeks 9 and 10

Digital Image Processing

536

Page 19

Lecture notes

Digital Image Processing by Jorma Kekalainen

Example: The 4th bit plane


c4=mod(floor(cd/16),2); figure, imshow(c4)

Jorma Kekalainen

Digital Image Processing

537

Example: The 5th bit plane


c5=mod(floor(cd/32),2); figure, imshow(c5)

Jorma Kekalainen

Lecture weeks 9 and 10

Digital Image Processing

538

Page 20

Lecture notes

Digital Image Processing by Jorma Kekalainen

Example: The 6th bit plane


c6=mod(floor(cd/64),2); figure, imshow(c6)

Jorma Kekalainen

Digital Image Processing

539

Example: The most significant


(=7st) bit plane
c7=mod(floor(cd/128),2); figure, imshow(c7)

Jorma Kekalainen

Lecture weeks 9 and 10

Digital Image Processing

540

Page 21

Lecture notes

Digital Image Processing by Jorma Kekalainen

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

Digital Image Processing

541

Original image

Jorma Kekalainen

Digital Image Processing

542

Hedy Lamarr aka Hedwig Kiesler Hollywood star and inventor (Patent US 2292387 A)

Lecture weeks 9 and 10

Page 22

Lecture notes

Digital Image Processing by Jorma Kekalainen

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

Digital Image Processing

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

Lecture weeks 9 and 10

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

Digital Image Processing

544

Page 23

Lecture notes

Digital Image Processing by Jorma Kekalainen

All commands used in the


previous example

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))

Digital Image Processing

545

Exercise
Transform the image cameraman.tif into a
sequence of binary images by breaking them
up into their bit-planes.

Jorma Kekalainen

Lecture weeks 9 and 10

Digital Image Processing

546

Page 24

Lecture notes

Digital Image Processing by Jorma Kekalainen

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

Digital Image Processing

547

Least significant bit planes

Jorma Kekalainen

Lecture weeks 9 and 10

Digital Image Processing

548

Page 25

Lecture notes

Digital Image Processing by Jorma Kekalainen

Most significant bit planes

Jorma Kekalainen

Digital Image Processing

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

Lecture weeks 9 and 10

Digital Image Processing

550

Page 26

Lecture notes

Digital Image Processing by Jorma Kekalainen

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

Lecture weeks 9 and 10

Digital Image Processing

552

Page 27

Lecture notes

Digital Image Processing by Jorma Kekalainen

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

Digital Image Processing

553

Exercise

Using a matrix A=floor(255*rand(5)) study the effect of command series


A0=rem(A,2)
A1=rem(floor(A/2),2)
A2=rem(floor(A/4),2)
A3=rem(floor(A/8),2)
A4=rem(floor(A/16),2)
A5=rem(floor(A/32),2)
A6=rem(floor(A/64),2)
A7=rem(floor(A/128),2)
AA=2*(2*(2*(2*(2*(2*(2*A7+A6)+A5)+A4)+A3)+A2)+A1)+A0
Ath=A>127
Compare A with AA and A7 with Ath.

Jorma Kekalainen

Lecture weeks 9 and 10

Digital Image Processing

554

Page 28

Lecture notes

Digital Image Processing by Jorma Kekalainen

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

Digital Image Processing

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

Lecture weeks 9 and 10

>>
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

Digital Image Processing

556

Page 29

Lecture notes

Digital Image Processing by Jorma Kekalainen

Digital Image Processing


Spatial Resolution

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

Lecture weeks 9 and 10

Digital Image Processing

558

Page 30

Lecture notes

Digital Image Processing by Jorma Kekalainen

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

Digital Image Processing

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

Lecture weeks 9 and 10

Digital Image Processing

560

Page 31

Lecture notes

Digital Image Processing by Jorma Kekalainen

Example: Spatial resolution


>> A=floor(255*rand(6))
A=

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

Example: Spatial resolution


>> imresize(imresize(A,1/2, 'nearest'),2,'nearest')
ans =

240
240
146
146
59
59

Jorma Kekalainen

Lecture weeks 9 and 10

240 165 165 236 236


240 165 165 236 236
146 139 139 124 124
146 139 139 124 124
59 189 189 113 113
59 189 189 113 113
Digital Image Processing

562

Page 32

Lecture notes

Digital Image Processing by Jorma Kekalainen

Example: Spatial resolution


>> imresize(imresize(A,1/4, 'nearest'),4,'nearest')
ans =

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

Digital Image Processing

563

Example: Spatial resolution


>> imresize(imresize(A,1/8, 'nearest'),8,'nearest')
ans =

159
159
159
159
159
159
159
159

Jorma Kekalainen

Lecture weeks 9 and 10

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

Digital Image Processing

159
159
159
159
159
159
159
159

564

Page 33

Lecture notes

Digital Image Processing by Jorma Kekalainen

Example: Spatial resolution


>>
Aold=imresize_old(imresize_old(
A,1/2),2)

>>
Aold1=imresize_old(imresize_old(
A,1/4),4)

Aold =

Aold1 =

240
240
146
146
59
59

240 165 165 236 236


240 165 165 236 236
146 139 139 124 124
146 139 139 124 124
59 189 189 113 113
59 189 189 113 113

Jorma Kekalainen

3
3
3
3

3
3
3
3

3
3
3
3

3
3
3
3

Digital Image Processing

565

Note
About the same effect we can achieve
applying the command
imresize_old( imresize_old(x,1/2),2);

Jorma Kekalainen

Lecture weeks 9 and 10

Digital Image Processing

566

Page 34

Lecture notes

Digital Image Processing by Jorma Kekalainen

Effective resolution
By changing the parameters of imresize, we
can change the effective resolution of the
image to smaller amounts:

Jorma Kekalainen

Digital Image Processing

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

Lecture weeks 9 and 10

Digital Image Processing

568

Page 35

Lecture notes

Digital Image Processing by Jorma Kekalainen

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

Digital Image Processing

569

Example: Increasing resolution of


an image
x6=imresize(imresize(x,1/64, 'nearest'),64,'nearest');
figure,imshow(x6)

Jorma Kekalainen

Lecture weeks 9 and 10

Digital Image Processing

570

Page 36

Lecture notes

Digital Image Processing by Jorma Kekalainen

Example: Increasing resolution of


an image
x5=imresize(imresize(x,1/32, 'nearest'),32,'nearest');
figure,imshow(x5)

Jorma Kekalainen

Digital Image Processing

571

Example: Increasing resolution of


an image
x4=imresize(imresize(x,1/16, 'nearest'),16,'nearest');
figure,imshow(x4)

Jorma Kekalainen

Lecture weeks 9 and 10

Digital Image Processing

572

Page 37

Lecture notes

Digital Image Processing by Jorma Kekalainen

Example: Increasing resolution of


an image
x3=imresize(imresize(x,1/8 ,'nearest'),8,'nearest');
figure,imshow(x3)

Jorma Kekalainen

Digital Image Processing

573

Example: Increasing resolution of


an image
x2=imresize(imresize(x,1/4, 'nearest'),4,'nearest');
figure,imshow(x2)

Jorma Kekalainen

Lecture weeks 9 and 10

Digital Image Processing

574

Page 38

Lecture notes

Digital Image Processing by Jorma Kekalainen

Example: Increasing resolution of


an image
x1=imresize(imresize(x,1/2 ,'nearest'),2,'nearest');
figure,imshow(x1)

Jorma Kekalainen

Digital Image Processing

575

Example: Original image


figure,imshow(x)

Jorma Kekalainen

Lecture weeks 9 and 10

Digital Image Processing

Deepika Bollywood star

576

Page 39

Potrebbero piacerti anche