Two dimensional fourier transform
7 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
How to determine and display the two dimensional fourier transform of a thin, rectangular object? The object should be 2 by 10 pixels in size and solid white against a black background. The size of the image should bye 22x30 pixels and the rectangular object should be placed in the center of the image. fft2 and n=128 as a mesh plot as well as an image plot. Before plotting log of [Fmat(u,v)] needs to be done to improve the details.
0 Commenti
Risposta accettata
Image Analyst
il 27 Gen 2013
For whatever it's worth, here's my FFT demo (that I've posted before). It does some filtering in the spatial domain by a 2D rect so you can see the 2D sinc multiplication effect in the Fourier domain. Not exactly what you want to do, but close. Once you understand it, you can adapt it.
% 2D FFT Demo
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures.
clear; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.
format longg;
format compact;
fontSize = 20;
% Change the current folder to the folder of this m-file.
if(~isdeployed)
cd(fileparts(which(mfilename)));
end
% Check that user has the Image Processing Toolbox installed.
hasIPT = license('test', 'image_toolbox');
if ~hasIPT
% User does not have the toolbox installed.
message = sprintf('Sorry, but you do not seem to have the Image Processing Toolbox.\nDo you want to try to continue anyway?');
reply = questdlg(message, 'Toolbox missing', 'Yes', 'No', 'Yes');
if strcmpi(reply, 'No')
% User said No, so exit.
return;
end
end
% Read in a standard MATLAB gray scale demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
baseFileName = 'cameraman.tif';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% File doesn't exist -- didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
% Read in image.
grayImage = imread('cameraman.tif');
[rows columns numberOfColorChannels] = size(grayImage)
if numberOfColorChannels > 1
grayImage = rgb2gray(grayImage);
end
% Display original grayscale image.
subplot(2, 2, 1);
imshow(grayImage)
title('Original Gray Scale Image', 'FontSize', fontSize)
% Perform 2D FFTs
fftOriginal = fft2(double(grayImage));
shiftedFFT = fftshift(fftOriginal);
subplot(2, 2, 2);
imshow(real(shiftedFFT));
title('Real Part of Spectrum', 'FontSize', fontSize)
subplot(2, 2, 3);
imshow(imag(shiftedFFT));
title('Imaginary Part of Spectrum', 'FontSize', fontSize)
% Display magnitude and phase of 2D FFTs
subplot(2, 2, 4);
imshow(log(abs(shiftedFFT)),[]);
colormap gray
title('Log Magnitude of Spectrum', 'FontSize', fontSize)
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Now convolve with a 2D rect function.
figure;
rectWidth = 10;
rectHeight = 5;
kernel = ones(rectHeight, rectWidth) / (rectHeight * rectWidth);
% Display it
subplot(2, 2, 1);
k = padarray(kernel, [3, 3]); % Just for display.
imshow(k, []);
axis on;
title('Kernel', 'FontSize', fontSize)
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Convolve kernel (box filter) with the image
filteredImage = conv2(double(grayImage), kernel, 'same');
% Display filtered image.
subplot(2, 2, 2);
imshow(filteredImage,[]);
title('Filtered Image', 'FontSize', fontSize)
% Perform 2D FFT on the filtered image to see its spectrum.
% We expect to see a sinc multiplication effect.
% It should look like the original but with a sinc pattern overlaid on it.
fftFiltered = fft2(double(filteredImage));
shiftedFFT = fftshift(fftFiltered);
% Display magnitude of the 2D FFT of the filtered image.
subplot(2, 2, 3);
imshow(log(abs(shiftedFFT)),[]);
colormap gray
title('Log Magnitude of Spectrum - Note sinc multiplication', 'FontSize', fontSize)
Più risposte (1)
Matt J
il 27 Gen 2013
Modificato: Matt J
il 27 Gen 2013
Well, a few hints since it's obviously homework
- Use ifftshift and fftshift in appropriate places
- Know where the origin is and where the "center" of the rectangle should be
A 1D example
>> x=[0 0 1 1 1 1 1 0]; fftshift( fft(ifftshift(x)) )
ans =
1.0000 -0.4142 -1.0000 2.4142 5.0000 2.4142 -1.0000 -0.4142
8 Commenti
Image Analyst
il 27 Gen 2013
I don't know what she means by a mesh plot. You can't use an fft with 128 samples on an image with less than 128 samples (like 22 or 30) - it just won't fit. Regarding the black display, since you're displaying a floating point image, make sure you use [] in imshow:
imshow(theImage, []);
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!