How to transform a FFT (Fast Fourier Transform) into a Polar Transformation with Python?
Question:
I was able to create a FFT Transformation from my image but I don’t know how to continue…
I am using this to solve my problem: Align text for OCR
This code was all that worked for me until now:
import cv2
import numpy as np
from matplotlib import pyplot as plt
%matplotlib inline
img = cv2.imread(r'test.jpg', cv2.IMREAD_GRAYSCALE)
f = np.fft.fft2(img)
fshift = np.fft.fftshift(f)
magnitude_spectrum = 20 * np.log(np.abs(fshift))
plt.subplot(121), plt.imshow(img, cmap='gray')
plt.title('Input Image'), plt.xticks([]), plt.yticks([])
plt.subplot(122), plt.imshow(magnitude_spectrum, cmap='gray')
plt.title('Magnitude Spectrum'), plt.xticks([]), plt.yticks([])
plt.show()
I need the mean value generated from a Polar Transformation, but I don’t know how to transform a FFT to a Polar Transformation in Python.
Answers:
This is roughly solution to you problem; It was tested on one sample image, and the result looks credible.
# your code goes here...
def transform_data(m):
dpix, dpiy = m.shape
x_c, y_c = np.unravel_index(np.argmax(m), m.shape)
angles = np.linspace(0, np.pi*2, min(dpix, dpiy))
mrc = min(abs(x_c - dpix), abs(y_c - dpiy), x_c, y_c)
radiuses = np.linspace(0, mrc, max(dpix, dpiy))
A, R = np.meshgrid(angles, radiuses)
X = R * np.cos(A)
Y = R * np.sin(A)
return A, R, m[X.astype(int) + mrc - 1, Y.astype(int) + mrc - 1]
angles, radiuses, m = transform_data(magnitude_spectrum)
plt.contourf(angles, radiuses, m)
Finally, we can get the angle we want to turn the original image:
sample_angles = np.linspace(0, 2 * np.pi, len(c.sum(axis=0))) / np.pi*180
turn_angle_in_degrees = 90 - sample_angles[np.argmax(c.sum(axis=0))]
For my sample image I got:
turn_angle_in_degrees = 3.2015810276679844 degrees.
Also, we can plot projected spectrum magnitude:
plt.plot(sample_angles, c.sum(axis=0))
Hope that helps…
I was able to create a FFT Transformation from my image but I don’t know how to continue…
I am using this to solve my problem: Align text for OCR
This code was all that worked for me until now:
import cv2
import numpy as np
from matplotlib import pyplot as plt
%matplotlib inline
img = cv2.imread(r'test.jpg', cv2.IMREAD_GRAYSCALE)
f = np.fft.fft2(img)
fshift = np.fft.fftshift(f)
magnitude_spectrum = 20 * np.log(np.abs(fshift))
plt.subplot(121), plt.imshow(img, cmap='gray')
plt.title('Input Image'), plt.xticks([]), plt.yticks([])
plt.subplot(122), plt.imshow(magnitude_spectrum, cmap='gray')
plt.title('Magnitude Spectrum'), plt.xticks([]), plt.yticks([])
plt.show()
I need the mean value generated from a Polar Transformation, but I don’t know how to transform a FFT to a Polar Transformation in Python.
This is roughly solution to you problem; It was tested on one sample image, and the result looks credible.
# your code goes here...
def transform_data(m):
dpix, dpiy = m.shape
x_c, y_c = np.unravel_index(np.argmax(m), m.shape)
angles = np.linspace(0, np.pi*2, min(dpix, dpiy))
mrc = min(abs(x_c - dpix), abs(y_c - dpiy), x_c, y_c)
radiuses = np.linspace(0, mrc, max(dpix, dpiy))
A, R = np.meshgrid(angles, radiuses)
X = R * np.cos(A)
Y = R * np.sin(A)
return A, R, m[X.astype(int) + mrc - 1, Y.astype(int) + mrc - 1]
angles, radiuses, m = transform_data(magnitude_spectrum)
plt.contourf(angles, radiuses, m)
Finally, we can get the angle we want to turn the original image:
sample_angles = np.linspace(0, 2 * np.pi, len(c.sum(axis=0))) / np.pi*180
turn_angle_in_degrees = 90 - sample_angles[np.argmax(c.sum(axis=0))]
For my sample image I got:
turn_angle_in_degrees = 3.2015810276679844 degrees.
Also, we can plot projected spectrum magnitude:
plt.plot(sample_angles, c.sum(axis=0))
Hope that helps…