0% found this document useful (0 votes)
77 views

Image Processing Toolbox (IPT) Functions

The document discusses various image processing functions in MATLAB's Image Processing Toolbox (IPT) for displaying and manipulating images. It provides code examples for: 1. Displaying an indexed image with a reduced color lookup table (CLUT) using imapprox. 2. Converting an RGB image to indexed format with varying numbers of colors using rgb2ind. 3. Converting an indexed image back to RGB format using different CLUTs with ind2rgb. 4. Displaying images using built-in colormaps or by cropping/warping images onto non-planar surfaces.

Uploaded by

pham tam
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
77 views

Image Processing Toolbox (IPT) Functions

The document discusses various image processing functions in MATLAB's Image Processing Toolbox (IPT) for displaying and manipulating images. It provides code examples for: 1. Displaying an indexed image with a reduced color lookup table (CLUT) using imapprox. 2. Converting an RGB image to indexed format with varying numbers of colors using rgb2ind. 3. Converting an indexed image back to RGB format using different CLUTs with ind2rgb. 4. Displaying images using built-in colormaps or by cropping/warping images onto non-planar surfaces.

Uploaded by

pham tam
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 50

Image Processing Toolbox

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

• An indexed image is a color image, where the pixel values do not


contain the actual color information but only index numbers. The
index numbers are converted to color values by looking up a
colormap or a table in memory with a set of rows and four columns,
called the color look up table (CLUT).
• The function imapprox is used to display an approximate version of
the image with reduced number of colors.
• Dithering can be used to improve the quality of image by simulating
colors not present in the new map
Write a program to convert an RGB image to indexed format and
display using varying number of colors

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 IPT function rgb2ind is used to convert an RGB image to indexed


format and create the corresponding colormap.

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

• The BM function rgbplot is used to represent the colormaps


graphically
Write a program to display an indexed
color image using various inbuilt colormaps
clear; clc;
rgb = imread('football.jpg');
[g, map] = rgb2ind(rgb, 64);
subplot(341), imshow(g, map); title('original');
subplot(342), imshow(g, hot); title('hot');
subplot(343), imshow(g, bone); title('bone');
subplot(344), imshow(g, copper); title('copper');
subplot(345), imshow(g, pink); title('pink');
subplot(346), imshow(g, flag); title('flag');
subplot(347), imshow(g, jet); title('jet');
subplot(348), imshow(g, prism); title('prism');
subplot(349), imshow(g, autumn); title('autumn');
subplot(3,4,10), imshow(g, winter); title('winter');
subplot(3,4,11), imshow(g, summer); title('summer');
subplot(3,4,12), imshow(g, cool); title('cool');
• MATLAB has a set of 18 inbuilt predefned colormaps: parula (default), jet, hsv,
hot, cool, spring, summer, autumn, winter, gray, bone, copper, pink, lines,
colorcube, prism, fag, and white. Each of these maps consist of 64 colors.

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

• A colormap can also be invoked by using its name as a function name


Write a program to display multiple images in
a rectangular grid
clear; clc;
% part 1
f = {'coins.png',
'circles.png',
'circlesBrightDark.png',
'coloredChips.png',
'eight.tif',
'pears.png'};
montage(f, 'Size', [2 3]); % cell array, 2 rows 3 columns
% part 2
a = imread('football.jpg'); b = imread('flamingos.jpg');
c = imread('fabric.png'); d = imread('foggysf1.jpg');
figure; montage({a,b,c,d}, 'Size', [1 4]); % character vector, 1 row 4 column
• The IPT function montage can be used to display multiple images
together in a rectangular grid. The size of the grid can be specifed by
including the number of rows and number of columns.

• A cell array is used to specify the filenames of the images. If the


images have been read and stored in variable names, then these can
be included in a character vector.

• In the following example, the first six images are displayed in a 2 × 3


grid and the next four images are displayed in a 1 × 4 grid
Write a program to display multiple images
in a temporal sequence one after another
clear; clc;
load cellsequence;
fps = 10;
implay(cellsequence, fps);
load mristack;
implay(mristack);
• Multiple images can also be displayed one after another along a
temporal timeline. The IPT function implay is used to display a
sequence of images one after another at a specifed frame rate, by
invoking the Movie Player. The default frame rate is 20 fps.
• The BM function load copies variables from a specifed fle into
workspace.
clear; clc;
f = {'coins.png',
'circles.png',
'circlesBrightDark.png',
'coloredChips.png',
'eight.tif',
'pears.png',
'football.jpg',
'flamingos.jpg',
'fabric.png',
'foggysf1.jpg'};
fps = 5;
for i=1:length(f)
t=imread(f{i});
implay(t, fps);
end
Write a program to display images on a non-
planar surface
clear; clc; load trees;
[x, y] = meshgrid(-100:100, -100:100);
z1 = -(x.^3 + y.^3); z2 = -(x.^2 + y.^4); z3 = -(x.^2 + y.^2);
figure,
subplot(221), imshow(X, map);
subplot(222), warp(x, y, z1, X, map); axis square;
view(-20, 30); grid; xlabel('x'); ylabel('y'); zlabel('z');
subplot(223), warp(x, y, z2, X, map); axis square;
view(-50, 30); grid; xlabel('x'); ylabel('y'); zlabel('z');
subplot(224), warp(x, y, z3, X, map); axis square;
view(-50, 30); grid; xlabel('x'); ylabel('y'); zlabel('z');
• Apart from displaying images simply as a 2-D rectangular plane, images
can also be displayed on a non-planar 3-D surface, which is referred to as
image warping.
• The IPT function warp is used to map an image as a texture on to a
graphical surface.
• A rectangular grid of equally spaced points are created using the BM
function meshgrid and for each point on the meshgrid, non-planar
functions are used to create a vertical elevation along the z-axis. The
image is then mapped to the surface created.
• Three surfaces are created, one using the function f(x, y) = -(x3+ y3), f(x,
y) = -(x2+ y2) and the other using the function f(x, y) = -(x2+y4). The
BM function view creates a viewpoint specification by specifying the
azimuth and elevation
Geometric Transformation and Image Registration
Common Geometric Transformations

Write a program to crop an image by specifying the crop rectangle


interactively

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.

• Cropping is one of the simplest image processing operations and involves


displaying a small portion of the whole image i.e. a subset of the entire pixel set.

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

• Here, the cropping rectangle is specified in terms of four parameters:


coordinates of the top-left corner of the rectangle
(xmin, ymin), its width and its height (width, height) in that order.

• In the example, an image is cropped into four quadrants which are


then displayed in a different order i.e. each quadrant is replaced by its
diagonally opposite quadrant.

• To avoid fractional number of pixels in each quadrant, the BM


function foor is used to round off to the nearest integer less than or
equal to the specified value
Write a program to translate an image viewing
the result as a full view and a cropped view
clear; clc;
I = imread('coins.png');
J = imtranslate(I, [25, -20], 'FillValues', 200, 'OutputView',
'full');
K = imtranslate(I, [-50.5, 60.6], 'FillValues', 0, 'OutputView',
'same');
subplot(131), imshow(I); title('I'); axis on;
subplot(132), imshow(J); title('J'); axis on; % full view
subplot(133), imshow(K); title('K'); axis on; % cropped view
• The IPT function imtranslate is used to translate an image by specifying a
translation vector (tx, ty) i.e. x and y values by which the image should be
moved.

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

• In case of a full view, the image canvas area is increased to accommodate


the image and the extra portion by which the image has moved.
• In case of a cropped view, the image canvas remains at its original size so
that the cropped portion of the image is viewed along with the extra
portion by which the image has moved.
• In the example, a translated image is viewed using the full and cropped
option
Write a program to rotate an image by +30 and
-40 degrees specifying the 'crop' and 'loose' options.

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.

• The example displays a rotated image using the crop


and loose options
'crop' Make output image J the same size as the input image I, cropping the rotated image to fit.
'loose' Make output image J large enough to contain the entire rotated image. J is larger than I.
Write a program to scale an image uniformly
and non-uniformly
clear; clc; I = imread('testpat1.png');
h = size(I, 1); w = size(I, 2);
J = imresize(I, 0.5);
K = imresize(I, [h/2, 1.5*w]);
L = imresize(I, [500, 300]);
M = imresize(I, [100, NaN]);
subplot(151), imshow(I); title('I'); axis on;
subplot(152), imshow(J); title('J'); axis on;
subplot(153), imshow(K); title('K'); axis on;
subplot(154), imshow(L); title('L'); axis on;
subplot(155), imshow(M); title('M'); axis on;
• The IPT function imresize is used to resize the image dimensions with
one or two following arguments indicating the scaling factors.
• The example illustrates the various ways in which scaling parameters
can be specified. If there is a single argument, it indicates the scaling
factor by which both the height and width are to be scaled.
• If there are two arguments, it indicates the number of rows and
number of columns, respectively.
• If one of the arguments is a number and the other argument is set as
NaN (not a number), then both the dimensions are set to the single
value
Write a program to illustrates the three types of
refection of an image
clear; clc;
I = imread('peppers.png');
J = fliplr(I); % reflection about Y-axis, Flip array left to right
• K = flipud(I); % reflection about X-axis, Flip array up to down
L = flipud(J); % reflection about origin
figure,
subplot(221), imshow(I); title('original');
subplot(222), imshow(J); title('reflected about Y-axis');
subplot(223), imshow(K); title('reflected about X-axis');
subplot(224), imshow(L); title('reflected about origin');
• The BM function fiplr is used to fip an image left to right which is
equivalent to a y-axis refection,

• while the BM function fipud fips an image up to down which is


equivalent to an x-axis refection. The example illustrates the three
types of refection
Write a program to transform images using transformation matrices.
clear; clc; C = imread('peppers.png');
sx = 3; sy = 1.5; cost = 0.7; sint = 0.7; hx = 1.5; hy = 3;
S = [sx 0 0 ; 0 sy 0 ; 0 0 1]; % scaling
R = [cost -sint 0 ; sint cost 0 ; 0 0 1]; % rotation
H = [1 hx 0 ; 0 1 0 ; 0 0 1]; % shear
F = [-1 0 0 ; 0 -1 0 ; 0 0 1]; % reflection
M1 = F*R; % rotation & reflection
M2 = R*S; % scaling & rotation
M3 = H; % shear
A1 = affine2d(M1'); A2 = affine2d(M2'); A3 = affine2d(M3');
I1 = imwarp(C, A1); I2 = imwarp(C, A2); I3 = imwarp(C, A3);
subplot(221),imshow(C); axis on; title('original');
subplot(222),imshow(I1); axis on; title('after rotation & reflection');
subplot(223),imshow(I2); axis on; title('after rotation & scaling');
subplot(224),imshow(I3); axis on; title('after shear');
• Generic transformations can be applied using a specified
transformation matrix which can be a single operation or a composite
of several operations.
• A composite transformation involving a combination of translation,
rotation, scaling, refection, and shear is referred to as affine
transformation.
• The example uses shear, scale, rotation, and refection in various
combinations and uses the corresponding transformation matrices to
display the output result.
• The IPT function affine2d creates a affine transformation object using
a mapping between the initial and final position of a pixel given a
specified transformation matrix,
• while the IPT function imwarp applies the transformation object to a
specified image
Affine and Projective Transformations
A pixel P(x1,y1) when subjected to affine transformation has
new coordinates Q(x2,y2) given by the following, where a, b, c,
d, e, f are constants:
x2 = ax1 + by1 + c
y2 = dx1 + ey1 + f

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

• The IPT function projective2d creates a projective


transformation object using a mapping between the initial
and final position of a pixel given a specified transformation
matrix M,

• while the IPT function imwarp applies the transformation


object to a specified image

You might also like