0% found this document useful (0 votes)
17 views4 pages

Harsh - DIP - LAB 9

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)
17 views4 pages

Harsh - DIP - LAB 9

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/ 4

LAB 09

AIM:- Write a program to implement DFT And LOW PASS FILTER in matlab

Code :-

% DFT and Low Pass Filter Implementation in MATLAB


clc;
clear;
close all;

% STEP 1: Load and preprocess the image


img = imread("cameraman.tif");
img = im2double(img); % Convert to double precision
[M, N] = size(img);

% STEP 2: Decide padding size


P = 2 * M;
Q = 2 * N;

% Display Original Image


subplot(3, 3, 1);
imshow(img, []);
title("Original Image");

% STEP 3: Create padded image


ap = padarray(img, [M N], 0, 'post');
subplot(3, 3, 2);
imshow(ap, []);
title("Padded Image");

% STEP 4: Center the spectrum


apc = ap .* ((-1).^(0:P-1)' * (-1).^(0:Q-1)); % Vectorized centering
subplot(3, 3, 3);
imshow(apc, []);
title("Centered Image");

% STEP 5: Compute Fourier Transform


F = fft2(apc);
magnitudeF = log(1 + abs(F)); % Magnitude Spectrum for visualization
subplot(3, 3, 4);
imshow(magnitudeF, []);
title("FFT Magnitude Spectrum");

% STEP 6: Create Ideal Low-Pass Filter


d0 = 30; % Cutoff frequency
H = zeros(P, Q);
for i = 1:P
for j = 1:Q
D = sqrt((i - P/2)^2 + (j - Q/2)^2);
if D < d0
H(i, j) = 1;
end
end
end
subplot(3, 3, 5);
imshow(H, []);
title("Ideal Low Pass Filter");

% STEP 7: Apply the filter


G = F .* H;
magnitudeG = log(1 + abs(G)); % Magnitude Spectrum of filtered result
subplot(3, 3, 6);
imshow(magnitudeG, []);
title("Filtered Spectrum");

% STEP 8: Compute Inverse Fourier Transform


g = ifft2(G);
g = real(g); % Take real part to avoid numerical errors
g = g .* ((-1).^(0:P-1)' * (-1).^(0:Q-1)); % Center the spectrum back
subplot(3, 3, 7);
imshow(g, []);
title("IFFT Result (Filtered Image)");

% STEP 9: Extract the filtered image


newImage = g(1:M, 1:N); % Crop back to original size
subplot(3, 3, 8);
imshow(newImage, []);
title("Extracted Image (Final Output)");
Output:

● Example is for d0=30.

You might also like