• Was ?
    • Mathematik
    • Physik
    • Technik
    • International
    • Literaturtipps
  • Wer ?
  • Diskussion
  • Datenschutz

Gerchberg-Saxton-Algorithm with 2 and 4 Phase-Discretisation
Default
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
import numpy as np
import matplotlib.pyplot as plt
import cmath as cm
from angles import r2d,normalize,d2r
from scipy.stats import multivariate_normal
 
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
 
Title: Gerchberg-Saxton-Algorithm with 2 and 4 Phase-Discretisation
Author: Dominik Doellerer
 
 
The Algorithm computes iterative the phase-shift for a given output image
as shown in "A Practical Algorithm for the Determination of Phase from Image
and Diffraction Plane Pictures" in Optik Vol. 35 No. 2 (1972)
by R. W. Gerchberg and W. O. Saxton
Today, its not easy to machine a continous phase-shift into a material.
The program computes a discrete phase-shift by simply dividing into 2 or 4 parts.
 
 
The Project is under the Attribution-NonCommercial-ShareAlike 4.0 International
(CC BY-NC-SA 4.0) License. This means you are free to copy the model or adapt
it for non-commercial purposes.
 
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
 
IMAGE_FILE = '/YOURFOLDER/IMAGE.png' # input image
INTERATIONS = 5 # iterations of the gerchberg-saxton algorithm default 30
NUMBER_OF_PHASES = 2 #1 no discretisation 2 discrete Phases or 4 discrete Phases
SOURCE = 'gauss' #amplitude of lightsource 'gauss' or 'constant'
FAC_MU = 4 # width of the target gauss-curve if in use 1 equals normal, higher than 1 flattens the curve
 
 
DISCRETE_2PHASE = '/YOURFOLDER/DISCRETE_2PHASE.jpg'
DISCRETE_4PHASE_1 = '/YOURFOLDER/DISCRETE_4PHASE_1.jpg'
DISCRETE_4PHASE_2 = '/YOURFOLDER/DISCRETE_4PHASE_2.jpg'
DISCRETE_4PHASE_3 = '/YOURFOLDER/DISCRETE_4PHASE_3.jpg'
DISCRETE_4PHASE_4 = '/YOURFOLDER/DISCRETE_4PHASE_4.jpg'
CONTINUOUS_PHASE = '/YOURFOLDER/CONTINUOUS_PHASE.jpg'
 
 
#=======================math==============================================
def ampl(x):
return np.sqrt(x.real*x.real+x.imag*x.imag)
 
def absdiff(array1,array2):
#returns the absolut difference of two arrays in form of a new array
diff = np.zeros_like(array1)
for i in range(len(diff)):
for j in range(len(diff[1])):
diff[i][j]= array1[i][j]-array2[i][j]
plt.figure()
plt.imshow(diff)
plt.colorbar()
plt.title("Diff")
return diff
 
def gaussian(x, mu,sig):
return np.exp(-np.power(x - mu, 2.) / (2 * np.power(sig, 2.)))
def gauss(sizey,sizex,fac_mu):
x = np.linspace(0, sizex, sizex)
y = np.linspace(0, sizey, sizey)
X, Y = np.meshgrid(x, y)
pos = np.dstack((X, Y))
mu = np.array([sizex/2, sizey/2])
cov = np.array([[sizex*fac_mu,0],[0, sizey*fac_mu]])
rv = multivariate_normal(mu, cov)
Z = rv.pdf(pos)
 
return Z
 
 
#=======================image==============================================
 
def con2bw(image):
image_gray = np.mean(image, -1)
 
image_bw = np.zeros_like(image_gray)
for i in range(len(image_gray)):
for j in range(len(image_gray[i])):
if image_gray[i][j]<0.5: image_bw [i][j]= 0
if image_gray[i][j]>=0.5: image_bw[i][j] = 1
return image_bw
 
def plot_amplitude(im):
plt.figure()
from matplotlib.colors import LogNorm
plt.imshow(np.abs(im), norm=LogNorm(vmin=5))
plt.colorbar
plt.title("FFT Amplitude Image")
def argand(a):
plt.figure()
for i in range (len(a)):
for x in range(len(a[i])):
plt.polar([0,normalize(r2d(cm.phase(a[i][x])),0,360)],[0,abs(a[i][x])],marker='o')
plt.title("Phase")
plt.show()
 
def plot3D(array):
import matplotlib.pyplot as plt
import numpy as np
np.random.seed(1234)
fig = plt.figure()
ax1 = fig.add_subplot(111, projection='3d')
A = array-3.0
x = np.array([[i] * len(array[1]) for i in range(len(array))]).ravel()
y = np.array([i for i in range(len(array[1]))] * len(array))
z = np.zeros(len(array)*len(array[1]))
dx = np.ones(len(array)*len(array[1]))
dy = np.ones(len(array)*len(array[1]))
dz = A.ravel()
ax1.bar3d(x, y, z, dx, dy, dz)
 
 
#=======================GSA==============================================
 
def gsa(source,target,n):
a = np.fft.ifftshift(np.fft.ifft2(np.fft.fftshift(target)))
phase_a = np.zeros_like(target)
b = np.complex128(np.zeros_like(target))
phase_c = np.zeros_like(target)
c = np.complex128(np.zeros_like(target))
d = np.complex128(np.zeros_like(target))
retrieved_phase = np.zeros_like(target)
 
for z in range(n):
for i in range(len(source)):
for j in range(len(source[i])):
phase_a[i][j] = cm.phase(a[i][j])
b = np.multiply(source, np.exp(1j*phase_a))
c = np.fft.ifftshift(np.fft.fft2(np.fft.fftshift(b)))
for i in range(len(source)):
for j in range(len(source[i])):
phase_c[i][j] = cm.phase(c[i][j])
d = np.multiply(ampl(target), np.exp(1j*phase_c))
a = np.fft.ifftshift(np.fft.ifft2(np.fft.fftshift(d)))
print(z)
for i in range(len(source)):
for j in range(len(source[i])):
retrieved_phase[i][j] = normalize(r2d(cm.phase(a[i][j])),0,360)
return retrieved_phase
 
#=======================quantisation==============================================
 
def storeSeperate(retrieved_phase,n_sep):
plt.figure()
if(n_sep==1):
plt.imsave(CONTINUOUS_PHASE,retrieved_phase, cmap='gray')
plt.imshow(retrieved_phase)
 
if(n_sep==2):
phase = np.zeros_like(retrieved_phase)
for i in range(len(retrieved_phase)):
for j in range(len(retrieved_phase[i])):
if( abs(retrieved_phase[i][j]) > 180):
phase[i][j]=1
else:
phase[i][j]=0
plt.imshow(phase)
plt.imsave(DISCRETE_2PHASE,phase, cmap='gray')
 
if(n_sep==4):
phase1 = np.zeros_like(retrieved_phase)
phase2 = np.zeros_like(retrieved_phase)
phase3 = np.zeros_like(retrieved_phase)
phase4 = np.zeros_like(retrieved_phase)
for i in range(len(retrieved_phase)):
for j in range(len(retrieved_phase[i])):
if( abs(retrieved_phase[i][j]) > 270):
phase4[i][j]=1
elif(abs(retrieved_phase[i][j]) > 180):
phase3[i][j]=1
elif(abs(retrieved_phase[i][j]) > 90):
phase2[i][j]=1
else:
phase1[i][j]=1
plt.imshow(phase1)
plt.imsave(DISCRETE_4PHASE_4,phase4, cmap='gray')
plt.imsave(DISCRETE_4PHASE_3,phase3, cmap='gray')
plt.imsave(DISCRETE_4PHASE_2,phase2, cmap='gray')
plt.imsave(DISCRETE_4PHASE_1,phase1, cmap='gray')
 
plt.title("saved discretisation")
#=======================main==============================================
 
image = plt.imread(IMAGE_FILE).astype(float)
 
plt.figure()
plt.imshow(image)
plt.title("original")
 
target = con2bw(image)
if(SOURCE=='gauss'):
source = gauss(len(target),len(target[1]),FAC_MU)
else:
source = np.ones_like(target)/(len(target)*len(target))
 
plt.figure()
plt.imshow(source)
plt.title("source")
 
retrieved_phase = gsa(source,target,INTERATIONS)
 
plt.figure()
plt.imshow(retrieved_phase)
plt.title("retrieved phase / no discretisation")
 
#=======================holores==============================================
out = np.zeros_like(source)
 
if(NUMBER_OF_PHASES==2):
phase = np.zeros_like(retrieved_phase)
for i in range(len(retrieved_phase)):
for j in range(len(retrieved_phase[i])):
if( abs(retrieved_phase[i][j]) > 180):
phase[i][j]=180
else:
phase[i][j]=0
holo1 = np.complex128(source)
for i in range(len(source)):
for j in range(len(source[i])):
holo1[i][j]= source[i][j] * np.exp(1j*phase[i][j]/360*cm.pi*2)
holo2 = np.fft.ifftshift(np.fft.fft2(np.fft.fftshift(holo1)))
for i in range(len(holo1)):
for j in range(len(holo2[i])):
out[i][j] = ampl(holo2[i][j])
 
 
if(NUMBER_OF_PHASES==4):
phase1 = np.zeros_like(retrieved_phase)
phase2 = np.zeros_like(retrieved_phase)
phase3 = np.zeros_like(retrieved_phase)
phase4 = np.zeros_like(retrieved_phase)
for i in range(len(retrieved_phase)):
for j in range(len(retrieved_phase[i])):
if( abs(retrieved_phase[i][j]) > 270):
phase4[i][j]=1
elif(abs(retrieved_phase[i][j]) > 180):
phase3[i][j]=1
elif(abs(retrieved_phase[i][j]) > 90):
phase2[i][j]=1
else:
phase1[i][j]=1
holo = np.fft.ifftshift(np.fft.fft2(np.fft.fftshift(source*np.exp(1j*(((phase1*90)+phase2*180+phase3*270+phase4*360)/360*3.1415*2)))))
for i in range(len(holo)):
for j in range(len(holo[i])):
out[i][j] = np.sqrt(holo[i][j].real*holo[i][j].real+holo[i][j].imag*holo[i][j].imag)
 
storeSeperate(retrieved_phase,NUMBER_OF_PHASES)
if(NUMBER_OF_PHASES==4 or NUMBER_OF_PHASES==2):
plt.figure()
plt.imshow(out)
plt.title("output of discretisation")
else:
holo1 = np.complex128(source)
for i in range(len(source)):
for j in range(len(source[i])):
holo1[i][j]= source[i][j] * np.exp(1j*retrieved_phase[i][j]/360*cm.pi*2)
holo2 = np.fft.ifftshift(np.fft.fft2(np.fft.fftshift(holo1)))
for i in range(len(holo1)):
for j in range(len(holo2[i])):
out[i][j] = ampl(holo2[i][j])
plt.figure()
plt.imshow(out)
plt.title("output without discretisation")

Neueste Beiträge

  • Praktika in der Physik
  • Weather in Munich

Neueste Kommentare

  • Dominik bei 3D Printing: Remote Direct Extruder with Flexible Drive Shaft and Worm Gear
  • Dominik bei Lightmodulation: Gerchberg-Saxton-Algorithmus (GSA)

Meta

  • Anmelden
  • Feed der Einträge
  • Kommentare-Feed
  • WordPress.org
wichtige datenschutzrechtliche Hinweise zur Benutzung dieser Website finden Sie auf: https://www.creatgraphy.com/datenschutzerklaerung/
↑

Diese Seite verwendet Cookies. Mit der weiteren Verwendung stimmst du dem zu. This website uses cookies. If further used, you agree to the policy.