You are on page 1of 77

Lecture notes

Digital Image Processing by Jorma Kekalainen

Digital Image Processing


Jorma Kekalainen

Digital Image Processing


Color Processing

Lecture weeks 23 and 24

Page 1

Lecture notes

Digital Image Processing by Jorma Kekalainen

What is a colormap?

A colormap is matrix of values between 0 and 1 that define the colors for
graphics objects such as surface and image.
Matlab draws the objects by mapping data values to colors in the
colormap.
Colormaps can be any length, but must be three columns wide.
Each row in the matrix defines one color using an RGB triplet.
An RGB triplet is a three-element row vector whose elements specify the
intensities of the red, green, and blue components of the color.
The intensities must be in the range [0,1].
A value of 0 indicates no color and a value of 1 indicates full intensity.
E.g., this is a colormap with five colors: black, red, green, blue, and white
mymap = [0 0 0; 1 0 0; 0 1 0; 0 0 1; 1 1 1];

Jorma Kekalainen

mymap =
0 0
1 0
0 1
0 0
1 1

0
0
0
1
1

Digital Image Processing

1175

Colormap
Colormaps are associated with axes and
figures.
Use the colormap function to change the
colormap for a particular axes or figure.
E.g., create a surface plot and use mymap as
the colormap for the figure
surf(peaks)
colormap(mymap)
Jorma Kekalainen

Lecture weeks 23 and 24

Digital Image Processing

1176

Page 2

Lecture notes

Digital Image Processing by Jorma Kekalainen

Example

Jorma Kekalainen

Digital Image Processing

1177

Built-in colormaps functions


Built-in colormap,
specified as a one of
the colormaps listed in
the table.
The default colormap is
parula.
Versions of MATLAB
prior to R2014b use jet
as the default.
The colormap function
applies the new
colormap with the
same number of colors
as the current
colormap. To change
the number of colors,
use the map input
argument.
Jorma Kekalainen

Lecture weeks 23 and 24

Digital Image Processing

1178

Page 3

Lecture notes

Digital Image Processing by Jorma Kekalainen

Example
load flujet
figure,image(X)
title('Using jet colormap')
figure,image(X)
colormap(lines)
title('Using lines colormap')

Jorma Kekalainen

Digital Image Processing

1179

Human visual system


The human visual system is particularly
attuned to two things: edges and color.
The human visual system is not particularly
good at recognizing small changes in gray
values.
For human beings, color provides one of the
most important descriptors of the world
around us.
Jorma Kekalainen

Lecture weeks 23 and 24

Digital Image Processing

1180

Page 4

Lecture notes

Digital Image Processing by Jorma Kekalainen

Color study
Color study consists of
1. the physical properties of light which give rise
to color,
2. the nature of the human eye and the ways in
which it detects color,
3. the nature of the human vision center in the
brain, and the ways in which messages from the
eye are perceived as color.

Jorma Kekalainen

Digital Image Processing

1181

Physical aspects of color


Visible light is a small part of the electromagnetic spectrum:
EM radiation in which the energy takes the form of waves
of varying wavelength.
EM radiation range from cosmic rays of very short wavelength
to electric power which has very long wavelength.

The values for the wavelengths of blue, green and red were
set in 1931 by the CIE (Commission Internationale
d'Eclairage), an organization responsible for color standards.
Jorma Kekalainen

Lecture weeks 23 and 24

Digital Image Processing

1182

Page 5

Lecture notes

Digital Image Processing by Jorma Kekalainen

Electromagnetic spectrum and


colors

Jorma Kekalainen

Digital Image Processing

1183

Primary and secondary colors


The human visual system tends to perceive color as being
made up of varying amounts of red, green and blue.
That is, human vision is particularly sensitive to those colors
called the primary colors.
If we add together any two primary colors we obtain the
secondary colors:

Jorma Kekalainen

Lecture weeks 23 and 24

Digital Image Processing

1184

Page 6

Lecture notes

Digital Image Processing by Jorma Kekalainen

Additive and subtractive color


mixing
The additive combination of
red, green and blue lights.

The subtractive combination of


yellow, purple and cyan pigments.

Note: The mixing of pigments is subtractive because it depends on the lights reflected
by those pigments (i.e., on the wavelengths that are not absorbed or subtracted by
Jorma Kekalainen
Digital Image Processing
1185
them)

Absorption of light by
the cones in the human eye

Jorma Kekalainen

Lecture weeks 23 and 24

Digital Image Processing

1186

Page 7

Lecture notes

Digital Image Processing by Jorma Kekalainen

Primary and secondary colors of


light (Additive color mixing)
Here, secondary colors are mixtures of two primary colors.
yellow = red + green
cyan = green + blue
magenta = red + blue
CRT
LCD
plasma

Jorma Kekalainen

Digital Image Processing

1187

Primary and secondary colors of


pigments (Subtractive color mixing)
A primary color of pigment absorbs one primary color of light
and reflects the others.
red = yellow + magenta
green = cyan + yellow
blue = magenta + cyan
Painting colors

Color printing is a mixture


of additive and subtractive
color mixing
Jorma Kekalainen

Lecture weeks 23 and 24

Digital Image Processing

1188

Page 8

Lecture notes

Digital Image Processing by Jorma Kekalainen

Color matching experiment


The amounts of red, green, and blue which make
up a given color can be determined by a color
matching experiment.
In such an experiment, people are asked to match
a given color (a color source) with different
amounts of the additive primaries red, green and
blue.
Such an experiment was performed for the first
time in 1931 by the CIE (Commission
Internationale d'Eclairage), an organization
responsible for color standards.
Jorma Kekalainen

Digital Image Processing

1189

RGB color matching functions


(CIE, 1964)
Note that for some
wavelengths, various
of the red, green or
blue values are
negative.
This is a physical
impossibility, but it
can be interpreted by
adding the primary
beam to the color
source, to maintain a
color match.

Jorma Kekalainen

Lecture weeks 23 and 24

Digital Image Processing

1190

Page 9

Lecture notes

Digital Image Processing by Jorma Kekalainen

XYZ RGB
To remove negative values from color information, the CIE
introduced the XYZ color model.
The values of X, Y and Z can be obtained from the
corresponding R, G and B values by a linear transformation:

The inverse transformation is easily obtained by inverting the


matrix:

Jorma Kekalainen

Digital Image Processing

1191

Exercise
Show with the help of Mablab that the
previous coefficient matrices of the color
model pair XYZRGB are the inverse
matrices.

Jorma Kekalainen

Lecture weeks 23 and 24

Digital Image Processing

1192

Page 10

Lecture notes

Digital Image Processing by Jorma Kekalainen

Solution

>> A=[0.431,0.342,0.178;0.222,0.707,0.071;0.020,0.130,0.939]
A=
0.4310 0.3420 0.1780
0.2220 0.7070 0.0710
0.0200 0.1300 0.9390
>> inv(A)
ans =
3.0596 -1.3927 -0.4747
-0.9676 1.8748 0.0417
0.0688 -0.2299 1.0693
>> invA=inv(A)
invA =
3.0596 -1.3927 -0.4747
-0.9676 1.8748 0.0417
0.0688 -0.2299 1.0693
>> B=inv(invA)
B=
0.4310 0.3420 0.1780
0.2220 0.7070 0.0710
0.0200 0.1300 0.9390

Jorma Kekalainen

Digital Image Processing

1193

XYZ color matching functions

The XYZ color


matching functions
corresponding to
the R, G, B curves.

Jorma Kekalainen

Lecture weeks 23 and 24

Digital Image Processing

1194

Page 11

Lecture notes

Digital Image Processing by Jorma Kekalainen

Note on the previous curves


The previous curves were plotted from the data
originated in Colour & Vision Research Laboratories
database.
The CIE required that the Y component
corresponded with luminance, or perceived
brightness of the color.
That is why the row corresponding to Y in the first
matrix (that is, the second row) sums to 1, and also
why the Y curve is symmetric about the middle of the
visible spectrum.
Jorma Kekalainen

Digital Image Processing

1195

Color matching curves according to


CIExyz (1964)

load OmaCIExyz64.mat; % CIE data


w=OmaCIExyz64(:,1);
x=OmaCIExyz64 (:,2);
y=OmaCIExyz64 (:,3);
z=OmaCIExyz64 (:,4);
figure,plot(w,x,'r:*',w,y,'g:*',w,z,'b:*')
text(590,1.25,'X','Fontsize',14, 'Color', 'red')
text(545,1.1,'Y','Fontsize',14, 'Color', 'green')
text(440,2.15,'Z','Fontsize',14,'Color', 'blue')
title('Color matching curves CIE 1964 from ''OmaCIExyz64.mat''')
xlabel('Wavelength [nm]'), ylabel('Tristimulus value')

Jorma Kekalainen

Lecture weeks 23 and 24

Digital Image Processing

1196

Page 12

Lecture notes

Digital Image Processing by Jorma Kekalainen

Color matching curves according to


CIExyz (1964)

Jorma Kekalainen

Digital Image Processing

1197

Color matching curves (RGB)


load OmaCIExyz64.mat; % CIE data
x2r=[3.063 -1.393 -0.476;-0.969 1.876 0.042;0.068 -0.229 1.069];
xyz=OmaCIExyz64(:,2:4)';
rgb=x2r*xyz;
w=OmaCIExyz64(:,1);
figure,plot(w,rgb(1,:)','r:*',w,rgb(2,:)','g:*',w,rgb(3,:)','b:*')
text(465,2,'Blue','Fontsize', 14,'Color', 'blue')
text(500,1.7,'Green','Fontsize',14, 'Color', 'green')
text(640,1.7,'Red','Fontsize',14, 'Color', 'red')
title('Color matching curves CIE 1964 from ''OmaCIExyz64.mat''')
ylabel('Tristimulus value'), xlabel('Wavelength [nm]')

Jorma Kekalainen

Lecture weeks 23 and 24

Digital Image Processing

1198

Page 13

Lecture notes

Digital Image Processing by Jorma Kekalainen

Color matching curves (RGB)

Jorma Kekalainen

Digital Image Processing

1199

Tristimulus values
In general, the values of X, Y and Z needed to form any
particular color are called the tristimulus values.
Values corresponding to particular colors can be obtained
from published tables.
In order to discuss color independent of brightness, the
tristimulus values can be normalized by dividing by X+Y+Z:

So x + y + z = 1.
Jorma Kekalainen

Lecture weeks 23 and 24

Digital Image Processing

1200

Page 14

Lecture notes

Digital Image Processing by Jorma Kekalainen

Chromaticity coordinates
Thus a color can be specified by x and y alone,
called the chromaticity coordinates.
Given x, y, and Y, we can obtain the tristimulus
values X and Z:

Jorma Kekalainen

Digital Image Processing

1201

Exercise
Show that given x, y, and Y, we can obtain the
tristimulus values X and Z:

Jorma Kekalainen

Lecture weeks 23 and 24

Digital Image Processing

1202

Page 15

Lecture notes

Digital Image Processing by Jorma Kekalainen

Solution
X

x = X + Y + Z

y =
X
+
Y +Z

z = X + Y + Z

x + y + z =1

(1)
(2)
(3)

(4)
Dividing equation (1) by equation (2)

X x
=
Y
y

X=

x
Y
y

Dividing equation (3) by equation (2)

Z z
=
Y y

Jorma Kekalainen

Z=

z
Y
y

z =1 x y

Digital Image Processing

1 x y
Y
y
1203

Exercise
Show that from equations
x+y+z=1

we obtain the normalized tristimulus values x, y, and


z.
Jorma Kekalainen

Lecture weeks 23 and 24

Digital Image Processing

1204

Page 16

Lecture notes

Digital Image Processing by Jorma Kekalainen

Solution
> eq4:=x+y+z=1;

eq4 := x + y + z = 1

> eq5:=X=(x/y)*Y;
eq5 := X =

> eq6:=Z=(1-x-y)*Y/y;
eq6 := Z =

xY
y

( 1 x y) Y
y

> solve({eq4,eq5,eq6},[x,y,z]);
X
Y
Z
x =

,y=
,z=

X+Y+Z
X+Y+Z
X + Y + Z

>
Jorma Kekalainen

Digital Image Processing

1205

Chromaticity diagram

We can plot a chromaticity diagram using the data of XYZ values


originated in Colour & Vision Research Laboratories database.
load OmaCIExyz64.mat; % CIE data
xyz=OmaCIExyz64(:,2:4);
xy=xyz./(sum(xyz')'*[1 1 1]);
x=xy(:,1)';
y=xy(:,2)';
figure,plot([x x(1)],[y y(1)]),axis square
title('CIE 1964 chromaticity coordinates from ''OmaCIExyz64.mat''')
xlabel('X coordinate'),ylabel('Y coordinate')

Note: Here the matrix xyz consists of the second, third and fourth columns of the data, and plot
is a function
which draws a polygon withDigital
vertexes
taken from the x and y vectors.
Jorma Kekalainen
Image Processing
1206
The extra x(1) and y(1) ensures that the polygon joins up.

Lecture weeks 23 and 24

Page 17

Lecture notes

Digital Image Processing by Jorma Kekalainen

Chromaticity diagram (CIE 1964)

Note: The values of x and y which lie within the horseshoe shape represent values
Jorma Kekalainen
Digital Image Processing
1207
which
correspond to physically realizable
colors.

Official CIE 1931 x,y chromaticity


diagram

Jorma Kekalainen

Lecture weeks 23 and 24

Digital Image Processing

1208

Page 18

Lecture notes

Digital Image Processing by Jorma Kekalainen

Digital Image Processing


Color models

Color models
A color model is a method for specifying colors in
some standard way.
It generally consists of a three dimensional
coordinate system and a subspace of that system
in which each color is represented by a single
point.
We shall investigate three models
RGB
HSV
YIQ
Jorma Kekalainen

Lecture weeks 23 and 24

Digital Image Processing

1210

Page 19

Lecture notes

Digital Image Processing by Jorma Kekalainen

RGB and color cube


In this model, each color is represented as three
values R, G and B , indicating the amounts of red,
green and blue which make up the color.
This model was originally used for computer
displays where three independent light source
are for the red, green and blue component of
each color.
We may imagine all the colors sitting inside a
color cube.
Jorma Kekalainen

Digital Image Processing

1211

RGB color cube

Backsides

Jorma Kekalainen

R=0

Lecture weeks 23 and 24

G=0

B=0

Digital Image Processing

1212

Page 20

Lecture notes

Digital Image Processing by Jorma Kekalainen

RGB color cube


The colors along the black-white diagonal, shown in the
diagram as a dotted line, are the points of the space where
all the R, G, B values are equal.
They are the different intensities of gray.
RGB is the standard for the display of colors on computer
monitors and TV sets.
But it is not a very good way of describing colors.
How, for example, would you define light brown using RGB?
Note also from the previous RGB color matching functions
that some colors require negative values of R, G or B.
These colors are not realizable on a computer monitor or
TV set, on which only positive values are possible.
Jorma Kekalainen

Digital Image Processing

1213

RGB gamut
The set of colors that can be represented
using a particular color space is called its
gamut (spectrum, range of colors, color
space)
The colors corresponding to positive values
form the RGB gamut; in general a color gamut
consists of all the colors realizable with a
particular color model.
Note: In color reproduction, including computer graphics and photography,
the gamut, or color gamut, is a certain complete subset of colors. The most
common usage refers to the subset of colors which can be accurately
represented in a given circumstance, such as within a given color space or by a
Jorma Kekalainen
Digital Image Processing
1214
certain
output device.

Lecture weeks 23 and 24

Page 21

Lecture notes

Digital Image Processing by Jorma Kekalainen

RGB gamut
To define the gamut, we shall create a
100*100*3 array, and to each point (i,j) in the
array, associate an XYZ triple defined by
(i/100, j/100, 1- i/100 - j/100).
We can then compute the corresponding RGB
triple, and if any of the RGB values are
negative, make the output value white.
This is done with the few commands.
Jorma Kekalainen

Digital Image Processing

1215

Computing the RGB gamut

load OmaCIExyz64.mat; % CIE data


x2r=[3.063 -1.393 -0.476;-0.969 1.876 0.042;0.068 -0.229 1.069];
for i=1:100,
for j=1:100,
cg(i,j,:)=x2r*[j/100 i/100 1-i/100-j/100]';
if min(cg(i,j,:))<0,
cg(i,j,:)=[1 1 1];
end;
end;
end;
figure, imshow(cg),line(x',y'),axis square,axis xy

title('RGB gamut')
Jorma Kekalainen

Lecture weeks 23 and 24

Digital Image Processing

1216

Page 22

Lecture notes

Digital Image Processing by Jorma Kekalainen

RGB gamut

Jorma Kekalainen

Digital Image Processing

1217

Chromaticity diagram and gamut

load OmaCIExyz64.mat; % CIE data


xyz=OmaCIExyz64(:,2:4);
xy=xyz./(sum(xyz')'*[1 1 1]);
x=100*xy(:,1)';
y=100*xy(:,2)';
figure,plot([x x(1)],[y y(1)]),axis square
hold on
xlabel('X coordinate'),ylabel('Y coordinate')
%
x2r=[3.063 -1.393 -0.476;-0.969 1.876 0.042;0.068 -0.229 1.069];
cg=zeros(100,100,3);
for i=1:100,
for j=1:100,
cg(i,j,:)=x2r*[j/100 i/100 1-i/100-j/100]';
if min(cg(i,j,:))<0,
cg(i,j,:)=[1 1 1];
end;
end;
end;
imshow(cg),line([x x(1)],[y y(1)]),axis square,axis xy,axis on,axis tight
title('Chromaticity diagram and RGB gamut')

Jorma Kekalainen

Lecture weeks 23 and 24

Digital Image Processing

1218

Page 23

Lecture notes

Digital Image Processing by Jorma Kekalainen

Chromaticity diagram and gamut

Jorma Kekalainen

Digital Image Processing

1219

A typical gamut
The grayed-out horseshoe
shape is the entire range of
possible chromaticities,
displayed in the CIE 1931
chromaticity
diagram format.
The colored triangle is the
gamut available to a typical
computer monitor; it does
not cover the entire space.
The corners of the triangle
are the primary colors for
this gamut; in the case of
a CRT, they depend on the
colors of the phosphors of
the monitor.
Jorma Kekalainen

Lecture weeks 23 and 24

Digital Image Processing

1220

Page 24

Lecture notes

Digital Image Processing by Jorma Kekalainen

Hue, Saturation, Value (HSV)


HSV color model terms have the following
meanings:
Hue: The true color attribute (red, green, blue,
orange, yellow, and so on).
Saturation: The amount by which the color has
been diluted with white.
The more white in the color, the lower the saturation.
So a deep red has high saturation, and a light red (a
pinkish color) has low saturation.

Value: The degree of brightness: a bright color


has high intensity; a dark color has low intensity.
Jorma Kekalainen

Digital Image Processing

1221

HSV (Hue, Saturation, Value)


This model is a quite natural method of
describing colors.
Since the intensity (value) is independent
of the color information, this is a very
useful model for image processing.
We can visualize this model as a cone.
Any point on the surface represents a
purely saturated color.
The saturation is thus given as the
relative distance to the surface from the
central axis of the structure.
Hue is defined to be the angle
measurement from a pre-determined
axis.
Jorma Kekalainen
Digital Image Processing

Lecture weeks 23 and 24

1222

Page 25

Lecture notes

Digital Image Processing by Jorma Kekalainen

Illustration of the HSV color


space

Any point on the surface


represents a purely saturated
color. The saturation is thus
given as the relative distance to
the surface from the central axis
of the structure. Hue is defined
to be the angle measurement
from a pre-determined axis, say
red. Jorma Kekalainen

Digital Image Processing

1223

Convert from HSV to RGB color


space
The HSV color space (Hue, Saturation, Value) is often used by
people who are selecting colors (e.g., of paints or inks) from a color
wheel or palette, because it corresponds better to how people
experience color than the RGB color space does.
The functions rgb2hsv and hsv2rgb convert images between the
RGB and HSV color spaces.
As hue varies from 0 to 1.0, the corresponding colors vary from red
through yellow, green, cyan, blue, magenta, and back to red, so that
there are actually red values both at 0 and 1.0.
As saturation varies from 0 to 1.0, the corresponding colors (hues)
vary from unsaturated (shades of gray) to fully saturated (no white
component).
As value, or brightness, varies from 0 to 1.0, the corresponding
colors become increasingly brighter.

Jorma Kekalainen

Lecture weeks 23 and 24

Digital Image Processing

1224

Page 26

Lecture notes

Digital Image Processing by Jorma Kekalainen

Separated color planes of an HSV image


As the hue plane image in the next figure
illustrates, hue values make a linear
transition from high to low. If we compare
the hue plane image against the original
image, we can see that shades of deep
blue have the highest values, and shades
of deep red have the lowest values. (As
stated previously, there are values of red
on both ends of the hue scale.)
Saturation can be thought of as the purity
of a color. As the saturation plane image
shows, the colors with the highest
saturation have the highest values and are Value is roughly equivalent to
brightness, and you will notice that
represented as white. In the center of the
the brightest areas of the value
saturation image, notice the various
plane correspond to the brightest
shades of gray. These correspond to a
colors in the original image.
mixture of colors; the cyans, greens, and
Jorma Kekalainen
Digital Image Processing
1225
yellow shades are mixtures of true colors.

Separate color planes

For closer inspection of the HSV color space, the next block of code displays the
separate color planes (hue, saturation, and value) of an HSV image.

RGB=reshape(ones(64,1)*reshape(jet(64),1,192),[64,64,3]);
HSV=rgb2hsv(RGB);
H=HSV(:,:,1);
S=HSV(:,:,2);
V=HSV(:,:,3);
subplot(2,2,1), imshow(H)
title('Hue')
subplot(2,2,2), imshow(S)
title('Saturation')
subplot(2,2,3), imshow(V)
title('Value')
subplot(2,2,4), imshow(RGB)
title('RGB')

B = reshape(A,m,n) returns the m-by-n matrix B


whose elements are taken column-wise from A. An
Jorma Kekalainen
Digital Image Processing
error results if A does not have m*n elements.

Lecture weeks 23 and 24

1226

Page 27

Lecture notes

Digital Image Processing by Jorma Kekalainen

reshape(A,siz)
B = reshape(A,siz) returns an n-dimensional array with
the same elements as A, but reshaped to siz, a vector
representing the dimensions of the reshaped array.
B = reshape(A,m,n,p,...) or B = reshape(A,[m n
p ...]) returns an n-dimensional array with the same
elements as A but reshaped to have the size m-by-nby-p-by-....
The product of the specified dimensions, m*n*p*...,
must be the same as numel(A).
n = numel(A) returns the number of elements, n, in
array A.
Jorma Kekalainen

Digital Image Processing

1227

Conversion between RGB and HSV


Suppose a color is specified by its RGB values.
If all the three values are equal, then the color
will be a gray scale; that is, an intensity of white.
Such a color, containing just white, will thus have
a saturation of zero.
Conversely, if the RGB values are very different,
we would expect the resulting color to have a
high saturation.
In particular, if one or two of the RGB values are
zero, the saturation will be one, the highest
possible value.
Jorma Kekalainen

Lecture weeks 23 and 24

Digital Image Processing

1228

Page 28

Lecture notes

Digital Image Processing by Jorma Kekalainen

Conversion between RGB and HSV


Hue is defined as the fraction around the circle starting from
red, which thus has a hue of zero.
Reading around the circle produces the following hues:
Color

Hue

Red

Yellow

1/6

Green

1/3

Cyan

1/2

Blue

2/3

Magenta

5/6

Jorma Kekalainen

Digital Image Processing

1229

Conversion between RGB and HSV


Suppose we are given three (R,G,B) values, which we
suppose to be between 0 and 1.
So if they are between 0 and 255, we first divide
each value by 255.
We then define:

Jorma Kekalainen

Lecture weeks 23 and 24

Digital Image Processing

1230

Page 29

Lecture notes

Digital Image Processing by Jorma Kekalainen

Conversion between RGB and HSV


To obtain a value for Hue, we consider several cases:

In the particular case (R,G,B) = (0,0,0), for which both V==0,


we define (H,S,V)=(0,0,0).
Jorma Kekalainen

Digital Image Processing

1231

Example

For example, let it be (R,G,B)=(0.1,0.3,0.5)


Now V=max(R,G,B)=0.5
=max(R,G,B)-min(R,G,B)=0.5-0.1=0.4,
S= /V=0.4/0.5= 0.8

Since B=V then

H=(4+(0.1-0.3)/0.4)/6= 0.5833
The result is
(R,G,B)=(0.1,0.3,0.5) (0.5833,0.8,0.5)=(H,S,V)
Jorma Kekalainen

Lecture weeks 23 and 24

Digital Image Processing

1232

Page 30

Lecture notes

Digital Image Processing by Jorma Kekalainen

Example

(R,G,B)=(0.1,0.3,0.5) (0.5833,0.8,0.5)=(H,S,V)

Checking:
>> hsv=rgb2hsv([0.1,0.3,0.5])
hsv =
0.5833 0.8000 0.5000
Checking also other way: >> rgb=hsv2rgb(hsv)
rgb =
0.1000 0.3000 0.5000
Jorma Kekalainen

Digital Image Processing

1233

Exercise
Convert (R,G,B)=(0.2,0.4,0.6) to the
corresponding HSV. Check the result with
conversion function rgb2hsv.

Jorma Kekalainen

Lecture weeks 23 and 24

Digital Image Processing

1234

Page 31

Lecture notes

Digital Image Processing by Jorma Kekalainen

Solution

(R,G,B)=(0.2,0.4,0.6)
Now V=max(R,G,B)=0.6
=max(R,G,B)-min(R,G,B)=0.6-0.2=0.4,
S= /V=0.4/0.6= 2/30.6667
Since B=V then

H=(4+(0.2-0.4)/0.4)/6=7/12 0.5833
The result is
(R,G,B)=(0.2,0.4,0.6) (0.5833,0.8,0.5)=(H,S,V)
Jorma Kekalainen

Digital Image Processing

1235

Solution

Checking:
>> hsv=rgb2hsv([0.1000 0.3000 0.5000])
hsv =
0.5833 0.8000 0.5000
Checking other way:
>> rgb=hsv2rgb([0.5833,0.8,0.5])
rgb =
0.1000 0.3001 0.5000
Note:
>> rgb=hsv2rgb([7/12,0.8,0.5])
rgb =
0.1000 0.3000 0.5000

Jorma Kekalainen

Lecture weeks 23 and 24

Digital Image Processing

1236

Page 32

Lecture notes

Digital Image Processing by Jorma Kekalainen

YIQ
This color space was originally used for TV/video in America
and other countries where NTSC was the video standard.
In this scheme Y is the luminance (this corresponds roughly
with intensity), and I and Q carry the color information
(chrominance).
The conversion between RGB is straightforward:

and

Jorma Kekalainen
Digital Image Processing
The
two conversion matrices are of course
inverses of each other.

1237

Exercise
Using Matlab show that the two previous
coefficient matrices are inverses of each other.

Jorma Kekalainen

Lecture weeks 23 and 24

Digital Image Processing

1238

Page 33

Lecture notes

Digital Image Processing by Jorma Kekalainen

Solution

>> %[YIQ]=[C]*[RGB]
C=[0.299,0.587,0.114;0.596,-0.274,-0.322;0.211,-0.523,0.312]
C=
0.2990 0.5870 0.1140
0.5960 -0.2740 -0.3220
0.2110 -0.5230 0.3120
>> invC=inv(C)
invC =
1.0000 0.9562 0.6214
A=
1.0000 -0.2727 -0.6468
0.2990 0.5870 0.1140
1.0000 -1.1037 1.7006
0.5960 -0.2740 -0.3220
>> inv(invC)
0.2110 -0.5230 0.3120
ans =
>> inv(A)
0.2990 0.5870 0.1140
0.5960 -0.2740 -0.3220
ans =
0.2110 -0.5230 0.3120
1.0000 0.9562 0.6214
>>
1.0000 -0.2727 -0.6468

1.0000 -1.1037 1.7006

Jorma Kekalainen

Digital Image Processing

1239

Note
Because
The human visual system assigns more
intensity to the green component of an image
than to the red and blue components

Jorma Kekalainen

Lecture weeks 23 and 24

Digital Image Processing

1240

Page 34

Lecture notes

Digital Image Processing by Jorma Kekalainen

RGB YIQ
Since YIQ is a linear transformation of RGB
and thus the conversions are linear, and hence
easy to do, makes YIQ a good choice for color
image processing.
Conversion between RGB and YIQ are
implemented with the Matlab functions
rgb2ntsc and ntsc2rgb.
Note:

Jorma Kekalainen

Digital Image Processing

>> rgb2ntsc([1,1,1])
ans =
1 0 0
>> rgb2ntsc([0.5,0.5,0.5])
ans =
1241
0.5000
0
0

Note

Jorma Kekalainen

Lecture weeks 23 and 24

>> rgb2ntsc([1,1,1])
ans =
1 0 0
>> rgb2ntsc([0.5,0.5,0.5])
ans =
0.5000
0
0
>> ntsc2rgb([0.5,0.5,0.5])
ans =
1.0000 0.0314 0.6197
>> ntsc2rgb([1,1,1])
ans =
1.0000 0.0314 0.6197
>> ntsc2rgb([1,0,0])
ans =
1 1 1
>> ntsc2rgb([0.5,0,0])
ans =
0.5000 0.5000
0.5000
Digital Image
Processing

1242

Page 35

Lecture notes

Digital Image Processing by Jorma Kekalainen

rgb2ntsc
Convert RGB color values to NTSC color space
Syntax:
yiqmap = rgb2ntsc(rgbmap)
YIQ = rgb2ntsc(RGB)

Description:
yiqmap = rgb2ntsc(rgbmap) converts the m-by-3 RGB
values in rgbmap to NTSC color space.
yiqmap is an m-by-3 matrix that contains the NTSC
luminance (Y) and chrominance (I and Q) color
components as columns that are equivalent to the colors
in the RGB colormap.
YIQ = rgb2ntsc(RGB) converts the truecolor image RGB to
the equivalent NTSC image YIQ.
Jorma Kekalainen

Digital Image Processing

1243

rgb2ntsc
Class Support
RGB can be of class uint8, uint16, int16, single, or double.
rgbmap can be double.
The output is double.

Note:
In the NTSC color space, the luminance is the grayscale
signal used to display pictures on monochrome (black and
white) televisions.
The other components carry the hue and saturation
information.
rgb2ntsc defines the NTSC components using

Jorma Kekalainen

Lecture weeks 23 and 24

Digital Image Processing

1244

Page 36

Lecture notes

Digital Image Processing by Jorma Kekalainen

ntsc2rgb
Convert NTSC values to RGB color space
Syntax:
rgbmap = ntsc2rgb(yiqmap)
RGB = ntsc2rgb(YIQ)

Description:
rgbmap = ntsc2rgb(yiqmap) converts the m-by-3 NTSC
(television) color values in yiqmap to RGB color space.
If yiqmap is m-by-3 and contains the NTSC luminance (Y) and
chrominance (I and Q) color components as columns, then
rgbmap is an m-by-3 matrix that contains the red, green, and
blue values equivalent to those colors.
Both rgbmap and yiqmap contain intensities in the range 0 to
1.0.
The intensity 0 corresponds to the absence of the component,
while the intensity 1.0 corresponds to full saturation of the
component.
Jorma Kekalainen
Digital Image Processing
1245

ntsc2rgb
RGB = ntsc2rgb(YIQ) converts the NTSC image YIQ
to the equivalent truecolor image RGB.
ntsc2rgb computes the RGB values from the NTSC
components using

Class Support:
The input image or colormap must be of class double.
The output is of class double.
Jorma Kekalainen

Lecture weeks 23 and 24

Digital Image Processing

1246

Page 37

Lecture notes

Digital Image Processing by Jorma Kekalainen

Examples

Convert RGB image to NTSC and back.


RGB = imread('board.tif');
NTSC = rgb2ntsc(RGB);
RGB2 = ntsc2rgb(NTSC);
figure,imshow(RGB)
title('RGB')
figure,imshow(NTSC)
title('NTSC')
figure,imshow(RGB2)
title('RGB2')

Jorma Kekalainen

Digital Image Processing

1247

Example

Jorma Kekalainen

Lecture weeks 23 and 24

Digital Image Processing

1248

Page 38

Lecture notes

Digital Image Processing by Jorma Kekalainen

Color images in Matlab


Since a color image requires three separate
items of information for each pixel, a (true)
color image of size m*n is represented in
Matlab by an array of size m*n*3 which is a
three dimensional array.
We can think of such an array as a single
entity consisting of three separate matrices
aligned vertically.
Jorma Kekalainen

Digital Image Processing

1249

Three dimensional array for an


RGB image

Jorma Kekalainen

Lecture weeks 23 and 24

Digital Image Processing

1250

Page 39

Lecture notes

Digital Image Processing by Jorma Kekalainen

Convert from HSV to RGB color


space
The rgb2hsv function converts colormaps or
RGB images to the HSV color space.
hsv2rgb performs the reverse operation.
These commands convert an RGB image to the
HSV color space.
RGB = imread('peppers.png');
HSV = rgb2hsv(RGB);

Jorma Kekalainen

Digital Image Processing

1251

RGB color image and its


components

%Suppose we read in an RGB image:


f=imread('flowers.tif');
size(f)
%We can isolate each color component by the colon operator:
f(:,:,1); %The first, or red component
f(:,:,2); %The second, or green component
f(:,:,3); %The third, or blue component
%These can all be viewed with imshow:
imshow(f)
figure,imshow(f(:,:,1))
figure,imshow(f(:,:,2))
figure,imshow(f(:,:,3))

Jorma Kekalainen

Lecture weeks 23 and 24

Digital Image Processing

1252

Page 40

Lecture notes

Digital Image Processing by Jorma Kekalainen

RGB color image and its


Color image
components
Note how the colors with particular hues show up
with high intensities in their respective components.
For the bright flowers the red component shows a very high
intensity. The green and blue components show much lower
intensities. Similarly the green leaves show slightly higher
intensity in the green component than the other two.

Red component

Jorma Kekalainen

Green component

Blue component

Digital Image Processing

1253

RGB HSV
We can convert RGB to HSV and view the
components again:
fh=rgb2hsv(f);
figure, imshow(fh)
figure,imshow(fh(:,:,1))
figure,imshow(fh(:,:,2))
figure,imshow(fh(:,:,3))
Jorma Kekalainen

Lecture weeks 23 and 24

Digital Image Processing

1254

Page 41

Lecture notes

Digital Image Processing by Jorma Kekalainen

RGB HSV

Hue

Saturation

Jorma Kekalainen

Digital Image Processing

Value

1255

RGB YIQ
We can do precisely the same thing for the
YIQ color space:
fn=rgb2ntsc(f);
figure,imshow(fn)
figure,imshow(fn(:,:,1))
figure,imshow(fn(:,:,2))
figure,imshow(fn(:,:,3))
Jorma Kekalainen

Lecture weeks 23 and 24

Digital Image Processing

1256

Page 42

Lecture notes

Digital Image Processing by Jorma Kekalainen

YIQ components

Jorma Kekalainen

Digital Image Processing

1257

Note
The National Television Systems Committee (NTSC) has
defined a color space known as YIQ.
One of the main advantages of this format is that
grayscale information is separated from color data, so
the same signal can be used for both color and black
and white sets.
In the NTSC color space, image data consists of three
components: luminance (Y), hue (I), and saturation
(Q).
The first component, luminance, represents grayscale
information, while the last two components make
up chrominance (color information).
Jorma Kekalainen

Lecture weeks 23 and 24

Digital Image Processing

1258

Page 43

Lecture notes

Digital Image Processing by Jorma Kekalainen

Note
The function rgb2ntsc converts colormaps of RGB images to the
NTSC color space and ntsc2rgb performs the reverse operation.
For example, these commands convert an RGB image to NTSC
format.
RGB = imread('peppers.png');
YIQ = rgb2ntsc(RGB);

Because luminance is one of the components of the NTSC format,


the RGB to NTSC conversion is also useful for isolating the gray level
information in an image.
In fact, the toolbox functions rgb2gray and ind2gray use the
rgb2ntsc function to extract the grayscale information from a color
image.
For example, these commands are equivalent to calling rgb2gray.
YIQ = rgb2ntsc(RGB);
I = YIQ(:,:,1);
Note:
InKekalainen
the YIQ color space, I is Digital
oneImage
of the
two color components, not the1259
Jorma
Processing
grayscale component.

Example

RGB = imread('peppers.png');
figure,imshow(RGB)
title('RGB')
YIQ = rgb2ntsc(RGB);
figure,imshow(YIQ)
title('YIQ')
I = YIQ(:,:,1);
figure,imshow(I)
title('Y component')
figure,imshow(rgb2gray(RGB))
title('rgb2gray(RGB)')

Jorma Kekalainen

Lecture weeks 23 and 24

Digital Image Processing

1260

Page 44

Lecture notes

Digital Image Processing by Jorma Kekalainen

Example

Jorma Kekalainen

Digital Image Processing

1261

YCbCr color space


The YCbCr color space is widely used for digital video.
In this format, luminance information is stored as a
single component (Y), and chrominance information is
stored as two color-difference components (Cb and
Cr).
Cb represents the difference between the blue
component and a reference value.
Cr represents the difference between the red
component and a reference value.
YCbCr data can be double precision, but the color
space is particularly well suited to uint8 data.
Note:
YUV,
another color space widely
used
for digital video, is very similar to YCbCr
Jorma
Kekalainen
Digital
Image Processing
1262
but not identical.

Lecture weeks 23 and 24

Page 45

Lecture notes

Digital Image Processing by Jorma Kekalainen

YCbCr color space


For uint8 images, the data range for Y is [16, 235], and
the range for Cb and Cr is [16, 240].
YCbCr leaves room at the top and bottom of the full
uint8 range so that additional (nonimage) information
can be included in a video stream.
The function rgb2ycbcr converts colormaps or RGB
images to the YCbCr color space and ycbcr2rgb
performs the reverse operation.
For example, these commands convert an RGB image
to YCbCr format.
RGB = imread('peppers.png');
YCBCR = rgb2ycbcr(RGB);
Jorma Kekalainen

Digital Image Processing

1263

Digital Image Processing


Pseudocoloring

Lecture weeks 23 and 24

Page 46

Lecture notes

Digital Image Processing by Jorma Kekalainen

Pseudocoloring
This means assigning colors to a gray-scale
image in order to make certain aspects of the
image more open to visual interpretation, e.g.,
for medical images.
There are different methods of
pseudocoloring:
Intensity slicing
Gray - color transformations
Jorma Kekalainen

Digital Image Processing

1265

Intensity slicing
In this method, we break up the image into various gray level
ranges.
We simply assign a different color to each range.
For example:
We can consider this as a mapping

Jorma Kekalainen

Lecture weeks 23 and 24

Digital Image Processing

1266

Page 47

Lecture notes

Digital Image Processing by Jorma Kekalainen

Gray - color transformations


We have three functions fR(x), fG(x), fB(x)
which assign red, green and blue values to
each gray level x.
These values (with appropriate scaling, if
necessary) are then used for display.
Using an appropriate set of functions can
enhance a gray-scale image with impressive
results.
Jorma Kekalainen

Digital Image Processing

1267

Example
The gray level x in the diagram is mapped onto
red, green and blue values of 0.375, 0.25, 0.75

Jorma Kekalainen

Lecture weeks 23 and 24

Digital Image Processing

1268

Page 48

Lecture notes

Digital Image Processing by Jorma Kekalainen

Colormaps
In Matlab, a simple way to view an image with
added color is to use imshow with an extra
colormap parameter.
We can add a colormap with the colormap
function; there are several existing colormaps to
choose from.
The available colormaps (and also other
information concerning 3d graphics) are listed in
the help file for graph3d
>> help graph3d
Jorma Kekalainen

Digital Image Processing

1269

Color maps

hsv - Hue-saturation-value color map.


hot - Black-red-yellow-white color map.
gray - Linear gray-scale color map.
bone - Gray-scale with tinge of blue color map.
copper - Linear copper-tone color map.
pink - Pastel shades of pink color map.
white - All white color map.
flag - Alternating red, white, blue, and black
color map.
lines - Color map with the line colors.
colorcube - Enhanced color-cube color map.
vga - Windows colormap for 16 colors.
jet - Variant of HSV.
prism - Prism color map.
cool - Shades of cyan and magenta color map.
autumn - Shades of red and yellow color map.
spring - Shades of magenta and yellow color
map.
winter - Shades of blue and green color map.
summer - Shades of green and yellow color
map.

Jorma Kekalainen

Lecture weeks 23 and 24

Digital Image Processing

1270

Page 49

Lecture notes

Digital Image Processing by Jorma Kekalainen

Applying a color map to a grayscale


image
We created the color image with:
[x,map]=imread('spine.tif');
b=uint8(ind2gray(x,map));
size(b)
figure, imshow(b)
title('Original grayscale image')
figure,imshow(b,colormap(jet(256)))
title('Pseudocoloring with
''colormap(jet(256))''')

Jorma Kekalainen

Digital Image Processing

1271

Example: Image spine.tif

% Pseudocoloring
[x,map]=imread('spine.tif');
b=uint8(ind2gray(x,map));
size(b)
figure, imshow(b)
title('Original grayscale image')
figure,imshow(b,colormap(jet(256)))
title('Pseudocoloring with ''colormap(jet(256))''')
% Reducing the number of grayscales in the image to 16
b16=grayslice(b,16);
figure,imshow(b16,colormap(vga))
title('Pseudocoloring with reduced to 16 tones using vga colormap')
mycolormap=[0 0 1;1 0 1;0 1 0;1 0 0]; % Creating own colormap
% Scaling the image down so that there are only the
% four grayscales 0, 1, 2 and 3
b4=grayslice(b,4);
figure,imshow(b4,mycolormap)
title('Pseudocolored image using homemade map consisting only 4 tones')

Jorma Kekalainen

Lecture weeks 23 and 24

Digital Image Processing

1272

Page 50

Lecture notes

Digital Image Processing by Jorma Kekalainen

Jorma Kekalainen

Digital Image Processing

1273

Applying a color map to a grayscale


image

We create the color image with:


[x,map]=imread('palikat.png');
figure,imshow(x,map)
title('Original image')
b=uint8(ind2gray(x,map));
figure,imshow(b)
title('Image converted grayscale')
figure,
imshow(b,colormap(jet(256)))
title('Image colormapped
''colormap(jet(256))''')
Jorma Kekalainen

Lecture weeks 23 and 24

Digital Image Processing

1274

Page 51

Lecture notes

Digital Image Processing by Jorma Kekalainen

Creating simple colormap


We can easily create our own colormap: it must be a matrix
with 3 columns, and each row consists of RGB values between
0.0 and 1.0.
Suppose we wish to create a blue, magenta, green, red
colormap.
Using the RGB values:

we can create our colormap with:


mycolormap=[0 0 1;1 0 1;0 1 0;1 0 0];
Jorma Kekalainen

Digital Image Processing

1275

Image colored with a handmade


colormap
Before we apply it to the image,
we need to scale the image
down so that there are only the
four grayscales 0, 1, 2 and 3:
b4=grayslice(b,4);
figure
imshow(b4,mycolormap)
The result is shown beside.

Jorma Kekalainen

Lecture weeks 23 and 24

Digital Image Processing

1276

Page 52

Lecture notes

Digital Image Processing by Jorma Kekalainen

Example

[x,map]=imread('palikat.png');
figure,imshow(x,map)
title('Original image')
b=uint8(ind2gray(x,map));
figure,imshow(b)
title('Image converted grayscale')
figure, imshow(b,colormap(jet(256)))
title('Image colormapped ''colormap(jet(256))''')
b16=grayslice(b,16);
figure,imshow(b16,colormap(vga))
title('Image colormapped ''colormap(vga)''')
mycolormap=[0 0 1;1 0 1;0 1 0;1 0 0];
b4=grayslice(b,4);
figure,imshow(b4,mycolormap)
title('Image colormapped ''mycolormap''')

Jorma Kekalainen

Digital Image Processing

1277

Jorma Kekalainen

Digital Image Processing

1278

Lecture weeks 23 and 24

Page 53

Lecture notes

Digital Image Processing by Jorma Kekalainen

imfinfo('palikat.png')

>> imfinfo('palikat.png')
ans =
Filename: 'C:\Users\ktkekjo\Documents\MATLAB\palikat.png'
FileModDate: '19-Aug-2015 01:49:20'
FileSize: 39932
Format: 'png'
FormatVersion: []
Width: 307
Height: 308
BitDepth: 8
ColorType: 'indexed'
FormatSignature: [137 80 78 71 13 10 26 10]
Colormap: [256x3 double]
Histogram: []

Jorma Kekalainen

Digital Image Processing

1279

Digital Image Processing


Processing of Color
Images

Lecture weeks 23 and 24

Page 54

Lecture notes

Digital Image Processing by Jorma Kekalainen

Processing of color images


There are two methods we can use:
1. we can process each R, G, B matrix separately,
2. we can transform the color space to one in which
the intensity is separated from the color, and process
the intensity component only.

Schemas for these are given in the following


diagrams.
We shall consider some of different image
processing tasks, and apply the above methods to
color images.
Jorma Kekalainen

Digital Image Processing

1281

RGB and intensity processing

Jorma Kekalainen

Lecture weeks 23 and 24

Digital Image Processing

1282

Page 55

Lecture notes

Digital Image Processing by Jorma Kekalainen

Example
Filtering a three-dimensional image with a twodimensional filter is equivalent to filtering each plane of the
three-dimensional image individually with the same twodimensional filter.
%Read truecolor image.
rgb = imread('peppers.png');
figure,imshow(rgb);
%Create a filter.
h = ones(7,7)/49
%Filter the image using imfilter and display it.
rgb2 = imfilter(rgb,h);
figure, imshow(rgb2)
Jorma Kekalainen

Digital Image Processing

1283

Example
h = ones(7,7)/49
h=
0.0204 0.0204
0.0204 0.0204
0.0204 0.0204
0.0204 0.0204
0.0204 0.0204
0.0204 0.0204
0.0204 0.0204

Original image

Jorma Kekalainen

Lecture weeks 23 and 24

0.0204
0.0204
0.0204
0.0204
0.0204
0.0204
0.0204

0.0204
0.0204
0.0204
0.0204
0.0204
0.0204
0.0204

0.0204
0.0204
0.0204
0.0204
0.0204
0.0204
0.0204

0.0204
0.0204
0.0204
0.0204
0.0204
0.0204
0.0204

h = ones(7,7)/49

Digital Image Processing

0.0204
0.0204
0.0204
0.0204
0.0204
0.0204
0.0204

h = ones(30,30)/900

1284

Page 56

Lecture notes

Digital Image Processing by Jorma Kekalainen

Contrast enhancement
Contrast enhancement is best done by processing
the intensity component.
Suppose we start with the image forest.tif, which
is an indexed color image, and convert it to a
truecolour (RGB) image.
[x,map]=imread('forest.tif');
c=ind2rgb(x,map);

Now we have to convert from RGB to YIQ, so as


to be able to isolate the intensity component:
cn=rgb2ntsc(c);
Jorma Kekalainen

Digital Image Processing

1285

RGB YIQ

Jorma Kekalainen

Lecture weeks 23 and 24

Digital Image Processing

1286

Page 57

Lecture notes

Digital Image Processing by Jorma Kekalainen

Histogram equalization to the


intensity component
Now we apply histogram Intensity processing
equalization to the
intensity component, and
convert back to RGB for
display:
cn(:,:,1)=histeq(cn(:,:,1));
c2=ntsc2rgb(cn);
imshow(c2)

The result is shown beside.

Jorma Kekalainen

Whether this is an improvement is


debatable, but contrast has enhanced and
Digital Image Processing
the green color has nearly washed 1287
out

Intensity components before and


after histogram equalization

Jorma Kekalainen

Lecture weeks 23 and 24

Digital Image Processing

1288

Page 58

Lecture notes

Digital Image Processing by Jorma Kekalainen

imfinfo('forest.tif')

>> imfinfo('forest.tif')

ans =

Filename: [1x64 char]

FileModDate: '04-joulu-2000 14:57:58'

FileSize: 124888

Format: 'tif'

FormatVersion: []

Width: 447

Height: 301

BitDepth: 8

ColorType: 'indexed'

FormatSignature: [73 73 42 0]

ByteOrder: 'little-endian'

NewSubFileType: 0

BitsPerSample: 8

Compression: 'PackBits'

PhotometricInterpretation: 'RGB Palette'

StripOffsets: [1x17 double]

SamplesPerPixel: 1

RowsPerStrip: 18

StripByteCounts: [1x17 double]

XResolution: 72

YResolution: 72

ResolutionUnit: 'Inch'
Jorma Kekalainen Colormap: [256x3 double]
Digital Image Processing

1289

Example
But suppose we try to apply histogram equalization to each of
the RGB components:
cr=histeq(c(:,:,1));
cg=histeq(c(:,:,2));
cb=histeq(c(:,:,3));

Now we have to put them all back into a single 3 dimensional


array for use with imshow.
The cat function is what we want:
c3=cat(3,cr,cg,cb);
imshow(c3)

Note: cat function concatenates arrays along specified dimension


C = cat(dim, A1, A2, A3, A4, ...)
Digital Image Processing
1290
The Jorma
firstKekalainen
variable to cat is the dimension
along which we want our arrays to be joined.

Lecture weeks 23 and 24

Page 59

Lecture notes

Digital Image Processing by Jorma Kekalainen

Histogram equalization of a color


image
It seems that the green
color is totally washed
out.

Jorma Kekalainen

Using each RGB


component

Digital Image Processing

1291

Digital Image Processing

1292

Comparison

Jorma Kekalainen

Lecture weeks 23 and 24

Page 60

Lecture notes

Digital Image Processing by Jorma Kekalainen

Example: Histogram equalization

% Test with true color


c=imread('pep.jpg');
figure,imshow(c)
title('Original truecolor image ''pep.jpg''')
cn=rgb2ntsc(c);
figure, imshow(cn)
title('RGB image converted to YIQ image')
figure,imshow(cn(:,:,1))
title('Intensity component (Y=luminance)
before its histogram equalization')
% we apply histogram equalization to the
intensity component,
% and convert back to RGB for
cn(:,:,1)=histeq(cn(:,:,1));
figure,imshow(cn(:,:,1))
title('Intensity component (Y) after its
histogram equalization')
c2=ntsc2rgb(cn);
figure,imshow(c2)
title('Image after intensity component (Y)
equalization')

Jorma Kekalainen

title('Intensity component (Y) after its


histogram equalization')
c2=ntsc2rgb(cn);
figure,imshow(c2)
title('Image after intensity component (Y)
equalization')
% we try to apply histogram equalization to
each of the RGB components
cr=histeq(c(:,:,1));
cg=histeq(c(:,:,2));
cb=histeq(c(:,:,3));
% we have to put them all back into a single
3 dimensional array for use with imshow.
%The cat function is what we want
c3=cat(3,cr,cg,cb);
figure,imshow(c3)
title('Histogram equalization applied to all
RGB components')

Digital Image Processing

1293

Example
Original RGB image

Colors are considerably


washed out

Jorma Kekalainen

Lecture weeks 23 and 24

Digital Image Processing

Some false colors have


been introduced

1294

Page 61

Lecture notes

Digital Image Processing by Jorma Kekalainen

Spatial filtering
It depends on the filter as to which schema we use.
For a low pass filter, say a blurring filter, we can apply the
filter to each RGB component:
Low pass filtering
a15=fspecial('average',15);
cr=filter2(a15,c(:,:,1));
cg=filter2(a15,c(:,:,2));
cb=filter2(a15,c(:,:,3));
blur=cat(3,cr,cg,cb);
figure,imshow(blur)
The result is shown beside.
Jorma Kekalainen

Digital Image Processing

1295

Example

Jorma Kekalainen

Lecture weeks 23 and 24

Digital Image Processing

1296

Page 62

Lecture notes

Digital Image Processing by Jorma Kekalainen

High pass filtering


For a high pass filter, for
example an unsharp masking
filter, may sometimes be better
working with the intensity
component only:

cn=rgb2ntsc(c);
a=fspecial('unsharp');
cn(:,:,1)=filter2(a,cn(:,:,1));
cu=ntsc2rgb(cn);
imshow(cu)

The result is shown beside.


Jorma Kekalainen

Digital Image Processing

1297

Jorma Kekalainen

Digital Image Processing

1298

Lecture weeks 23 and 24

Page 63

Lecture notes

Digital Image Processing by Jorma Kekalainen

Jorma Kekalainen

Digital Image Processing

1299

Example

Jorma Kekalainen

Lecture weeks 23 and 24

Digital Image Processing

1300

Page 64

Lecture notes

Digital Image Processing by Jorma Kekalainen

Jorma Kekalainen

Digital Image Processing

1301

Conclusion: Spatial filtering of a


color image
In general, we will obtain reasonable results
using the intensity component only.
Although we can apply a filter to each of the
RGB components, as we did for the example
above, we cannot be guaranteed a good
result.
The problem is that any filter will change the
values of the pixels, and this may introduce
unwanted colors.
Jorma Kekalainen

Lecture weeks 23 and 24

Digital Image Processing

1302

Page 65

Lecture notes

Digital Image Processing by Jorma Kekalainen

Noise reduction
Now we use the image pears.png:
p=imread('pears.png');
We can add noise, and look at the noisy image, and its RGB
components:
pn=imnoise(p,'salt & pepper');
%About 5% of pixel corrupted (default value)
figure,imshow(pn)
figure,imshow(pn(:,:,1))
figure,imshow(pn(:,:,2))
figure,imshow(pn(:,:,3))
Jorma Kekalainen

Digital Image Processing

1303

Original and noisy image

Jorma Kekalainen

Lecture weeks 23 and 24

Digital Image Processing

1304

Page 66

Lecture notes

Digital Image Processing by Jorma Kekalainen

Noise on a color image


Salt & pepper
noise

Red
component

Green
component

Blue
component

Jorma Kekalainen

Digital Image Processing

1305

Denoising each RGB component


We can apply median filtering to each of the
RGB components:
prm=medfilt2(pn(:,:,1));
pgm=medfilt2(pn(:,:,2));
pbm=medfilt2(pn(:,:,3));
pm=cat(3,prm,pgm,pbm);
figure,imshow(pm)

The result is beside.


Jorma Kekalainen

Lecture weeks 23 and 24

Digital Image Processing

1306

Page 67

Lecture notes

Digital Image Processing by Jorma Kekalainen

Denoising Y only
Now we apply the median filter to the intensity component
only and we remove the noise from luminance (Y) :

pnn=rgb2ntsc(pn);
pnn(:,:,1)=medfilt2(pnn(:,:,1));
pm2=ntsc2rgb(pnn);
figure,imshow(pm2)

Noise has been clearly


diminished, but it is still
there. As we see from the
following slice.
Jorma Kekalainen

Digital Image Processing

1307

Jorma Kekalainen

Digital Image Processing

1308

Lecture weeks 23 and 24

Page 68

Lecture notes

Digital Image Processing by Jorma Kekalainen

Example: Noise reduction

p=imread('pears.png');
figure, imshow(tw)
title('Original image')
% Now we add noise, and look at the noisy image, and its RGB components
pn=imnoise(tw,'salt & pepper');
figure,imshow(pn)
title('Noisy image')
figure,imshow(pn(:,:,1))
title('Red component')
figure,imshow(pn(:,:,2))
title('Green component')
figure,imshow(pn(:,:,3))
title('Blue component')
% Applying median filtering to each of the RGB components
prm=medfilt2(pn(:,:,1));
pgm=medfilt2(pn(:,:,2));
pbm=medfilt2(pn(:,:,3));
pm=cat(3,prm,pgm,pbm);
figure,imshow(pm)
title('Median filtered each of the RGB components')
% If we remove the noise from Y only
pnn=rgb2ntsc(pn);
pnn(:,:,1)=medfilt2(pnn(:,:,1));
pm2=ntsc2rgb(pnn);
figure,imshow(pm2)
title('Median filtered luminance (Y) component only')

Jorma Kekalainen

Digital Image Processing

1309

Heavy noise

Jorma Kekalainen

Lecture weeks 23 and 24

Digital Image Processing

1310

Page 69

Lecture notes

Digital Image Processing by Jorma Kekalainen

Reduction of heavy noise heavy noise

Jorma Kekalainen

Digital Image Processing

1311

Example: Reduction of heavy noise

p=imread('pears.png');

figure, imshow(p)

title('Original image')

d=0.2;% Noise increased to d*100%

pn=imnoise(tw,'salt & pepper',d);

figure,imshow(tn)

title(['Noisy image (',num2str(d*100),')% from pixel corrupted'])

figure,imshow(pn(:,:,1))

title('Red component')

figure,imshow(pn(:,:,2))

title('Green component')

figure,imshow(pn(:,:,3))

title('Blue component')

% Apply median filtering to each of the RGB components

prm=medfilt2(pn(:,:,1));

pgm=medfilt2(pn(:,:,2));

pbm=medfilt2(pn(:,:,3));

pm=cat(3,prm,pgm,pbm);

figure,imshow(pm)

title('Median filtered each of the RGB components')

% We remove the noise from Y only

pnn=rgb2ntsc(pn);

pnn(:,:,1)=medfilt2(pnn(:,:,1));

pm2=ntsc2rgb(pnn);

figure,imshow(pm2)
Jorma
Kekalainen
Digital Image Processing

title('Median filtered luminance (Y) component only')

Lecture weeks 23 and 24

1312

Page 70

Lecture notes

Digital Image Processing by Jorma Kekalainen

Note
f=imread('flowers.tif');
figure,imshow(f)
title('Original image ''flowers.tif''')
% 20% of pixel corrupted by noise
fsp20=imnoise(f,'salt & pepper',0.2);% 20% of pixel corrupted by noise
figure, imshow(fsp20)
title('Image with 20% of its pixels corrupted by binary noise')
fsp20m3r=medfilt2(fsp20(:,:,1));% Red component
fsp20m3g=medfilt2(fsp20(:,:,2));
fsp20m3b=medfilt2(fsp20(:,:,1));
fsp20m3=cat(3,fsp20m3r, fsp20m3g, fsp20m3b);% all components back into
a single 3D array for use with imshow
figure, imshow(fsp20m3)
title('Image filtered by 3*3 median filter')
Jorma Kekalainen
Digital Image Processing
1313
xlabel('Image with 20% of its pixels
corrupted by binary noise')

Note
f=imread('flowers.tif');
figure,imshow(f)
title('Original image ''flowers.tif''')
% 20% of pixel corrupted by noise
fsp20=imnoise(f,'salt & pepper',0.2);% 20% of pixel corrupted by noise
figure, imshow(fsp20)
title('Image with 20% of its pixels corrupted by binary noise')
a3=fspecial('average');
fsp20a3r=filter2(a3,fsp20(:,:,1));%Red component
fsp20a3g=filter2(a3,fsp20(:,:,2));
fsp20a3b=filter2(a3,fsp20(:,:,3));
fsp20a3=cat(3,fsp20a3r, fsp20a3g, fsp20a3b);% all components back into a
single 3D array for use with imshow
Jorma Kekalainen

Lecture weeks 23 and 24

Digital Image Processing

1314

Continued on the following page

Page 71

Lecture notes

Digital Image Processing by Jorma Kekalainen

Note
figure,imshow(uint8(fsp20a3))
title('Image filtered by 3*3 average filter')
xlabel('Image with 20% of its pixels corrupted by binary noise')
%
fsp20m3r=medfilt2(fsp20(:,:,1));% Red component
fsp20m3g=medfilt2(fsp20(:,:,2));
fsp20m3b=medfilt2(fsp20(:,:,1));
fsp20m3=cat(3,fsp20m3r, fsp20m3g, fsp20m3b);% all
components back into a single 3D array for use with imshow
figure, imshow(fsp20m3)
title('Image filtered by 3*3 median filter')
xlabel('Image with 20% of its pixels corrupted by binary noise')
Jorma Kekalainen

Digital Image Processing

1315

Note

Jorma Kekalainen

Lecture weeks 23 and 24

Digital Image Processing

1316

Page 72

Lecture notes

Digital Image Processing by Jorma Kekalainen

Note

Jorma Kekalainen

Digital Image Processing

1317

Edge detection
An edge image will be a binary image
containing the edges of the input.
We can go about obtaining an edge image in
two ways:
1. we can take the intensity component only, and
apply the edge function to it,
2. we can apply the edge function to each of the
RGB components, and join the results.

Jorma Kekalainen

Lecture weeks 23 and 24

Digital Image Processing

1318

Page 73

Lecture notes

Digital Image Processing by Jorma Kekalainen

Applying the edge function to the


intensity component only
To implement the first
method, we start with the
rgb2gray function:

fe1: Edges after rgb2gray

fg=rgb2gray(f);
fe1=edge(fg);
figure,imshow(fe1)

Recall that edge with no


parameters implements
Sobel edge detection.
The result is shown beside.

Jorma Kekalainen

Digital Image Processing

1319

Applying the edge function to


each of the RGB components
For the second method, we can join the
results with the logical or:
f1=edge(f(:,:,1));
f2=edge(f(:,:,2));
f3=edge(f(:,:,3));
fe2=f1 | f2 | f3;
figure,imshow(fe2)

Result is shown beside.


Jorma Kekalainen

Lecture weeks 23 and 24

Digital Image Processing

1320

Page 74

Lecture notes

Digital Image Processing by Jorma Kekalainen

Example

% Edge detection (Sobel method)


% An edge image will be a binary image containing the edges of the input image.
% We can go about obtaining an edge image in two ways
% To implement the first method, we start with the rgb2gray function
f=imread('pep.jpg');
figure,imshow(f)
title('Original')
fg=rgb2gray(f);% First method: Take the intensity component only,
fe1=edge(fg);% and apply the edge function to it
figure,imshow(fe1)
title('Edge applied to the intensity component only taking first rgb2gray conversion')
% Note edge with no parameters implements Sobel edge detection
% Second method: Apply the edge function to each of the RGB components
f1=edge(f(:,:,1));
f2=edge(f(:,:,2));
f3=edge(f(:,:,3));
fe2=f1 | f2 | f3;% Join the results with the logical "or"
figure,imshow(fe2)
title('Edge function is applied to each of the RGB components, and joined the results')

Jorma Kekalainen

Digital Image Processing

1321

Comment
The edge image fe2 is a
much more complete
edge image than fe1.

Jorma Kekalainen

Lecture weeks 23 and 24

Digital Image Processing

1322

Page 75

Lecture notes

Digital Image Processing by Jorma Kekalainen

Example

% Edge detection (Canny method)


% An edge image will be a binary image containing the edges of the input image.
% We can go about obtaining an edge image in two ways
% To implement the first method, we start with the rgb2gray function
f=imread('pep.jpg');
figure,imshow(f)
title('Original')
fg=rgb2gray(f);% First method: Take the intensity component only,
fe1=edge(fg, 'canny');% and apply the edge function to it
figure,imshow(fe1)
title('Edge applied to the intensity component only taking first rgb2gray conversion')
% Note edge with no parameters implements Sobel edge detection
% Second method: Apply the edge function to each of the RGB components
f1=edge(f(:,:,1), 'canny');
f2=edge(f(:,:,2), 'canny');
f3=edge(f(:,:,3), 'canny');
fe2=f1 | f2 | f3;% Join the results with the logical "or"
figure,imshow(fe2)
title('Edge function is applied to each of the RGB components, and joined the results')

Jorma Kekalainen

Digital Image Processing

1323

As a reference: Canny method

Jorma Kekalainen

Lecture weeks 23 and 24

Digital Image Processing

1324

Page 76

Lecture notes

Digital Image Processing by Jorma Kekalainen

Exam Up to This
Effective Image File
Compression Methods

Lecture weeks 23 and 24

Page 77

You might also like