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.
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.
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




