Digital
image processing using
Matlab
Color images have 3 values per
Digital Image pixel; monochrome images have 1
value per pixel.
a grid of squares,
each of which
contains a single
color
each square is
called a pixel (for
picture element)
Pixels
• A digital image, I, is a mapping from a
2D grid of uniformly spaced discrete
points, {p = (r,c)}, into a set of positive
integer values, {I( p)}, or a set of vector
values, e.g., {[R G B]T(p)}.
• At each column location in each row of I
there is a value.
• The pair ( p, I( p) ) is called a “pixel” (for
picture element).
3
Pixels
• p = (r,c) is the pixel location indexed by
row, r, and column, c.
• I( p) = I(r,c) is the value of the pixel at
location p.
• If I( p) is a single number then I is
monochrome.
• If I( p) is a vector (ordered list of numbers)
then I has multiple bands (e.g., a color
image).
4
Pixels
Pixel Location: p = (r , c)
Pixel Value: I(p) = I(r , c) Pixel : [ p, I(p)]
5
Pixels Pixel : [ p, I(p)]
p = (r , c ) ⎡ red ⎤ ⎡12 ⎤
= (row # , col # ) I ( p ) = ⎢ green ⎥ = ⎢ 43 ⎥
⎢ ⎥ ⎢ ⎥
= (272, 277 ) ⎢⎣ blue ⎥⎦ ⎢⎣ 61⎥⎦
6
Sampling and Quantization
real image sampled quantized sampled &
quantized
7
Sampling and Quantization
pixel grid column index
row index
real image sampled quantized sampled &
quantized
8
Sampling Take the average
within each square.
I C (ρ , χ ) I S ( r, c )
continuous image sampled image
9
Sampling Take the average
within each square.
I C (ρ , χ ) I S ( r, c )
continuous image sampled image
10
Sampling Take the average
within each square.
I C (ρ , χ ) I S ( r, c )
continuous image sampled image
11
Sampling Take the average
within each square.
I C (ρ , χ ) I S ( r, c )
continuous image sampled image
12
Read a Truecolor Image into Matlab
13
Read a Truecolor Image into Matlab
14
Read a Truecolor Image into Matlab
15
Read a Truecolor Image into Matlab
http://boingboing.net/
David Pescovitz Cory Doctorow
Mark Frauenfelder John Battelle Xeni Jardin
16
Crop the Image
left click here and hold
First, select a
region using
the magnifier.
Cut out a region
from the image drag to here and release
17
From this close-up
we can estimate
Crop the Image
the coordinates of
the region:
rows: about 125 to 425
cols: about 700 to 1050
18
Crop the Image
Here it is:
Now close the
other image
19
Crop the Image
Bring it to the
front using the
figure command,
20
Crop the Image
then type ‘close’
at the prompt.
21
Read a Colormapped Image into Matlab
22
Read a Colormapped Image into Matlab
23
Colormapped vs. Truecolor in Matlab
24
Colormapped vs. Truecolor in Matlab
25
Colormapped vs. Truecolor in Matlab
T(231,326,:)
Intensity values
are integers 227
between 0 and 255.
col: 326
222
96
image class: uint8
image type: truecolor
row: 231
26
Colormapped vs. Truecolor in Matlab
T(231,326,:)
Intensity values
are numbers 0.89
between 0 and 1.
col: 326
0.87
0.38
image class: double
image type: truecolor
row: 231
27
Number at pixel Intensity values
location is an index are integers
into a colormap. 1 colormap between 0 and 1.
…
Colormapped vs. Truecolor in Matlab
I(231,326,:) = 214
0.1804
0.6863
0.8863
0.1882
0.7098
0.9059
0.0627
0.2902
0.2549
× 255 = [226 231 65]T
…
…
256 red green blue 226
col: 326
231
65
row: 231
image class: uint8
image type: colormapped
28
Example truecolor and colormapped images
24-bit truecolor 8-bit colormapped to 24 bits
29
Example truecolor and colormapped images
24-bit truecolor 8-bit colormapped to 24 bits
30
Colormapped Image, Indices, & Color Map
>> [M,CMAP] = imread(‘button_mapped.bmp’,’bmp);
Indices contained in M(254:258,254:258) actual values in CMAP(109:113,:)
… 111 121 48 111 48 … R G B
… 110 48 111 110 111 … 109 0.6588 0.4706 0.8471
… 110 111 121 48 121 … 110 0.2196 0.1569 0.2824
… 121 48 110 111 48 … 111 0.4706 0.3451 0.5961
… 110 121 48 121 110 … 112 0.5333 0.4078 0.6588
113 0.2824 0.2196 0.3451
255*CMAP(109:113,:)
R G B
Last
109 168 120 216 3
110 56 40 72 cols.
111 120 88 152 only
112 136 104 168
113 72 56 88
31
How to convert a colormapped image to true color
M is a 512x512x1, 8-bit image. cmap is the colormap that is stored in ‘button_mapped.bmp’
It has 262,144 pixels. along with image. cmap is a 256x3 type-double matrix, each
Each pixel has a value between row of which lists a color in terms of its R, G, & B intensities,
0 & 255. which are given as fractions between 0 and 1.
>> [M,cmap] = imread(‘button_mapped.bmp’,’bmp);
>> T = uint8(reshape(cmap(M+1,:),[size(M) 3])*255);
The 262,144 x 3 matrix of By concatenating M’s columns, Matlab
intensity values is reshaped rearranges M into a 262,144 x 1 list. Each
into a 512x512x3 image of number in the list (if it has 1 added to it)
type double. The values are refers to a row of the colormap. Then,
scaled to lie between 0 & 255 cmap(M+1,:) produces a 262,144 x 3 matrix of
then converted to type uint8. intensity values of type double between 0 & 1.
32
How to Make Colormaps
This code, 0:255 ,
generates a 1 row by
>> ramp = (0:255)'/255; 0 256 element vector
>> kcm = [ramp ramp ramp]; gray colormap: 0.0039 of class double that
R(k)=G(k)=B(k) contains numbers 0
>> 0.0078
>> 256 × 3
>> matrix
0.0118 through 255 inclusive.
0.0157
>> rcm = [ramp zeros(256,2)]; red colormap:
…
>>
G = B = 0;
>>
>> 0.9843
>> gcm = [zeros(256,1) ramp zeros(256,1)];
0.9882
>>
>> green colormap: 0.9922
>> R = B = 0; 0.9961
ramp 1.0000
>> rcm = [zeros(256,2) ]; This, (0:255)’ , has
>>
blue colormap: the same contents
and class but is a 256
>>
>> % apply one by selecting the figure R = G = 0;
>> % then entering: row by 1 column
>> vector. The
>> colormap(kcm) apostrophe (‘) is the
matrix transpose
operator.
33
R, G, & B bands of a
truecolor image displayed
with grayscale colormaps
>> I = imread('blue_grapes_sm.jpg','jpg'); >> Rd = I(:,:,1);
>> colormap(kcm);
>> Gn = I(:,:,2); >> Bl = I(:,:,3);
>> colormap(kcm); >> colormap(kcm);
34
R, G, & B bands of a
truecolor image displayed
with grayscale colormaps
>> I = imread('blue_grapes_sm.jpg','jpg'); >> Rd = I(:,:,1);
>> colormap(kcm);
>> Gn = I(:,:,2); >> Bl = I(:,:,3);
>> colormap(kcm); >> colormap(kcm);
G B
35
R, G, & B bands of a
truecolor image displayed
with tinted colormaps
>> I = imread('blue_grapes_sm.jpg','jpg'); >> Rd = I(:,:,1);
>> colormap(rcm);
>> Gn = I(:,:,2); >> Bl = I(:,:,3);
>> colormap(gcm); >> colormap(bcm);
36
R, G, & B bands of a
truecolor image displayed
with tinted colormaps
>> I = imread('blue_grapes_sm.jpg','jpg'); >> Rd = I(:,:,1);
>> colormap(rcm);
>> Gn = I(:,:,2); >> Bl = I(:,:,3);
>> colormap(gcm); >> colormap(bcm);
G B
37
R, G, & B bands of a
truecolor image displayed
with grayscale colormaps
>> I = imread('blue_grapes_sm.jpg','jpg'); >> Rd = I(:,:,1);
>> colormap(kcm);
>> Gn = I(:,:,2); >> Bl = I(:,:,3);
>> colormap(kcm); >> colormap(kcm);
G B
38
Assuming that
Saving Images as Files ‘I’ contains the image of
the correct class,
that
‘cmap’ is a colormap,
>> and that
‘image_name’ is the
>> % truecolor as .bmp
file-name that you
>> imwrite(I,’image_name.bmp’,’bmp’); want.
>>
>> % truecolor as .jpg (default quality = 75)
>> imwrite(I,’image_name.jpg’,’jpg’);
>>
>> % truecolor as .jpg (quality = 100)
>> imwrite(I,’image_name.jpg’,’jpg’,’Quality’,100);
>>
>> % colormapped as .bmp
>> imwrite(I,cmap,’image_name.bmp’,’bmp’);
>>
>> % colormapped as .gif
>> imwrite(I,cmap,’image_name.gif’,’gif’);
>>
39
Pixel Indexing in Matlab “IP_Function” is
some arbitrary
image processing
“For” loops in Matlab are inefficient, whereas Matlab’s function that you or
native indexing procedures are very fast. someone else has
written.
Rather than
for r = 1:R
for c = 1:C
J(r,c,:) = IP_Function(I(r,c,:));
end
end
use, if possible
J = IP_Function(I);
But, sometimes that is not possible.
For example, if the output, J, is decimated with respect to the input, I,
the above will not work (unless, of course, it is done within IP_function).
40
r = [1 4 7 10 13 16 19 22 25 28 31]
Pixel Indexing in Matlab Here,
n=3
r = 1:n:R;
I(r,:,:)
c = [1 4 7 10 13 16 19 22 25 28 31]
c = 1:n:C;
I(:,c,:)
To decimate the above image by a factor of n,
create a vector, r, that contains the index of
every nth row, and a similar vector, c.
41
Pixel Indexing in Matlab
Here,
n=3
Take the
pixels
indexed
by both
This is called, r and c.
‘vectorizing’.
I(r,c)
Then, vectors r and c used as index
arguments for image I select every
nth column in every nth row.
42
Pixel Indexing in Matlab
Here,
n=3
J = I(r,c,:);
image, I
r = 1:n:R; c = 1:n:C;
43
Pixel Indexing in Matlab
Indexing in Matlab is fully general.
If I is R x C x B, vectors r and c
can contain any numbers 1 ≤ rk ≤ R
and 1 ≤ ck ≤ C.
The numbers can be in any order
and can be repeated within r and c.
The result of I(r,c) is an ordinal
shuffling of the pixels from I as
indexed by r and c.
Whenever possible, avoid
using ‘for’ loops;
vectorize instead.
44
Pixel Indexing in Matlab
Indexing in Matlab is fully general.
If I is R x C x B, vectors r and c
can contain any numbers 1 ≤ rk ≤ R
and 1 ≤ ck ≤ C.
The numbers can be in any order
and can be repeated within r and c.
The result of I(r,c) is an ordinal
shuffling of the pixels from I as
indexed by r and c.
Whenever possible, avoid
using ‘for’ loops;
vectorize instead.
45
Pixel Indexing in Matlab
Indexing in Matlab is fully general.
If I is R x C x B, vectors r and c
can contain any numbers 1 ≤ rk ≤ R
and 1 ≤ ck ≤ C.
The numbers can be in any order
and can be repeated within r and c.
The result of I(r,c) is an ordinal
shuffling of the pixels from I as
indexed by r and c.
Whenever possible, avoid
using ‘for’ loops;
vectorize instead.
46
Pixel Indexing in Matlab
Indexing in Matlab is fully general.
If I is R x C x B, vectors r and c
can contain any numbers 1 ≤ rk ≤ R
and 1 ≤ ck ≤ C.
The numbers can be in any order
and can be repeated within r and c.
The result of I(r,c) is an ordinal
shuffling of the pixels from I as
indexed by r and c.
Whenever possible, avoid
using ‘for’ loops;
vectorize instead.
47
Pixel Indexing in Matlab
Indexing in Matlab is fully general.
If I is R x C x B, vectors r and c
can contain any numbers 1 ≤ rk ≤ R
and 1 ≤ ck ≤ C.
The numbers can be in any order
and can be repeated within r and c.
The result of I(r,c) is an ordinal
shuffling of the pixels from I as
indexed by r and c.
Whenever possible, avoid
using ‘for’ loops;
vectorize instead.
48