python-opencv performs Fourier transform on the image and displays its spectrum information

python-opencv performs Fourier transform on the image and displays its spectrum information

import os
import cv2
import numpy as np
import matplotlib.pyplot as plt

def read_img(img_7_path):
    img=cv2.imread(img_7_path,0)
    dft=cv2.dft(np.float32(img),flags=cv2.DFT_COMPLEX_OUTPUT)
    dftshift=np.fft.fftshift(dft)
    result=20*np.log(cv2.magnitude(dftshift[:,:,0],dftshift[:,:,1]))
    plt.subplot(121), plt.imshow(img, cmap='gray')
    plt.title('original'),plt.axis('off')
    plt.subplot(122),plt.imshow(result,cmap='gray')
    plt.title('original'), plt.axis('off')
    plt.show()
if __name__ == '__main__':
    work_path=os.getcwd()
    data_path=os.path.join(work_path,'data')
    img_7_path=os.path.join(data_path,'7.jpg')
    read_img(img_7_path)

Insert picture description here

Guess you like

Origin blog.csdn.net/qestion_yz_10086/article/details/107904347