s1093705 作業4
影像還原練習
題目說明:
附件中的image4 似乎受到某種頻域雜訊干擾,撰寫一個程式嘗試復原此圖像(將圖中雜
訊去除)。
開發環境:
- Windows 11
- Visual Studio Code
- Python 3.11.2
- Opencv 4.7.0
實作結果:
程式說明:
自訂函式說明:
def notch_filter_circle(shape, radius, column, row): 給定半徑、圓心座標,畫出一個的圓、
以及對稱於照片中心的另一個圓形,當作 notch filter 的屏蔽區域。
def notch_filter_rectangle(shape, start_col, start_row, end_col, end_row): 給定長方形
的其中兩個對稱點座標,畫出出一個長方形、以及對稱於照片中心的另一個長方形。
實作步驟:
1. 讀取圖片
img = cv2.imread():
2. 計算圖片的傅立葉轉換,算出團片的頻譜
f = np.fft.fft2(img)
fshift = np.fft.fftshift(f)
magnitude_spectrum = np.log(np.abs(fshift))
3. 算出 notch_filter
C1 = notch_filter_circle(img_shape, radius, 790, 447)
C2 = notch_filter_circle(img_shape, radius, 805, 416)
C3 = notch_filter_circle(img_shape, radius, 795, 383)
R1 = notch_filter_rectangle(img_shape, 790, 0, 810, 370)
R2 = notch_filter_rectangle(img_shape, 1495, 0, 1515, 960)
NotchFilter = C1*C2*C3*R1*R2
4. 算出 notched_spectrum
NotchRejectCenter = fshift * NotchFilter
NotchReject = np.fft.ifftshift(NotchRejectCenter)
inverse_NotchReject = np.fft.ifft2(NotchReject) # Compute the inverse DFT of the result
Result = np.abs(inverse_NotchReject)
5. 去除頻域雜訊干擾後的還原圖
magnitude_spectrum * NotchFilter
留言
張貼留言