Dip 03
Dip 03
Experiment 3
Introduction to Digital Image Processing with MATLAB
To import an image from any supported graphics image file format, in any of the supported bit
depths, use the imread function.
Syntax
A = imread(filename.fmt)
Description
A = imread(filename,fmt) reads a greyscale or color image from the file specified by the string
filename, where the string fmt specifies the format of the file. If the file is not in the current directory
or in a directory in the MATLAB path, specify the full pathname of the location on your system.
Example 1 :
% Read a sample image.
A = imread('ngc6543a.jpg');
% Display the image.
image(A)
2. Displaying Multiple Images in the Same Figure
You can use the imshow function with the MATLAB subplot function or the MATLAB subimage
function to display multiple images in a single figure window.
Dividing a Figure Window into Multiple Display Regions.
subplot divides a figure into multiple display regions. The syntax of subplot is
subplot(m,n,p)
This syntax divides the figure into an m-by-n matrix of display regions and makes the pth display
region active.
Note When you use subplot to display multiple color images in one figure window, the images must
share the colormap of the last image displayed. In some cases, as illustrated by the following example,
the display results can be unacceptable. As an alternative, you can use the subimage function,
For example, you can use this syntax to display two images side by side.
X1=imread('forest.tif');
[X2,map2]=imread('trees.tif');
subplot(1,2,1),imshow(X1);
subplot(1,2,2),imshow(X2);
In the figure, note how the first image displayed, X1, appears dark after the second image is displayed.
Two Images in Same Figure Using the Same Colormap
Example 2:
a=imread('pout.tif');
imwrite(a,gray(256),'b.bmp');%write image image data of a to image b.bmp
imshow('b.bmp') % imshow is used to display image
r=
291
c=
240
4. Image Display
imshow(f,g) % f is an image array & g is no of intensity levels used to display it
imshow('pout.tif') % here by default intensity level is 256
pixval % displays the Euclidean distance between the initial and current cursor conditions
imshow(f,[low high])
Displays as black all values less than or equal to low and as white all values greater than or equal to
high
Example::
I = imread('cameraman.tif');
h = imshow(I,[0 80]);
5. Accessing the Pixel data
There is a one-to-one correspondence between pixel coordinates and the coordinates MATLAB® uses
for matrix subscripting. This correspondence makes the relationship between an image's data matrix
and the way the image is displayed easy to understand. For example, the data for the pixel in the fifth
row, second column is stored in the matrix element (5,2). You use normal MATLAB matrix
subscripting to access values of individual pixels.
For example, the MATLAB code
A(2,15) %returns the value of the pixel at row 2, column 15 of the image A.
Lab tasks: