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

INDEX

Name of Page
S.o. Date Sign Remarks
EXPERIMENT No.
EXPERIMENT NO. 1

Aim : Introduction to MATLAB

Introduction
MATLAB is a high-performance language for technical computing. It integrates computation,
visualization, and programming in an easy-to-use environment where problems and solutions are
expressed in familiar mathematical notation. Typical uses include:
● Math and computation
● Algorithm development
● Modeling, simulation, and prototyping
● Data analysis, exploration, and visualization
● Scientific and engineering graphics
● Application development, including Graphical User Interface building
MATLAB is an interactive system whose basic data element is an array that does not require
dimensioning. This allows you to solve many technical computing problems, especially those
with matrix and vector formulations, in a fraction of the time it would take to write a program in
a scalar noninteractive language such as C or Fortran.
The name MATLAB stands for matrix laboratory. MATLAB was originally written to provide
easy access to matrix software developed by the LINPACK and EISPACK projects, which
together represent the state-of-the-art in software for matrix computation.
MATLAB has evolved over a period of years with input from many users. In university
environments, it is the standard instructional tool for introductory and advanced courses in
mathematics, engineering, and science. In industry, MATLAB is the tool of choice for
high-productivity research, development, and analysis.
MATLAB features a family of application-specific solutions called toolboxes. Very important to
most users of MATLAB, toolboxes allow you to ​learn and ​apply specialized technology.
Toolboxes are comprehensive collections of MATLAB functions (M-files) that extend the
MATLAB environment to solve particular classes of problems. Areas in which toolboxes are
available include signal processing, control systems, neural networks, fuzzy logic, wavelets,
simulation, and many others.

History
Cleve Moler, the chairman of the computer science department at the University of New Mexico,
started developing MATLAB in the late 1970s. He designed it to give his students access to
LINPACK and EISPACK without them having to learn Fortran. It soon spread to other
universities and found a strong audience within the applied mathematics community. Jack Little,
an engineer, was exposed to it during a visit Moler made to Stanford University in 1983.
Recognizing its commercial potential, he joined with Moler and Steve Bangert. They rewrote
MATLAB in C and founded MathWorks in 1984 to continue its development. These rewritten
libraries were known as JACKPAC. In 2000, MATLAB was rewritten to use a newer set of
libraries for matrix manipulation, LAPACK.

MATLAB was first adopted by researchers and practitioners in control engineering, Little's
specialty, but quickly spread to many other domains. It is now also used in education, in
particular the teaching of linear algebra, numerical analysis, and is popular amongst scientists
involved in image processing.

Syntax

Variables
The MATLAB® workspace consists of the variables you create and store in memory during a
MATLAB session. You can create new variables in the workspace by running MATLAB
code or using existing variables.
To create a new variable, enter the variable name in the Command Window, followed by an
equal sign (=) and the value you want to assign to the variable. For example, if you run
these statements, MATLAB adds the three variables x, A, and I to the workspace:
x​ = ​5.71​;
A​ = [​1​ ​2​ ​3​; ​4​ ​5​ ​6​; ​7​ ​8​ ​9​];
I​ = log10(​100​);

You do not have to declare variables before assigning values to them. MATLAB is a loosely
typed language. Specifying the data type of variable is not required. If you do not end the
assignment statement with a semicolon (;), MATLAB displays the result in the Command
Window.

Vectors & Matrices


Matlab has a facility to create large vectors easily, which having elements with repetitive pattern
by using colons (:). For example to create a vector whose first element is 1, second
element is 2, third element is 3, up to 8 can be created by the following command.
>> v = [1:7]
v =1 2 3 4 5 6 7
EXPERIMENT NO. 2

Aim : Introduction to basic MATLAB data structures

1. Creating a matrix
>>​ a = [​1​ ​2​ ​3​; ​4​ ​5​ ​6​; ​7​ ​8​ ​9​; ​10​ ​11​ ​12​]

a =
1 2 3
4 5 6
7 8 9
10 11 12

2. Creating a row matrix


>>​ a = [​1​ ​2​ ​3​ ​4​]

a = 1 2 3 4

3. Creating a column matrix


>>​ a = [​1​; ​2​; ​3​; ​4​;​5​;]

a =

5
4. Creating a square matrix
>>​ a = [​1​ ​2​ ​3​; ​4​ ​5​ ​6​; ​7​ ​8​ ​9​]

a =
1 2 3
4 5 6
7 8 9

5. Creating a identity matrix


>>​ a = [​1​ ​0​ ​0​; ​0​ ​1​ ​0​; ​0​ ​0​ ​1​]

a =
1 0 0
0 1 0
0 0 1

6. Creating a zero matrix


>>​ a =
zeros(​3​)

a =
0 0 0
0 0 0
0 0 0

7. Use of eye function


>>​ a =
eye(​4​)

a =
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1

>>​ A = [​1​ ​2​ ​3​; ​4​ ​5​ ​6​; ​7​ ​8​ ​9​]

A =

1 2 3
4 5 6
7 8 9

8. Column sum of a matrix


>>
sum(A)

ans =
12 15 18

9. Transpose of a matrix
>​>
A​'

ans =
1 4 7
2 5 8
3 6 9

10. Diagonal of a matrix


>>
diag(A)
ans =

11. Diagonal sum of a matrix


>>
trace(a)

ans =

15
EXPERIMENT NO. 3

Aim : Image import functions and basic processing of an image


A = ​imread​(filename) reads the image from the file specified by filename, inferring the format
of the file from its contents. If filename is a multi-image file, then imread reads the first image in
the file.
imshow​(I) displays the grayscale image I in a figure. imshow optimizes figure, axes, and image
object properties for image display.
I = imread(​'​ E:\digital_image\torres.jpg​'​);
imshow(I);

Figure 3.1 :Original IMAGE

Enhancing image contrast using histeq()


I =imread(​'E:\digital_image\fifa.jpg'​);
I2 = histeq(I);
figure, imshow(I);
figure, imshow(I2);

Figure 3.2 :Original IMAGE Figure 3.3 :Contrast enhanced

EXPERIMENT NO. 4
Aim : Reading and Displaying images using different color models
Convert RGB image to Grayscale image.
RGB = imread(​'E:\digital_image\football.jpg'​);
GRAY = rgb2gray(RGB);
figure, imshow(RGB);
figure, imshow(GRAY);

Figure 4.1 :Original IMAGE Figure 4.2 :Grayscale IMAGE

Convert RGB image to Black and White image.


RGB = imread(​'E:\digital_image\messi.jpg'​);
BW = im2bw(RGB);
figure, imshow(RGB);
figure, imshow(BW);

Figure 4.3 :Original IMAGE Figure 4.4 :Black and White IMAGE
Convert RGB image to NTSC pictures.
RGB = imread(​'E:\digital_image\messi.jpg'​);
figure, imshow(RGB);
NTSC = rgb2ntsc(RGB);
figure, imshow(NTSC);

Figure 4.5 :Original IMAGE Figure 4.6 :NTSC IMAGE

EXPERIMENT NO. 5
Aim : Image enhancement using various transformations functions
Photographic negative (using imcomplement function)
I = imread(​'E:\digital_image\football.jpg'​);
NEGATIVE = imcomplement(I);
imshow(NEGATIVE);

Figure 5.1 :Original IMAGE Figure 5.2 :negative IMAGE

Gamma transformation (using imadjust function)


I = imread(​'E:\digital_image\football.jpg'​);
GRAY = rgb2gray(I);
GAMMA1 = imadjust(GRAY, [], [], 0.3);imshow(GAMMA1);

Figure 5.3 :Original IMAGE Figure 5.4 :Gamma transformed IMAGE

Logarithmic transformation (using c*log(1+f))

I = imread(​'E:\digital_image\football.jpg'​);
figure, imshow(I);
GRAY = rgb2gray(I);
GRAY = double(GRAY);
LOG1 = 0.5*log10(1+GRAY);
figure, imshow(LOG1);
LOG2 = 0.2*log10(1+GRAY);
figure, imshow(LOG2);

​Figure 5.5 :Original IMAGE Figure 5.6 :Using c=0.5


​Figure 5.7 :using c=0.5
Contrast stretching
I = imread(​'E:\digital_image\football.jpg'​);
GRAY = rgb2gray(I);
STRETCHED = imadjust(GRAY, [0.02 0.8], []);
figure, imshow(STRETCHED);

Figure 5.8 :Original IMAGE Figure 5.9 :transformed with limits=0.02,0.80

EXPERIMENT 6
AIM : PERFORM ZOOMING AND SHRINKING OF IMAGE
matlab function imresize()
J = imresize(I,scale) ​ ​returns image J that is scale times the size of grayscale, RGB, or binary
image I. If I has more than two dimensions, then imresize only resizes the first two dimensions

img = imread('E:\DIP\img.jpg');
bigger_img = imresize(img, 1.9);
smaller_img = imresize(img , 0.3);
imshow(img);

Figure 6.1 :Original IMAGE

imshow(bigger_img);
Figure 6.2 :Zoomed IMAGE
imshow(smaller_img);

Figure 6.3 :shrunken IMAGE

Perform zooming and shrinking of image by replication and interpolation method


a) by replication method
img = imread(​'D:\Image Processing\UlAvI.png'​);
[r,c] = ​size​(img);
output = ​zeros​(​2​*r,​2​*c,class(img));
for​ x = ​1​:r
​for​ y = ​1​:c
​j​ = ​2​*(x​-1​) + ​1​;
​i​ = ​2​*(y​-1​) + ​1​;
output(​j​,​i​) = img(x,y);
output(​j​+​1​,​i​) = img(x,y);
output(​j​,​i​+​1​) = img(x,y);
output(​j​+​1​,​i​+​1​) = img(x,y);
​end
end
imshow(output);

Figure 6.3 :Original IMAGE


Figure 6.4 :Zoomed IMAGE

b) by interpolation method

Nearest neighbour interpolation is the simplest approach to interpolation. Rather than calculate
an average value by some weighting criteria or generate an intermediate value based on
complicated rules, this method simply determines the “nearest” neighbouring pixel, and assumes
the intensity value of it.

Bilinear Interpolation : is a resampling method that uses the distance weighted average of the
four nearest pixel values to estimate a new pixel value. The four cell centers from the input raster
are closest to the cell center for the output processing cell will be weighted and based on distance
and then averaged.

I = imread('D:\Image Processing\UlAvI.png');
Z = imresize(I, 2, 'nearest');
figure, imshow(Z);
Z = imresize(I, 2, 'bilinear');
figure, imshow(Z);
Z = imresize(I, 2, 'bicubic');
figure, imshow(Z);

Figure 6.5 :Nearest Neighbour Interpolation Figure 6.6 :Bilinear Interpolation

Figure 6.7 :Bicubic Interpolation


EXPERIMENT : 7(A)

Aim : filtering in spatial domain using lowpass filters


T​heory :
Low pass filtering as the name suggest removes the high frequency content from the image.
It is used to remove the noise present in the image.
The mask for lowpass filter is

1/9 | 1/9 | 1/9


1/9 | 1/9 | 1/9
1/9 | 1/9 | 1/9

we can also use 5*5 or 7*7 mask on the image as per our requirement.
We place a 3*3 mask on each pixel of an image.
We start from the left hand top corner. We cannot work with the borders and hence are normally
left as they are. We then multiply each component of the image with the corresponding value of
the mask.
Add these values to get the response.
Replace the center pixel of the output image with these response. We now shift the mask towards
the right till we reach the end of the line and then move it downwards.

Algorithm :

1) read input image


2) ignore the border pixels
3) Apply low mask to each and every pixel.
4) Display the output image.

Conclusion :
Low mask filtering makes the image blurred.

img = imread('E:\DIP\img.jpg');
img = rgb2gray(img);
smooth_matrix = ones(5)/25;
smoothed_img = conv2(img, smooth_matrix, 'same');
imshow(smoothed_img);
Figure 7.1 :Original IMAGE

Figure 7.2 :smoothed using Low Pass filter


EXPERIMENT NO. 7 (b)

Aim : ​filtering in spatial domain using highpass filters

Theory ​: A high-pass filter can be used to make an image appear sharper. These filters
emphasize fine details in the image – exactly the opposite of the low-pass filter. High-pass
filtering works in exactly the same way as low-pass filtering; it just uses a different convolution
kernel. In the example below, notice the minus signs for the adjacent pixels. If there is no change
in intensity, nothing happens. But if one pixel is brighter than its immediate neighbors, it gets
boosted.

The mask for high pass filter is

-1/9 | -1/9 | -1/9


-1/9 | 8/9 | -1/9
-1/9 | -1/9 | -1/9

we can also use 5*5 or 7*7 mask on the image as per our requirement.
We place a 3*3 mask on each pixel of an image.
We start from the left hand top corner. We cannot work with the borders and hence are normally
left as they are. We then multiply each component of the image with the corresponding value of
the mask.
Add these values to get the response.
Replace the center pixel of the output image with these response. We now shift the mask towards
the right till we reach the end of the line and then move it downwards.

Algorithm ​:
1) read input image
2) ignore the border pixels
3) apply high mask to each and every pixel.
4) display the output image.
Conclusion ​:
High mask filtering makes the image sharpened​.
img = imread('D:\Image Processing\einstien.jpg');
img = rgb2gray(img);
figure, imshow(img);
smooth_matrix = ones(3)/-9;
smooth_matrix(2,2) = 8/9;
smoothed_img = conv2(img, smooth_matrix, 'same');
figure, imshow(smoothed_img);

Figure 7.3 :Original IMAGE

Figure 7.4 :Sharpened Using High Pass Filter


EXPERIMENT NO. 8(A)

Aim : Insert ‘salt & pepper’ noise


img = imread('D:\Image Processing\smooth.jpg');
img = rgb2gray(img);
img2 = imnoise(img, 'salt & pepper', 0.02);
img3 = imnoise(img, 'salt & pepper', 0.05);
img4 = imnoise(img, 'salt & pepper', 0.10);
figure
subplot(2,2,1), imshow(img);
title('Original Image');
subplot(2,2,2), imshow(img2);
title('Density 0.02');
subplot(2,2,3), imshow(img3);
title('Density 0.05');
subplot(2,2,4), imshow(img4);
title('Density 0.1');

Figure 8.1 :Salt and pepper effect at various densities


EXPERIMENT NO. 8(B)

Aim : Remove ‘salt & pepper’ noise


img = imread('C:\code\matlab\lilly.jpg');
img = rgb2gray(img);
img2 = imnoise(img, 'salt & pepper', 0.02);
img3 = filter2(fspecial('average',3),img2)/255;
img4 = medfilt2(img2);
figure
subplot(2,2,1), imshow(img);
title('Original Image');
subplot(2,2,2), imshow(img2);
title('Noisy image');
subplot(2,2,3), imshow(img3);
title('Average filter');
subplot(2,2,4), imshow(img4);
title('Median filter');

Figure 8.2 :Removing salt and pepper noise using various filters
EXPERIMENT NO. 9

Aim : Detect Edges in Images using Sobel,Canny and Prewitt Detectors

newimg = imread('c:\\code\matlab\lilly.jpg');

I = rgb2gray(newimg);

BW_sobel= edge(I,'sobel',0.1) ;

BW_canny=edge(I,'canny',0.1);

BW_prew=edge(I,'prewitt',0.1);

figure

subplot(2,2,1), imshow(newimg);

title('Original Image');

subplot(2,2,2), imshow(BW_sobel);

title('Sobel');

subplot(2,2,3), imshow(BW_canny);

title('Canny');

subplot(2,2,4), imshow(BW_prew);

title('prewitt');

Conclusion:

out of the three detectors CANNY has the best detection.


Fig. 9.1:Image detection using various detectors

You might also like