0% found this document useful (0 votes)
12 views16 pages

Tutorial 1

The document provides an overview of various MATLAB image processing commands and programming tasks, including noise addition, histogram equalization, and filtering techniques. It includes code examples for generating semester results, multiplication tables, basic arithmetic operations, factorial calculation, and file handling. Additionally, it discusses point processing techniques like image negation, thresholding, and contrast stretching, along with programs for low pass, high pass, and median filtering.

Uploaded by

paulbogale
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views16 pages

Tutorial 1

The document provides an overview of various MATLAB image processing commands and programming tasks, including noise addition, histogram equalization, and filtering techniques. It includes code examples for generating semester results, multiplication tables, basic arithmetic operations, factorial calculation, and file handling. Additionally, it discusses point processing techniques like image negation, thresholding, and contrast stretching, along with programs for low pass, high pass, and median filtering.

Uploaded by

paulbogale
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

1. Study of Matlab image processing toolkit and various commands on Matlab.

a) imnoise – used to add noise to the image


histeq – used to perform histogram equalization
imfilter – used to filter multidimensional images
imhist – calculate the histogram of an image
imrotate – rotate an image
im2double – convert image to double precision
imresize – resize an image
zeros – used to create zero matrix
ones – used to create a matrix with all 1’s.
imadjust – adjust images intensity values

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:

c) Program Title: Write a program to print multiplication tables.


Program Code:
num = input("Enter a number");

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');

myfile = fopen("hello.txt", 'w');

fprintf(myfile, '%s', word);

fclose(myfile);

fprintf("String successfully written to Hello.txt file. Please check!");


Program Output:

2. Point processing in spatial domain


A. Negation of an image
B. Thresholding of an image
C. Contrast Stretching of an image

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.

3. Program Title: Write a program for histogram equalization.

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:

4. Write a program to apply various filtering techniques in matlab.

a. Low pass filtering


b. High pass filtering

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);

%post process operation


h3 = zeros(p,q);

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).

b) High Pass filering:


Program 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) = 0;
else
H(i,j) = 1;
end
end
end

h1 = e.*H;
h2 = iff2(h1);

%post process operation


h3 = zeros(p,q);

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");

imgNoise = imnoise(img, 'salt & pepper', 0.02);


imgFiltered = medfilt2(imgNoise, true(5));

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:

You might also like