problem :
In image processing , After Fourier transform, the image needs to be passed through cv2.imshow() Display or pass cv2.imwrite() Save to the specified path , But the displayed or saved images are all white , Only through plt.imshow() To display properly . For example, the following code corresponds to the processing result :
import cv2
import numpy as np
import matplotlib.pyplot as plt
img = cv2.imread("Images/ExperimentalImage4/originalImage/architecture2.png",-1)
dft = cv2.dft(np.float32(img),flags = cv2.DFT_COMPLEX_OUTPUT)
dftShift = np.fft.fftshift(dft)
ishift = np.fft.ifftshift(dftShift)
iImage = cv2.idft(ishift)
iImg = cv2.magnitude(iImage[:,:,0],iImage[:,:,1])
plt.subplot(121),plt.imshow(img,cmap = "gray"),plt.title("original"),plt.axis('off')
plt.subplot(122),plt.imshow(iImg,cmap = "gray"),plt.title("inverse"),plt.axis('off')
cv2.imshow("inverse",iImg)
cv2.waitKey(10)
plt.show()
terms of settlement :
Divide each pixel value after inverse Fourier transform by the product ( Row number * Number of columns , For example, the image separation rate in this example is 640*512, Divide by 327680), Finally, convert the floating-point number to 8 Bit unsigned integer , Specific code and processing The results are shown below :
import cv2
import numpy as np
import matplotlib.pyplot as plt
img = cv2.imread("Images/ExperimentalImage4/originalImage/architecture2.png",-1)
dft = cv2.dft(np.float32(img),flags = cv2.DFT_COMPLEX_OUTPUT)
dftShift = np.fft.fftshift(dft)
ishift = np.fft.ifftshift(dftShift)
iImage = cv2.idft(ishift)
iImg = cv2.magnitude(iImage[:,:,0],iImage[:,:,1])
plt.subplot(121),plt.imshow(img,cmap = "gray"),plt.title("original"),plt.axis('off')
plt.subplot(122),plt.imshow(iImg,cmap = "gray"),plt.title("inverse"),plt.axis('off')
iImg = np.uint8(iImg / 327680) ## Divide each pixel value after inverse Fourier transform by the product ( Row number * Number of columns ), Finally, convert the floating-point number to 8 Bit unsigned integer
cv2.imwrite('./.inverse.png',iImg)
cv2.imshow("inverse",iImg)
cv2.waitKey(10)
plt.show()
Final display results :
That's the solution , I hope I can help you !