简体   繁体   English

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

[英]np.fft.ifft2 turns image completely black

I have been working on this for about five hours.我已经为此工作了大约五个小时。 I have read the opencv docs and tried copying their sample code and am still unable to get the results I want.我已阅读 opencv 文档并尝试复制他们的示例代码,但仍然无法获得我想要的结果。

I just want to do the inverse Fourier transform on my image, but after I run the np.fft.ifft2 function all the pixel values drop to almost 0.我只想对我的图像进行傅里叶逆变换,但是在我运行 np.fft.ifft2 function 之后,所有像素值都下降到几乎为 0。

I'm sorry for the long post but I would really appreciate the help.我很抱歉这篇长文,但我真的很感激你的帮助。

# 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应用逆变换后的图像

Input:输入:

在此处输入图像描述

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)

Low Pass Mask:低通掩模:

在此处输入图像描述

Low Pass Mask blurred (antialiased):低通蒙版模糊(抗锯齿):

在此处输入图像描述

IFT from Mask:来自面具的 IFT:

在此处输入图像描述

IFT from filtered Mask:过滤掩码的 IFT:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM