Tutorial 1
Tutorial 1
b) Program Title: Write a program to generate your previous semester result and print
your result on console also plot your semester wise result.
Program Code:
x = [8.9 9.4 9.26 9.2];
y = 1:4;
for i=1:length(x)
fprintf("Semester%d\t", i);
end
fprintf("\n");
for i=1:length(x)
fprintf("%d\t\t", x(i));
end
plot(x,y);
xlabel('Result');
ylabel('Semesters');
title ("Plot of Semester wise result");
Program Output:
for i=1:10
fprintf("%d X %d = %d\n", num, i, num*i);
end
Program Output:
d) Program Title: Write a program using the select case for calculation of addition,
multiplication, division and subtraction.
Program Code:
op1 = input("Enter first operand : ");
op2 = input("Enter second operand : ");
operator = input("Enter operator : ", 's');
switch operator
case ('+');
fprintf("Ans : %d", op1+op2);
case ('-');
fprintf("Ans : %d", op1-op2);
case ('*');
fprintf("Ans : %d", op1*op2);
case ('/');
fprintf("Ans : %d", op1+op2);
otherwise
fprintf("Wrong entry");
end
Program Output:
e) Program Title: Write a program that accepts a number and calculate its factorial.
Program Code:
num = input("Enter a number : ");
fact = 1;
for i=num:-1:1
fact = fact*i;
end
fprintf("The factorial of %d is %d", num, fact);
Program Output:
f) Program Title: Write a program to read and write a text file in Matlab.
Program Code:
word = input("Enter any string to write to a file : ", 's');
fclose(myfile);
A) The negative of an image with intensity levels in the range [0, L-1] is obtained by
using the negative transformation. The negative transformation is given by the
following expression
s = (L – 1) – r, where s is a negative image
L has a maximum gray level range of values 0 to 255
R is an original gray-scale image
Reversing the intensity levels of an image in this manner produces the equivalent of
photographic negative. This type of processing is particularly suited for enhancing
white or gray detail embedded in dark regions of an image.
B) Thresholding of an Image
Thresholding of an image is a type of segmentation where we change the pixels
of an image to make it easier to analyze. In thresholding, we convert an image
from color or grayscale into a binary image, i.e., simply black and
white(represented by 0 and 1).
C) Contrast Stretching of an image
Contrast stretching is an image enhancement method which attempts to improve
an image by stretching the range of intensity values.
Program Code:
I = imread('Einstein.jpg');
figure
subplot (2, 2, 1)
imshow(I)
subplot(2, 2, 2)
imhist(I,64)
img = histeq(I);
subplot(2,2,3)
imshow(img)
subplot(2,2,4)
imhist(img,64)
Program Output:
c. Median filtering
a) Low pass filtering
Code:
img = imread("Einstein.jpg");
img2 = im2double(img);
[m,n] = size(img2);
c = zeros(2*m, 2*n);
[p, q] = size(c);
for i=1:p
for j=1:q
if i<=m && j<=n
c(i,j) = img2(i,j);
else
c(i,j)=0;
end
end
end
d = zeros(p, q);
% Multiplying the padded image by (-1)^(x+y)
for i = 1:p
for j=1:q
d(i,j) = c(i,j).*((-1).^(i+j));
end
end
[x,y] = freqspace([p,q],'meshgrid');
z = zeros(p, q);
for i = 1:p
for j=1:q
z(i,j) = sqrt(x(i,j).^2+y(i,j).^2);
end
end
%Choosing cutoff frequency and hence defining the low pass filter mask
H = zeros(p,q);
for i=1:p
for j=1:q
if z(i,j) <=0.4
H(i,j) = 1;
else
H(i,j) = 0;
end
end
end
h1 = e.*H;
h2 = iff2(h1);
for i = 1:p
for j = 1:q
h3(i,j) = h2(i,j).*((-1).^(i+j))
end
end
out = zeros(m,n);
for i = 1:m
for j=1:n
out(i,j) = h3(i,j);
end
end
figure;
subplot(3,2,1);
imshow(img2);title("Original Image");
subplot(3,2,2);
imshow(c);title("Padded Image");
subplot(3,2,3);
imshow(d);title("Preprocessed image for calculating DFT");
e= fft2(d);
subplot(3,2,4);
imshow(e); title("2D DFT of the preprocessed image");
subplot(3,2,5);
imshow(H); title("Low pass filter mask");
Program Output (in octave ‘freqspace’ function is not included yet. So, I couldn’t get the
output for the low pass filter image but here are the other images which are included in the
stapes).
c = zeros(2*m, 2*n);
[p, q] = size(c);
for i=1:p
for j=1:q
if i<=m && j<=n
c(i,j) = img2(i,j);
else
c(i,j)=0;
end
end
end
d = zeros(p, q);
% Multiplying the padded image by (-1)^(x+y)
for i = 1:p
for j=1:q
d(i,j) = c(i,j).*((-1).^(i+j));
end
end
[x,y] = freqspace([p,q],'meshgrid');
z = zeros(p, q);
for i = 1:p
for j=1:q
z(i,j) = sqrt(x(i,j).^2+y(i,j).^2);
end
end
%Choosing cutoff frequency and hence defining the low pass filter mask
H = zeros(p,q);
for i=1:p
for j=1:q
if z(i,j) <=0.4
H(i,j) = 0;
else
H(i,j) = 1;
end
end
end
h1 = e.*H;
h2 = iff2(h1);
for i = 1:p
for j = 1:q
h3(i,j) = h2(i,j).*((-1).^(i+j))
end
end
out = zeros(m,n);
for i = 1:m
for j=1:n
out(i,j) = h3(i,j);
end
end
figure;
subplot(3,2,1);
imshow(img2);title("Original Image");
subplot(3,2,2);
imshow(c);title("Padded Image");
subplot(3,2,3);
imshow(d);title("Preprocessed image for calculating DFT");
e= fft2(d);
subplot(3,2,4);
imshow(e); title("2D DFT of the preprocessed image");
subplot(3,2,5);
imshow(H); title("Low pass filter mask");
Program Output (in octave ‘freqspace’ function is not included yet. So, I couldn’t get the
output for the low pass filter image but here are the other images which are included in the
stapes).
c) Median filtering
Program code:
img = imread("Einstein.jpg");
subplot(2,2,1)
imshow(img); title("Original Image");
subplot(2,2,2)
imshow(imgNoise); title("Image with noise");
subplot(2,2,3)
imshow(imgFiltered); title("Noise filtered image");
Program Ouput: