Created
May 24, 2021 09:04
-
-
Save JustinSDK/1724b3eae4dc3a1767fcb272589f9e81 to your computer and use it in GitHub Desktop.
傅立葉轉換圓形濾波
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import cv2 | |
| import numpy as np | |
| f_range = 50 # 低頻範圍 | |
| # 圖片來源:https://openhome.cc/Gossip/images/caterpillar.jpg | |
| img = cv2.imread('caterpillar.jpg', cv2.IMREAD_GRAYSCALE) | |
| rows, cols = img.shape | |
| cy, cx = int(rows / 2), int(cols/2) | |
| x = np.arange(-cx, cx) | |
| X, Y = np.meshgrid(np.arange(-cx, cx), np.arange(-cy, cy)) | |
| # 位移後的頻域表示 | |
| shifted = np.fft.fftshift(np.fft.fft2(img)) | |
| max_amp = np.max(np.abs(shifted)) | |
| # 低頻範圍內設為 0 | |
| shifted[X ** 2 + Y ** 2 > f_range ** 2] = 0 | |
| cv2.imshow('FFT 2D', np.abs(shifted) / max_amp * 255) | |
| # 轉回圖片 | |
| inversed = np.fft.ifft2(np.fft.ifftshift(shifted)) | |
| inversed_img = np.abs(inversed).astype('uint8') | |
| cv2.imshow('INVERSE FFT 2D', inversed_img) | |
| cv2.waitKey(0) | |
| cv2.destroyAllWindows() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment