Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Laboratory 1: DIP Spring 2015: Introduction To The MATLAB Image Processing Toolbox

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

Laboratory 1: DIP Spring 2015

Introduction to the MATLAB Image Processing Toolbox


Written by: Shreekand Mandayam
Modified by: Ying (Gina) Tang and Bingyao Huang

ABSTRACT
The purpose of this lab is to familiarize students with the MATLAB image processing
toolbox, and to understand how digital sampling and quantization impact the quality
of images

Basic

A digital image is equivalent to a matrix.


Displaying an image essentially involves placing a pixel of the correct color or
brightness, depending on the value of the matrix element, at the correct location,
corresponding to the position of the element in the matrix.
The matrix-to-display mapping is as shown in this figure:

The MATLAB Image Processing Toolbox is a complete color image processing


system. For this Digital Image Processing class, majority of our time will deal with
gray-level or binary-level images. Thus, we have to ensure that we use its
monochromatic capabilities.

Types of Images in MATLAB


For this class, will use the following three image types:
1. RGB or True-Color Images: There are three matrices associated with a true-color
image of size M x N - the Red matrix, the Green Matrix and the Blue matrix, each of
size M x N. Typically, each of these matrices contain values between 1 and 256; 1
indicating the least intensity and 256 the highest for each of the colors. The three
matrices are combined into a single M x N x 3 multidimensional matrix with 3 planes
- the R plane, the G plane and the B plane.
2. Indexed Images: There are two matrices associated with an indexed image of size M
x N to be displayed using G gray levels:
o

The image matrix which contains elements corresponding to the pixel intensities.
The elements take on values from between 1 and infinity.

The colormap matrix is G x 3 matrix, with each row containing the Red, Blue and
Green components for each level of intensity. For a monochromatic gray-level
image, this matrix starts with [0 0 0] for black and ends with [1 1 1] for white.

DIP Lab 1 Spring 2015

Each row inbetween contains equal numbers between 0 and 1 for various shades
of gray.
An image matrix element with a value of, say 5, will be displayed with the graylevel corresponding to the 5th row of the colormap matrix.
3. Grayscale Images: A grayscale image M pixels tall and N pixels wide is represented
as a matrix of double datatype of size MN. Element values denote the pixel
grayscale intensities.
4. Binary Image: A binary image is represented by an MN logical matrix where pixel
values are 1 (true) or 0 (false)

Importing and Exporting Image Files

To import an image into MATLAB:


>> [x,map] = imread ('filename.fmt', 'fmt');
where fmt refers to an image file format such as jpeg, pcx, etc. Then, matrix [x] will
contain the intensity information and matrix [map] the colormap information.

To export an image matrix [x] and a colormap matrix [map] in a specified format,
use:
>> imwrite (x, map, 'filename.fmt');

Occasionally, data may be in other formats; I will provide commands and m-files to
read them.

DIP Lab 1 Spring 2015

Image Conversion

Image Display
1. Indexed Images: If [x] is the image matrix and [map] is the colormap matrix,
>> imshow (x, map);
2. Intensity Images: If [x] is the image matrix and G is the no. of gray levels,
>> imshow(x, G);
3. Binary Images: If [x] is the image matrix,
>> imshow(x, 2);

Down sampling Images


There are several ways to downsample an M x N image f (x, y) by a factor of K.
1. Down sampling the signals x and y, respectively
For grayscale:
>>fdown = f(1:K:M,1:K:N);
>>imshow(fdown);
For RGB:
>>fdown = f(1:K:M,1:K:N,:);
>>imshow(fdown);
2. Use image resize function in MATLAB
>> fdown = imresize (f, 'scale', 'method');
where f refers to an image, scale specifies down or up sampling size, and method
specifies the interpolation method
DIP Lab 1 Spring 2015

Method

Description

'nearest'

Nearest-neighbor interpolation; the output pixel is assigned the value of the pixel that the point
falls within. No other pixels are considered.

'bilinear' Bilinear interpolation; the output pixel value is a weighted average of pixels in the nearest 2-by-2
neighborhood

'bicubic'

Bicubic interpolation (the default); the output pixel value is a weighted average of pixels in the
nearest 4-by-4 neighborhood

'box'

Box-shaped kernel

'triangle' Triangular kernel (equivalent to 'bilinear')


'cubic'

Cubic kernel (equivalent to 'bicubic')

'lanczos2' Lanczos-2 kernel


'lanczos3' Lanczos-3 kernel

Quantize Images

To quantize an image in MATLAB:


>> [x,map] = imquantize (f, 'level','value');
where f refers to an image, level specifies quantization level, and value specifies
quantization values contained in the element vector levels.

To specify multilevel image thresholds


>> thresh = multithresh (f, N)
where f refers to an image, and N specifies threshold vector size (i.e., a 1 by N vector)

Lab Assignment
Part I
1. Download a true-color (RGB) image off the web. Import this image into MATLAB
and display it. Save the image to a file; open with any other imaging software and
display it.
>> imread, >> imshow and >> imwrite commands.
DIP Lab 1 Spring 2015

2. Convert this image into a gray-level image and display it. Save the image to a file;
open with any other imaging software and display it.
>> rgb2gray, >> imshow and >> imwrite commands.
3. Convert the indexed image into an intensity image and display it. Save the image to a
file; open with any other imaging software and display it.
>> rgb2ind, >>ind2gray, >> imshow and >> imwrite commands.
4. Convert the intensity image into a binary image and display it. Save the image to a
file; open with any other imaging software and display it.
>> im2bw, >> imshow and >> imwrite commands.
5. Generate a 256x256 matrix of numbers; convert this to an intensity image and display
it. Save the image to a file; open with any other imaging software and display it.
>> mat2gray, >> imshow and >> imwrite commands.
6. Convert the indexed image you have downloaded earlier into an intensity matrix.
Save this matrix in MATLAB's native format (.mat file). Read this file and display
image.
>> mat2gray, >> imshow and >> save and >> load commands.
7. Download the image lena.jpg off the web. Down sampling it by a factor of 2, 4, 8
and 16 using the first downsampling method explained above, display and save the
results into jpg files
8. Repeat the step 7, but using imresize function with any two interpolation methods of
your choice, display and save the results into jpg files
9. Compare the results from Steps 7 and 8, and analyze the differences you observe.
10. Quantize the image lena.jpg by 20, 18, 10, 6 levels, display and save the results into
jpg files

Part II: Labeling of Connected Components


Using the techniques that you have learned so far, design a digital image
processing system that provides the red blood cells (RBCs) count in a blood
sample micrograph as shown in Figure 1. Verify your algorithm by counting
the number of bacteria in the image shown in Figure 2.

DIP Lab 1 Spring 2015

Fig. 1: A binary image of blood sample micrograph

Fig. 2: A binary image of bacteria micrograph

Deliverables
A comprehensive report on the experiments conducted in the lab. In order to achieve a
maximum grade on your lab report, it must be typed and appropriately organized
covering at least all the topics identified in the Laboratory Report Format.

DIP Lab 1 Spring 2015

You might also like