Data sets from image
1 view (last 30 days)
Show older comments
I am new to Matlab and I have a jpg image that I have loaded. the image is not shown as data points that are plotted... not sure if this makes sense.. you can display the image with imshow.
what i am wanting to do is take the fourier transform and multiply the image by functions of my choosing however i dont know how to go about doing this as the image dosnt seem to be displayed as "data points" per say??
0 Comments
Accepted Answer
Jonas Reber
on 7 Jun 2011
does it help if I provide you the following code as an example...?
%%Two Dimensional DFT Example
% 1.Load the sample image HeadCT.jpg, calculate its 2D DFT and plot the
% Fourier spectrum (increase its contrast if necessary) and phaseangles.
img=double(imread('HeadCT.jpg'));
figure, imshow(img,[]);
IMG=fftshift(fft2(img));
logspect=log(1+abs(IMG));
theta=angle(IMG);
figure
subplot(1,2,1);
imshow(theta,[]);title('Phase');
subplot(1,2,2);
imshow(logspect,[]);title('Spectrum');
% 2.Calculate the 2D Fourier transformation of the sample image using only
% the 1D Fourier transform fft().
IMG2=fftshift( fft(fft(img).').' );
max(max(abs(IMG-IMG2)))
% 3.Keep the phase of the Fourier transform and replace the magnitude of
% each frequency sample by its square root. Take the inverse DFT.
spect=sqrt(abs(IMG));
Z=spect.*exp(i*theta);
z=abs(ifft2(fftshift(Z)));
figure, imshow(z,[]);
% what do you see?
% the operation enhanced the edges. the reason being, that the low
% frequencies with lots of energy thus having an amplitude>1 are damped.
% The high frequencies on the other hand generally have little energy,
% i.e. amplitude<1, and are therefore enhanced by the sqrt.
% 4.Set the magnitude and phase of the high frequencies to zero and
% take the inverse DFT, and explain what you see.
[M N]=size(img);
u=0:M-1;
v=0:N-1;
%
idx=find(u>M/2);
u(idx)=u(idx)-M;
idx=find(v>N/2);
v(idx)=v(idx)-N;
%
[U,V]=meshgrid(v,u);
D=fftshift(sqrt(U.^2+V.^2));
mesh(D);
%
Z=IMG.*(D<75);
z=abs(ifft2(fftshift(Z)));
figure, imshow(z,[0 255]);
% setting the high frequencies to 0 equals a low pass filter
5 Comments
Jonas Reber
on 7 Jun 2011
exactly. if you have a grayscale image (IMG) of size MxN you can access the pixels IMG(y,x) (where y=1..N and x=1..M)
note. x and y are changed as indeces are handeled row x column.
M = 200; % the real width of the image
N = 100; % the real height of the image
IMG = zeros(N,M);
% put a white line to the half of the image,
% top left corner to center of left edge
IMG(1:end/2,1) = 1;
% put a gray square to the center of the image
IMG(end/2-10:end/2+10,end/2-5:end/2+5) = 0.5;
% put a horizontal line on the right side of the image
IMG(end/2,end/2:end) = 0.8;
imshow(IMG,[]);
it's basically just important to remember that x and y somehow seem to be changed :) as we are used to think in an x first way when we talk about images...
More Answers (0)
See Also
Categories
Find more on Image Processing Toolbox in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!