How to show the 1D power spectrum ( by averaging the 2D spectrum of an image from fft2) in frequency domain?
1 次查看(过去 30 天)
显示 更早的评论
I've already read this one(will post below), but I hope you can help me how can I show the 1D spectrum in frequency domain? Here is the code . Thanks you
% 2D FFT Demo to get average radial profile of the spectrum of an image. 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 = fileparts(which('cameraman.tif')); % Determine where demo folder is (works with all versions). 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, 3, 1); imshow(grayImage) axis on; title('Original Gray Scale Image', 'FontSize', fontSize) % Enlarge figure to full screen. set(gcf, 'units','normalized','outerposition',[0 0 1 1]); % Perform 2D FFTs fftOriginal = fft2(double(grayImage)); % Move center from (1,1) to (129, 129) (the middle of the matrix). shiftedFFT = fftshift(fftOriginal); subplot(2, 3, 2); scaledFFTr = 255 * mat2gray(real(shiftedFFT)); imshow(log(scaledFFTr), []); title('Log of Real Part of Spectrum', 'FontSize', fontSize) subplot(2, 3, 3); scaledFFTi = mat2gray(imag(shiftedFFT)); imshow(log(scaledFFTi), []); axis on; title('Log of Imaginary Part of Spectrum', 'FontSize', fontSize) % Display magnitude and phase of 2D FFTs subplot(2, 3, 4); shiftedFFTMagnitude = abs(shiftedFFT); imshow(log(abs(shiftedFFTMagnitude)),[]); axis on; colormap gray title('Log Magnitude of Spectrum', 'FontSize', fontSize) % Get the average radial profile midRow = rows/2+1 midCol = columns/2+1 maxRadius = ceil(sqrt(129^2 + 129^2)) radialProfile = zeros(maxRadius, 1); count = zeros(maxRadius, 1); for col = 1 : columns for row = 1 : rows radius = sqrt((row - midRow) ^ 2 + (col - midCol) ^ 2); thisIndex = ceil(radius) + 1; radialProfile(thisIndex) = radialProfile(thisIndex) + shiftedFFTMagnitude(row, col); count(thisIndex) = count(thisIndex) + 1; end end % Get average radialProfile = radialProfile ./ count; subplot(2, 3, 5:6); plot(radialProfile, 'b-', 'LineWidth', 2); grid on; title('Average Radial Profile of Spectrum', 'FontSize', fontSize)
0 个评论
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Spectral Analysis 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!