Home > Back-end >  transforming image using OpenCV dft and idft, looks completely white when saved
transforming image using OpenCV dft and idft, looks completely white when saved

Time:02-20

I wrote the following code to convert several images. Until img_back, the image was converted correctly, but when this image is saved, only the white image is saved.
how to solve? thank you.


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()

CodePudding user response:

The data in img_back at a very high scale. I suggest normalizing the data before writing it to an image:

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

CodePudding user response:

You are missing the DFT_SCALE flag in either the dft() call or the idft() call.

Sections from the documentation:

When you encounter issues, always review the documentation of the things you use.

  • Related