├── Gerchberg-Saxton-algorithm.py ├── README.md ├── Weighted-Gerchberg-Saxton-algorithm.py └── img ├── hologram.bmp ├── reconstruction.bmp ├── target.bmp ├── weighted_hologram.bmp └── weighted_reconstruction.bmp /Gerchberg-Saxton-algorithm.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Sat Jan 19 23:58:43 2019 4 | 5 | @author: ohman 6 | """ 7 | 8 | import cv2 9 | import numpy as np 10 | import matplotlib.pyplot as plt 11 | 12 | #均一性の評価 13 | def check_uniformity(u_int, target): 14 | u_int = u_int / np.max(u_int) 15 | maxi = np.max(u_int[target==1]) 16 | mini = np.min(u_int[target==1]) 17 | uniformity = 1 - (maxi-mini)/(maxi+mini) 18 | print("均一性:", uniformity) 19 | return uniformity 20 | 21 | def normalization(origin): 22 | maxi = np.max(origin) 23 | mini = np.min(origin) 24 | norm = ((origin - mini) / (maxi - mini)) 25 | return norm 26 | 27 | 28 | def hologram(phase): 29 | phase = np.where(phase<0, phase+2*np.pi, phase) 30 | p_max = np.max(phase) 31 | p_min = np.min(phase) 32 | holo = ((phase - p_min)/(p_max- p_min)) * 255 33 | holo = holo.astype("uint8") 34 | return holo 35 | 36 | 37 | def reconstruct(norm_int): 38 | rec = norm_int * 255 39 | rec = rec.astype("uint8") 40 | return rec 41 | 42 | def main(): 43 | target = cv2.imread("img/target.bmp",0) 44 | cv2.imshow("target",target) 45 | cv2.waitKey(0) 46 | 47 | height, width = target.shape[:2] 48 | target[target>150] = 255 49 | 50 | target = target / 255 51 | laser = 1 52 | phase = np.random.rand(height, width) 53 | u = np.empty_like(target, dtype="complex") 54 | 55 | iteration = 30 56 | uniformity = [] 57 | 58 | for num in range(iteration): 59 | u.real = laser * np.cos(phase) 60 | u.imag = laser * np.sin(phase) 61 | 62 | #-------レンズ--------- 63 | u = np.fft.fft2(u) 64 | u = np.fft.fftshift(u) 65 | #-------レンズ--------- 66 | 67 | u_abs = np.abs(u) 68 | u_int = u_abs ** 2 69 | norm_int = normalization(u_int) 70 | 71 | uniformity.append(check_uniformity(u_int,target)) 72 | 73 | phase = np.angle(u) 74 | 75 | u.real = target * np.cos(phase) 76 | u.imag = target * np.sin(phase) 77 | 78 | #-----レンズ--------- 79 | u = np.fft.ifftshift(u) 80 | u = np.fft.ifft2(u) 81 | #-------レンズ--------- 82 | 83 | phase = np.angle(u) 84 | 85 | #効率性 86 | efficiency = np.sum(norm_int[target==1]) / np.sum(target[target==1]) 87 | print("効率性 : ", efficiency) 88 | 89 | holo_name = "hologram" 90 | rec_name = "reconstruction" 91 | 92 | holo = hologram(phase) 93 | cv2.imwrite("img/{}.bmp".format(holo_name), holo) 94 | cv2.imshow("Hologram", holo) 95 | cv2.waitKey(0) 96 | 97 | rec = reconstruct(norm_int) 98 | cv2.imwrite("img/{}.bmp".format(rec_name), rec) 99 | cv2.imshow("Reconstruction", rec) 100 | cv2.waitKey(0) 101 | 102 | plt.figure(figsize=(8,5)) 103 | plt.plot(np.arange(1,iteration+1),uniformity) 104 | plt.xlabel("Iteration") 105 | plt.ylabel("Uniformity") 106 | plt.ylim(0,1) 107 | 108 | if __name__ == "__main__": 109 | main() 110 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Gerchberg-Saxton-algorithm 2 | *Phase recovery using the GS method.* 3 | 4 | *Reference of GS method* 5 | R. W. Gerchberg and W. O. Saxton, "A practical algorithm for the determination of the phase from image and diffraction plane pictures", Optik 35, 237 (1972) 6 | 7 | *Reference of WGS method* 8 | R. Di Leonardo, F. Ianni, and G. Ruocco,"Computer generation of optimal holograms for optical trap arrays",Opt. Express 15,1913 (2007) 9 | 10 | By executing the GS method (loop number of times 30) on the sample target, a reconstructed image with uniformity of about 0.68 is obtained. 11 | By executing the WGS method (loop number of times 30) on the sample target, a reconstructed image with uniformity of about 0.97 can be obtained. 12 | 13 | ![image](https://user-images.githubusercontent.com/40331166/51429302-b029c680-1c50-11e9-93c1-c4179802c52d.png) 14 | -------------------------------------------------------------------------------- /Weighted-Gerchberg-Saxton-algorithm.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Sun Jan 20 00:32:16 2019 4 | 5 | @author: ohman 6 | """ 7 | 8 | import cv2 9 | import numpy as np 10 | import matplotlib.pyplot as plt 11 | 12 | #重み付け 13 | def weight(w_u, target, before_u, norm_int): 14 | w_u[target==1] = ((target[target==1] / norm_int[target==1]) ** 0.5) * before_u[target==1] 15 | return w_u 16 | 17 | #均一性の評価 18 | def check_uniformity(u_int, target): 19 | u_int = u_int / np.max(u_int) 20 | 21 | maxi = np.max(u_int[target==1]) 22 | mini = np.min(u_int[target==1]) 23 | uniformity = 1 - (maxi-mini)/(maxi+mini) 24 | print("均一性:", uniformity) 25 | return uniformity 26 | 27 | def normalization(origin): 28 | maxi = np.max(origin) 29 | mini = np.min(origin) 30 | norm = ((origin - mini) / (maxi - mini)) 31 | return norm 32 | 33 | 34 | def hologram(phase): 35 | phase = np.where(phase<0, phase+2*np.pi, phase) 36 | holo = normalization(phase) * 255 37 | holo = holo.astype("uint8") 38 | return holo 39 | 40 | 41 | def reconstruct(norm_int): 42 | rec = norm_int * 255 43 | rec = rec.astype("uint8") 44 | return rec 45 | 46 | def main(): 47 | target = cv2.imread("img/target.bmp",0) 48 | cv2.imshow("target",target) 49 | cv2.waitKey(0) 50 | 51 | height, width = target.shape[:2] 52 | target[target>150] = 255 53 | 54 | 55 | target = target / 255 56 | laser = 1 57 | phase = np.random.rand(height, width) 58 | u = np.empty_like(target, dtype="complex") 59 | before_u = target 60 | w_u = np.empty_like(target) 61 | 62 | iteration = 30 63 | uniformity = [] 64 | 65 | for num in range(iteration): 66 | u.real = laser * np.cos(phase) 67 | u.imag = laser * np.sin(phase) 68 | 69 | #-------レンズ--------- 70 | u = np.fft.fft2(u) 71 | u = np.fft.fftshift(u) 72 | #-------レンズ--------- 73 | 74 | u_int = np.abs(u) ** 2 75 | norm_int = normalization(u_int) 76 | 77 | uniformity.append(check_uniformity(u_int, target)) 78 | 79 | phase = np.angle(u) 80 | 81 | w_u = weight(w_u, target, before_u, norm_int) 82 | w_u = normalization(w_u) 83 | before_u = w_u 84 | 85 | u.real = w_u * np.cos(phase) 86 | u.imag = w_u * np.sin(phase) 87 | 88 | #-----レンズ--------- 89 | u = np.fft.ifftshift(u) 90 | u = np.fft.ifft2(u) 91 | #-------レンズ--------- 92 | 93 | phase = np.angle(u) 94 | 95 | 96 | #効率性 97 | efficiency = np.sum(norm_int[target==1]) / np.sum(target[target==1]) 98 | print("効率性 : ", efficiency) 99 | 100 | holo_name = "weighted_hologram" 101 | rec_name = "weighted_reconstruction" 102 | 103 | holo = hologram(phase) 104 | cv2.imwrite("img/{}.bmp".format(holo_name), holo) 105 | cv2.imshow("Weighted Hologram", holo) 106 | cv2.waitKey(0) 107 | 108 | rec = reconstruct(norm_int) 109 | cv2.imwrite("img/{}.bmp".format(rec_name), rec) 110 | cv2.imshow("Weighted Reconstruction", rec) 111 | cv2.waitKey(0) 112 | 113 | plt.figure(figsize=(10,8)) 114 | plt.plot(np.arange(1,iteration+1),uniformity) 115 | plt.xlabel("Iteration") 116 | plt.ylabel("Uniformity") 117 | plt.ylim(0,1) 118 | 119 | if __name__ == "__main__": 120 | main() 121 | -------------------------------------------------------------------------------- /img/hologram.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kurokuman/Gerchberg-Saxton-algorithm/141da15bd33650b8086904e8fec7147ca017a637/img/hologram.bmp -------------------------------------------------------------------------------- /img/reconstruction.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kurokuman/Gerchberg-Saxton-algorithm/141da15bd33650b8086904e8fec7147ca017a637/img/reconstruction.bmp -------------------------------------------------------------------------------- /img/target.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kurokuman/Gerchberg-Saxton-algorithm/141da15bd33650b8086904e8fec7147ca017a637/img/target.bmp -------------------------------------------------------------------------------- /img/weighted_hologram.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kurokuman/Gerchberg-Saxton-algorithm/141da15bd33650b8086904e8fec7147ca017a637/img/weighted_hologram.bmp -------------------------------------------------------------------------------- /img/weighted_reconstruction.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kurokuman/Gerchberg-Saxton-algorithm/141da15bd33650b8086904e8fec7147ca017a637/img/weighted_reconstruction.bmp --------------------------------------------------------------------------------