UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
SOFTWARE ENGINEERING DEPARTMENT
DIGITAL IMAGE PROCESSING
LAB MANUAL 3
Digital Image Processing 6th Term-SE UET Taxila
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
SOFTWARE ENGINEERING DEPARTMENT
DIGITAL IMAGE FUNDAMENTALS
LAB OBJECTIVE:
The objective of this lab is to understand
1. Images types in MATLAB toolbox
2. Mirror Image generation.
3. Flipped Image generation.
4. Binary image with various thresholds.
BACKGROUND MATERIAL:
Images in MATLAB and the Image Processing Toolbox
The basic data structure in MATLAB is the array, an ordered set of real or
complex elements.
This object is naturally suited to the representation of images, real-valued
ordered sets of color or intensity data.
MATLAB stores most images as two-dimensional arrays (i.e., matrices),
in which each element of the matrix corresponds to a single pixel in the
displayed image.
(Pixel is derived from picture element and usually denotes a single dot on
a computer display.)
For example, an image composed of 200 rows and 300 columns of
different colored dots would be stored in MATLAB as a 200-by-300 matrix.
Digital Image Processing 6th Term-SE UET Taxila
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
SOFTWARE ENGINEERING DEPARTMENT
Some images, such as RGB, require a three-dimensional array, where
first plane in the third dimension represents the red pixel intensities,.
second plane represents the green pixel intensities.
Third plane represents the blue pixel intensities.
This convention makes working with images in MATLAB similar to working
with any other type of matrix data, and makes the full power of MATLAB
available for image processing applications.
For example, you can select a single pixel from an image matrix using
normal matrix subscripting. I(2,15)
This command returns the value of the pixel at row 2, column 15 of the
image I.
Image Types in the Toolbox
The Image Processing Toolbox supports four basic types of images:
Indexed images
Intensity images
Binary images
RGB images
Digital Image Processing 6th Term-SE UET Taxila
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
SOFTWARE ENGINEERING DEPARTMENT
Indexed Images
An indexed image consists of a data matrix, X, and a colormap matrix,
map.
The data matrix can be of class uint8, uint16, or double.
The colormap matrix is an m-by-3 array of class double containing
floating-point values in the range [0,1].
Each row of map specifies the red, green, and blue components of a
single color.
An indexed image uses direct mapping of pixel values to colormap values.
A colormap is often stored with an indexed image and is automatically
loaded with the image when you use the imread function.
However, you are not limited to using the default colormap.
you can use any colormap that you choose.
Example
The figure below illustrates the structure of an indexed image.
The pixels in the image are represented by integers, which are pointers
(indices) to color values stored in the colormap.
The color of each image pixel is determined by using the corresponding
value of X as an index into map.
Digital Image Processing 6th Term-SE UET Taxila
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
SOFTWARE ENGINEERING DEPARTMENT
The value 1 points to the first row in map, the value 2 points to the
second row, and so on.
The following figure depicts an indexed image.
Intensity Images
An intensity image is a data matrix, I, whose values represent
intensities within some range.
MATLAB stores an intensity image as a single matrix, with each element
of the matrix corresponding to one image pixel.
Digital Image Processing 6th Term-SE UET Taxila
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
SOFTWARE ENGINEERING DEPARTMENT
The matrix can be of class double, uint8, or uint16.
While intensity images are rarely saved with a colormap, MATLAB uses a
colormap to display them.
The elements in the intensity matrix represent various intensities, or gray
levels, where the intensity 0 usually represents black and the intensity
1, 255, or 65535 usually represents full intensity, or white.
The figure below depicts an intensity image of class double.
Digital Image Processing 6th Term-SE UET Taxila
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
SOFTWARE ENGINEERING DEPARTMENT
Binary Images
In a binary image, each pixel assumes one of only two discrete values.
Essentially, these two values correspond to on and off.
A binary image is stored as a logical array of 0's (off pixels) and 1's (on
pixels).
The figure below depicts a binary image.
RGB Images
An RGB image, sometimes referred to as a true-color image.
It is stored in MATLAB as an m-by-n-by-3 data array that defines red,
green, and blue color components for each individual pixel.
Digital Image Processing 6th Term-SE UET Taxila
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
SOFTWARE ENGINEERING DEPARTMENT
The color of each pixel is determined by the combination of the red, green,
and blue intensities stored in each color plane at the pixel's location.
Graphics file formats store RGB images as 24-bit images, where the red,
green, and blue components are 8 bits each.
This yields a potential of 16 million colors.
The precision with which a real-life image can be replicated has led to the
commonly used term true-color image.
RGB Array
An RGB array can be of class double, uint8, or uint16.
In an RGB array of class double, each color component is a value between
0 and 1.
A pixel whose color components are (0,0,0) is displayed as black
A pixel whose color components are (1,1,1) is displayed as white.
The three color components for each pixel are stored along the third
dimension of the data array.
Example
For example, the red, green, and blue color components of the pixel
(10,5) are stored in RGB(10,5,1), RGB(10,5,2), and RGB(10,5,3),
respectively.
Digital Image Processing 6th Term-SE UET Taxila
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
SOFTWARE ENGINEERING DEPARTMENT
The following figure depicts an RGB image of class double.
Display the truecolor image RGB
use the image function: image(RGB)
C = imread('ngc6543a.jpg');
%imread returns a 650-by-600-by-3 array, C.
%Display the image.
image(C)
Separating the 3 Channels of RGB Image
rgbImage = imread('ngc6543a.jpg');
% Extract the individual red, green, and blue color channels.
Digital Image Processing 6th Term-SE UET Taxila
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
SOFTWARE ENGINEERING DEPARTMENT
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
each one of those arrays is just a simple monochrome gray scale array,
and will appear as gray scale if you use imshow().
There is no color anymore in them since they just have a single gray level,
instead of 3 values, an R value, a G value, and B value.
Displaying only one color image
If you want, you can take any of those gray scale images and make them
appear red with a colormap.
Then you can turn each into an RGB color image by putting that channel
into the proper channel, and making the other channels all zero.
If you want to see how it appears in the color of the color channel that it
represents then you'd have to either use cat() to make an RGB image out
of it:
cat(dim,A1,A2,…,An) concatenates A1, A2, … , An along dimension dim.
z = zeros(size(redChannel));
redAppearingImage = cat(3, redChannel, z, z);
Digital Image Processing 6th Term-SE UET Taxila
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
SOFTWARE ENGINEERING DEPARTMENT
imshow(redAppearingImage);
Complete Code
rgbImage = imread('ngc6543a.jpg');
% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
z = zeros(size(redChannel));
redAppearingImage = cat(3, redChannel, z, z);
imshow(redAppearingImage);
How to convert between double and uint8
When you store an image, you should store it as a uint8 image since this
requires far less memory than double.
When you are processing an image (that is performing mathematical
operations on an image) you should convert it into a double.
Converting back and forth between these classes is easy.
I = imread('ngc6543a.jpg');
%converts an image named I from uint8 to double.
I=im2double(I);
%converts an image named I from double to uint8.
Digital Image Processing 6th Term-SE UET Taxila
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
SOFTWARE ENGINEERING DEPARTMENT
I=im2uint8(I);
Miror Image Generation
We can get the mirror image of the given image if we reverse the order of
the pixels (elements of the matrix) in each row.
Code #1: Using MATLAB: flip(dim,A)
flip(A,dim) reverses the order of the elements in A along dimension dim.
if A is a matrix, then flip(A,1) reverses the elements in each column.
flip(A,2) reverses the elements in each row.
% Read the target image file
img = imread('cameraman.tif');
% Reverse the element in each row
mirror_image = flip(img, 2);
Digital Image Processing 6th Term-SE UET Taxila
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
SOFTWARE ENGINEERING DEPARTMENT
subplot(1,2,1), imshow(img)
subplot(1,2,2), imshow(mirror_image)
Code #2: Using matrix manipulation
Img(:,:,3):
M-by-N-by-3 data array
img(:, end : -1: 1);
% Read the target image file
img = imread('cameraman.tif');
% Flip the columns left to right
mirror_image = img(:, end : -1: 1 );
subplot(1,2,1), imshow(img)
subplot(1,2,2), imshow(mirror_image)
Code # 3: matrix manipulation Using Loops
% this program produces mirror image of the image passed to it n also
% displays both the original and mirror image
f=imread('cameraman.tif');
[r,c]=size(f);
for x=1:1:r
k=1;
for y=c:-1:1
Digital Image Processing 6th Term-SE UET Taxila
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
SOFTWARE ENGINEERING DEPARTMENT
temp=f(x,k);
result(x,k)=f(x,y);
result(x,y)=temp;
k=k+1;
end
end
imshow(f)
figure
imshow(result)
ORIGINAL IMAGE MIRROR IMAGE
******************************************************************
Digital Image Processing 6th Term-SE UET Taxila
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
SOFTWARE ENGINEERING DEPARTMENT
TASK 1
Write a MATLAB code that reads a gray scale image given as an input and
generates the following resultant image given below
Your output should be like the one given below
******************************************************************
INPUT IMAGE RESULTANT IMAGE
TASK 2
Write a MATLAB code that will do the following
1. Read True Color image.
Extract the individual red, green, and blue color channels by turning
original into an 3 RGB color images (Red image, blue image and
green image) by putting that channel into the proper channel, and
making the other channels all zero.
Digital Image Processing 6th Term-SE UET Taxila
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
SOFTWARE ENGINEERING DEPARTMENT
Display the Original image along with the (Red image, blue image and
green image) as montage.
******************************************************************
Digital Image Processing 6th Term-SE UET Taxila