python - np.fft.ifft2 将图像完全变黑

标签 python numpy opencv

我已经为此工作了大约五个小时。我已经阅读了 opencv 文档并尝试复制他们的示例代码,但仍然无法获得我想要的结果。

我只想对我的图像进行逆傅里叶变换,但在我运行 np.fft.ifft2 函数后,所有像素值几乎降为 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()

Image before and after transformation

Image after shifting, applying masks, and shifting back

Image after applying inverse transform

最佳答案

输入:

enter image description here

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)

低通掩模:

enter image description here

低通蒙版模糊(抗锯齿):

enter image description here

来自 Mask 的 IFT:

enter image description here

来自过滤掩码的 IFT:

enter image description here

关于python - np.fft.ifft2 将图像完全变黑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66287425/

相关文章:

Python如何对二维数据的大点云进行插值

python - 部分读取大型 numpy 文件的有效方法?

opencv - OPEN_CV ERROR 没有足够的值来解包(预期为 3,实际为 2)

python - 如何计算每个月或某个月份的事件日期

python - 如何在标识空列表的 numpy bool 数组中创建空列表?

c++ - 如何估计相机从场景中拍摄优质图像的曝光时间

python - 如何使用 Python OpenCV 计算图像中形状的面积?

python - 在Python中生成所有可能的组合

python - 如何从 Pandas 数据框中满足条件的位置获取前一行

python - 如何使用循环将文件导入 pandas?