[英]How to reverse Fourier Transform on an image with np.fft.ifft2 - Python
[英]np.fft.ifft2 turns image completely black
我已經為此工作了大約五個小時。 我已閱讀 opencv 文檔並嘗試復制他們的示例代碼,但仍然無法獲得我想要的結果。
我只想對我的圖像進行傅里葉逆變換,但是在我運行 np.fft.ifft2 function 之后,所有像素值都下降到幾乎為 0。
我很抱歉這篇長文,但我真的很感激你的幫助。
# Reading and Resizing image
img = cv.imread('image.jpg', cv.IMREAD_GRAYSCALE)
width = int(img.shape[1] * 0.25)
height = int(img.shape[0] * 0.15)
dim = (width, height)
img = cv.resize(img, dim, interpolation=cv.INTER_CUBIC)
f = np.fft.fft2(img)
fshift = np.fft.fftshift(f)
magnitude_spectrum = 20*np.log(np.abs(fshift))
plt.subplot(121), plt.imshow(img, cmap='gray')
plt.title('Input Image'), plt.xticks([]), plt.yticks([])
plt.subplot(122), plt.imshow(magnitude_spectrum, cmap='gray')
plt.title('Magnitude Spectrum'), plt.xticks([]), plt.yticks([])
plt.show()
# Making masks
rows, cols = img.shape
crow, ccol = rows//2, cols//2
low_pass_mask = np.zeros((rows, cols), np.uint8)
low_pass_mask[crow-100:crow+100, ccol-100:ccol+100] = 1
high_pass_mask = np.ones((rows, cols), np.uint8)
high_pass_mask[crow-100:crow+100, ccol-100:ccol+100] = 0
# Applying filters
low_pass_filtered = magnitude_spectrum * low_pass_mask
low_pass_filtered_shifted = np.fft.ifftshift(low_pass_filtered)
high_pass_filtered = magnitude_spectrum * high_pass_mask
high_pass_filtered_shifted = np.fft.ifftshift(high_pass_filtered)
# Displaying results of filters in frequency domain
plt.subplot(121), plt.imshow(low_pass_filtered_shifted, cmap='gray')
plt.title('low_pass_filtered_shifted'), plt.xticks([]), plt.yticks([])
plt.subplot(122), plt.imshow(high_pass_filtered_shifted, cmap='gray')
plt.title('high_pass_filtered_shifted'), plt.xticks([]), plt.yticks([])
plt.show()
# Transforming images back to the space domain
low_pass_result = np.fft.ifft2(low_pass_filtered_shifted)
low_pass_result = np.abs(low_pass_result)
high_pass_result = np.fft.ifft2(high_pass_filtered_shifted)
high_pass_result = np.abs(high_pass_result)
# Displaying Results
plt.subplot(121), plt.imshow(low_pass_result, cmap='gray')
plt.title('Low Pass Result'), plt.xticks([]), plt.yticks([])
plt.subplot(122), plt.imshow(high_pass_result, cmap='gray')
plt.title('High Pass Result'), plt.xticks([]), plt.yticks([])
plt.show()
輸入:
import numpy as np
import cv2
# read input and convert to grayscale
#img = cv2.imread('lena_gray.png', cv2.IMREAD_GRAYSCALE)
img = cv2.imread('lena.png')
# do dft saving as complex output
dft = np.fft.fft2(img, axes=(0,1))
# apply shift of origin to center of image
dft_shift = np.fft.fftshift(dft)
# generate spectrum from magnitude image (for viewing only)
mag = np.abs(dft_shift)
spec = np.log(mag) / 20
# create circle mask
radius = 32
mask = np.zeros_like(img)
cy = mask.shape[0] // 2
cx = mask.shape[1] // 2
cv2.circle(mask, (cx,cy), radius, (255,255,255), -1)[0]
# blur the mask
mask2 = cv2.GaussianBlur(mask, (19,19), 0)
# apply mask to dft_shift
dft_shift_masked = np.multiply(dft_shift,mask) / 255
dft_shift_masked2 = np.multiply(dft_shift,mask2) / 255
# shift origin from center to upper left corner
back_ishift = np.fft.ifftshift(dft_shift)
back_ishift_masked = np.fft.ifftshift(dft_shift_masked)
back_ishift_masked2 = np.fft.ifftshift(dft_shift_masked2)
# do idft saving as complex output
img_back = np.fft.ifft2(back_ishift, axes=(0,1))
img_filtered = np.fft.ifft2(back_ishift_masked, axes=(0,1))
img_filtered2 = np.fft.ifft2(back_ishift_masked2, axes=(0,1))
# combine complex real and imaginary components to form (the magnitude for) the original image again
img_back = np.abs(img_back).clip(0,255).astype(np.uint8)
img_filtered = np.abs(img_filtered).clip(0,255).astype(np.uint8)
img_filtered2 = np.abs(img_filtered2).clip(0,255).astype(np.uint8)
cv2.imshow("ORIGINAL", img)
cv2.imshow("SPECTRUM", spec)
cv2.imshow("MASK", mask)
cv2.imshow("MASK2", mask2)
cv2.imshow("ORIGINAL DFT/IFT ROUND TRIP", img_back)
cv2.imshow("FILTERED DFT/IFT ROUND TRIP", img_filtered)
cv2.imshow("FILTERED2 DFT/IFT ROUND TRIP", img_filtered2)
cv2.waitKey(0)
cv2.destroyAllWindows()
# write result to disk
cv2.imwrite("lena_dft_numpy_mask.png", mask)
cv2.imwrite("lena_dft_numpy_mask_blurred.png", mask2)
cv2.imwrite("lena_dft_numpy_roundtrip.png", img_back)
cv2.imwrite("lena_dft_numpy_lowpass_filtered1.png", img_filtered)
cv2.imwrite("lena_dft_numpy_lowpass_filtered2.png", img_filtered2)
低通掩模:
低通蒙版模糊(抗鋸齒):
來自面具的 IFT:
過濾掩碼的 IFT:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.