Can someone break down this code and tell me what it does so I can better understand it?
1 view (last 30 days)
Show older comments
So far I know the following:
This code first asked the user to choose an image file of choice. The rgb2gray command converts the selected image to grey scale. In the main for loop, the Discrete Fourier Transform of the image is calculated. Once the transform has been done calculating. It is changed to a colormap to reflect the magnitudes of the transform. The absolute value is then plotted on a graph.
Here is the code:
clc;
clear all;
close all;
[filename, pathname, filterindex] = uigetfile( ...
{ '*.jpg','JPEG (*.jpg)'; ...
'*.bmp','Windows Bitmap (*.bmp)'; ...
'*.fig','Figures (*.fig)'; ...
'*.*', 'All Files (*.*)'}, ...
'Choose image(s) to be processed', ...
'MultiSelect', 'off');
if filterindex==0, break;end
filename=cellstr(filename);
im2= imread(horzcat(pathname,char(filename)));
im1=rgb2gray(im2);
im3=im2double(im1);
[n,m]=size(im3);
c1=0;
h = waitbar(0,'Calculating XXX please wait......');
k=1;l=1;
for l=0:1:m-1
for k=0:1:n-1
for x=0:1:n-1
for y=0:1:m-1
a=x+1;b=y+1;
c= im3(a,b) * exp(-1i*2*pi*(k*x/n + l*y/m));
c1=c1+c;
end
end
aa=l+1;bb=k+1;
im(bb,aa)=c1;
c1=0;
end
waitbar(l / m);
end
ims = im*255;
close(h)
imshow(ims);title('XXX plot');
figure
imshow(log(abs(ims)),[-1 5]); colormap(jet); colorbar;title('absolute value of XXX plot');
7 Comments
Accepted Answer
John BG
on 2 Mar 2018
Hi Westin Messer
1. instead of all those for loops, for a 2D fft, would you consider using the command fft2 instead?
clc;clear all;close all;
im1=imread('atrium.jpg');
figure;imshow(im1);im2=rgb2gray(im1);
figure;imshow(im2);im3=im2double(im2);
figure;imshow(im3);
ims=fft2(im3);
figure;imshow(abs(ims));title('this is FFT2D(im) plot');
figure;imshow(10*log10(abs(ims)));
colormap(jet); colorbar;title('absolute value of FFT(im) plot');
2.-
there's something wrong with the way you are attempting to use the waitbars.
If I were you I would leave this cosmetics feature for the end of the coding process. Just a hint, if you produce a handle to the waitbar named 'h' then you should be using that handle in the gradual update of waitbar, but you are currently not using such handle, in the update I mean.
Contrary to many other MATLAB commands, the handle goes at the end of input fields of waitbar.
3.-
Instead of log have you tried 10*log10?
figure;surf(abs(ims),'EdgeColor','none');
figure;surf(10*log10(abs(ims)),'EdgeColor','none');
4.-
At Mathworks they allow us to read the invoking part of the function fft2, key in the following
type fft2
Basically fft2 uses fft recursively.
fft2 explained here
the command fft2 performs a spectral sorting of the image components, in X dimension and Y dimension.
A vertical curtain has all horizontal variations and no vertical variations.
A horizontal curtain has all vertical variations and no horizontal variations.
In the equivalent SIMULINK block for FFT 2D there is a short explanation
.
Herr Messer
If you find this answer useful would you please be so kind to consider marking my answer as Accepted Answer?
To any other reader, if you find this answer useful please consider clicking on the thumbs-up vote link
thanks in advance
John BG
More Answers (0)
See Also
Categories
Find more on Image Processing Toolbox 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!