简体   繁体   English

傅立叶变换opencv python FFT和DFT

[英]fourier transform opencv python FFT & DFT

How are you? 你好吗?

I've been trying to do the fourier transformed and the inverse fourier transformed but i have to do the following. 我一直在尝试进行傅立叶变换和逆傅立叶变换,但是我必须做以下事情。

  1. delete all the negative values of the real part and show the result of the reverse transformation. 删除实部的所有负值,并显示逆变换的结果。

  2. Show an image of the transform, highlighting those points where the value of the magnitude is greater than 50,000. 显示变换的图像,突出显示幅度值大于50,000的那些点。

Code: 码:

import numpy as np
import cv2
from matplotlib import pyplot as plt

img = cv2.imread('testQ.png',0)

img_float32 = np.float32(img)

dft = cv2.dft(img_float32, flags = cv2.DFT_COMPLEX_OUTPUT)
dft_shift = np.fft.fftshift(dft)

rows, cols = img.shape
crow, ccol = rows/2 , cols/2     # center

# create a mask first, center square is 1, remaining all zeros
mask = np.zeros((rows, cols, 2), np.uint8)
mask[int(crow-30):int(crow+30), int(ccol-30):int(ccol+30)] = 1

# apply mask and inverse DFT
fshift = dft_shift*mask
f_ishift = np.fft.ifftshift(fshift)
img_back = cv2.idft(f_ishift)
img_back = cv2.magnitude(img_back[:,:,0],img_back[:,:,1])
plt.subplot(121),plt.imshow(img, cmap = 'gray')
plt.title('Input Image'), plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(img_back, cmap = 'gray')
plt.title('Magnitude Spectrum'), plt.xticks([]), plt.yticks([])

plt.show()    

I tried to do the first point by doing this 我试图通过这样做做到第一点

img_back = img_back[img_back>=0]

but i got this error: 但是我得到了这个错误:

TypeError: Invalid dimensions for image data

Here is the image 这是图片

这是图像

也许您正在尝试做的是:

img_back[img_back<0] = 0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM