The discrete Fourier transform of Python images

Source: Internet
Author: User

The two-dimensional discrete Fourier transform of image (MxN) can transform the image from the spatial domain to the frequency domain, the spatial coordinates are represented by x, y in the space domain, the frequency domain is represented by u,v, and the formula of the two-dimensional discrete Fourier transform is as follows:

In Python, the FFT module of the NumPy Library has a two-dimensional discrete Fourier transform function, the function is fft2, input a grayscale graph, output after two-dimensional discrete Fourier transform results, but the implementation is not directly using the above formula, but with the fast Fourier transform. The results need to be visualized by using ABS for absolute value, but the visual effect is not ideal because the Fourier spectrum is so large that the log logarithmic transform is used to improve the visual effect.

In addition, the origin of the image transformation needs to be moved to the center of the frequency domain rectangle, so use the Fftshift function for the fft2 result. Finally, you can use log to improve visualization.

The code is as follows:

1 ImportNumPy as NP2 ImportMatplotlib.pyplot as Plt3 4img = Plt.imread ('photo.jpg')5 6 #turn to grayscale by formula7IMG = 0.2126 * img[:,:,0] + 0.7152 * img[:,:,1] + 0.0722 * img[:,:,2]8 9 #Show OriginalTenPlt.subplot (231), Plt.imshow (IMG,'Gray'), Plt.title ('Original') One  A #Fourier transforms and displays the results -FFT2 =np.fft.fft2 (IMG) -Plt.subplot (232), Plt.imshow (Np.abs (fft2),'Gray'), Plt.title ('fft2') the  - #moves the origin of the image transformation to the center of the frequency domain rectangle and displays the effect -Shift2center =Np.fft.fftshift (fft2) -Plt.subplot (233), Plt.imshow (Np.abs (Shift2center),'Gray'), Plt.title ('Shift2center') +  - #logarithmic transformation of the results of Fourier transforms and display of effects +LOG_FFT2 =Np.log (Np.abs (fft2)) APlt.subplot (235), Plt.imshow (log_fft2,'Gray'), Plt.title ('log_fft2') at  - #logarithmic transformation of the result after centering and displaying the result -Log_shift2center =Np.log (Np.abs (shift2center)) -Plt.subplot (236), Plt.imshow (Log_shift2center,'Gray'), Plt.title ('Log_shift2center')

Operation Result:

The two-dimensional discrete Fourier transforms are implemented according to the formula as follows:

1 ImportNumPy as NP2 ImportMatplotlib.pyplot as Plt3PI = 3.1415912654img = Plt.imread ('temp.jpg')5 6 #turn to grayscale by formula7IMG = 0.2126 * img[:,:,0] + 0.7152 * img[:,:,1] + 0.0722 * img[:,:,2]8 9 #Show OriginalTenPlt.subplot (131), Plt.imshow (IMG,'Gray'), Plt.title ('Original') One  A #Fourier transforms and displays the results -FFT2 =np.fft.fft2 (IMG) -LOG_FFT2 =Np.log (Np.abs (fft2)) thePlt.subplot (plt.imshow), log_fft2,'Gray'), Plt.title ('log_fft2') -  -H, W =Img.shape - #generate a complex matrix of the same size +F = Np.zeros ([h,w],'complex128') -  forUinchRange (h): +      forVinchRange (W): Ares =0 at          forXinchRange (h): -              forYinchRange (W): -Res + = img[x,y] * NP.EXP ( -1.J * 2 * PI * (U * x/h + v * Y/W)) -F[U,V] =Res -Log_f =Np.log (Np.abs (F)) -Plt.subplot (133), Plt.imshow (Log_f,'Gray'), Plt.title ('Log_f')

It's so slow to get straight to the formula that I don't want to wait for it to run out ...

The discrete Fourier transform of Python images

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.