Exp 1

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

EXPERIMENT 1

MATLAB software and its basic commands for image


processing

Aim: To learn the effective MATLAB in-built functions and the image processing basic commands
in MATLAB.

Theory:

Command-1: ‘clc’

‘clc’ stands for clear command window. It will clear any instructions that are present in the
command window.
Fig: 1 Use of “clc” in MATLAB.
Command-2: ‘clf’

‘clf’ stands for clear any existing figure window so the display of
the current program will be on a new axes window.

Example:

Img = imread('coins.png');
figure;imshow(Img);

% Clear Figure window clf


Fig: 2 Use of “clf” in MATLAB

Command-3: ‘clear all’

The command “clear all” will clear all the variables from the MATLAB memory. The command
“clear” removes all variables from the workspace.

Type the following in MATLAB command window:

Matrix1 = [1 2 3; 4 5 6]

Matrix2 = [-1 -2 -3; -4 -5 -6]

Matrix3 = Matrix1 - Matrix2

% Use of clear function clear

Matrix1

The above function will clear matrix 1 from MATLAB memory.


Fig: 3 Use of “clear” in MATLAB

Command-4: ‘close all’


The command ‘close all’ in MATLAB will close all the open existing figure windows. The below
example will make the use of this function clearer.

Img = imread('coins.png'); figure; imshow(Img);

figure;imshow(Img); close (gcf); close all;

As shown above “close(gcf)” will close the current figure window where “gcf” stands to get the
current figure.

Significance of Semicolon in MATLAB (;)

In MATLAB if semicolon is not used at the end of each command line syntax, the MATLAB
will display the output of that line in the command window. Use of semicolons prevents
MATLAB from displaying line by line results in the command window.

Use of colon in Matlab

>> Vector1 = 1:10


Vector1 =

1 2 3 4 5 6 7 8

9 10
Colon operator acts as a separator here and produces numbers from 1 to 10.
>> Vector2 = 1:2:10
Vector2 =

1 3 5 7 9

Here the numbers from 1 to 10 in step increment of 2 are generated by the MATLAB.

Defining Arrays and Matrices in MATLAB

Arrays and matrices can be defined in many ways in MATLAB. Most of the arrays and matrices
are usually initialized before they are called anywhere in the program.

Array1 = [1:3;4:6]

Array1 =

1 2 3

4 5 6

The above example shows how arrays can be generated in MATLAB using the colon operator.

Zeros and Ones as Initialization Variables

In MATLAB often use zeros and ones to initialize many variables. For this MATLAB provides
two inbuilt functions namely ‘zeros’ and ‘ones’.

>> X = zeros(2,2)

X=

0 0
0 0
>> Y = ones(1,3)

Y=

1 1 1

Here MATLAB initializes X with a zero matrix of size 2 rows and 3 columns and Y with ones of
size 1 row and 3 columns.
Trigonometry and Geometric Functions in MATLAB

MATLAB supports most of the common trigonometric, algebraic, complex and geometric
functions of Mathematics.

Y = sin(0:2*pi/10:2*pi)

Y=

Columns 1 through 9

0 0.5878 0.9511 0.9511 0.5878

0.0000 -0.5878 -0.9511 -0.9511

Columns 10 through 11

-0.5878 -0.0000

Plotting graphs and figures in MATLAB

One of the most powerful features of MATLAB is its graphical visualization. MATLAB
provides many features for plotting and representing graphs.

Fig: 4 Plotting of graph in MATLAB


Example:1

Y = sin(0:2*pi/10:2*pi);figure;plot(Y);

Example:2

x= 1:10;
y1 = sin(x);
y2 = sin(2*x);

y3 = sin(4*x);

y4 = sin(8*x);

figure; subplot(2,2,1);

plot(x,y1); title('Subplot 1: sin(x)');

subplot(2,2,2); plot(x,y2); title('Subplot 2: sin(2x)');

subplot(2,2,3);plot(x,y3);title('Subplot 3: sin(4x)');

subplot(2,2,4);plot(x,y4);title('Subplot 4: sin(8x)');

In the above example the subplot enables viewing of multiple plots in one window.
“subplot(2,2,X)” represents a plotting window for 4 figures with 2 rows, 2 columns and X denoting
the position of each graph.

Fig: 5 Subplots in MATLAB


In MATLAB the plot function also supports a lot of keywords which will used to make the
graph more readable and better looking.

Example:3

Y = sin(0:2*pi/10:2*pi);

figure; plot(Y,'b--*','LineWidth',1.5, 'MarkerSize', 10);

xlabel('X-Axis');

ylabel('Y-Axis');

title('Sine Wave');

Fig: 6 Plotting of graph with markers in MATLAB


Code:1
fprintf("This will not be printed as it written before clc");
clc;
fprintf("This will be printed as is written after clc");

2 clc;clear;
Img = imread('coins.png');
figure; imshow(Img);
resizedimage = imresize(Img,[100 100]);
imshow(resizedimage);

3 clc; clear;
Img = imread('coins.png');
figure;imshow(Img);
close all;

4
Y = sin(0:2*pi/10:2*pi) figure; plot(Y);

5 close all; clear all; clc;


x = 0:0.0625:10;
y1= sin(x); y2 = sin(2*x); y3 = sin(4*x); y4 = sin(8*x);
figure; subplot(2,2,1); plot(x,y1); title('Subplot 1: sin(x)');
subplot(2,2,2); plot(x,y2);
title('Subplot2: sin(2x)');
subplot(2,2,3);plot(x,y3);
title ('Subplot 3: sin(4x)');
subplot(2,2,4); plot(x,y4);
title ('Subplot 4: sin(8x)');

6
Y = sin (0:2*pi/10:2*pi);
figure; plot(Y,'b--*','LineWidth',1.5, 'MarkerSize', 10);
xlabel('X-Axis'); ylabel('Y-Axis'); title ('Sine Wave')
7
clc; clf; close all; clear; x = -10:10;
y1 = zeros(1,21); y1(1,11) = 1; y2= zeros(1,21); y2(1,11:21) = 1; y3 = 0:10;
y4 = exp(0:2*pi/10:2*pi); y5= exp(-(0:2*pi/10:2*pi)); y6= sin(0:2*pi/10:2*pi);
subplot(3,2,1); stem(x,y1,'b','filled'); title('Unit Impulse');
subplot(3,2,2); stem(x,y2,'b', 'filled'); title('Unit Step');
subplot(3,2,3); stem(y3,y3, 'b', 'filled'); title('Unit Ramp');
subplot(3,2,4); stem(y3,y4, 'b', 'filled'); title('Exponential Increasing');
subplot(3,2,5); stem(y3,y5, 'b', 'filled'); title('Exponential Decreasing');
subplot(3,2,6); stem(y3,y6, 'b', 'filled'); title('Sinusoid');
Pre Lab Questions:

Q1. What is the advantage of MATLAB over other programming languages?

Q2. What are toolboxes available in MATLAB for image and video processing?

Post Lab Questions:

Q1. How to select a fixed element from a matrix in Matlab? Explain with examples

Q2. How do you plot two graphs on the same figure window? Explain with examples

RESULT:
The experiment was successfully completed and the observations and readings were recorded and verified.

You might also like