使用 OpenCV dft 和 idft 转换图像,保存时看起来完全是白色

transforming image using OpenCV dft and idft, looks completely white when saved

我写了下面的代码来转换几个图像。直到img_back,图像都被正确转换,但是当保存这张图像时,只保存了白色图像。
怎么解决?谢谢。


import cv2
import numpy as np

n=100
for number in range(0,n+1):
    filename_o = 'image/{1:06d}.png'.format(number)
    original_img = cv2.imread(filename_o,0)
    print(original_img)

    #convert img->magnitude inverse ft
    dft = cv2.dft(np.float32(original_img),flags = cv2.DFT_COMPLEX_OUTPUT)
    dft_shift = np.fft.fftshift(dft)
    rows, cols = original_img.shape
    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 = cv2.idft(f_ishift)
    img_back = cv2.magnitude(img_back[:,:,0],img_back[:,:,1])

    print(type(img_back))
    #save as ...
    #cv2.cvtColor(np.uint8(img_back), cv2.COLOR_GRAY2RGB)

    cv2.imwrite('image_mag/{1:06d}.png'.format(number), img_back)



print("finish")
cv2.waitKey(0)
cv2.destroyAllWindows()

img_back 中的数据规模非常大。我建议在将数据写入图像之前对其进行标准化:

normalized = img_back / img_back.max() * 255.0
cv2.imwrite('image_mag/{0:06d}.png'.format(number), normalized)

您在 dft() 调用或 idft() 调用中缺少 DFT_SCALE 标志。

文档中的部分:

当您遇到问题时,请始终查看您使用的东西的文档。