Digital Image Processing Using Matlab Pixels
Digital Image Processing Using Matlab Pixels
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
7
Sampling and Quantization
pixel grid column index
row index
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/
16
Crop the Image
left click here and hold
First, select a
region using
the magnifier.
17
From this close-up
we can estimate
Crop the Image
the coordinates of
the region:
18
Crop the Image
Here it is:
19
Crop the Image
Bring it to the
front using the
figure command,
20
Crop the Image
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
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
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
28
Example truecolor and colormapped images
29
Example truecolor and colormapped images
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,:)
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.
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
34
R, G, & B bands of a
truecolor image displayed
with grayscale colormaps
G B
35
R, G, & B bands of a
truecolor image displayed
with tinted colormaps
36
R, G, & B bands of a
truecolor image displayed
with tinted colormaps
G B
37
R, G, & B bands of a
truecolor image displayed
with grayscale colormaps
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);
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,:)
41
Pixel Indexing in Matlab
Here,
n=3
Take the
pixels
indexed
by both
This is called, r and c.
‘vectorizing’.
I(r,c)
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