How to calculate a gradient by fft ???

 Accepted Answer

It's still not clear what you mean by 'simple "gradient"' - that could refer to a variety of things.
One possibility is that you mean just the differences between adjacent values - what you'd get by computing
diff(im, 1, 2) % x differences - x component of gradient
and
diff(im, 1, 1) % y differences - y component of gradient
for example. You can do a similar operation in the frequency domain using the fact that the differentiation operator transforms to multiplication with ik (where k is the transform variable). The code looks like this for differentiating with respect to x:
im = imread('pout.tif'); % data
% compute differencing operator in the frequency domain
nx = size(im, 2);
hx = ceil(nx/2)-1;
ftdiff = (2i*pi/nx)*(0:hx); % ik
ftdiff(nx:-1:nx-hx+1) = -ftdiff(2:hx+1); % correct conjugate symmetry
% compute "gradient" in x using fft
g = ifft2( bsxfun(@times, fft2(im), ftdiff) );
imshow(g, []); % see result
As you can see, it's simpler to do it in the space domain. Why use the FFT?

7 Comments

thanks for your response ;
I would like to calculate gradient (function [GradX,GradY] = gradient(I) in MatLab) not to use this usual method, but calculate with fft.
my professeur wants to see the results in two ways.
This code calculates the gradient of the fft along the x axis, in y axis is the same?
OK I understand now. The code I've given you above computes a result which is close to (but not quite the same as) GradX from the gradient function. As it's an exercise, you should understand what my code does, so that you can adjust it to compute the y gradient - the principle is the same.
I read your code, but i still not understood how can i calculate the gradient in y direction ???
you may give me an idea or steps that i should follows ??
step 1: copy the portion of the code after the imread.
step 2: in the copy, change all of the x to y
step 3: in the copy, change size(im,2) to something appropriate for calculating the size in the other direction
I actually changed size(im,2) by size(im,1), but it gives me the same results ??
this is the following code
im = imread('pout.tif');
ny = size(im, 1);
hy = ceil(ny/2)-1;
ftdiff2 = (2i*pi/ny)*(0:hy);
ftdiff2(ny:-1:ny-hy+1) = -ftdiff2(2:hy+1);
% compute "gradient" in x using fft
g2 = ifft2( bsxfun(@times, fft2(im), ftdiff2) );
figure(2),
imshow(g2, []);
Well, you have to think about the shapes of the arrays. Differentiating with respect to x means multiplying each row of the the Fourier transform by ftdiff. If you want to differentiate with respect to y, you have to multiply each column by ftdiff. Changing the size of ftdiff is a start, but you also have to change it from a row vector to a column vector.

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 19 Sep 2011
Is that plain "gradient", or is it "conjugate gradient" ?
(I notice you posted the same question to some other locations, all of which seem to have replied asking for clarification.)

1 Comment

I want to calculate simple "gradient" for images by using "fft"

Sign in to comment.

Categories

Find more on Fourier Analysis and Filtering 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!