My goal is to obtain a plot with the spatial frequencies of an image - kind of like doing a fourier transformation on it. I don't care about the position on the image of features with the frequency f (for instance); I'd just like to have a graphic which tells me how much of every frequency I have (the amplitude for a frequency band could be represented by the sum of contrasts with that frequency).
I am trying to do this via the numpy.fft.fft2
function.
Here is a link to a minimal example portraying my use case.
As it turns out I only get distinctly larger values for frequencies[:30,:30]
, and of these the absolute highest value is frequencies[0,0]
. How can I interpret this?
frequency[0,0]
What is a 0 Hz
frequency?Description. Y = fft2( X ) returns the two-dimensional Fourier transform of a matrix using a fast Fourier transform algorithm, which is equivalent to computing fft(fft(X). ').
Compute the 2-dimensional discrete Fourier Transform. This function computes the n-dimensional discrete Fourier Transform over any axes in an M-dimensional array by means of the Fast Fourier Transform (FFT). By default, the transform is computed over the last two axes of the input array, i.e., a 2-dimensional FFT.
fft2 Discrete Fourier transform in two dimensions. ifft2 Inverse discrete Fourier transform in two dimensions. fftn Discrete Fourier transform in N-dimensions.
numpy. fft. fftfreq - returns a float array of the frequency bin centers in cycles per unit of the sample spacing.
freq
has a few very large values, and lots of small values. You can see that by plotting
plt.hist(freq.ravel(), bins=100)
(See below.) So, when you use
ax1.imshow(freq, interpolation="none")
Matplotlib uses freq.min()
as the lowest value in the color range (which is by default colored blue), and freq.max()
as the highest value in the color range (which is by default colored red). Since almost all the values in freq
are near the blue end, the plot as a whole looks blue.
You can get a more informative plot by rescaling the values in freq
so that the low values are more widely distributed on the color range.
For example, you can get a better distribution of values by taking the log
of freq
. (You probably don't want to throw away the highest values, since they correspond to frequencies with the highest power.)
import matplotlib as ml
import matplotlib.pyplot as plt
import numpy as np
import Image
file_path = "data"
image = np.asarray(Image.open(file_path).convert('L'))
freq = np.fft.fft2(image)
freq = np.abs(freq)
fig, ax = plt.subplots(nrows=2, ncols=2, figsize=(14, 6))
ax[0,0].hist(freq.ravel(), bins=100)
ax[0,0].set_title('hist(freq)')
ax[0,1].hist(np.log(freq).ravel(), bins=100)
ax[0,1].set_title('hist(log(freq))')
ax[1,0].imshow(np.log(freq), interpolation="none")
ax[1,0].set_title('log(freq)')
ax[1,1].imshow(image, interpolation="none")
plt.show()
From the docs:
The output, analogously to fft, contains the term for zero frequency in the low-order corner of the transformed axes,
Thus, freq[0,0]
is the "zero frequency" term. In other words, it is the constant term in the discrete Fourier Transform.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With