I need to calculate the first derivative of a greyscale image (a 2D array) using a DFT function I built (which works). Unfortunately, the results don't seem to be correct.
In the fourier domain, the derivative d/dx is given as F(u,v)* 2*pi*i/N * u, where u is the x-axis transformed, N is the size of one of the matrix's dimensions, v being the other one.
Attached is the code. What bothers me is that I'm not getting the same results as I would differentiating by convolution with (1,-1) or (1,-1) as a column vector.
def derivative(fourier_signal): """ Derivative in fourier domain is multiplying by u or v, and 2pi*i/N :param fourier_signal: :return: """ N = np.shape(fourier_signal)[ZERO] M = np.shape(fourier_signal)[ONE] u = np.arange(N) v = np.arange(M) du = fourier_signal * (u*TWO_PI*1j)/N dv = fourier_signal * (v*TWO_PI*1j)/M return du, dv def fourier_der(im): # Calculate DFT2 dft_image = DFT2(im) # Function that Multiply by rows by u, columns by y du, dv = derivative(dft_image) shifted_du, shifted_dv = np.fft.fftshift(du), np.fft.fftshift(dv) dx, dy = IDFT2(shifted_du), IDFT2(shifted_dv)
I'm not looking for easy answers on how to do it, but rather a direction to as to why my output is incorrect.