1. 程式人生 > 實用技巧 >python 實現 凸透鏡成像

python 實現 凸透鏡成像

 1 import cv2
 2 import numpy as np
 3 import math
 4 
 5 # 讀取圖片
 6 img = cv2.imread('lena.pgm', cv2.IMREAD_GRAYSCALE) / 255  # 歸一化
 7 cv2.namedWindow("lena", cv2.WINDOW_AUTOSIZE)
 8 cv2.imshow("lena", img)
 9 print("原圖:", img)
10 # cv2.waitKey(0)
11 # cv2.destroyAllWindows()
12 
13 N = 512
14 m, n = img.shape[0], img.shape[1]
15 lamd = 623*10**(-9) 16 d0 = 0.1 17 d = 0.01 18 f = 0.025 19 di = f * d0 / (d0 - f) # 透鏡成像原理 1/d0 + 1/di = 1/f 20 21 # 進行N*N取樣,根據Shannon取樣定理 22 dx0 = N / m 23 dy0 = N / n 24 dks = lamd * d0 / (N * dx0) 25 dep = lamd * d0 / (N * dy0) 26 dfx = 1 / (N * dx0) 27 dfy = 1 / (N * dy0) 28 29 # 相位因子 30 yinzi_1 = np.zeros((m, n), dtype=complex)
31 for k in range(m): 32 for l in range(n): 33 yinzi_1[k][l] = np.exp(1j * math.pi * ((k * dx0)**2 + (l * dy0)**2) / (lamd * d0)) 34 # 明文資訊進行離散菲涅爾變換 35 U0 = np.fft.fft2(img * yinzi_1) 36 # print("U0:", U0) 37 38 # 透鏡透過率函式 39 t = np.zeros((m, n), dtype=complex) 40 for i in range(m): 41 for j in
range(n): 42 t[i][j] = np.exp((-1j) * math.pi * ((i * dks)**2 + (j * dep)**2) / (lamd * f)) 43 44 # 透鏡前表面轉換到透鏡後表明經過透鏡透過率函式的調製 45 UL2 = U0 * t 46 47 # 相位因子 48 yinzi_3 = np.zeros((m, n), dtype=complex) 49 for i in range(m): 50 for j in range(n): 51 yinzi_3[i][j] = np.exp(1j * math.pi * ((i * dks)**2 + (j * dep)**2) / (lamd * di)) 52 # 透鏡後表明資訊進行離散菲涅爾變換 53 U0_ = np.fft.fft2(UL2 * yinzi_3) 54 # 結果是複數形式,取絕對值提取振幅並且進行歸一化處理 55 U0_s = np.abs(U0_) / np.max(np.abs(U0_)) 56 # print("原圖:", U0_) 57 58 # 顯示影象,凸透鏡成像的結果是倒立的影象 59 cv2.namedWindow("origin", cv2.WINDOW_AUTOSIZE) 60 cv2.imshow("origin", U0_s) 61 cv2.waitKey(0) 62 cv2.destroyAllWindows()

結果顯示 圖片用的是512*512的lena灰度影象