Image Processing Toolbox (IPT) Functions
Image Processing Toolbox (IPT) Functions
(IPT) Functions
Write a program to display an indexed image
using a CLUT with a reduced number of colors.
clear; clc;
load trees;
%f=imread('trees.tif');
[Y, newmap] = imapprox(X, map, 8, 'dither');
[Z, newmap] = imapprox(X, map, 8, 'nodither');
a = subplot(131), image(X);
colormap(a, map); colorbar; axis square; title('original');
b = subplot(132), image(Y);
colormap(b, newmap); colorbar; axis square; title('with dither');
c = subplot(133), image(Z);
colormap(c, newmap); colorbar; axis square; title('without dither');
C:\Program Files\MATLAB\R2018a\toolbox\images\imdata\trees.tif
(1.69MB) (TIFF-Tagged Image File Format, TIF-Tagged Image Format
Double data type: 64-bit (8-byte)
clear; clc;
a = imread('peppers.png');
[image_1, map_1] = rgb2ind(a, 2, 'nodither');
[image_2, map_2] = rgb2ind(a, 5, 'nodither');
[image_3, map_3] = rgb2ind(a, 10, 'nodither');
subplot(221), imshow(a); title('original');
subplot(222), imshow(image_1, map_1); title('2 colors');
subplot(223), imshow(image_2, map_2); title('5 colors');
subplot(224), imshow(image_3, map_3); title('10 colors');
• A color RGB image can be converted to an indexed image. The pixel
values are replaced by index values of the CLUT, and the color table is
created in memory to reference those values. The CLUT contains as
many entries as there are colors in the image and each entry
contains the R, G, B components of the corresponding color.
• The index values and the actual colors they represent can be directly
viewed by using the Data Cursor tool of the figure window and
clicking on a point in the image.
Write a program to convert an indexed color image to RGB format by
using different colormaps and represent each colormap graphically.
clear; clc;
a = imread('peppers.png');
[b1, map1] = rgb2ind(a, 2, 'nodither');
[b2, map2] = rgb2ind(a, 5, 'nodither');
[b3, map3] = rgb2ind(a, 10, 'nodither');
p = ind2rgb(b3, map3);
q = ind2rgb(b3, map2);
r = ind2rgb(b3, map1);
subplot(231), imshow(p); title('image-3, map-3');
subplot(232), imshow(q); title('image-3, map-2');
subplot(233), imshow(r); title('image-3, map-1');
subplot(234), rgbplot(map3); title('map-3'); axis tight;
subplot(235), rgbplot(map2); title('map-2'); axis tight;
subplot(236), rgbplot(map1); title('map-1'); axis tight;
• The IPT function ind2rgb converts an indexed color image to RGB
format by reading the color values from the specifed colormaps and
substituting them back into the image.
• An indexed color image can be displayed using any of the predefned maps. The
number of colors for a specifc colormap, say winter, can be obtained using the
command: size(winter).
• The following example shows the same image being displayed using different
colormaps, which are specifed as arguments to the imshow function.
clear; clc;
I = imread('cameraman.tif');
[J, rect] = imcrop(I);
imshowpair(I, J, 'montage');
• Geometrical transformation is a family of image processing techniques, where
the pixel values of an image are usually not modified, rather the location,
orientation, and number of pixels of the original image are changed.
• To crop an image a rectangle needs to be specified smaller than the whole image
so that only the portion of the image within the rectangle will be displayed.
• Two parameters of the rectangle need to be specified viz. (in other words) start
point (upper-left corner) and size (number of rows and columns within it).
• The IPT function imcrop is used for the purpose. The crop rectangle can be
specified either interactively or parametrically. The example illustrates interactive
cropping.
Write a program to crop an image into four quadrants and display each
separately. Then re-assemble the image by swapping each quadrant with
its diagonally opposite quadrant.
clear; clc;
I = imread('cameraman.tif');
H = floor(size(I,1)); W = floor(size(I,2));
h = floor(H/2); w = floor(W/2); % dimensions of crop rectangle
% syntax: imcrop(I, [xmin, ymin, width, height])
Q1 = imcrop(I, [0, 0, w, h]); Q2 = imcrop(I, [w+1, 0, w, h]);
Q3 = imcrop(I, [0, h+1, w, h]); Q4 = imcrop(I, [w+1, h+1, w, h]);
figure, subplot(221), imshow(Q1); title('Q1'); subplot(222), imshow(Q2);
title('Q2');
subplot(223), imshow(Q3); title('Q3'); subplot(224), imshow(Q4); title('Q4');
J = [Q4 Q3 ; Q2 Q1]; figure, imshowpair(I, J, 'montage');
• The example illustrates parametric cropping.
• In the example, the FillValues option is used to fill the region outside the
image by a graylevel intensity between 0 and 255. The OutputView option
is used to specify whether the translated image will be viewed as cropped
or full.
clear; clc;
I = imread('saturn.png');
J = imrotate(I, 30, 'crop');
K = imrotate(I, -40, 'loose');
figure,
subplot(131), imshow(I); title('I');
subplot(132), imshow(J); title('J'); % crop option
subplot(133), imshow(K); title('K'); % loose option
• The IPT function imrotate is used to rotate an image by
specifying the angle in degrees.
• The crop option makes the output image size same as the
input image cropping the additional portions of the image,
while the loose option makes the image canvas large
enough to view the entire image.
x2 5 2 5 x1 5 x1 2 y1 5
y 2 5 5 y 2 x 5 y 5
2 1 1 1
1 0 0 1 1 1
Affine and Projective Transformations
• However if the above constraint is not applied i.e. the opposite sides
are not changed by equal amounts, then a rectangle is converted to
an arbitrary quadrilateral, and the transformation is referred to as
projective or perspective transform.
• A pixel P(x1,y1) when subjected to projective transformation has new
coordinates Q(x2,y2) given by the following,
where a, b, c, d, e, f, g, h are constants:
ax1 by1 c x2, 5 2 5 x1 5 x1 2 y1 5
x2 ,
y 2x 5 y 5
gx1 hy1 1 y
2 2 5 5 1 1 1
w 0.01 0.01 1 1 0.01x1 0.01 y1 1
,
dx1 ey1 f
y2 5 x1 2 y1 5 2 x1 5 y1 5
gx1 hy1 1 x2 ; y2
0.01x1 0.01 y1 1 0.01x1 0.01 y1 1
Write a program to apply affne transformation
and projective transformation to an image
clear; clc; C = checkerboard(10);
M = [5 2 5 ; 2 5 5 ; 0 0 1];
A = affine2d(M'); I = imwarp(C, A);
M = [5 2 5 ; 2 5 5 ; 0.01 0.01 1];
B = projective2d(M');
J = imwarp(C, B);
figure,
subplot(131), imshow(C); title('original');
subplot(132), imshow(I); title('affine transform');
subplot(133), imshow(J); title('perspective transform');
• The example illustrates the difference between an affine
transformation and a projective transformation.