exp_5
exp_5
Use Fourier Transform to switch to frequency domain, apply low-pass and high-pass filters, and
then convert back to spatial domain.
clc;
clear;
close all;
% Read the image
img = imread('image.jpg'); % Replace 'image.jpg' with your image file
if size(img, 3) == 3
img_gray = rgb2gray(img); % Convert to grayscale if RGB
else
img_gray = img;
end
% Fourier Transform
F = fft2(double(img_gray));
Fshift = fftshift(F); % Shift zero frequency component to the center
% Get image size
[M, N] = size(img_gray);
[u, v] = meshgrid(-N/2:N/2-1, -M/2:M/2-1);
D = sqrt(u.^2 + v.^2); % Distance matrix
% Low-pass filter (Ideal)
D0 = 30; % Cutoff frequency
LPF = double(D <= D0);
F_low = Fshift .* LPF;
img_low = real(ifft2(ifftshift(F_low))); % Inverse FFT
% High-pass filter (Ideal)
HPF = double(D > D0);
F_high = Fshift .* HPF;
img_high = real(ifft2(ifftshift(F_high))); % Inverse FFT
% Display results
figure;
subplot(2,3,1); imshow(img_gray); title('Original Image');
subplot(2,3,2); imshow(log(1 + abs(Fshift)), []); title('Fourier Spectrum');
subplot(2,3,3); imshow(LPF, []); title('Low-pass Filter');
subplot(2,3,4); imshow(img_low, []); title('Low-pass Filtered Image');
subplot(2,3,5); imshow(HPF, []); title('High-pass Filter');
subplot(2,3,6); imshow(img_high, []); title('High-pass Filtered Image');