Name: Rahul Tripathy Reg No.: 15bec0253
Name: Rahul Tripathy Reg No.: 15bec0253
Name: Rahul Tripathy Reg No.: 15bec0253
: 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')
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:
(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');
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')
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:
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: