how can i blur an image by removing high frequencies of it's DFT
Show older comments
Hello everyone, I'm trying to blur an image by removing the high frequencies from the DFT of that image. after reading the image i used the fft command, now i have the frequencies but can't figure out how to remove the high ones
any suggestion??
Accepted Answer
More Answers (2)
Walter Roberson
on 1 Jan 2012
0 votes
zero them in the dft.
Dr. Seis
on 1 Jan 2012
I modified an example I posted earlier. It generates a random 2D image and then applies a simple low-pass filter (i.e., sets high-frequencies beyond some cut-off to 0):
Nx = 32; % Number of samples collected along x dimension
Ny = 64; % Number of samples collected along y dimension
dx = .1; % x increment (i.e., Spacing between each column)
dy = .1; % y increment (i.e., Spacing between each row)
x = 0 : dx : (Nx-1)*dx;
y = 0 : dy : (Ny-1)*dy;
data_spacedomain = randn(Ny,Nx); % random 2D matrix
Nyq_kx = 1/(2*dx); % Nyquist of data in x dimension
Nyq_ky = 1/(2*dy); % Nyquist of data in y dimension
dkx = 1/(Nx*dx); % x Wavenumber increment
dky = 1/(Ny*dy); % y Wavenumber increment
kx = -Nyq_kx : dkx : Nyq_kx-dkx; % x wavenumber
ky = -Nyq_ky : dky : Nyq_ky-dky; % y wavenumber
data_wavenumberdomain = fftshift(fft2(data_spacedomain)); % transform data
data_wavenumberdomain_filtered = data_wavenumberdomain; % copy
% Set high-frequency components with hypot(kx,ky) > 2 to 0
for i1 = 1:Nx
for j1 = 1:Ny
if hypot(kx(i1),ky(j1)) > 2
data_wavenumberdomain_filtered(j1,i1) = 0;
end
end
end
data_spacedomain_filtered = ifft2(ifftshift(data_wavenumberdomain_filtered));
figure;
subplot(3,1,1);
imagesc(kx,ky,abs(data_wavenumberdomain_filtered));
colorbar; v = caxis;
title(sprintf('Wavenumber Domain\n\nFiltered'));
xlabel('kx'); ylabel('ky');
subplot(3,1,2);
imagesc(kx,ky,abs(data_wavenumberdomain));
colorbar; caxis(v);
title('Unfiltered');
xlabel('kx'); ylabel('ky');
subplot(3,1,3);
imagesc(kx,ky,abs(data_wavenumberdomain_filtered-data_wavenumberdomain));
colorbar; caxis(v);
title('Difference');
xlabel('kx'); ylabel('ky');
figure;
subplot(3,1,1);
imagesc(x,y,data_spacedomain_filtered);
colorbar; v = caxis;
title(sprintf('Space Domain\n\nFiltered'));
xlabel('x'); ylabel('y');
subplot(3,1,2);
imagesc(x,y,data_spacedomain);
colorbar; caxis(v);
title('Unfiltered');
xlabel('x'); ylabel('y');
subplot(3,1,3);
imagesc(x,y,data_spacedomain_filtered-data_spacedomain);
colorbar; caxis(v);
title('Difference');
xlabel('x'); ylabel('y');
1 Comment
chris crowley
on 6 May 2020
This code works, but is pretty slow. That for loop and be completely eliminated. Replace the for loop with:
[KX,KY] = meshgrid(ky,kx);
data_wavenumberdomain_filtered(hypot(KX,KY) > 2) = 0;
Instead of looping over each index and checking the if condition, this utilizes MATLABs vectorized functionality. This runs in fractions of a second for the images I have been trying it on as apposed to a few minutes it took the above code.
Categories
Find more on Images in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!