iptdemos
help imdemos
doc imdemos
imshow
, which opens a separate window displaying the image. For instance:imshow('football.jpg');
imshow('coins.png');
imshow('autumn.tif');
imshow('board.tif');
imread
. For instance, to create an array I
that holds the information about an image called pout.tif
, we use the following syntax: I=imread('pout.tif');
imshow(I);
Command  Description 

I=imread('filename'); 
Reads a picture stored in 'filename' and stores it in I 
imshow(I) 
Displays the image stored in I in a separate window 
imtool(I) 
Displays the image. Allows you to look at pixel values in a region, etc. 
Don't panic. When we get to images, we are still working with matrices that can be accessed through indices. The upper lefthand corner of the image is indexed by (1,1). The general format of indexing is (row, col). Be careful here because you are used to thinking in x and y. The following diagram shows the indices to all four corners of a small image:
All the basic matrix operations that you have learned still apply. What you might be asking yourself is what is stored in an image matrix? Let's use a grayscale image ("pout.tif") as an example. This image is stored in a 291 x 240 matrix. That means there are 291 rows and 240 columns. Each element corresponds to a single pixel value.
In the case of "pout.tif", the values are between zero and 255. Zero is full black and 255 is white. Values between zero and 255 are shades of gray.
I
.imtool(I)
, you will start up the image tool. Note: The "Pixel info" at the bottom displays the value at (column, row). This is different from the regular subscripting syntax, which is in (row, column) format. 
The following table summarizes some ways to get information about an image. These are not specific to the Image Processing Toolbox.
Command  Description 

whos 
To get information about size, type, and bytes, of all variables. 
whos I 
For information about an image stored in I . 
size(I) 
To get the size of the image stored in I . 
class(I) 
To get type of data stored in I 
There can be different types of data stored in the image array (or matrix). The following table, modified from 23 of the Image Processing Toolbox User's Guide, is meant to summarize the different image types and values.
Image Type  Intensity Values/Ranges 

Binary 

Indexed (pseudocolor) 

Grayscale (intensity) 

Truecolor (RGB) 

Now that you know a little about grayscale and that there are different image types, let's introduce truecolor. With truecolor, we have to add another dimension. For instance, if you read image "autumn.tif" into I2 and view the details of I2 using:
I2=imread('autumn.tif'); whos I2
The image size is: 206x345x3
The extra dimension is the color component. For each pixel, we now have a representation of the intensity of red, green, and blue. In autumn.tif
, 255 represents the highest intensity, and 0 represents the lowest intensity. For instance, to make a pure red, red would be 255, green would be 0, and blue would be 0.
At pixel (93,180) in autumn.tif
,
The following diagram represents the three color components at pixel values. Often, they are represented as channels as is shown in red text in the diagram below. For instance, there is a red channel, a green channel and a blue channel.
If you only wanted to view the red channel, which of the following statements would you write?
What does the image look like?
The following table shows how to convert between the different formats given above. All these commands require the Image processing tool box!
Image format conversion (Within the parenthesis you type the name of the image you wish to convert.) 

Operation: 
Matlab command: 
Convert between intensity/indexed/RGB format to binary format. 
dither() 
Convert between intensity format to indexed format. 
gray2ind() 
Convert between indexed format to intensity format. 
ind2gray() 
Convert between indexed format to RGB format. 
ind2rgb() 
Convert a regular matrix to intensity format by scaling. 
mat2gray() 
Convert between RGB format to intensity format. 
rgb2gray() 
Convert between RGB format to indexed format.  rgb2ind() 
Convert image to binary image. 
im2bw() 
The command mat2gray is useful if you have a matrix representing an image but the values representing the gray scale range between, let's say, 0 and 1000. The command mat2gray automatically re scales all entries so that they fall within 0 and 255 (if you use the uint8 class) or 0 and 1 (if you use the double class).
The following images show the results of some basic format conversion on the RGB image "Bambi"( from the disney website).
Original image: img=imread('bambi.jpg')  graydeer=rgb2gray(img) 
binarydeer=im2bw(img)  transposeddeer=graydeer' 
Histograms are a way of visualizing the predominate intensities of an image. As a definition, image histograms are a count of the number of pixels that are at a certain intensity. When represented as a plot, the xaxis is the intensity value, and the yaxis is the number of pixels with that intensity value. The following are examples of histograms with predominately low, mid and high range intensities.
Which image would you expect to be darkest? lightest?
Notice how the xaxis is the intensity value from 0 to 256 (the images are uint8). The yaxis varies depending on the number of pixels in the image.
MATLAB easily displays image histograms using the function imhist(I). The above plots were created with the following syntax:
tire=imread('tire.tif') figure,imhist(tire) title('Histogram of tire.tifPredominately Low Intensity') pout=imread('pout.tif') figure,imhist(pout) title('Histogram of pout.tifPredominately Midrange Intensity') eight=imread('eight.tif') figure, imhist(eight) title('Histogram of eight.tifPredominately High Intensity')
The idea behind Histogram Equalization is that we try to evenly distribute the occurrence of pixel intensities so that the entire range of intensities is used more fully. We are trying to give each pixel intensity equal opportunity; thus, equalization. Especially for images with predominately low intensities, histograms will improve the contrast in the image.
In MATLAB, the function to perform Histogram Equalization is histeq(I).
The following provide some examples of using Histogram Equalization and the results on the images that have predominately low and mid range intensities:
Image  Histogram  

Original  
With Histogram Equalization 
The above diagrams were created using the following calls:
tire=imread('tire.tif'); imshow(tire) figure, imhist(tire) tire_eq=histeq(tire); figure, imshow(tire_eq) figure, imhist(tire_eq)
Image  Histogram  

Original  
With Histogram Equalization 
The above diagrams were created using the following calls:
pout=imread('pout.jpg'); imshow(pout); pout=rgb2gray(pout); figure, imhist(pout) pout_eq=histeq(pout); figure, imshow(pout_eq) figure, imhist(pout_eq) Note: imshow(image,[low, high]) also has the functioning of histogram stretch: all value that no larger than low will be shown as black; all pixel value that no less than high will be shown as white; the rest will be stretched to 0~255.
imshow(pout, [75, 160]) 
Sometimes, histogram equalization does not produce the contrast or results that we expect. Consider the following example, taken from page 86 of Digital Image Processing, Using MATLAB, by Rafael C. Gonzalez, Richard E. Woods, and Steven L. Eddins. The original image is courtesy of NASA.
Image  Histogram  

Original  
With Histogram Equalization 
The above images were created using the following MATLAB calls:
%double click on downloaded Fig0310(a)(MoonPhobos).tif moon=Fig03100x28a0x290x28MoonPhobos0x29; imshow(moon) figure, imhist(moon) moon_eq=histeq(moon); figure, imshow(moon_eq) figure, imhist(moon_eq)
You will notice that there are very few low intensity values in the histogram equalized image above. To fix this, we will use histogram matching (or specification).
To provide some background, histogram equalization tries to create an equal probability of each intensity occurring. This equal probability is represented by a horizontal line. If you wanted to emulate the results above, you could pass a horizontal line as a second argument to histeq. The following code can be used to yield results similar to the above histogram equalization
p(1:256)=1 g=histeq(moon,p); figure, imshow(g) figure, imhist(g)
With histogram matching, we provide a multimodal (multipeaked) Gaussian function as an argument to histeq. In contrast to the horizontal line of histogram equalization, the Gaussian function specifies that certain intensity ranges will have more probability of occurring than others. To speak intuitively, by passing a multimodal Gaussian function as a second argument to histeq, the modified image histogram will have peaks that approximately "match" the peaks of the Gaussian function. Note this approximate match in the the moon example below:
In order to create the above results, two Mfunctions from pages 86 to 88 of Digital Image Processing, Using MATLAB, by Rafael C. Gonzalez, Richard E. Woods, and Steven L. Eddins were used. The two functions are:
With the above two functions in the "Current Directory", the following calls were made to produce the results in the above table:
p=manualhist; %the following arguments were typed at the prompt % 0.15 0.05 0.75 0.05 1 0.1 0.002 g=histeq(moon, p); figure, imshow(g); figure, imhist(g);
At the end of this section, we are going to take a look at a function, written in MATLAB, that is able to apply all of the intensity transformations mentioned above. Before doing that, the following provides some background information about MFiles and functions.
In Lab 1, we briefly looked at MFiles. Specifically, we saw an mfile script. Remember:
.m
myScript.m
, to run it in MATLAB:
myScript.m
myScript
Scripts do not take input arguments or return anything as output. However, any variables that are created when the script is run remain in the workspace.
By contrast, functions can accept input arguments and return output. Functions are clearly identified with the keyword function
in the first line of code. Let's take a look at a sample function acosd
. The code below can be seen in MATLAB using the command:
type acosd
function y = acosd(x) %ACOSD Inverse cosine, result in degrees. % ACOSD(X) is the inverse cosine, expressed in degrees, % of the elements of X. % % Class support for input X: % float: double, single % % See also COSD, ACOS. % Copyright 19842004 The MathWorks, Inc. % $Revision: 1.1.6.3 $ $Date: 2004/06/25 18:51:38 $ if ~isreal(x) error('MATLAB:acosd:ComplexInput', 'Argument should be real.'); end y = 180/pi*acos(x);
A couple of things to note about this code are the following:
function
is usedacosd
. Therefore, the name of the MFile will be acosd.m
y
", is indicated in the function statement as y = acosd(x)
acosd
, "x
" is the only input argument.%
is ignored as a comment help
acosd
MATLAB has some special functions that can be used with regards to the input and output arguments of a function. The following table summarizes these functions:
Function  Description 

nargin 
returns the number of arguments input into the function 
nargout 
returns the number of arguments output from a function 
nargchk(low, high, number) 
returns " 
varargin 
accepts a variable number of arguments 
varargout 
returns a variable number of arguments 
We will discuss vargin
and varargout
a little more here. To write a function with a variable number of arguments, you can write:
function m=testFunction(varargin)
This means that you could have zero to as many arguments as you want for
testFunction
. The following code is meant to test this.
function m=testFunction(varargin) %TESTFUNCTION tests variable number of arguments % The following are valid calls to testFunction % a=testFunction % b=testFunction(val) % c=testFunction(val1,val2) % d=testFunction(val1,val2,...moreval) number=nargin; switch number case 0 m=0; case 1 m=1; case 2 m=2; otherwise m='more than two'; end
Notice that there are no return statements, instead, m
(the return variable) is assigned a value. For instance, if you have zero arguments, then m
will be set to 0; if you have one argument, then m
will be set to 1; if you have three or more arguments, then m
will be assigned a message of: 'more than two
'.
If you want to access any one of the arguments sent as a variable argument, you can use the notation varargin{#}
. For instance, varargin{1}
will return the first argument. The following function constructs m
as an array that contains all of the input arguments.
function m=testFunction2(varargin) %TESTFUNCTION2 displays all of arguments sent to the function % The following are valid calls to testFunction2 % a=testFunction % b=testFunction(val) % c=testFunction(val1,val2) % d=testFunction(val1,val2,moreval) number=nargin; % m=[]; %this goes with the commented line below if number>0 for i=1:number m(i)=varargin{i}; %add one more argument to m % m=[m varargin{i}]; %this way works as well end else m='no arguments'; end
This function uses if/else
and for
statements. Notice that end
is used to end these statements. The for
makes use of the colon operator; in each loop, i will have an incremented value from 1 to the number of arguments.
It is not necessary, to use varargin
only on its own. If you require one argument and then a variable number of arguments, you can write syntax like:
function m=testFunction3(x, varargin)
In this case, you will require at least one argument and it will be stored in x
.
The idea behind varargout
is not far from varargin
. However, first we will describe something that you might not be familiar with in other languagesMATLAB can return multiple arguments. To have a function with multiple returns, you can write:
function [m, n] = testFunction4(x)
This indicates that there are two returns to be stored in m
and n
.
You can also specify a variable number of arguments to be returned. The following code returns a variable number of arguments depending on the number of arguments specified when the function was called:
function [varargout] = testFunction5(x) %TESTFUNCTION5 tests how variable arguments work % testFunction5(x) does not return anything % m=testFunction5(x) returns one argument (1+x) % [m,n]=testFunction5(x) returns two arguments (1+x and 2+x) % [m,n,o]=testFunction5(x) returns three arguments (1+x, 2+x, and 3+x) % so on. number=nargout; if number>0 for i=1:number varargout{i}=i+x; end end
For instance, if you make a call like:
[m,n,o]=testFunction5(1)
The following will be returned:
m = 2 n = 3 o = 4
intrans
and changeclass
FunctionsThe following code (taken with permission from pages 73 and 74 of Digital Image Processing, Using MATLAB, by Rafael C. Gonzalez, Richard E. Woods, and Steven L. Eddins) is capable of doing all of the intensity transformations mentioned above.
function g = intrans(f, varargin) %INTRANS Performs intensity (graylevel) transformations. % G = INTRANS(F, 'neg') computes the negative of input image F % % G = INTRANS(F, 'log', C, CLASS) computes C*log(1 + F) and % multiplies the result by (positive) constant C. If the last two % parameters are omitted, C defaults to 1. Because the log is used % frequently to display Fourier spectra, parameter CLASS offers the % option to specify the class of the output as 'uint8' or % 'uint16'. If parameter CLASS is omitted, the output is of the % same class as the input. % % G = INTRANS(F, 'gamma', GAM) performs a gamma transformation on % the input image using parameter GAM (a required input). % % G = INTRANS(F, 'stretch', M, E) computes a contraststretching % transformation using the expression 1./(1 + (M./(F + eps)).^E). % Parameter M must be in the range [0, 1]. The default value for % M is mean2(im2double(F)), and the default value for E is 4. % % For the 'neg', 'gamma', and 'stretch' transformations, double % input images whose maximum value is greater than 1 are scaled % first using MAT2GRAY. Other images are converted to double first % using IM2DOUBLE. For the 'log' transformation, double images are % transformed without being scaled; other images are converted to % double first using IM2DOUBLE. % % The output is of the same class as the input, except if a % different class is specified for the 'log' option. % Verify the correct number of inputs. error(nargchk(2, 4, nargin)) % Store the class of the input for use later. classin = class(f); % If the input is of class double, and it is outside the range % [0, 1], and the specified transformation is not 'log', convert the % input to the range [0, 1]. if strcmp(class(f), 'double') & max(f(:)) > 1 ... ~strcmp(varargin{1}, 'log') f = mat2gray(f); else % Convert to double, regardless of class(f). f = im2double(f); end % Determine the type of transformation specified. method = varargin{1}; % Perform the intensity transformation specified. switch method case 'neg' g = imcomplement(f); case 'log' if length(varargin) == 1 c = 1; elseif length(varargin) == 2 c = varargin{2}; elseif length(varargin) == 3 c = varargin{2}; classin = varargin{3}; else error('Incorrect number of inputs for the log option.') end g = c*(log(1 + double(f))); case 'gamma' if length(varargin) < 2 error('Not enough inputs for the gamma option.') end gam = varargin{2}; g = imadjust(f, [ ], [ ], gam); case 'stretch' if length(varargin) == 1 % Use defaults. m = mean2(f); E = 4.0; elseif length(varargin) == 3 m = varargin{2}; E = varargin{3}; else error('Incorrect number of inputs for the stretch option.') end g = 1./(1 + (m./(f + eps)).^E); otherwise error('Unknown enhancement method.') end % Convert to the class of the input image. g = changeclass(classin, g);
The above code relies on a function called changeclass
, which is on pages 562 and 563 of the Digital Image Processing, Using MATLAB, by Rafael C. Gonzalez, Richard E. Woods, and Steven L. Eddins:
function image = changeclass(class, varargin) %CHANGECLASS changes the storage class of an image. % I2 = CHANGECLASS(CLASS, I); % RGB2 = CHANGECLASS(CLASS, RGB); % BW2 = CHANGECLASS(CLASS, BW); % X2 = CHANAGECLASS(CLASS, X, 'indexed'); % Copyright 19932002 The MathWorks, Inc % $Revision: 1.2 $ $Date:2003/02/19 22:09:58 $ switch class case 'uint8' image = im2uint8(varargin{:}); case 'uint16' image = im2uint16(varargin{:}); case 'double' image = im2double(varargin{:}); otherwise error('Unsupported IPT data class.'); end
The first part of the intrans
function describes how to use it. The following table provides some examples of using intrans
to correspond to the four Intensity Transformation Functions. Assume that I=imread('tire.tif');
Transformation  Intensity Transformation Function  Corresponding intrans Call 

photographic negative  neg=imcomplement(I); 
neg=intrans(I,'neg'); 
logarithmic  I2=im2double(I); 
log=intrans(I,'log',5); 
gamma  gamma=imadjust(I,[],[],0.4); 
gamma=intrans(I,'gamma',0.4); 
contraststretching  I2=im2double(I); 
contrast=intrans(I,'stretch',0.2,5); 
Create a MATLAB function called returnSubImage
.
Given an image and starting x and y locations, return a square subimage.
If the size of the subimage is not specified, then the default size is 100 x 100.
This function will take, in this order, three arguments and an optional fourth argument:
imageMatrix
(contains the image data already read from a file)startingRow
(yvalue to start subimagerepresents upper lefthand corner)startingCol
(xvalue to start subimagerepresents upper lefthand corner)Include error checking into your function. If the square is outside of the bounds of the original image, then produce an error message.You can use the size
function to find out the size of the image with which you are dealing.
To help you create the MFile you can use the MATLAB editor. Access it through File >New > MFile
When you have completed your code, try the following calls to returnSubImage
; the results should be consistent to what is described below:
% read in the image to get things started
lifting = imread('liftingbody.jpg');
imshow(lifting);
% get the subimage of the closer aircraft
lifting_sub1=returnSubImage(lifting,159,33,330);
figure, imshow(lifting_sub1)
% get the subimage of the second aircraft using the default 100x100 square
lifting_sub2=returnSubImage(lifting,110,360)
figure, imshow(lifting_sub2)
% test the error checking
lifting_sub=returnSubImage(lifting,159,33,500)