You are on page 1of 5

COMSATS University Islamabad WAH CAMPUS

Department Of Computer Science

Lab # 14

Summary

Items Description

Course Title Artificial Intelligence

Lab Title Exploring MATLAB

Duration 3 Hours

Operating System Windows Operating System/ MATLAB


/Tool/Language

Objective To get familiar with MATLAB

Image processing in MATLAB


In this Lab, we'll scan through the key features/functions of image processing. It won't be a
comprehensive but a very short while we can grasp what's going on Matlab's image processing
very quickly. In later labs, we'll study deeper.

We'll use the following basic image processing functions:

1. imread()
2. imshow()

Instructor Manual/Artificial Intelligence BS(CS) 1


3. imwrite()
4. rgb2gray()
5. imhist()
6. imadjust()
7. im2bw()

imread() :
The imread() command will read an image into a matrix:

Example:

img = imread('ImageProcessing_1/BerkeleyTower.png');
>> size(img)

ans =
499 748 3

imshow():
To show our image, we use the imshow() command. The imshow() command shows an image in
standard 8-bit format, like it would appear in a web browser.

rgb2gray():
To convert color image into a gray-scale image we use rgb2gray command.

Example:

img = imread('ImageProcessing_1/BerkeleyTower.png');
gray = rgb2gray(img);
imshow(gray);

imhist():

Display a histogram of image data. imhist(img,n) displays a histogram with n bins for the
intensity image above a grayscale color bar of length n.

Instructor Manual/Artificial Intelligence BS(CS) 2


Example:

img = imread('ImageProcessing_1/BerkeleyTower.png');
gray = rgb2gray(img);
imhist(gray);

imadjust():

imadjust() adjust image intensity values. It maps the values in intensity image of an input to new
values in output image. This increases the contrast of the output image.

Example:

img = imread('ImageProcessing_1/ BerkeleyTower.png);


gray = rgb2gray(img);
adj_img = imadjust(gray, [0.3,0.7],[]);

subplot(121);
imshow(gray);
title('input');

subplot(122);
imshow(adj_img);
title('adjusted');

im2bw() :
im2bw() converts the grayscale image to a binary image. We'll use the adjusted image.

Example:

img = imread('ImageProcessing_1/ BerkeleyTower.jpg');


gray = rgb2gray(img);
adj_img = imadjust(gray, [0.3,0.7],[]);
bw_img = im2bw(adj_img);

subplot(121);
imshow(adj_img);
title('input image');

subplot(122);
imshow(bw_img);
title('binary image');

MIRROR IMAGE GENERATION:

Instructor Manual/Artificial Intelligence BS(CS) 3


% 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
temp=f(x,k);
result(x,k)=f(x,y);
result(x,y)=temp;
k=k+1;
end
end
imshow(f)
figure,imshow(result)

Sharpen an Image:

Example:

a = imread('hestain.png');
imshow(a), title('Original Image');

Sharpen the image and display sharpened version.

b = imsharpen(a);

Figure, imshow(b), title('Sharpened Image');

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

Instructor Manual/Artificial Intelligence BS(CS) 4


TASK 2
Write a MATLAB code that will do the following
1. Read any gray scale image.
2. Display that image.

3. Again display the image such that the pixels having intensity values below than 50 will
display as black and pixels having intensity values above than 150 will display as white. And the
pixels between these will display as it is.

TASK3

Write a program to display five different images by using Switch statement

Switch can handle multiple conditions in a single case statement by enclosing the
case expression in a cell array. switchvar
case 1
disp('1')
case {2,3,4}
disp('2 or 3 or4')
case 5
disp('5')
otherwise
disp('something else')

end

Instructor Manual/Artificial Intelligence BS(CS) 5

You might also like