def do_stuff(): image = get_image() dft = cv2.dft(np.float32(image), flags=cv2.DFT_COMPLEX_OUTPUT) # shift the zero-frequncy component to the center of the spectrum dft_shift = np.fft.fftshift(dft) # save image of the image in the fourier domain. magnitude_spectrum = 20 * np.log( cv2.magnitude(dft_shift[:, :, 0], dft_shift[:, :, 1])) # magnitude_spectrum = 20 * np.log(np.sqrt(dft_shift[:, :, 0] + dft_shift[:, :, 1])) cv2.imwrite("magnitude_spectrum.png", magnitude_spectrum) rows, cols = image.shape crow, ccol = rows // 2, cols // 2 # create a mask first, center square is 1, remaining all zeros mask = np.zeros((rows, cols, 2), np.uint8) mask[crow - 30:crow + 30, ccol - 30:ccol + 30] = 1 # mask -= 1 # mask = np.abs(mask) # apply mask and inverse DFT fshift = dft_shift * mask f_ishift = np.fft.ifftshift(fshift) img_back = cv2.idft(f_ishift) img_back = cv2.magnitude(img_back[:, :, 0], img_back[:, :, 1]) img_back = img_back / img_back.max() * 255 cv2.imwrite("filtered_image_low.png", img_back)
def LPK_OpenCv(mask, r, c, D0, dftShift): mask[r - D0:r + D0, c - D0:c + D0] = 1 fshift = dftShift * mask ishift = np.fft.ifftshift(fshift) iimage = cv2.idft(ishift) iimage = cv2.magnitude(iimage[:, :, 0], iimage[:, :, 1]) return iimage
def apply_mask(img, mask): dft_shift = apply_fourier_transform(img) fshift = dft_shift * mask f_ishift = np.fft.ifftshift(fshift) img_back = cv2.idft(f_ishift) img_back = cv2.magnitude(img_back[:, :, 0], img_back[:, :, 1]) return img_back
def DFT_IDFT_OpenCv(image): dft = cv2.dft(np.float32(image), flags=cv2.DFT_COMPLEX_OUTPUT) dftShift = np.fft.fftshift(dft) fourier = 20 * np.log(cv2.magnitude(dftShift[:, :, 0], dftShift[:, :, 1])) ishift = np.fft.ifftshift(dftShift) iimage = cv2.idft(ishift) iimage = cv2.magnitude(iimage[:, :, 0], iimage[:, :, 1]) Plt_Contrast(image, fourier, iimage)
def HPK_OpenCv(r, c, D0, dftShift): dftShift[r - D0:r + D0, c - D0:c + D0] = 0 ishift = np.fft.ifftshift(dftShift) iimage = cv2.idft(ishift) iimage = cv2.magnitude(iimage[:, :, 0], iimage[:, :, 1]) return iimage
from matplotlib import pyplot as plt import numpy as np img = cv.imread('tree.jpg', 0) dft = cv.dft(np.float32(img), flags=cv.DFT_COMPLEX_OUTPUT) dft_shift = np.fft.fftshift(dft) magnitude_spectrum = 20 * \ np.log(cv.magnitude(dft_shift[:,:, 0], dft_shift[:,:, 1])) plt.subplot(121), plt.imshow(img, cmap='gray') plt.title('Source'), plt.xticks([]), plt.yticks([]) plt.subplot(122), plt.imshow(magnitude_spectrum, cmap='gray') plt.title('Magnitude Spectrum'), plt.xticks([]), plt.yticks([]) plt.show() rows, cols = img.shape crow, ccol = int(rows / 2), int(cols / 2) mask = np.zeros((rows, cols, 2), np.uint8) mask[crow - 30:crow + 30, ccol - 30:ccol + 30] = 1 fshift = dft_shift * mask f_ishift = np.fft.ifftshift(fshift) img_back = cv.idft(f_ishift) plt.subplot(121), plt.imshow(img, cmap='gray') plt.title('Source'), plt.xticks([]), plt.yticks([]) plt.subplot(122), plt.imshow(img_back, cmap='gray') plt.title('Magnitude Spectrum'), plt.xticks([]), plt.yticks([]) plt.imshow([])