Академический Документы
Профессиональный Документы
Культура Документы
Page 1
Lecture notes
Matlab
Matlab is a data analysis and visualization tool which
has been designed with powerful support for matrices
and matrix operations.
Matlab has excellent graphics capabilities, and its own
powerful programming language.
One of the reasons that Matlab has become such an
important tool is through the use of sets of Matlab
programs designed to support a particular task.
These sets of programs are called toolboxes, and the
particular toolbox of interest to us is the image
processing toolbox.
Jorma Kekalainen
390
Images in Matlab
The basic data structure in Matlab is the array, an
ordered set of real or complex elements.
This object is naturally suited to the
representation of images, real-valued ordered
sets of color or intensity data.
Matlab stores most images as two-dimensional
arrays (i.e., matrices), in which each element of
the matrix corresponds to a single pixel in the
displayed image.
Pixel or picture element usually denotes a single
dot on a computer display.
Jorma Kekalainen
391
Page 2
Lecture notes
Example
For example, an image composed of 200 rows and 300
columns of different colored dots would be stored in
Matlab as a 200-by-300 matrix.
Some images, such as true color images, require a threedimensional array, where the first plane in the third
dimension represents the red pixel intensities, the second
plane represents the green pixel intensities, and the third
plane represents the blue pixel intensities.
This convention makes working with images in Matlab
similar to working with any other type of matrix data, and
makes the full power of Matlab available for image
processing applications.
Jorma Kekalainen
392
Example
Create the image of a ramp (256*256):
Jorma Kekalainen
393
Page 3
Lecture notes
Example
for i = 1 : 256
for j = 1 : 256
A(i, j) =j -1;
end
end
image(A)
colormap(gray(256));
axis image;
Jorma Kekalainen
394
Example
The image of a circle
(256*256) of radius 80
pixels centered at (128;
128):
Jorma Kekalainen
for i = 1 : 256
for j = 1 : 256
dist = ((i-128)^2 + (j-128)^2)^(.5);
if (dist < 80)
B(i,j) = 255;
else
B(i,j) = 0;
end
end
end
image(B);
colormap(gray(256));
axis image;
395
Page 4
Lecture notes
Example
The image of a graded
circle (256*256):
C(i,j) = A(i,j)*B(i,j)/255
Jorma Kekalainen
for i = 1 : 256
for j = 1 : 256
C(i,j) = A(i,j)*B(i,j)/255;
end
end
image(C);
colormap(gray(256));
axis image;
396
Example
Create the image of a ramp (256*256):
Jorma Kekalainen
397
Page 5
Lecture notes
Example
>> A=ones(256,1)*(0:255);
D=uint8(A);
figure,imshow(A)
figure,imshow(D)
whos
Name
Size
Bytes Class
A
D
Jorma Kekalainen
256x256
256x256
Attributes
524288 double
65536 uint8
398
399
Page 6
Lecture notes
Matrix
Matlab's standard data type is the matrix.
All data are considered to be matrices of some
sort.
Images, of course, are matrices whose elements
are the gray values (or possibly the RGB values) of
its pixels.
Single values are considered by Matlab to be
(1*1) matrices, while a string is merely a (1*n)
matrix of characters; n being the string's length.
Jorma Kekalainen
400
Grayscale images
Matrices can be handled very efficiently in
Matlab.
Images may be considered as matrices whose
elements are the pixel values of the image.
Suppose you will have a Matlab command
window open, and in it the Matlab prompt >>
ready to receive commands.
Type in the command
>> c=imread('coins.png');
Jorma Kekalainen
401
Page 7
Lecture notes
c=imread('coins.png');
This takes the gray values of all the pixels in
the grayscale image coins.png and puts them
all into a matrix c.
This matrix c is now a Matlab variable, and so
we can perform various matrix operations on
it.
In general the imread function reads the pixel
values from an image file, and returns a matrix
of all the pixel values.
Jorma Kekalainen
402
c=imread('coins.png');
A few notes about this command:
It ends in a semicolon; this has the effect of not
displaying the results of the command to the screen.
As the result of this particular command is a matrix of
size 246*300 = 73800 elements, so we don't want all
its values displayed.
The name coins.png is given in single quote marks.
Without them, Matlab would assume that coins.png
was the name of a variable, rather than the name of a
file.
Jorma Kekalainen
403
Page 8
Lecture notes
figure,imshow(c)
Now we can display this matrix as a grayscale image typing in
the command
>> figure, imshow(c)
This is really two commands on the one line.
Matlab allows many commands to be entered on the same
line; using commas to separate the different commands.
The two commands we are using here are:
figure, which creates a figure on the screen.
A figure is a window in which a graphics object can be placed.
Objects may include images, or various types of graphs.
imshow(c), which displays the matrix c as an image.
This is a display of the gray values of the pixels in the image.
Jorma Kekalainen
404
figure,imshow ('coins.png')
If there are no figures open,
then an imshow command,
or any other command
which generates a graphics
object, will open a new
figure for displaying the
object.
However, it is good practice
to use the figure command
whenever you wish to
create a new figure.
We could display this image
directly, without saving its
gray values to a matrix, with
the command
>>imshow('coins.png')
Jorma Kekalainen
Page 9
Lecture notes
406
Jorma Kekalainen
407
Page 10
Lecture notes
Jorma Kekalainen
408
Jorma Kekalainen
409
Page 11
Lecture notes
Command sequence
F=imread('flowers.tif');
class(F)
size(F)
figure
image(F)
title('flowers.tif')
xlabel('Introduction to Image Processing')
truesize
Jorma Kekalainen
410
411
Page 12
Lecture notes
Jorma Kekalainen
412
413
Page 13
Lecture notes
414
Example
>> a(100,200,2)
>> a(100,200,:)
ans =
25
ans(:,:,1) =
75
>> a(100,200,1:3)
ans(:,:,1) =
75
ans(:,:,2) =
25
ans(:,:,2) =
25
ans(:,:,3) =
ans(:,:,3) =
30
Jorma Kekalainen
30
415
Page 14
Lecture notes
impixel
A useful function for obtaining RGB values is impixel; the
command
>> impixel(a,200,100)
ans =
75 25 30
returns the red, green, and blue values of the pixel at
column 200, row 100.
This command also applies to grayscale images:
>> impixel(c,100,200)
ans =
175 175 175
will return three values, but since c is a single twodimensional matrix, all three values will be the same.
Jorma Kekalainen
416
Note
The order of indexing is opposite to the row,
column order for matrix indexing.
Jorma Kekalainen
>> a(100,200,:)
ans(:,:,1) =
75
ans(:,:,2) =
25
ans(:,:,3) =
30
>> impixel(a,200,100)
ans =
75 25 30
>> impixel(a,100,200)
ans =
161 161 155
Digital Image Processing
417
Page 15
Lecture notes
418
Jorma Kekalainen
419
Page 16
Lecture notes
Gray
B
Jorma Kekalainen
420
Colormaps
Sometimes, the pixels are quantified to the interval [0,255].
These values are transformed through a colormap in the
computer to
Grayscale values, i.e. 0 black and 255 white or
Arbitrary colors (pseudocolors)
421
Page 17
Lecture notes
To D/A-converter and
further to the red
Jorma Kekalainen
channel of the screen.
To D/A-converter and
further
to the green
Digital Image Processing
channel of the screen.
Over 16
million
colors
To D/A-converter and
further to the blue422
channel of the screen.
Jorma Kekalainen
423
Page 18
Lecture notes
Jorma Kekalainen
424
Jorma Kekalainen
425
Page 19
Lecture notes
Jorma Kekalainen
426
427
Page 20
Lecture notes
Example
Jorma Kekalainen
428
Indexed images
Note: A colormap is often stored with an indexed image and is automatically loaded with
the image when you use the imread function. However, you are not limited to using the
Jorma Kekalainen
429
default
colormapuse any colormapDigital
thatImage
youProcessing
choose.
Page 21
Lecture notes
The pixels in the image are represented by integers, which are pointers
(indices) to color values stored in the colormap.
The relationship between the values in the image matrix and the
colormap depends on the class of the image matrix. If the image
matrix is of class double, the value 1 points to the first row in the
colormap, the value 2 points to the second row, and so on. If the
image matrix is of class uint8 or uint16, there is an offsetthe
value 0 points to the first row in the colormap, the value 1 points
to the second row, and so on.
The offset is also used in
graphics file formats to
maximize the number of
colors that can be supported.
In the following image, the
image matrix is of class
double. Because there is no
offset, the value 5 points to
Jorma Kekalainen
the fifth
row of the colormap.
430
Intensity images
An intensity image is a data matrix, I, whose values
represent intensities within some range.
An intensity image is represented as a single matrix,
with each element of the matrix corresponding to one
image pixel.
The matrix can be of class double, uint8, or uint16.
While intensity images are rarely saved with a
colormap, a colormap is still used to display them.
In essence, intensity images are treated as indexed
images.
Jorma Kekalainen
431
Page 22
Lecture notes
Intensity images
To display an intensity image, we can
use the imagesc ("image scale")
function, which enables us to set the
range of intensity values.
imagesc scales the image data to use
the full colormap. Use the two-input
form of imagesc to display an intensity
image.
E.g.:
imagesc(I,clims); colormap(gray);
The second input argument
to imagesc specifies the desired
intensity range. The imagesc function
displays I by mapping the first value in
the range to the first colormap entry,
and the second value to the last
colormap entry. Values in between are
linearly distributed throughout the
remaining colormap colors
Jorma Kekalainen
432
Note
imagesc(C) displays C as an image.
Each element of C corresponds to a rectangular area in the
image.
The values of the elements of C are indices into the current
colormap that determine the color of each patch.
imagesc(...,clims) normalizes the values in C to the range
specified by clims and displays C as an image.
clims is a two-element vector that limits the range of data values in C.
Jorma Kekalainen
433
Page 23
Lecture notes
Example
We can expand midrange color
resolution by mapping low
values to the first color and
high values to the last color in
the colormap by specifying
color value limits (clims).
If the size of the current
colormap is 81-by-3, the
statements
clims = [ 10 60 ]
imagesc(C,clims)
map the data values in C to
the colormap as shown beside.
Jorma Kekalainen
434
Example
Jorma Kekalainen
435
Page 24
Lecture notes
Jorma Kekalainen
436
437
Page 25
Lecture notes
Note
Jorma Kekalainen
438
Example
Jorma Kekalainen
439
Page 26
Lecture notes
Example
Jorma Kekalainen
440
imfinfo('clown.bmp')
imfinfo('clown.bmp')
ans =
Filename: 'C:\Users\ktkekjo\Documents\MATLAB\clown.bmp'
FileModDate: '17-Aug-2015 20:31:42'
FileSize: 65078
Format: 'bmp'
FormatVersion: 'Version 3 (Microsoft Windows 3.x)'
Width: 320
Height: 200
BitDepth: 8
ColorType: 'indexed'
FormatSignature: 'BM'
NumColormapEntries: 256
Colormap: [256x3 double]
Jorma Kekalainen
441
Page 27
Lecture notes
442
Example
>> imshow('ancienttrees.tif')
Jorma Kekalainen
>> AT=imread('ancienttrees.tif');
>> figure,imshow(AT)
443
Page 28
Lecture notes
Example
>> whos('AT')
Name
Size
AT
258x350
Jorma Kekalainen
444
Example
load trees
>> T=ind2gray(X,map);
>> imshow(X,map)
>> figure,imshow(T)
Jorma Kekalainen
445
Page 29
Lecture notes
Note
load trees
T=ind2gray(X,map);
imshow(X,map)
figure,imshow(T)
figure,imagesc(X)
figure,imagesc(X),colormap(map)
Jorma Kekalainen
446
Imfinfo
>> imwrite(X,map,'ancienttrees.tif')
>> imshow('ancienttrees.tif')
>> imfinfo('ancienttrees.tif')
ans =
Filename:
'C:\Users\ktkekjo\Documents\MATLAB\ancienttrees
.tif'
FileModDate: '23-kes-2015 16:40:36'
FileSize: 75764
Format: 'tif'
FormatVersion: []
Width: 350
Height: 258
BitDepth: 8
ColorType: 'indexed'
FormatSignature: [73 73 42 0]
ByteOrder: 'little-endian'
NewSubFileType: 0
BitsPerSample: 8
Compression: 'PackBits'
Jorma Kekalainen
447
Page 30
Lecture notes
Note
AT=imread('ancienttrees.tif');
imshow(AT)
Jorma Kekalainen
[AT,cmap]=imread('ancienttrees.tif');
figure,image(AT)
colormap(cmap)
448
449
Page 31
Lecture notes
Size
258x350
256x3
Jorma Kekalainen
Bytes
90300
6144
Class Attributes
uint8
double
450
Jorma Kekalainen
>> round(255*emap(199:203,:))
237 123 176
63 60 99
239 95 149
218 208 207
107 80 88
451
Page 32
Lecture notes
0.9020
0.9294
0.2471
0.9373
0.8549
0.4196
0.8902
0.3098
0.6902
0.3882
0.5843
0.8118
0.3451
0.4549
Jorma Kekalainen
red
0.2235
0.4824
0.2353
0.3725
0.8157
0.3137
0.3137
256
ind(100,100,:)+1=201
emap(198:204,:)
Colormap
Number+1 at
pixel location is
an index into a
colormap.
green
blue
Intensity values
are between 0
and 1.
452
Example
Jorma Kekalainen
24-bit truecolor
8-bit colormapped
453
Page 33
Lecture notes
actual values in
map(112:116,:)
Index+1
0.8392
0.6471
0.8078
0.7098
0.9686
0.7098
0.8078
0.8078
0.8392
0.7098
0.8706
0.9373
0.6118
0.8706
0.8706
255*map(112:116,:)
214
165
206
181
247
Jorma Kekalainen
181
206
206
214
181
222
239
156
222
222
454
The m x n x 3 matrix of
intensity values is
reshaped into a m x n x
3 image of type double.
The values are scaled
to lie between 0 and
255 then converted to
Jorma Kekalainen
type uint8.
455
Page 34
Lecture notes
181
206
206
214
181
222
239
156
222
222
Jorma Kekalainen
456
Jorma Kekalainen
457
Page 35
Lecture notes
Jorma Kekalainen
458
Jorma Kekalainen
459
Page 36
Lecture notes
Jorma Kekalainen
460
Jorma Kekalainen
461
Page 37
Lecture notes
Jorma Kekalainen
462
Example
Jorma Kekalainen
463
Page 38
Lecture notes
464
Example: Scramble/unscramble
an image
D = imread('Dee.jpg');
r = randperm(333);
The image can be unscrambled
c = randperm(500);
using the row and column
Scr = D(r,c,:);
permutation vectors, r and c.
figure
image(Scr)
truesize
title('Scrambled Image')
xlabel('What is it?')
pause
Unscr(r,c,:)=Scr;
figure
image(Unscr)
truesize
title('Unscrambled Image')
Jorma Kekalainen
465
Page 39
Lecture notes
Example
he= imread('he.tif');
r = randperm(300);
c = randperm(480);
Scrambled = he(r,c,:);
figure
image(Scrambled)
truesize
title('Scrambled Image')
xlabel('What is this?')
pause
Unscrambled(r,c,:)=Scrambled;
figure
image(Unscrambled)
truesize
title('Unscrambled Image')
466
Scrambled/unscrambled
D = imread('Dee.jpg');
rng(2,'twister');
s=rng;
r = randperm(333);
c = randperm(500);
Scr = D(r,c,:);
figure
image(Scr)
truesize
title('Scrambled Image')
xlabel('What is it?')
imwrite(Scr,'Scrambled_Image.bmp',
'bmp');
Scrambled=imread('Scrambled_I
mage.bmp');
rng(2,'twister');
s=rng;
figure, imshow(Scrambled)
title('Scrambled image')
pause
r = randperm(333);
c = randperm(500);
Unscrambled(r,c,:)=Scrambled;
figure
image(Unscrambled)
truesize
title('Unscrambled Image')
Note: rng controls random number generation. Generator = 'twister' referred to the Mersenne
Jorma Kekalainen
Digital Image Processing
467
Twister
generator which is now Matlab startup
generator.
Page 40
Lecture notes
Example
Jorma Kekalainen
468
Note: rng
Jorma Kekalainen
469
Page 41
Lecture notes
Jorma Kekalainen
470
imfinfo('ancienttrees.tif')
>> imfinfo('ancienttrees.tif')
ans =
Filename: [1x50 char]
FileModDate: '23-kes-2015 16:40:36'
FileSize: 75764
Format: 'tif'
FormatVersion: []
Width: 350
Height: 258
BitDepth: 8
ColorType: 'indexed'
FormatSignature: [73 73 42 0]
ByteOrder: 'little-endian'
NewSubFileType: 0
BitsPerSample: 8
Compression: 'PackBits'
Jorma Kekalainen
471
Page 42
Lecture notes
472
>> imfinfo('flowers.tif')
>> imfinfo('flowers.tif')
ans =
Filename: [1x45 char]
FileModDate: '22-kes-2015 23:32:10'
FileSize: 547618
Format: 'tif'
FormatVersion: []
Width: 500
Height: 362
BitDepth: 24
ColorType: 'truecolor'
Jorma Kekalainen
473
Page 43
Lecture notes
>> imfinfo('text.png')
Now we shall test this function on a binary image:
>> imfinfo('text.png')
ans =
Filename: [1x62 char]
FileModDate: '13-Oct-2002 08:48:20'
FileSize: 1322
Format: 'png'
FormatVersion: []
Width: 256
Height: 256
BitDepth: 1
ColorType: 'grayscale
Jorma Kekalainen
474
Note
Matlab does not distinguish between grayscale and binary
images: a binary image is just a special case of a grayscale
image which has only two intensities.
>> whos
Name
Size
Te
256x256
Jorma Kekalainen
475
Page 44
Lecture notes
Jorma
Kekalainen
Digital
Processing
Note:
There
are other data types; see
theImage
help
for datatypes.
476
Example
>> a=23;
>> b=uint8(a);
>> b
b=
23
>> whos a b
Name Size Bytes
Class
a
1x1 8
double array
b
1x1 1
uint8 array
Note: Even though the variables a and b have the same numeric value, they are of
different data types. An important consideration is that arithmetic operations are not
Jorma Kekalainen
Digital Image Processing
477
permitted
with the data types int8, int16,
uint8 and uint16.
Page 45
Lecture notes
Example
>> whos a b
Name Size
a
b
1x1
1x1
Bytes Class
Attributes
8 double
1 uint8
>> a/2
ans =
11.5000
>> b/2
ans =
12
Jorma Kekalainen
478
Note
A grayscale image may consist of pixels whose values
are of data type uint8.
These images are thus reasonably efficient in terms
of storage space, since each pixel requires only one
byte.
Arithmetic operations are not permitted on this data
type; a uint8 image should be converted to double
before arithmetic is attempted.
However, most Image Processing Toolbox functions
accept uint8 and uint16 input.
Jorma Kekalainen
479
Page 46
Lecture notes
intArray = uint8(array)
converts the elements of an array into unsigned 8-bit (1-byte) integers of
class uint8.
Input argument array is array of any numeric class, such as single or
double.
If array is already of class uint8, the uint8 function has no effect.
Output argument intArray is array of class uint8.
Values range from 0 to 28 1.
The uint8 function maps any values in array that are outside the limit to
the nearest endpoint.
For example,
>> uint8(2^8)
ans =
255
Jorma Kekalainen
480
Use
Format
rgb2gray
RGB to grayscale
y=rgb2gray(x);
rgb2ind
RGB to indexed
[ind,cmap]=rgb2ind(x);
ind2rgb
Indexed to RGB
y=ind2rgb(ind,cmap);
gray2ind
Grayscale to indexed
[ind,cmap]=gray2ind(x);
ind2gray
Indexed to grayscale
y=ind2gray(ind,cmap);
Jorma Kekalainen
481
Page 47
Lecture notes
rgb2gray
362 500
Jorma Kekalainen
482
Note: 4 color
rgb2ind
0.1176
0.2627
0.5451
0.2588
0.8706 0.6941
0.8706 0.8431
Jorma Kekalainen
0.1412
0.2314
0.6392
0.4000
0.1490
0.8196
483
Page 48
Lecture notes
Example
Read and display a truecolor uint8 JPEG image of a nebula.
RGB = imread('ngc6543a.jpg');
figure('Name','RGB Image')
imagesc(RGB)
title('ngc6543a.jpg')
axis tight
zoom(5)
Jorma Kekalainen
484
Example
Convert RGB to an indexed image with 32 colors
[IND,map] = rgb2ind(RGB,32);
figure('Name','Indexed image')
imagesc(IND)
title('Indexed image with 32 Colors')
colormap(map)
axis tight
zoom(5)
Note: [X,map] = rgb2ind(RGB,n) converts the RGB image to an indexed image X using
minimum variance quantization. map contains at most n colors. n must be less than or
65,536.
The input image can be of class uint8, uint16, single, or double. If the length of map is
less than or equal to 256, the output image is of class uint8. Otherwise, the output image
is of class uint16.
Jorma Kekalainen
Digital Image Processing
485
The value 0 in the output array X corresponds to the first color in the colormap.
Page 49
Lecture notes
imfinfo('ngc6543a.jpg')
>> imfinfo('ngc6543a.jpg')
ans =
Filename: 'C:\Program
Files\MATLAB\R2013a\toolbox\matlab\demos\ngc6543a.jpg'
FileModDate: '01-Oct-1996 16:19:44'
FileSize: 27387
Format: 'jpg'
FormatVersion: ''
Width: 600
Height: 650
BitDepth: 24
ColorType: 'truecolor'
Jorma Kekalainen
486
ind2rgb
Jorma Kekalainen
487
Page 50
Lecture notes
gray2ind
Jorma Kekalainen
488
ind2gray
Jorma Kekalainen
489
Page 51
Lecture notes
Gray to RGB
z(:,:,1)=J;
z(:,:,2)=J;
z(:,:,3)=J;
figure, imshow(z)
title('Gray to RGB')
xlabel('Gray to RGB')
RGBsize=size(z)
Graysize=size(J)
RGBsize =
362 500
Graysize =
362 500
Note: There is not such a gray2rgb function, which create a color image. This
transform is done by simply replicating the gray values of each pixel: grays in an RGB
Jorma Kekalainen
Digital Image Processing
490
image
are obtained by equality of the
red, green and blue values.
Description:
[Y,newmap] = cmunique(X,map) returns the indexed image Y and associated
colormap, newmap, that produce the same image as (X,map) but with the
smallest possible colormap. The cmunique function removes duplicate rows
from the colormap and adjusts the indices in the image matrix accordingly.
[Y,newmap] = cmunique(RGB) converts the truecolor image RGB to the
indexed image Y and its associated colormap, newmap. The return value
newmap is the smallest possible colormap for the image, containing one entry
for each unique color in RGB.
[Y,newmap] = cmunique(I) converts the grayscale image I to an indexed image
Y and its associated colormap, newmap. The return value, newmap, is the
smallest possible colormap for the image, containing one entry for each
unique intensity level in I.
Jorma Kekalainen
491
Page 52
Lecture notes
Example
Jorma Kekalainen
492
Example
figure
image(X)
colormap(map)
title('X and map')
figure
image(Y)
colormap(newmap)
title('Y and newmap')
The input image can be of class uint8, uint16, or double. The class of the output image Y is uint8
if the length of newmap is less than or equal to 256. If the length of newmap is greater than
Kekalainen
Digital Image Processing
493
256, Jorma
Y is of
class double.
Page 53
Lecture notes
Example
X = magic(4);
map = [gray(8); gray(8)];
size(map)
[Y, newmap] = cmunique(X, map);
size(newmap)
figure
image(X)
colormap(map)
title('X and map')
figure
image(Y)
colormap(newmap)
title('Y and newmap')
Jorma Kekalainen
494
Exercise 1
Test and study all the previously presented
Matlab command sequences.
Jorma Kekalainen
495
Page 54
Lecture notes
Exercise 2
Type in the command
>> help imdemos
This will give you a list of, amongst other
things, all the sample PNG images which come
with the Image Processing Toolbox.
(a) Make a list of these sample images, and
(b) for each image determine its type (binary,
grayscale, true color or indexed color), and its
size (in pixels)
Jorma Kekalainen
496
Exercise 3
Pick a grayscale image, e.g., cameraman.tif.
Using the imwrite function,write it to files of
type JPEG, PNG and BMP.
What are the sizes of those files?
Jorma Kekalainen
497
Page 55
Lecture notes
Exercise 4
Jorma Kekalainen
498
Page 56