Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Name: Rahul Tripathy Reg No.: 15bec0253

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

NAME: RAHUL TRIPATHY REG NO.

: 15BEC0253

Assignment Questions:

Q.1 Let a grayscale image with matrix size [128 x128] pixels. Assume
that one quarter of image pixels has intensity equal to 8, a second
quarter 64, annd output image histograms. Also, display input and
output imad the remaining half 128. Perform histogram
equalization using Matlab. What are the intensities of the equalized
image? Plot graphs of the input ages.
Q.2 Perform point to point operation on an image and compute the
following. Also, interpret changes in image.
(a). Image Negative
(b). Power law transformation
(c). Logarithmic transformation
Sol:
a) Image Negative
MATLAB code :
x=imread(‘tree.jpeg’);
x=rgb2gray(x);
x=im2double(x);
[row,col]=size(x);
for i=1:row
for j=1:col
N(i,j)=1-x(I,j);
end
end
subplot(1,2,1)
imshow(x);
title(‘original’);
subplot(1,2,2)
imshow(N);
title(‘negative’);
b) Power law transformation
MATLAB code:
a=imread(‘blocks.tif’);
ad=im2double(a);
x=ad;
[r,c]=size(ad);
factor=1;
for i=1:r
for j=1:c
x(I,j)=factor*log(1+ad(I,j));
end
end
subplot(1,2,1)
imshow(ad);
title(‘Before’);
subplot(1,2,2)
imshow(x);
title(‘After’);
c) Logarithmic transformation
MATLAB code:
a=imread(‘camerman.tif’);
ad=im2double(a);
x=ad; y=ad;
[r,c]=size(ad);
factor=1;
for i=1:r
for j=1:c
x(I,j)=factor*log(1+ad(I,j));
x(I,j)=factor*ad(I,j)^20;
end
end
subplot(1,2,1)
imshow(ad);
title(‘Before’);
subplot(1,2,2)
imshow(x);
title(‘After’);
figure,imshow(y);
Q.3 Write a matlab program to execute the following steps.
(a). Apply forward 2-D DFT to an image.
(b). Display frequency, amplitude and phase using DC shifting and
log transform.
(c). Reconstruct the image using inverse 2-D DFT.
(d). Compute and report RMS error between the original and
reconstructed image.
Matlab code:
clc;
clear all;
close all;
%Import images
imageA = imread('C:
\Users\rajas\OneDrive\Desktop\greekchurch','png');

%Display images
figure, imshow(imageA)
title('Image A - Greek Church')
%Perform 2D FFTs
fftA = fft2(double(imageA));
%Display magnitude and phase of 2D FFTs
figure, imshow(abs(fftshift(fftA)),[24 100000]), colormap gray
title('Image A FFT2 Magnitude')
figure, imshow(angle(fftshift(fftA)),[-pi pi]), colormap gray
title('Image A FFT2 Phase')

%Switch magnitude and phase of 2D FFTs


fftC = abs(fftA).*exp(i*angle(fftB));
%Perform inverse 2D FFTs on switched images
imageC = ifft2(fftC);
%Calculate limits for plotting
cmin = min(min(abs(imageC)));
cmax = max(max(abs(imageC)));

%Display switched images


figure, imshow(abs(imageC), [cmin cmax]), colormap gray
title('Image C Magnitude')
function MSE= MSE(greekchurch, imageC);
[M, N] = size(greekchurch);
error = greekchurch-(imageC);
MSE = sum(sum(error .* error)) / (M * N);
disp(MSE);
End
Q.4 Write a matlab program to execute the following steps.
(a). Apply forward 2-D DCT to a test image.
(b). Display the basis image.
(c). Reconstruct image using inverse 2-D DCT.
(d). Compute and report RMS error between the original and
reconstructed images.

MARLAB CODE:
RGB = imread('autumn.tif');
I = rgb2gray(RGB);
J = dct2(I);
figure
imshow(log(abs(J)),[])
colormap(gca,jet(64))
colorbar
J(abs(J) < 10) = 0;
K = idct2(J);
figure
imshowpair(I,K,'montage')
title('Original Grayscale Image (Left) and Processed Image (Right)');
Q.5 For an image, implement the following using 3 x 3 spatial domain
filtering.
(a). Low Pass Filtering
MATLAB CODE:
clc;
clear all;
close all;
im=imread('lenaTest
1.jpg'); fc=20 n=1;
[co,ro]=size(im)
; cx=round(co/
2);
cy=round(ro/2);
imf=fftshift(fft2
(im));
H=zeros(co,ro);
for i=1:co
for j=1:ro
d=(i-cx).^2+(j-cy).^2;
H(i,j)=1/(1+((d/fc/
fc).^(2*n))); end
end
outf=imf.*H;
out=abs(ifft2(o
utf));
subplot(2,2,1),imshow(im),title('Original image');
subplot(2,2,2),imshow(uint8(out)),title('Low Pass Filtered
image'); subplot(2,2,3),imshow(H),title('2D view of H');
subplot(2,2,4),surf(H),title('3D view of H');
OUTPUT:

B) HIGH PASS FILTER:


MAT LAB
CODE:
clc;
clear all;
close all;
im=imread('lenaTest1.jpg');
fc=20
%cutoff frequency
n=1;
[co,ro]=size(im);
cx=round(co/2);
% find centre of image
cy=round(ro/2);
imf=fftshift(fft2(im));
H=zeros(co,ro);
for i=1:co
for j=1:ro
d=(i-cx).^2+(j-cy).^2;
if d~=0
H(i,j)=1/(1+((fc*fc/
d).^(2*n))); end
end
end
outf=imf.*H;
out=abs(ifft2(o
utf));
subplot(2,2,1),imshow(im),title('Original image');
subplot(2,2,2),imshow(uint8(out)),title('High Pass Filtered
image'); subplot(2,2,3),imshow(H),title('2D view of H');
subplot(2,2,4),surf(H),title('3D view of H');
OUTPUT:

(c). M e d i a n
Filtering
MATLAB CODE:
clc
clear all
close all
a = imread('lenaTest1.jpg');
b = imnoise(a,'salt & pepper', 0.2);
h1 = 1/9*ones(3,3);
h2 = 1/25*ones(5,5);
c1 = conv2(b,h1,'same');
c2 = conv2(b,h2,'same');
c3 = medfilt2(b,[3,3]);
c4 = medfilt2(b,[5,5]);
subplot(3,2,1),imshow(a),title('Original Image');
subplot(3,2,2),imshow(b),title('Salt & Pepper Noise');

subplot(3,2,3),imshow(uint8(c1)),title('3x3 Smoothing using Mean


Filter');
subplot(3,2,4),imshow(uint8(c2)),title('5x5 Smoothing using Mean
Filter');
subplot(3,2,5),imshow(uint8(c3)),title('3x3 Median Filter');
subplot(3,2,6),imshow(uint8(c4)),title('5x5 Median Filter');

Q.6 Write a matlab program to perform image enhancement process by


following the steps as illustrated in Figure 1. Display and comment
on the result. Also, measure execution time.
Code:
clc
clear all
close all
i=imread('washed_out_pollen_image.tif');
t=imnoise(i,'salt & pepper',.05);%adding noise
x=t;
t=medfilt2(t,[3 3]);%med filtering
x2=t;
a=min(t(:));%min pixel
b=max(t(:));%max pixel
t=(t-a).*(255/(b-a));%contrast stretching
x3=t;
l=[1 1 1;1 -8 1;1 1 1];
b=uint8(filter2(l,t,'same'));
t=imsubtract(t,b);%sharpening using laplacian filter
x4=t;
t=histeq(t);%output image
disp(' Entropy of input')
e1=entropy(i)%entropy of input
disp(' Entropy of output')
e2=entropy(t)%entropy of output
a1=min(t(:));%min pixel
b1=max(t(:));%max pixel
a2=min(i(:));%min pixel
b2=max(i(:));%max pixel
disp(' Contrast of input')
c1=b1-a1
disp(' Contrast of output')
c2=b2-a2
disp(' PSNR Value of the input image:')
psnr1=psnr(x,i)
disp(' PSNR Value of the output image:')
psnr2=psnr(t,x)
disp(' Structural similarity of the input image:')
ssimval=ssim(x,i)
disp(' Structural similarity of the output image:')
ssimval2=ssim(t,x)
disp(' Number of visible edges of input image:')
edge1=sum(sum(edge(i)))
disp(' Number of visible edges of the output image:')
edge2=sum(sum(edge(t)))
subplot(2,4,1),imshow(i),title('input gray image');
subplot(2,4,2),imshow(t),title('output gray image');
subplot(2,4,3),imhist(i),title('input histogram');
subplot(2,4,4),imshow(t),title('output histogram');
Q.7 Write a matlab program to perform the task as illustrated in Figure
2. Display and comment on the result obtained. Measure execution time.

MATLAB CODE:
clear all; close all; clc
imdata = imread('YOUR IMAGE');
figure(1);imshow(imdata); title('Original Image');
imdata = rgb2gray(imdata);
figure(2); imshow(imdata); title('Gray Image');
%Get Fourier Transform of an image
F = fft2(imdata);
% Fourier transform of an image
S = abs(F);
figure(3);imshow(S,[]);title('Fourier transform of an image');
%get the centered spectrum

Fsh = fftshift(F);
figure(4);imshow(abs(Fsh),[]);title('Centered fourier transform of
Image')
%apply log transform
S2 = log(1+abs(Fsh));
figure(5);imshow(S2,[]);title('log transformed Image')
%reconstruct the Image
F = ifftshift(Fsh);
f = ifft2(F);
figure(6);imshow(f,[]),title('reconstructed Image')

Q.8 Perform frequency domain image enhancement of an image using


following filtering methods:
(a). Ideal Low Pass Filtering and
Matlab code:

close all
clear
clc
a=imread('1.tif');
[M N]=size(a);
a=im2double(a);
F1=fft2(a); %1.Obtain the Fourier transform
% Set up range of variables.
u = 0:(M-1); %0-255
v = 0:(N-1);%0-255
% center (u,v) = (M/2,N/2)
% Compute the indices for use in meshgrid
idx = find(u > M/2);% indices 130-255
u(idx) = u(idx) - M;
idy = find(v > N/2);
v(idy) = v(idy) - N;
%set up the meshgrid arrays needed for
% computing the required distances.
[U, V] = meshgrid(u, v);
% Compute the distances D(U, V).
D = sqrt(U.^2 + V.^2);
disp('IDEAL LOW PASS FILTERING IN FREQUENCY DOMAIN');
D0=input('Enter the cutoff distance==>');
% Begin filter computations.
H = double(D <= D0);
G=H.*F1; %Multiply
G=ifft2(G);
G=real(G);
ff=abs(fftshift(H));
subplot(131),imshow(a),title('original image')
subplot(132),imshow(ff),title('IDEAL LPF Image')
subplot(133),imshow(G),title('IDEAL LPF Filtered Image')
figure,
mesh(ff),
axis off,
grid off
output:

(b). Ideal Gaussian High Pass Filtering


MATLAB CODE:
close all
clear
clc
a=imread('1.tif');
[M N]=size(a);
a=im2double(a);
F1=fft2(a); %1.Obtain the Fourier transform
% Set up range of variables.
u = 0:(M-1); %0-255
v = 0:(N-1);%0-255
% center (u,v) = (M/2,N/2)
% Compute the indices for use in meshgrid
idx = find(u > M/2);% indices 130-255
u(idx) = u(idx) - M;
idy = find(v > N/2);
v(idy) = v(idy) - N;
%set up the meshgrid arrays needed for
% computing the required distances.
[U, V] = meshgrid(u, v);
% Compute the distances D(U, V).
D = sqrt(U.^2 + V.^2);
disp('Gaussian highpass FILTERING IN FREQUENCY DOMAIN');
D0=input('Enter the cutoff distance==>');
% Begin filter computations.
H = 1 - (exp(-(D.^2)./(2*(D0^2))));
G=H.*F1; %Multiply
G=ifft2(G);
G=real(G);
ff=abs(fftshift(H));
subplot(131),imshow(a),title('original image')
subplot(132),imshow(ff),title('Gaussian HPF Image')
subplot(133),imshow(G),title('Gaussian HPF Filtered Image')
figure, mesh(ff),axis off,grid off

OUTPUT:

Q.9)
Write a matlab program to obtain Gradient Magnitude and Direction
using Sobel operator. Display the results obtained.

Matlab code:
I = imread('coins.png');
.
[Gmag, Gdir] = imgradient(I,'sobel');
figure
imshowpair(Gmag, Gdir, 'montage');
title('Gradient Magnitude, Gmag (left), and Gradient Direction, Gdir
(right))
output:

Q.10 Write a matlab program that will implement morphological dilation


and erosion using structuring element of your choice.
Matlab code:
a=imread(‘cat.jpg’);
b=imresize(a,[400 500]);
g=im2bw(b);
ss=strel(‘square’,3);
i=imerode(g,ss);
i2=imdilate(g,ss);
subplot(3,1,1)
imshow(g);
subplot(3,1,2)
imshow(i);
subplot(3,1,2)
imshow(i2);
OUTPUT:

You might also like