The Image Processing Toolbox at A Glance
The Image Processing Toolbox at A Glance
glance
What will we learn?
How do I read an image from a file using MATLAB?
What are the main data classes used for image
representation and how can I convert from one to
another?
Why is it important to understand the data class
and range of pixel values of images stored in
memory?
How do I display an image using MATLAB?
How can I explore the pixel contents of an image?
How do I save an image to a file using MATLAB?
By Oge Marques Copyright © 2011 by John Wiley & Sons, Inc. All rights reserved.
The Image Processing Toolbox (IPT)
IPT: A collection of functions (.m files most of the time inspectable )
that extend the basic capability of Matlab environment to
enable specialzed signal and image processing operations,
such as the following:
Spatial Transformation
IPT Help:
Image Analysis and Enhancement doc IPT
Neighborhood and Block Operations Functions
Linear Filtering and Filter Design Examples
By Oge Marques Copyright © 2011 by John Wiley & Sons, Inc. All rights reserved.
IPT’s Essential functions and features
imfinfo: Displays information about an image file
Without opening
Without storing its contents in workspace
Try: imfinfo('pout.tif‘)
By Oge Marques Copyright © 2011 by John Wiley & Sons, Inc. All rights reserved.
IPT’s Essential functions and features
imread: opens and reads the contents of an image file in most popular
formats (e.g., TIFF, JPEG, BMP, GIF, and PNG).
Before an image can be processed, it must first be loaded into memory.
Binary, intensity, and truecolor images can ALL be read with the imread( ).
When reading in an indexed image, we must specify variables for both
the image and its color map.
IPT also contains some functions for reading specialized image format like:
By Oge Marques Copyright © 2011 by John Wiley & Sons, Inc. All rights reserved.
IPT’s Essential functions and features
Data classes and data conversions
It’s imperative to understand how image contents are stored
in memory
Most common data classes for images:
uint8 – 1 byte per pixel, in the [0 , 255] range
double – 8 bytes per pixel, usually in the [0.0 , 1.0] range
logical – 1 byte per pixel either true(1) or false (0)
Once the contents of an image have been read and stored into
one or more variables, inspect the data class of these
variables and their range of values.
MATLAB allows data class conversion (typecasting) but this
type of conversion does not handle the range problem and is
usually not what you want to do.
By Oge Marques Copyright © 2011 by John Wiley & Sons, Inc. All rights reserved.
IPT’s Essential functions and features
Data classes and data conversions
IPT Functions to Perform Image Data Class Conversions
The input data class for these functions can be logical, uint8, uint16, int16, single, or double.
im2bw – data class conversion + global thresholding algo
im2frame – an image to movie frame conversion
frame2im – returns image data associated with image frame
movie – plays recorded movie frames
mat2gray – a matrix to gray-scale image
Example: imshow(mat2gray(rand(50,150)))
imshow(im2bw(mat2gray(rand(50,150))))
By Oge Marques Copyright © 2011 by John Wiley & Sons, Inc. All rights reserved.
Typecasting Examples
IntArray = uint8(array)
conversion consisted of truncation (all
negative values became zero) & rounding
off
By Oge Marques Copyright © 2011 by John Wiley & Sons, Inc. All rights reserved.
Typecasting Examples
There was no data class
convention (A already ‘double’)
I = mat2gray(A) but simply a range
conversion – the smallest
value (−8.0) became 0.0, the
largest value (4.0) became 1.0,
and all intermediate values were
scaled within the new range.
17/2412= 8.5
17/241 = 0.7083
(2/3 + 1/24 = 17/24)
0.0 1.0
By Oge Marques Copyright © 2011 by John Wiley & Sons, Inc. All rights reserved.
Typecasting Examples
bwI = im2bw(I, level) Any value greater than 0.4
becomes 1 (true), otherwise it
becomes 0 (false).
By Oge Marques Copyright © 2011 by John Wiley & Sons, Inc. All rights reserved.
IPT’s Essential functions and features
Displaying the contents of an image
imagesc: scales image data to the full range of the current colormap
(cmap=colormap;) and displays the image.
By Oge Marques Copyright © 2011 by John Wiley & Sons, Inc. All rights reserved.
IPT’s Essential functions and features
Displaying the contents of an image
Examples:
load mandrill;
image(X); colormap(map)
=======================================
load clown ;
subplot(121); image(X); colormap(gray);
subplot(122); imagesc(X), colormap(gray)
======================================= For midrange expansion
I = imread('pout.tif'); subplot(131); imshow(I); See: example_imagesc.m
I = imread('pout.tif'); imtool(I);
OR I=imread('peppers.png'); imtool(I)
By Oge Marques Copyright © 2011 by John Wiley & Sons, Inc. All rights reserved.
IPT’s Essential functions and features
Exploring the contents of an image
Imtool
By Oge Marques Copyright © 2011 by John Wiley & Sons, Inc. All rights reserved.
IPT’s Essential functions and features
Exploring the contents of an image
Often need of inspecting the image contents more closely
Usually done using imtool function
imtool provides:
Image display capabilities of imshow, e.g, I=imread('cameraman.tif');
imtool(I); figure; imtool(I,[0 80]);
other tools like
Pixel Region Tool
Image Information Tool
Adjust Contrast Tool
These tools can also be directly accessed using their library functions
impixelinfo, imageinfo, and imcontrast, respectively.
Examples: imshow('hestain.png'); impixelinfo;
imshow('pout.tif'); imdistline;
I = imread('pout.tif'); P = impixel(I) [Mouse click(s) to select and then press Enter]
RGB = imread(’peppers.png’); [c,r,p] = impixel(RGB);
Obsolete: imshow('hestain.png'); pixval on;
By Oge Marques Copyright © 2011 by John Wiley & Sons, Inc. All rights reserved.
IPT’s Essential functions and features
Exploring the contents of an image
More Examples:
For indexed image: [X,map]=imread('trees.tif');
imshow(X,map),impixelinfo;
By Oge Marques Copyright © 2011 by John Wiley & Sons, Inc. All rights reserved.
IPT’s Essential functions and features
Writing the Resulting Image onto a File
imwrite function is used to write the contents of an image in
one of the most popular graphic file formats.
If the output file format uses lossy compression (e.g., JPEG), imwrite allows
the specification of a quality parameter, used as a trade-off between the
resulting image’s subjective quality and the file size.
Example:
I = imread(’peppers.png’);
imwrite(I, ’pep75.jpg’); % Default 75.
imwrite(I, ’pep05.jpg’, ’quality’, 5); % Poor Quality, Small Size
imwrite(I, ’pep95.jpg’, ’quality’, 95); % Better Quality, Large Size
By Oge Marques Copyright © 2011 by John Wiley & Sons, Inc. All rights reserved.
IPT’s Essential functions and features
Intensity Values along a line (or multiline path)
improfile function is used to compute and plot the inensity
values along a line or a multiline path in an image.
Example:
r1 = 17; c1 = 17; r2 = 201; c2 = 201;
I = imread('coins.png'); imshow(I);
line([c1, c2], [r1, r2], 'Color', 'g', 'LineWidth', 2);
figure(2);
improfile(I, [c1, c2], [r1, r2]); grid;
ylabel('Gray Level');
By Oge Marques Copyright © 2011 by John Wiley & Sons, Inc. All rights reserved.
IPT’s Essential functions and features
Displaying Multiple Images Within One Figure Window
subplot function is used to display multiple images within one
figure window.
The Syntax is:
subplot(m,n,p);
One associated problem
is that images with different
colormaps cannot be properly
displayed.
The Solution is subimage.
By Oge Marques Copyright © 2011 by John Wiley & Sons, Inc. All rights reserved.
IPT’s Essential functions and features
Displaying Multiple Images Within One Figure Window
Subimage function converts the image to an equivalent RGB
image and then displays that image.
We can easily do this ourselves, but there is no direct conversion
from intensity to RGB, so we must first convert from intensity to
indexed, and then from indexed to RGB (gray2ind ind2rgb).
Using
subplot()
Using
subimage()
By Oge Marques Copyright © 2011 by John Wiley & Sons, Inc. All rights reserved.