Professional Documents
Culture Documents
Experiment No. 1 Introduction To Image Processing Toolbox in MATLAB
Experiment No. 1 Introduction To Image Processing Toolbox in MATLAB
Experiment No. 1
Object:
The purpose of this experiment is to:
Gain familiarity with MATLAB’s Image Processing Toolbox.
Learn Image Processing Commands in MATLAB.
Introduction:
This experiment introduces the fundamentals of image processing using MATLAB and the Image
Processing Toolbox. It describes the types of images supported, and how MATLAB represents them. It also
explains the basics of working with image data and coordinate systems. It is very useful to go to
Help\MATLAB Help in the MATLAB window to additional information.
(1 -1)
University of Duhok Image Processing Lab
College of Engineering Class: Fourth Year
Electrical and Computer Department 2019-2020
2. The Command Window is used to both enter commands for MATLAB to execute, and to
view the results of these commands.
3. The Command History Window displays the commands that have been recently entered
into the Command Window.
4. The Current Directory tells the user which M-files are currently in use.
5. The Workspace Window displays which variables are currently being used and how big
they are.
If these three windows do not all appear as tabs below the window space, simply go to View and select the
ones you want to appear.
(2 -1)
University of Duhok Image Processing Lab
College of Engineering Class: Fourth Year
Electrical and Computer Department 2019-2020
6. Resaving: After writing code, you must save your work before you can run it. Save your
code by going icon Save.
7. Running Code: To run code, simply go to the main MATLAB window and type the name
of your M-file after the >> prompt. Other ways to run the M-file are to select icon Run as
shown in Figure 3, the result will appear in the command window.
(3 -1)
University of Duhok Image Processing Lab
College of Engineering Class: Fourth Year
Electrical and Computer Department 2019-2020
d. Procedure
In this lab we will introduces the fundamentals of image processing using MATLAB and the Image
Processing Toolbox. It describes the types of images supported, and how MATLAB represents them. It also
explains the basics of working with image data and coordinate systems. These are some MATLAB
commands:
1. Reading Image File: the following command can be used to read image file
myImage = imread (‘File name with path’)
If we have an image file with the name of test.bmp and if it is in the D:\ path, above commands can be
written as:
myImage = imread(‘D:\test.bmp’)
The image filename can be given as a full file path or as a file path relative to the MATLAB current directory.
The current directory can be changed from the main MATLAB interface window or by CD (change directory
command). The supported file formats include bmp, gif, jpg and png.
After giving above command image data is available in myImage variable and one can use any variable
name. We will take an example image (cameraman image) and read it, after running the M-file the read data
will appear as an array shown in Figure below:
BMP (Microsoft Windows Bitmap)
GIF (Graphics Interchange Format)
JPEG (Joint Photographic Experts Group)
PNG (Portable Network Graphics)
(4 -1)
University of Duhok Image Processing Lab
College of Engineering Class: Fourth Year
Electrical and Computer Department 2019-2020
2. Displaying Image: After reading image data using above function, one can display images in
MATLAB using imshow function.
imshow(<variable name>)
This function simply takes the array storing the image values as its only parameter. We will take the
same example above and write imshow(myImage) in the M-file and run it, the image(cameraman image)
will appear as shown in Figure below:
3. Knowing size of image in pixels: Size of the image in pixels can be found out by following
command
[Rows, Cols] = size(myImage) or size(myImage)
Taking the same image with writing the command in the M-file, the size of the image will appear in the
command window as shown Figure below:
(5 -1)
University of Duhok Image Processing Lab
College of Engineering Class: Fourth Year
Electrical and Computer Department 2019-2020
4. Whos Function: Displays in alphabetical order all variables in the currently active workspace, with
information about their sizes and types. It can display only the specified variables or display
variables in the specified location. It also can display all variables stored in the sample M-file,
durer.mat and this can be done by following command:
whos ('myImage')
But this command can do after reading the image data as shown in Figure below:
myImage = imread(' File name with path ');
whos ('myImage')
(6 -1)
University of Duhok Image Processing Lab
College of Engineering Class: Fourth Year
Electrical and Computer Department 2019-2020
(7 -1)
University of Duhok Image Processing Lab
College of Engineering Class: Fourth Year
Electrical and Computer Department 2019-2020
(8 -1)
University of Duhok Image Processing Lab
College of Engineering Class: Fourth Year
Electrical and Computer Department 2019-2020
6. Separate color image into three separate R,G,B planes: If we read given color image using
imread() function, we get 3-D matrix. To separate out R,G and B planes, we can read each plane
separately as follows:
myImage = imread(‘RGBBar.bmp’);
RED = myImage(:,:,1);
GREEN = myImage(:,:,2);
BLUE = myImage(:,:,3);
(9 -1)
University of Duhok Image Processing Lab
College of Engineering Class: Fourth Year
Electrical and Computer Department 2019-2020
(10 -1)
University of Duhok Image Processing Lab
College of Engineering Class: Fourth Year
Electrical and Computer Department 2019-2020
7. Converting Color image into Grayscale image: Color image can be converted into Grayscale
image by matlab function rgb2gray.
myGrayImage = rgb2gray(myImage)
(11 -1)
University of Duhok Image Processing Lab
College of Engineering Class: Fourth Year
Electrical and Computer Department 2019-2020
8. Converting Color image into Binary Image: Color image can be converted into Binary image
using function im2bw:
myBWImage = im2bw(myImage)
Where myImage is 2D image data
(12 -1)
University of Duhok Image Processing Lab
College of Engineering Class: Fourth Year
Electrical and Computer Department 2019-2020
9. Converting RGB image into an indexed Image: RGB image can be converted into an indexed
image using matlab function rgb2ind:
[X,map] = rgb2ind(RGB,tol);
10. Converting an indexed image into RGB Image: an indexed image can be converted into image
RGB using matlab function ind2rgb:
RGB = ind2rgb(X,map);
(13 -1)
University of Duhok Image Processing Lab
College of Engineering Class: Fourth Year
Electrical and Computer Department 2019-2020
11. Converting an indexed image into Grayscale Image: an indexed image can be converted into
Grayscale image by matlab function ind2gray.
myGrayImage = ind2gray(X,map);
(14 -1)
University of Duhok Image Processing Lab
College of Engineering Class: Fourth Year
Electrical and Computer Department 2019-2020
13. Saving two dimensional matrix in Image file: when one read image, image data is available in
two dimensional matrix. After made some process on the matrix, one need to save the processed
data in form of image. To save image data in image file, the following command isused.
imwrite(ImageData, 'Testout.bmp', 'bmp')
This command writes ImageData in file “Testout.bmp”. This command also supports ‘gif, ‘jpg’ and ‘png’
file formats.
(15 -1)