Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

University of Duhok Image Processing Lab

College of Engineering Class: Fourth Year


Electrical and Computer Department 2019-2020

Experiment No. 1

Introduction to Image processing


Toolbox in MATLAB

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.

a. Opening MATLAB in the computer lab.


Double click on MATLAB icon on desktop or open through C:\MATLAB\bin\win64\matlab.exe.
b. MATLAB Windows
1. When MATLAB opens, the screen should look in Figure 1, below

Figure1: MATLAB Window

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

c. The M-file (Matlab-file)


1. M-file: An M-file is a MATLAB document the user creates to store the code (program)
they write for their specific application. Creating an M-file is highly recommended,
although not entirely necessary. An M-file is useful because it saves the code the user has
written for their application. It can be manipulated and tested until it meets the user’s
specifications. The advantage of using an M-file is that the user, after modifying their code,
must only tell MATLAB to run the M-file, rather than reenter each line of code individually.
2. Creating an M-file: To create an M-file, select New ►Script or a direct icon New Script.
3. Saving: The next step is to save the newly created M-file. In the M-file window, select icon
Save►Save As… Choose a location that suits your needs, such as the hard drive. It is not
recommended that you work from your CD or from the USB drive, so before editing and
testing your M-file you may want to move your file to the hard drive.
4. Opening an M-file: Open the M-file by going to icon named Open…, and selecting your
file.
5. Writing Code: After creating and saving your M-file, the next step is to begin writing code.
A suggested first move is to begin by writing comments at the top of the M-file with a
description of what the code is for. Comments are declared by placing a % symbol before
them.
Comments appear in green in the M-file window as shown in Figure 2

(2 -1)
University of Duhok Image Processing Lab
College of Engineering Class: Fourth Year
Electrical and Computer Department 2019-2020

Figure 2: Example of M-file

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.

Figure 3: Running the M-file

(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

Figure 4: Read an Image

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:

Figure 5: Display Image

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

Figure 6: Size of the Image

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

Figure 8: Display Image Information

(7 -1)
University of Duhok Image Processing Lab
College of Engineering Class: Fourth Year
Electrical and Computer Department 2019-2020

5. Image resizing: Image resizing can be done by following command:


X=imresize(Image,{Parameters});
Consider that we read the image in variable myImage using imread function, and then we can resize the
image stored in this variable by following command
M_new=imresize(myImage,[128,128],’nearest’)
This command will convert image of any size into image of 128x128 using nearest neighbor technique
shown in Figure below:

Figure 9: Image resizing

(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

RED Plane Image GREEN Plane Image

BLUE Plane Image RGB Plane Image

Figure 10: Separate color image into R,G,B planes

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

Color image Grayscale image


Figure 11: Color Image to Gray Image

(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

Figure 12: Color Image into Binary Image

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

Figure 13: RGB into Indexed Image

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

Figure 14: Indexed into RGB Image

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

Figure 15: Indexed into Grayscale Image


12. Converting Grayscale image into an indexed image: Grayscale image can be converted into an
indexed image by matlab function ind2gray.
[X, map] = gray2ind(myGrayImage,tol)

Figure 16: Grayscale into Indexed Image

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

Figure 17: Write an Image Data

(15 -1)

You might also like