You are on page 1of 59

EECE/CS 253 Image Processing

Lecture Notes: Digital Images and Matlab


Richard Alan Peters II
Department of Electrical Engineering and
Computer Science
Fall Semester 2007

This work is licensed under the Creative Commons Attribution-Noncommercial 2.5 License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc/2.5/ or
send a letter to Creative Commons, 543 Howard Street, 5th Floor, San Francisco, California, 94105, USA.

Digital Image

Color images have 3 values per


pixel; monochrome images have 1
value per pixel.

gridof
ofsquares,
squares,
aagrid
eachof
ofwhich
which
each
containsaasingle
single
contains
color
color
eachsquare
squareisis
each
calledaapixel
pixel(for
(for
called
pictureelement)
element)
picture

March 6, 2015

1999-2007 by Richard Alan Peters II

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

March 6, 2015

1999-2007 by Richard Alan Peters II

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

March 6, 2015

1999-2007 by Richard Alan Peters II

Pixels

Pixel Location: p = (r , c)
Pixel Value: I(p) = I(r , c)
March 6, 2015

1999-2007 by Richard Alan Peters II

Pixel : [ p, I(p)]
5

Pixels

March 6, 2015

Pixel : [ p, I(p)]

1999-2007 by Richard Alan Peters II

Sampling and Quantization

real image

March 6, 2015

sampled

quantized

1999-2007 by Richard Alan Peters II

sampled &
quantized

Sampling and Quantization


column index
column index

row index
row index

pixel grid

real image

March 6, 2015

sampled

quantized

1999-2007 by Richard Alan Peters II

sampled &
quantized

Take
Takethe
theaverage
average
within
each
within eachsquare.
square.

Sampling

IC ,

I S ( r, c )

continuous image

sampled image

March 6, 2015

1999-2007 by Richard Alan Peters II

Take
Takethe
theaverage
average
within
each
within eachsquare.
square.

Sampling

IC ,

I S ( r, c )

continuous image

sampled image

March 6, 2015

1999-2007 by Richard Alan Peters II

10

Take
Takethe
theaverage
average
within
each
within eachsquare.
square.

Sampling

IC ,

I S ( r, c )

continuous image

sampled image

March 6, 2015

1999-2007 by Richard Alan Peters II

11

Take
Takethe
theaverage
average
within
each
within eachsquare.
square.

Sampling

IC ,

I S ( r, c )

continuous image

sampled image

March 6, 2015

1999-2007 by Richard Alan Peters II

12

Read a Truecolor Image into Matlab

March 6, 2015

1999-2007 by Richard Alan Peters II

13

Read a Truecolor Image into Matlab

March 6, 2015

1999-2007 by Richard Alan Peters II

14

Read a Truecolor Image into Matlab

March 6, 2015

1999-2007 by Richard Alan Peters II

15

Read a Truecolor Image into Matlab

http://boingboing.net/
http://boingboing.net/

David Pescovitz Cory Doctorow


Mark Frauenfelder
John Battelle
Xeni Jardin

March 6, 2015

1999-2007 by Richard Alan Peters II

16

Crop the Image


First,
First,select
selectaa
region
regionusing
using
the
magnifier.
the magnifier.

left
leftclick
clickhere
hereand
andhold
hold

Cut out a region


from the image

March 6, 2015

1999-2007 by Richard Alan Peters II

drag
dragto
tohere
hereand
andrelease
release

17

From
Fromthis
thisclose-up
close-up
we
can
estimate
we can estimate
the
thecoordinates
coordinates of
of
the
theregion:
region:

Crop the Image

rows:
rows:about
about125
125to
to425
425
cols:
about
700
to
1050
cols: about 700 to 1050

March 6, 2015

1999-2007 by Richard Alan Peters II

18

Crop the Image

Here
Hereititis:
is:

Now
Nowclose
closethe
the
other
otherimage
image

March 6, 2015

1999-2007 by Richard Alan Peters II

19

Crop the Image

Bring
Bringititto
tothe
the
front
using
the
front using the
figure
figurecommand,
command,

March 6, 2015

1999-2007 by Richard Alan Peters II

20

Crop the Image

then
thentype
typeclose
close
at
atthe
theprompt.
prompt.

March 6, 2015

1999-2007 by Richard Alan Peters II

21

Read a Colormapped Image into Matlab

March 6, 2015

1999-2007 by Richard Alan Peters II

22

Read a Colormapped Image into Matlab

March 6, 2015

1999-2007 by Richard Alan Peters II

23

Colormapped vs. Truecolor in Matlab

March 6, 2015

1999-2007 by Richard Alan Peters II

24

Colormapped vs. Truecolor in Matlab

March 6, 2015

1999-2007 by Richard Alan Peters II

25

Colormapped vs. Truecolor in Matlab


T(231,326,:)
col: 326

Intensityvalues
values
Intensity
areintegers
integers
are
between00and
and255.
255.
between

227
222
96

imageclass:
class:uint8
uint8
image
imagetype:
type:truecolor
truecolor
image
row: 231

March 6, 2015

1999-2007 by Richard Alan Peters II

26

Colormapped vs. Truecolor in Matlab


T(231,326,:)
col: 326

Intensityvalues
values
Intensity
arenumbers
numbers
are
between00and
and1.1.
between

0.89
0.87
0.38

imageclass:
class:double
double
image
imagetype:
type:truecolor
truecolor
image
row: 231

March 6, 2015

1999-2007 by Richard Alan Peters II

27

Numberat
atpixel
pixel
Number
locationisisan
anindex
index
location
intoaacolormap.
colormap.
into

Intensityvalues
values
Intensity
areintegers
integers
are
between00and
and1.1.
between

colormap

0.1804
0.6863
0.8863

0.1882
0.7098
0.9059

0.0627
0.2902
0.2549

red

green

blue

Colormapped vs. Truecolor in Matlab


I(231,326,:) = 214

col: 326

256

255 = [226 231 65]T

226
231
65

row: 231

imageclass:
class:uint8
uint8
image
imagetype:
type:colormapped
colormapped
image

March 6, 2015

1999-2007 by Richard Alan Peters II

28

Example truecolor and colormapped images

24-bit truecolor

March 6, 2015

8-bit colormapped to 24 bits

1999-2007 by Richard Alan Peters II

29

Example truecolor and colormapped images

24-bit truecolor

March 6, 2015

8-bit colormapped to 24 bits

1999-2007 by Richard Alan Peters II

30

Colormapped Image, Indices, & Color Map


>> [M,CMAP] = imread(button_mapped.bmp,bmp);
Indices contained
contained in
in M(254:258,254:258)
M(254:258,254:258)
Indices

111 121
48 111
48
110
48 111 110 111
110 111 121
48 121
121
48 110 111
48
110 121
48 121 110

actual values
values in
in CMAP(109:113,:)
CMAP(109:113,:)
actual

R
G
B

109
110
111
112
113

0.6588
0.2196
0.4706
0.5333
0.2824

0.4706
0.1569
0.3451
0.4078
0.2196

0.8471
0.2824
0.5961
0.6588
0.3451

255*CMAP(109:113,:)
255*CMAP(109:113,:)

R
G
B

109
110
111
112
113

168
56
120
136
72

120
40
88
104
56

216
72
152
168

Last
Last
33
cols.
cols.
only
only

88
March 6, 2015

1999-2007 by Richard Alan Peters II

31

How to convert a colormapped image to true color


M is a 512x512x1, 8-bit image.
It has 262,144 pixels.
Each pixel has a value between
0 & 255.

cmap is the colormap that is stored in button_mapped.bmp


along with image. cmap is a 256x3 type-double matrix, each
row of which lists a color in terms of its R, G, & B intensities,
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


intensity values is reshaped
into a 512x512x3 image of
type double. The values are
scaled to lie between 0 & 255
then converted to type uint8.
March 6, 2015

By concatenating Ms columns, Matlab


rearranges M into a 262,144 x 1 list. Each
number in the list (if it has 1 added to it)
refers to a row of the colormap. Then,
cmap(M+1,:) produces a 262,144 x 3 matrix of
intensity values of type double between 0 & 1.
1999-2007 by Richard Alan Peters II

32

How to Make Colormaps


ramp = (0:255)'/255;
kcm = [ramp ramp ramp];

graycolormap:
colormap:
gray
R(k)=G(k)=B(k)
R(k)=G(k)=B(k)

25633matrix
matrix
256

redcolormap:
colormap:
red
GG==BB==0;0;

rcm = [ramp zeros(256,2)];

gcm = [zeros(256,1) ramp zeros(256,1)];

greencolormap:
colormap:
green
RR==BB==0;0;

rcm = [zeros(256,2) ramp ];


% apply one by selecting the figure
% then entering:

bluecolormap:
colormap:
blue
RR==GG==0;0;

colormap(kcm)

March 6, 2015

1999-2007 by Richard Alan Peters II

0
0.0039
0.0078
0.0118
0.0157

>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>

Thiscode,
code, 0:255
0:255, ,
This
generatesaa11row
rowby
by
generates
256element
elementvector
vector
256
ofclass
classdouble
doublethat
that
of
containsnumbers
numbers00
contains
through255
255inclusive.
inclusive.
through

0.9843
0.9882
0.9922
0.9961
1.0000

This, (0:255)
(0:255), ,has
has
This,
thesame
samecontents
contents
the
andclass
classbut
butisisaa256
256
and
rowby
by11column
column
row
vector. The
The
vector.
apostrophe()()isisthe
the
apostrophe
matrixtranspose
transpose
matrix
operator.
operator.

33

R,
R,G,
G,&&BBbands
bandsofofaa
truecolor
truecolorimage
imagedisplayed
displayed
with
grayscale
colormaps
with grayscale colormaps

>> I = imread('blue_grapes_sm.jpg','jpg');
>> I = imread('blue_grapes_sm.jpg','jpg');

>>
>> Rd
Rd == I(:,:,1);
I(:,:,1);
>>
colormap(kcm);
>> colormap(kcm);

>>
>> Gn
Gn == I(:,:,2);
I(:,:,2);
>>
>> colormap(kcm);
colormap(kcm);

>>
>> Bl
Bl == I(:,:,3);
I(:,:,3);
>>
>> colormap(kcm);
colormap(kcm);

March 6, 2015

1999-2007 by Richard Alan Peters II

34

R,
R,G,
G,&&BBbands
bandsofofaa
truecolor
truecolorimage
imagedisplayed
displayed
with
grayscale
colormaps
with grayscale colormaps

>> I = imread('blue_grapes_sm.jpg','jpg');
>> I = imread('blue_grapes_sm.jpg','jpg');

>>
>> Rd
Rd == I(:,:,1);
I(:,:,1);
>>
colormap(kcm);
>> colormap(kcm);
R

>>
>> Gn
Gn == I(:,:,2);
I(:,:,2);
>>
>> colormap(kcm);
colormap(kcm);
G

March 6, 2015

>>
>> Bl
Bl == I(:,:,3);
I(:,:,3);
>>
>> colormap(kcm);
colormap(kcm);
B

1999-2007 by Richard Alan Peters II

35

R,
R,G,
G,&&BBbands
bandsofofaa
truecolor
truecolorimage
imagedisplayed
displayed
with
tinted
colormaps
with tinted colormaps

>> I = imread('blue_grapes_sm.jpg','jpg');
>> I = imread('blue_grapes_sm.jpg','jpg');

>>
>> Rd
Rd == I(:,:,1);
I(:,:,1);
>>
colormap(rcm);
>> colormap(rcm);

>>
>> Gn
Gn == I(:,:,2);
I(:,:,2);
>>
>> colormap(gcm);
colormap(gcm);

>>
>> Bl
Bl == I(:,:,3);
I(:,:,3);
>>
>> colormap(bcm);
colormap(bcm);

March 6, 2015

1999-2007 by Richard Alan Peters II

36

R,
R,G,
G,&&BBbands
bandsofofaa
truecolor
truecolorimage
imagedisplayed
displayed
with
tinted
colormaps
with tinted colormaps

>> I = imread('blue_grapes_sm.jpg','jpg');
>> I = imread('blue_grapes_sm.jpg','jpg');

>>
>> Rd
Rd == I(:,:,1);
I(:,:,1);
>>
colormap(rcm);
>> colormap(rcm);
R

>>
>> Gn
Gn == I(:,:,2);
I(:,:,2);
>>
>> colormap(gcm);
colormap(gcm);
G

March 6, 2015

>>
>> Bl
Bl == I(:,:,3);
I(:,:,3);
>>
>> colormap(bcm);
colormap(bcm);
B

1999-2007 by Richard Alan Peters II

37

R,
R,G,
G,&&BBbands
bandsofofaa
truecolor
truecolorimage
imagedisplayed
displayed
with
grayscale
colormaps
with grayscale colormaps

>> I = imread('blue_grapes_sm.jpg','jpg');
>> I = imread('blue_grapes_sm.jpg','jpg');

>>
>> Rd
Rd == I(:,:,1);
I(:,:,1);
>>
colormap(kcm);
>> colormap(kcm);
R

>>
>> Gn
Gn == I(:,:,2);
I(:,:,2);
>>
>> colormap(kcm);
colormap(kcm);
G

March 6, 2015

>>
>> Bl
Bl == I(:,:,3);
I(:,:,3);
>>
>> colormap(kcm);
colormap(kcm);
B

1999-2007 by Richard Alan Peters II

38

Saving Images as Files


>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>

% truecolor as .bmp
imwrite(I,image_name.bmp,bmp);
% 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);

March 6, 2015

1999-2007 by Richard Alan Peters II

Assuming
Assumingthat
that
IIcontains
containsthe
theimage
image
of
the
correct
class,
of the correct class,
that
that
cmap
cmapisisaacolormap,
colormap,
and
that
and that
image_name
image_nameisisthe
the
file-name
that
you
file-name that you
want.
want.

39

Double Exposure:
Adding Two Images

Jim Woodring - Bumperillo

Rayden Woodring The Ecstasy of Bumperillo (?)

Mark Rayden The Ecstasy of Cecelia

March 6, 2015

1999-2007 by Richard Alan Peters II

40

Double Exposure: Adding Two Images


>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>

cd 'D:\Classes\EECE253\Fall 2006\Graphics\matlab intro'


Example
Example
JW = imread('Jim Woodring - Bumperillo.jpg','jpg');
MatlabCode
Code
Matlab
figure
image(JW)
truesize
title('Bumperillo')
xlabel('Jim Woodring')
MR = imread('Mark Ryden - The Ecstasy of Cecelia.jpg','jpg');
figure
image(MR)
truesize
title('The Ecstasy of Cecelia')
xlabel('Mark Ryden')
[RMR,CMR,DMR] = size(MR);
[RJW,CJW,DJW] = size(JW);
rb = round((RJW-RMR)/2);
cb = round((CJW-CMR)/2);
JWplusMR = uint8((double(JW(rb:(rb+RMR-1),cb:(cb+CMR-1),:))+double(MR))/2);
figure
image(JWplusMR)
truesize
title('The Ecstasy of Bumperillo')
xlabel('Jim Woodring + Mark Ryden')

March 6, 2015

1999-2007 by Richard Alan Peters II

41

Double Exposure: Adding Two Images


>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>

cd 'D:\Classes\EECE253\Fall 2006\Graphics\matlab intro'


Example
Example
JW = imread('Jim Woodring - Bumperillo.jpg','jpg');
MatlabCode
Code
Matlab
figure
image(JW)
truesize
title('Bumperillo')
xlabel('Jim Woodring')
MR = imread('Mark Ryden - The Ecstasy of Cecelia.jpg','jpg');
figure
image(MR)
truesize
title('The Ecstasy of Cecelia')
xlabel('Mark Ryden')
Cutaasection
sectionout
outof
ofthe
themiddle
middleof
ofthe
thelarger
larger
Cut
[RMR,CMR,DMR] = size(MR);
imagethe
thesame
samesize
sizeas
asthe
thesmaller
smallerimage.
image.
image
[RJW,CJW,DJW] = size(JW);
rb = round((RJW-RMR)/2);
cb = round((CJW-CMR)/2);
JWplusMR = uint8((double(JW(rb:(rb+RMR-1),cb:(cb+CMR-1),:))+double(MR))/2);
figure
image(JWplusMR)
truesize
title('The Ecstasy of Bumperillo')
xlabel('Jim Woodring + Mark Ryden')

March 6, 2015

1999-2007 by Richard Alan Peters II

42

Double Exposure: Adding Two Images


>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>

cd 'D:\Classes\EECE253\Fall 2006\Graphics\matlab intro'


Example
Example
JW = imread('Jim Woodring - Bumperillo.jpg','jpg');
MatlabCode
Code
Matlab
figure
image(JW)
truesize
title('Bumperillo')
xlabel('Jim Woodring')
MR = imread('Mark Ryden - The Ecstasy of Cecelia.jpg','jpg');
figure
image(MR)
truesize
title('The Ecstasy of Cecelia')
xlabel('Mark Ryden')
Notethat
thatthe
theimages
imagesare
areaveraged,
averaged,
Note
[RMR,CMR,DMR] = size(MR);
pixelwise.
pixelwise.
[RJW,CJW,DJW] = size(JW);
rb = round((RJW-RMR)/2);
cb = round((CJW-CMR)/2);
JWplusMR = uint8((double(JW(rb:(rb+RMR-1),cb:(cb+CMR-1),:))+double(MR))/2);
figure
image(JWplusMR)
truesize
title('The Ecstasy of Bumperillo')
xlabel('Jim Woodring + Mark Ryden')

March 6, 2015

1999-2007 by Richard Alan Peters II

43

Double Exposure: Adding Two Images


>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>

cd 'D:\Classes\EECE253\Fall 2006\Graphics\matlab intro'


Example
Example
JW = imread('Jim Woodring - Bumperillo.jpg','jpg');
MatlabCode
Code
Matlab
figure
image(JW)
truesize
title('Bumperillo')
xlabel('Jim Woodring')
MR = imread('Mark Ryden - The Ecstasy of Cecelia.jpg','jpg');
figure
image(MR)
truesize
title('The Ecstasy of Cecelia')
xlabel('Mark Ryden')
[RMR,CMR,DMR] = size(MR);
[RJW,CJW,DJW] = size(JW);
rb = round((RJW-RMR)/2);
cb = round((CJW-CMR)/2);
JWplusMR = uint8((double(JW(rb:(rb+RMR-1),cb:(cb+CMR-1),:))+double(MR))/2);
figure
Notethe
thedata
dataclass
class
image(JWplusMR)
Note
conversions.
truesize
conversions.
title('The Ecstasy of Bumperillo')
xlabel('Jim Woodring + Mark Ryden')

March 6, 2015

1999-2007 by Richard Alan Peters II

44

Intensity Masking:
Multiplying Two Images

Jim Woodring - Bumperillo

Rayden Woodring Bumperillo Ecstasy (?)

Mark Rayden The Ecstasy of Cecelia

March 6, 2015

1999-2007 by Richard Alan Peters II

45

Intensity Masking: Multiplying Two Images


>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>

JW = imread('Jim Woodring - Bumperillo.jpg','jpg');


Example
Example
MR = imread('Mark Ryden - The Ecstasy of Cecelia.jpg','jpg');
MatlabCode
Code
Matlab
[RMR,CMR,DMR] = size(MR);
[RJW,CJW,DJW] = size(JW);
rb = round((RJW-RMR)/2);
cb = round((CJW-CMR)/2);
JWplusMR = uint8((double(JW(rb:(rb+RMR-1),cb:(cb+CMR-1),:))+double(MR))/2);
figure
image(JWplusMR)
truesize
title('The Extacsy of Bumperillo')
xlabel('Jim Woodring + Mark Ryden')
JWtimesMR = double(JW(rb:(rb+RMR-1),cb:(cb+CMR-1),:)).*double(MR);
M = max(JWtimesMR(:));
m = min(JWtimesMR(:));
JWtimesMR = uint8(255*(double(JWtimesMR)-m)/(M-m));
figure
image(JWtimesMR)
truesize
title('EcstasyBumperillo')

March 6, 2015

1999-2007 by Richard Alan Peters II

46

Intensity Masking: Multiplying Two Images


>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>

JW = imread('Jim Woodring - Bumperillo.jpg','jpg');


Example
Example
MR = imread('Mark Ryden - The Ecstasy of Cecelia.jpg','jpg');
MatlabCode
Code
Matlab
[RMR,CMR,DMR] = size(MR);
[RJW,CJW,DJW] = size(JW);
rb = round((RJW-RMR)/2);
cb = round((CJW-CMR)/2);
JWplusMR = uint8((double(JW(rb:(rb+RMR-1),cb:(cb+CMR-1),:))+double(MR))/2);
figure
image(JWplusMR)
Notethat
thatthe
theimages
imagesare
aremultiplied,
multiplied,pixelwise.
pixelwise.
Note
truesize
title('The Extacsy of Bumperillo')
xlabel('Jim Woodring + Mark Ryden')
JWtimesMR = double(JW(rb:(rb+RMR-1),cb:(cb+CMR-1),:)).*double(MR);
M = max(JWtimesMR(:));
m = min(JWtimesMR(:));
JWtimesMR = uint8(255*(double(JWtimesMR)-m)/(M-m));
figure
image(JWtimesMR)
truesize
Notehow
howthe
theimage
imageintensities
intensitiesare
are
title('EcstasyBumperillo')
Note

March 6, 2015

scaledback
backinto
intothe
therange
range0-255.
0-255.
scaled

1999-2007 by Richard Alan Peters II

47

Pixel Indexing in Matlab


For loops in Matlab are inefficient, whereas Matlabs
native indexing procedures are very fast.

IP_Function
IP_Functionisis
some
somearbitrary
arbitraryimage
image
processing
function
processing function
that
thatyou
youor
orsomeone
someone
else
has
written.
else has written.

Rather than
for
for rr == 1:R
1:R
for
c
for c == 1:C
1:C
J(r,c,:)
J(r,c,:) == IP_Function(I(r,c,:));
IP_Function(I(r,c,:));
end
end
end
end
use, if possible
JJ == IP_Function(I);
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).
March 6, 2015

1999-2007 by Richard Alan Peters II

48

Here,
Here,
n=3
n=3

1:n:R;
rr == 1:n:R;
I(r,:,:)
I(r,:,:)

I(:,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.

March 6, 2015

1999-2007 by Richard Alan Peters II

c = [1 4 7 10 13 16 19 22 25 28 31]

1:n:C;
cc == 1:n:C;

r = [1 4 7 10 13 16 19 22 25 28 31]

Pixel Indexing in Matlab

49

Pixel Indexing in Matlab

Here,
Here,
n=3
n=3

Takethe
the
Take
pixels
pixels
indexed
indexed
byboth
both
by
andc.c.
r rand

This
Thisisiscalled,
called,
vectorizing.
vectorizing.

I(r,c)
I(r,c)

Then, vectors r and c used as index


arguments for image I select every
nth column in every nth row.

March 6, 2015

1999-2007 by Richard Alan Peters II

50

Pixel Indexing in Matlab

Here,
Here,
n=3
n=3

J = I(r,c,:);

image,
image, II
rr == 1:n:R;
1:n:R;

March 6, 2015

cc == 1:n:C;
1:n:C;

1999-2007 by Richard Alan Peters II

51

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
Wheneverpossible,
possible,
avoid
using
for
avoid using forloops;
loops;
vectorize
instead.
vectorize instead.
March 6, 2015

1999-2007 by Richard Alan Peters II

52

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
Wheneverpossible,
possible,
avoid
using
for
avoid using forloops;
loops;
vectorize
instead.
vectorize instead.
March 6, 2015

1999-2007 by Richard Alan Peters II

53

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
Wheneverpossible,
possible,
avoid
using
for
avoid using forloops;
loops;
vectorize
instead.
vectorize instead.
March 6, 2015

1999-2007 by Richard Alan Peters II

54

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
Wheneverpossible,
possible,
avoid
using
for
avoid using forloops;
loops;
vectorize
instead.
vectorize instead.
March 6, 2015

1999-2007 by Richard Alan Peters II

55

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
Wheneverpossible,
possible,
avoid
using
for
avoid using forloops;
loops;
vectorize
instead.
vectorize instead.
March 6, 2015

1999-2007 by Richard Alan Peters II

56

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
Wheneverpossible,
possible,
avoid
using
for
avoid using forloops;
loops;
vectorize
instead.
vectorize instead.
March 6, 2015

1999-2007 by Richard Alan Peters II

57

Pixel Indexing in Matlab


>> I = imread('Lawraa - Flickr - 278635073_883bd891ec_o.jpg','jpg');
>> size(I)
ans =
576
768
3
>> r = randperm(576);
>> c = randperm(768);
>> J = I(r,c,:);
>> figure
>> image(J)
>> truesize
>> title('Scrambled Image')
>> xlabel('What is it?')

Fun(if
(ifyoure
yourean
animaging
imaginggeek)
geek)
Fun
thingto
totry
trywith
withMatlab
Matlab
thing
indexing: Scramble
Scramblean
animage!
image!
indexing:

March 6, 2015

1999-2007 by Richard Alan Peters II

58

Pixel Indexing in Matlab


Theimage
imagecan
canbe
be
The
unscrambledusing
using
unscrambled
the
row
and
column
the row and column
permutation
permutation
vectors,rr&&c.c.
vectors,

>>
>>
>>
>>
>>
>>
>>
>>

xlabel('What is it?')
K(r,c,:) = J;
figure
image(K)
truesize
title('Yay!!!')
xlabel('Photo: Lawraa on Flickr.com')

March 6, 2015

1999-2007 by Richard Alan Peters II

59