clear; close all; clc;
filename = 'images/lena_std.tif';
if ndims(imread(filename)) == 3
originalImage = rgb2gray(imread(filename));
else
originalImage = imread(filename);
end
originalFFT = fftshift(fft2(originalImage));
FFT_pts = 2 .^ ceil(log2(size(originalImage)));
[f1,f2] = freqspace(FFT_pts,'meshgrid');
SF = sqrt(f1.^2 + f2.^2);
filterType = 'bandpass';
Hd = ones(size(f1));
cutoffFreq = 0.6;
cutoffFreq2 = 0.3;
Bandpass = Hd;
Lowpass = Hd;
Highpass = Hd;
Bandpass((SF < cutoffFreq2) | (SF > cutoffFreq)) = 0;
Lowpass(SF > cutoffFreq) = 0;
Highpass(SF < cutoffFreq) = 0;
show_log_amp = true;
min_amp_to_show = 10 ^ -10;
switch filterType
case 'lowpass'
filt = Lowpass;
case 'bandpass'
filt = Bandpass;
case 'highpass'
filt = Highpass;
end
filteredFFT = filt .* originalFFT;
filteredImage = real(ifft2(ifftshift(filteredFFT)));
filteredImage = filteredImage(1: size(originalImage, 1), 1: size(originalImage, 2));
filteredFFT = fftshift(fft2(filteredImage));
filteredFFT = abs(filteredFFT);
figure(1);
colormap gray;
subplot(2, 3, 1);
imagesc(originalImage);
colorbar;
axis square;
set(gca, 'TickDir', 'out');
title('original image');
xlabel('x');
ylabel('y');
originalFFT = abs(originalFFT);
if show_log_amp
originalFFT(originalFFT < min_amp_to_show) = min_amp_to_show;
originalFFT = log10(originalFFT);
filteredFFT(filteredFFT < min_amp_to_show) = min_amp_to_show;
filteredFFT = log10(filteredFFT);
end
subplot(2, 3, 2);
imagesc(mean(f1), mean(f2,2), originalFFT);
axis xy;
axis square;
set(gca, 'TickDir', 'out');
title('amplitude spectrum');
xlabel('fx (cyc/pix)');
ylabel('fy (cyc/pix)');
subplot(2, 3, 3);
imagesc(mean(f1), mean(f2,2), abs(filt));
axis xy;
axis square;
set(gca, 'TickDir', 'out');
title('filter in the SF domain');
xlabel('fx (cyc/pix)');
ylabel('fy (cyc/pix)');
subplot(2, 3, 4);
imagesc(filteredImage);
colorbar;
axis square;
set(gca, 'TickDir', 'out');
title('filtered image');
xlabel('x');
ylabel('y');
subplot(2, 3, 5);
imagesc(mean(f1), mean(f2,2), filteredFFT);
axis xy;
axis square;
set(gca, 'TickDir', 'out');
title('amplitude spectrum');
xlabel('fx (cyc/pix)');
ylabel('fy (cyc/pix)');
4 Comments
Direct link to this comment
https://it.mathworks.com/matlabcentral/answers/1498-plotting-frequency-distribution-of-fft2-transformed-image#comment_2757
Direct link to this comment
https://it.mathworks.com/matlabcentral/answers/1498-plotting-frequency-distribution-of-fft2-transformed-image#comment_2757
Direct link to this comment
https://it.mathworks.com/matlabcentral/answers/1498-plotting-frequency-distribution-of-fft2-transformed-image#comment_2759
Direct link to this comment
https://it.mathworks.com/matlabcentral/answers/1498-plotting-frequency-distribution-of-fft2-transformed-image#comment_2759
Direct link to this comment
https://it.mathworks.com/matlabcentral/answers/1498-plotting-frequency-distribution-of-fft2-transformed-image#comment_2760
Direct link to this comment
https://it.mathworks.com/matlabcentral/answers/1498-plotting-frequency-distribution-of-fft2-transformed-image#comment_2760
Direct link to this comment
https://it.mathworks.com/matlabcentral/answers/1498-plotting-frequency-distribution-of-fft2-transformed-image#comment_2761
Direct link to this comment
https://it.mathworks.com/matlabcentral/answers/1498-plotting-frequency-distribution-of-fft2-transformed-image#comment_2761
Sign in to comment.