Introduction To Digital Image Processing by Using Matlab: Objectives
Introduction To Digital Image Processing by Using Matlab: Objectives
Introduction To Digital Image Processing by Using Matlab: Objectives
EQUIPMENT / REQUIREMENT:
Hardware Requirement
Personal computer.
Software Requirement
MATLAB.
PROCEDURE:
LAB ACTIVITY
To get started, click on the icon from the desktop or choose from the bottom menu:
1. Start
2. Programs
3. MATLAB
4. R2009a
5. MATLAB R2009a
LAB TASK:
READING AN IMAGE
Images can be read into Matlab environment using the function imread.
>>Img = imread(‘path\filename.ext’)
The following table shows the commonly used file formats supported by Matlab
Page 1-1
TE312 Lab # 1 Digital Image Processing
IMAGE SIZE
Function size can be used to determine the dimensions of an image.
>> [M N] = size(Img)
OR
>> [M N B] = size(Img)
Where B gives the number of channels in the image. (3 for colored image and 1 for gray
image)
The function whos gives additional information about the array.
DISPLAYING AN IMAGE
>> imshow(Img)
To display another image using imshow while keeping the first image, use:
Page 1-2
TE312 Lab # 1 Digital Image Processing
Move the mouse over the image and you will get the pixel coordinates and the
respective value at the bottom of the window as indicated in the following figure.
You may also use the command impixelregion to see the pixel values in an image
interactively:
>> figure, imshow(Img)
>> impixelregion
WRITING AN IMAGE
Images are written to disk using the function imwrite which has the following basic
syntax:
>> imwrite (Img, ‘filename’)
Page 1-3
TE312 Lab # 1 Digital Image Processing
The imwrite function can have additional parameters depending upon the file format
selected. E.g.
You may use this function to get the size of the file as:
>> K = imfinfo(‘rice.png’)
>> ImageSize = K.FileSize.
DATA CLASSES
Some important data classes in Matlab are:
Name Description
Double Double precision floating point numbers 8 bytes per element
uint8 Unsigned 8 bit integers [0 255]
uint16 Unsigned 16 bit integers [0 65535]
uint32 Unsigned 32 bit integers
int8 Signed 8 bit integers [-128 127]
int16 Signed 16 bit integers [-32768 32767]
int32 Signed 32 bit integers
Single Single precision floating point numbers 4 bytes per element
Char Characters (2 bytes)
Logical Values 0 or 1
Page 1-4
TE312 Lab # 1 Digital Image Processing
Dealing with images, you will mostly encounter uint8 and logical data types.
IMAGE TYPES
The Image Processing Toolbox supports four types of images:
• Intensity images
• Binary images
• RGB images
• Indexed images
Intensity Images
Pixel values represent image intensities.
Class uint8 [0 255]
Class double [0 1]
Binary Images
Binary images are logical arrays of 0s and 1s. An array of type uint8 having values 0
and 1 is NOT considered a binary image. An array can be converted to binary using:
>> A =
5 0 1
2 0 3
Page 1-5
TE312 Lab # 1 Digital Image Processing
f = [ -0.5 0.5
0.75 1.5 ]
g = [0 128
191 255 ]
The conversion of an arbitrary array of class double to an array of class double scaled
to the range [0 1] can be done via function mat2gray.
>>g = mat2gray(A)
The output values are in the range 0 (black) to 1 (white).
Finally, for conversion between an intensity image and a binary image, the function
im2bw can be used:
>> g=im2bw(f,T)
Produces a binary image g from the intensity image f by thresholding (will be covered
in detail in the lectures). The output image g has a value 0 for all pixels in the input
image with a value less than T, and 1 for all other pixels. The value of T has to be in
the range [0 1] regardless of the type of input image f. (If the input image is of type
uint8, im2bw will first divide all pixels by 255 and then apply the threshold).
Page1-6
TE312 Lab # 1 Digital Image Processing
LAB EXERCISE:
1. Load the image ‘cameraman_gray.tif’, find its dimensions and number of channels
and display it.
Page1-7