'Saving files with cv2.imwrite() making the output white completely after FFT conversion
I am trying to denoise images using Fast Fourier Transformation (FFT) in python using the reference from this site. Unfortunately my output image is becoming completely white. However, plotting them using matplotlib perfectly outputs! I do not know much about denoising. Any other suggestion is also welcome to properly denoise image. Could someone please help?
Follwoing is the code
import numpy as np
import cv2
from matplotlib import pyplot as plt
img = cv2.imread('sample.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[crow-30:crow+30, ccol-30: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])
cv2.imwrite('conv_sample.png', img_back)
plt.figure(figsize=(20, 20))
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()
Here is the input (sample.png) image

this is the output (sample.png) image (completely white!)

and here is the plot comparing two images

I am trying to save an image like the right one in the plot or with a better one. Thank you.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
