Professional Documents
Culture Documents
Page 1
Lecture notes
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
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
1176
Page 2
Lecture notes
Example
Jorma Kekalainen
1177
1178
Page 3
Lecture notes
Example
load flujet
figure,image(X)
title('Using jet colormap')
figure,image(X)
colormap(lines)
title('Using lines colormap')
Jorma Kekalainen
1179
1180
Page 4
Lecture notes
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
1181
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
1182
Page 5
Lecture notes
Jorma Kekalainen
1183
Jorma Kekalainen
1184
Page 6
Lecture notes
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
1186
Page 7
Lecture notes
Jorma Kekalainen
1187
1188
Page 8
Lecture notes
1189
Jorma Kekalainen
1190
Page 9
Lecture notes
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:
Jorma Kekalainen
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
1192
Page 10
Lecture notes
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
1193
Jorma Kekalainen
1194
Page 11
Lecture notes
1195
Jorma Kekalainen
1196
Page 12
Lecture notes
Jorma Kekalainen
1197
Jorma Kekalainen
1198
Page 13
Lecture notes
Jorma Kekalainen
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
1200
Page 14
Lecture notes
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
1201
Exercise
Show that given x, y, and Y, we can obtain the
tristimulus values X and Z:
Jorma Kekalainen
1202
Page 15
Lecture notes
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
Z z
=
Y y
Jorma Kekalainen
Z=
z
Y
y
z =1 x y
1 x y
Y
y
1203
Exercise
Show that from equations
x+y+z=1
1204
Page 16
Lecture notes
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
1205
Chromaticity diagram
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.
Page 17
Lecture notes
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.
Jorma Kekalainen
1208
Page 18
Lecture notes
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
1210
Page 19
Lecture notes
1211
Backsides
Jorma Kekalainen
R=0
G=0
B=0
1212
Page 20
Lecture notes
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.
Page 21
Lecture notes
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
1215
title('RGB gamut')
Jorma Kekalainen
1216
Page 22
Lecture notes
RGB gamut
Jorma Kekalainen
1217
Jorma Kekalainen
1218
Page 23
Lecture notes
Jorma Kekalainen
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
1220
Page 24
Lecture notes
1221
1222
Page 25
Lecture notes
1223
Jorma Kekalainen
1224
Page 26
Lecture notes
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')
1226
Page 27
Lecture notes
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
1227
1228
Page 28
Lecture notes
Hue
Red
Yellow
1/6
Green
1/3
Cyan
1/2
Blue
2/3
Magenta
5/6
Jorma Kekalainen
1229
Jorma Kekalainen
1230
Page 29
Lecture notes
1231
Example
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
1232
Page 30
Lecture notes
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
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
1234
Page 31
Lecture notes
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
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
1236
Page 32
Lecture notes
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
1238
Page 33
Lecture notes
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
Jorma Kekalainen
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
1240
Page 34
Lecture notes
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
>> rgb2ntsc([1,1,1])
ans =
1 0 0
>> rgb2ntsc([0.5,0.5,0.5])
ans =
1241
0.5000
0
0
Note
Jorma Kekalainen
>> 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
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
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
1244
Page 36
Lecture notes
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
1246
Page 37
Lecture notes
Examples
Jorma Kekalainen
1247
Example
Jorma Kekalainen
1248
Page 38
Lecture notes
1249
Jorma Kekalainen
1250
Page 39
Lecture notes
Jorma Kekalainen
1251
Jorma Kekalainen
1252
Page 40
Lecture notes
Red component
Jorma Kekalainen
Green component
Blue component
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
1254
Page 41
Lecture notes
RGB HSV
Hue
Saturation
Jorma Kekalainen
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
1256
Page 42
Lecture notes
YIQ components
Jorma Kekalainen
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
1258
Page 43
Lecture notes
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);
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
1260
Page 44
Lecture notes
Example
Jorma Kekalainen
1261
Page 45
Lecture notes
1263
Page 46
Lecture notes
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
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
1266
Page 47
Lecture notes
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
1268
Page 48
Lecture notes
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
1269
Color maps
Jorma Kekalainen
1270
Page 49
Lecture notes
Jorma Kekalainen
1271
% 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
1272
Page 50
Lecture notes
Jorma Kekalainen
1273
1274
Page 51
Lecture notes
1275
Jorma Kekalainen
1276
Page 52
Lecture notes
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
1277
Jorma Kekalainen
1278
Page 53
Lecture notes
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
1279
Page 54
Lecture notes
1281
Jorma Kekalainen
1282
Page 55
Lecture notes
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
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
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
0.0204
0.0204
0.0204
0.0204
0.0204
0.0204
0.0204
h = ones(30,30)/900
1284
Page 56
Lecture notes
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);
1285
RGB YIQ
Jorma Kekalainen
1286
Page 57
Lecture notes
Jorma Kekalainen
Jorma Kekalainen
1288
Page 58
Lecture notes
imfinfo('forest.tif')
>> imfinfo('forest.tif')
ans =
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'
SamplesPerPixel: 1
RowsPerStrip: 18
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));
Page 59
Lecture notes
Jorma Kekalainen
1291
1292
Comparison
Jorma Kekalainen
Page 60
Lecture notes
Jorma Kekalainen
1293
Example
Original RGB image
Jorma Kekalainen
1294
Page 61
Lecture notes
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
1295
Example
Jorma Kekalainen
1296
Page 62
Lecture notes
cn=rgb2ntsc(c);
a=fspecial('unsharp');
cn(:,:,1)=filter2(a,cn(:,:,1));
cu=ntsc2rgb(cn);
imshow(cu)
1297
Jorma Kekalainen
1298
Page 63
Lecture notes
Jorma Kekalainen
1299
Example
Jorma Kekalainen
1300
Page 64
Lecture notes
Jorma Kekalainen
1301
1302
Page 65
Lecture notes
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
1303
Jorma Kekalainen
1304
Page 66
Lecture notes
Red
component
Green
component
Blue
component
Jorma Kekalainen
1305
1306
Page 67
Lecture notes
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)
1307
Jorma Kekalainen
1308
Page 68
Lecture notes
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
1309
Heavy noise
Jorma Kekalainen
1310
Page 69
Lecture notes
Jorma Kekalainen
1311
p=imread('pears.png');
figure, imshow(p)
title('Original image')
figure,imshow(tn)
figure,imshow(pn(:,:,1))
title('Red component')
figure,imshow(pn(:,:,2))
title('Green component')
figure,imshow(pn(:,:,3))
title('Blue component')
prm=medfilt2(pn(:,:,1));
pgm=medfilt2(pn(:,:,2));
pbm=medfilt2(pn(:,:,3));
pm=cat(3,prm,pgm,pbm);
figure,imshow(pm)
pnn=rgb2ntsc(pn);
pnn(:,:,1)=medfilt2(pnn(:,:,1));
pm2=ntsc2rgb(pnn);
figure,imshow(pm2)
Jorma
Kekalainen
Digital Image Processing
1312
Page 70
Lecture notes
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
1314
Page 71
Lecture notes
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
1315
Note
Jorma Kekalainen
1316
Page 72
Lecture notes
Note
Jorma Kekalainen
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
1318
Page 73
Lecture notes
fg=rgb2gray(f);
fe1=edge(fg);
figure,imshow(fe1)
Jorma Kekalainen
1319
1320
Page 74
Lecture notes
Example
Jorma Kekalainen
1321
Comment
The edge image fe2 is a
much more complete
edge image than fe1.
Jorma Kekalainen
1322
Page 75
Lecture notes
Example
Jorma Kekalainen
1323
Jorma Kekalainen
1324
Page 76
Lecture notes
Exam Up to This
Effective Image File
Compression Methods
Page 77