img=cv.imread("E:\python\project\gray.jpg") img_float32=np.float32(img) dft=cv.dft(img_float32,flags=cv.DFT_COMPLEX_OUTPUT) dft_shift=np.fft.fftshift(dft) rows,cols=img.shape crow,ccol=int(row/2),int(cols/2) mask=np.zeros((rows,cols,2),np.uint8) mask[crow-30:crow+30,ccol-30:ccol+30]=1 fshift=dft_shift*mask f_ishift=np.fft.ifftshift(fshift) img_back=cv.idft(f_ishift) img_back=cv.magnitude(img_back[:,:,0],img_back[:,:,1]) plt.subplot(121).plt.imshow(img,camp='gray') plt.title('input image'),plt.xticks([]),plt.yticks([]) plt.subplot(122),plt.imshow(img_back,camp=gray) plt.titele('result'),plt.xticks([]),plt.yticks([]) plt.show() cv.waitKey(0) #按下任意键后再关闭显示窗口 cv.destroyAllWindows() 修改代码,解决图像不显示问题
时间: 2024-04-17 08:24:25 浏览: 109
Python各类图像库的图片读写方式总结(推荐)
要解决图像不显示的问题,你可以尝试按照以下方式修改代码:
```python
import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt
img = cv.imread("E:\python\project\gray.jpg")
img_float32 = np.float32(img)
dft = cv.dft(img_float32, flags=cv.DFT_COMPLEX_OUTPUT)
dft_shift = np.fft.fftshift(dft)
rows, cols = img.shape[:2]
crow, ccol = int(rows/2), int(cols/2)
mask = np.zeros((rows, cols, 2), np.uint8)
mask[crow-30:crow+30, ccol-30:ccol+30] = 1
fshift = dft_shift * mask
f_ishift = np.fft.ifftshift(fshift)
img_back = cv.idft(f_ishift)
img_back = cv.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('Result'), plt.xticks([]), plt.yticks([])
plt.show()
cv.waitKey(0) # 按下任意键后再关闭显示窗口
cv.destroyAllWindows()
```
这样修改后,应该可以正确显示图像了。注意检查文件路径是否正确,并确保安装了必要的库(如cv2、numpy和matplotlib)。
阅读全文