Introduction To Digital Image Processing by Using Matlab: Objectives

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

TE312 Lab # 1 Digital Image Processing

INTRODUCTION TO DIGITAL IMAGE PROCESSING BY


USING MATLAB
OBJECTIVES:

1) To learn how to read, write and display image by using MATLAB.

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

You may use the following image to test the function:

>> Img = imread(‘rice.jpg’)

The following table shows the commonly used file formats supported by Matlab

Image Format Description Recognized Extensions


TIFF Tagged Image File Format tif, tiff
JPEG Joint Photographic Experts Group jpeg, jpg
BMP Windows Bitmap bmp
GIF Graphics Interchange Format gif
PNG Portable Network Graphics png

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.

>> whos Img

Name Size Bytes Class


Img 256x256 65536 uint8

DISPLAYING AN IMAGE

Images can be displayed using the function imshow.

>> imshow(Img)

To display another image using imshow while keeping the first image, use:

>> imshow(Img), figure, imshow(Img2)


To see the pixel values in an image, use:
>> Figure, imshow(Img)
>> impixelinfo

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

>> imwrite (Img, ‘testImage.tif’)

Page 1-3
TE312 Lab # 1 Digital Image Processing

The imwrite function can have additional parameters depending upon the file format
selected. E.g.

>> imwrite (Img, ‘filename.jpg’, ‘quality’, q)


Where q is an integer between 0 and 100.

GETTING INFORMATION ABOUT IMAGES


The function imfinfo can be used to get information about a file stored on the disk.
>> imfinfo ‘rice.jpg’
Filename: 'C:\Program
Files\MATLAB\R2007b\toolbox\images\imdemos\rice.png' FileModDate:
'26‐janv.‐2003 05:03:06'
FileSize: 44607
Format: 'png'
FormatVersion: []
Width: 256
Height: 256
BitDepth: 8
ColorType: 'grayscale'

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

>> B = logical (A)


>> B =
1 0 1
1 0 1
The function islogical can be used to check if in array is logical or not. The other two
types will be discussed at a later stage.

CONVERSION BETWEEN DATA CLASSES


The general syntax of conversion between data classes is:
>> B = data_class_name (A)
For example:
B= double(A) % Converts the type of A to double

Page 1-5
TE312 Lab # 1 Digital Image Processing

CONVERSION BETWEEN IMAGE TYPES AND CLASSES


The toolbox provides a number of functions to convert between image classes and
types. Some of these are:
im2uint8, im2double, im2bw, … etc.
For example consider the following 2x2 image of type double which could result from
some intermediate calculations:

f = [ -0.5 0.5
0.75 1.5 ]

Performing the conversion:

>> g = im2uint8(f) yields:

g = [0 128

191 255 ]

So this function sets:

All values less than 0 to 0.

All values greater than 1 to 255.

And multiplies all other values by 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.

2. Load the image ‘cameraman_gray.tif’, convert it into logical format by using


‘logical’ command and display the logical image.
3. Load the image ‘rice.jpg’, binarize it using the function ‘im2bw’ with a
threshold of 0.5 and display both original and binarized images.

Page1-7

You might also like