# Can someone break down this code and tell me what it does so I can better understand it?

2 Ansichten (letzte 30 Tage)
Westin Messer am 2 Mär. 2018
Kommentiert: John BG am 2 Mär. 2018
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);
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 Kommentare5 ältere Kommentare anzeigen5 ältere Kommentare ausblenden
John BG am 2 Mär. 2018
good, can we now focus on the question?
Westin Messer am 2 Mär. 2018

Melden Sie sich an, um zu kommentieren.

### Akzeptierte Antwort

John BG am 2 Mär. 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;
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
John BG
##### 1 Kommentar-1 ältere Kommentare anzeigen-1 ältere Kommentare ausblenden
John BG am 2 Mär. 2018
Danke Schön

Melden Sie sich an, um zu kommentieren.

### Kategorien

Mehr zu Convert Image Type finden Sie in Help Center und File Exchange

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by